File: data_shared_media.cpp

package info (click to toggle)
telegram-desktop 4.6.5%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 53,300 kB
  • sloc: cpp: 605,857; python: 3,978; ansic: 1,636; sh: 965; makefile: 841; objc: 652; javascript: 187; xml: 165
file content (525 lines) | stat: -rw-r--r-- 15,243 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.

For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "data/data_shared_media.h"

#include <rpl/combine.h>
#include "main/main_session.h"
#include "apiwrap.h"
#include "storage/storage_facade.h"
#include "history/history.h"
#include "history/history_item.h"
#include "data/data_document.h"
#include "data/data_media_types.h"
#include "data/data_photo.h"
#include "data/data_scheduled_messages.h"
#include "data/data_session.h"
#include "core/crash_reports.h"

namespace {

using Type = Storage::SharedMediaType;

bool IsItemGoodForType(const not_null<HistoryItem*> item, Type type) {
	const auto media = item->media();
	if (!media || media->webpage()) {
		return false;
	}
	const auto photo = media->photo();
	const auto photoType = (type == Type::Photo);
	const auto photoVideoType = (type == Type::PhotoVideo);
	if ((photoType || photoVideoType) && photo) {
		return true;
	}

	const auto document = media->document();
	if (!document) {
		return false;
	}
	const auto voiceType = (type == Type::VoiceFile);
	const auto voiceDoc = document->isVoiceMessage();

	const auto roundType = (type == Type::RoundFile);
	const auto roundDoc = document->isVideoMessage();

	const auto audioType = (type == Type::MusicFile);
	const auto audioDoc = document->isAudioFile();

	const auto gifType = (type == Type::GIF);
	const auto gifDoc = document->isGifv();

	const auto videoType = (type == Type::Video);
	const auto videoDoc = document->isVideoFile();

	const auto voiceRoundType = (type == Type::RoundVoiceFile);
	const auto fileType = (type == Type::File);

	return (audioType && audioDoc)
		|| (voiceType && voiceDoc)
		|| (roundType && roundDoc)
		|| (voiceRoundType && (roundDoc || voiceDoc))
		|| (gifType && gifDoc)
		|| ((videoType || photoVideoType) && videoDoc)
		|| (fileType && (document->isTheme()
			|| document->isImage()
			|| !document->canBeStreamed(item)));
}

} // namespace

std::optional<Storage::SharedMediaType> SharedMediaOverviewType(
		Storage::SharedMediaType type) {
	switch (type) {
	case Type::Photo:
	case Type::Video:
	case Type::MusicFile:
	case Type::File:
	case Type::RoundVoiceFile:
	case Type::Link: return type;
	}
	return std::nullopt;
}

bool SharedMediaAllowSearch(Storage::SharedMediaType type) {
	switch (type) {
	case Type::MusicFile:
	case Type::File:
	case Type::Link: return true;
	default: return false;
	}
}

rpl::producer<SparseIdsSlice> SharedMediaViewer(
		not_null<Main::Session*> session,
		Storage::SharedMediaKey key,
		int limitBefore,
		int limitAfter) {
	Expects(IsServerMsgId(key.messageId) || (key.messageId == 0));
	Expects((key.messageId != 0) || (limitBefore == 0 && limitAfter == 0));

	return [=](auto consumer) {
		auto lifetime = rpl::lifetime();
		auto builder = lifetime.make_state<SparseIdsSliceBuilder>(
			key.messageId,
			limitBefore,
			limitAfter);
		auto requestMediaAround = [
			peer = session->data().peer(key.peerId),
			topicRootId = key.topicRootId,
			type = key.type
		](const SparseIdsSliceBuilder::AroundData &data) {
			peer->session().api().requestSharedMedia(
				peer,
				topicRootId,
				type,
				data.aroundId,
				data.direction);
		};
		builder->insufficientAround(
		) | rpl::start_with_next(requestMediaAround, lifetime);

		auto pushNextSnapshot = [=] {
			consumer.put_next(builder->snapshot());
		};

		using SliceUpdate = Storage::SharedMediaSliceUpdate;
		session->storage().sharedMediaSliceUpdated(
		) | rpl::filter([=](const SliceUpdate &update) {
			return (update.peerId == key.peerId)
				&& (update.topicRootId == key.topicRootId)
				&& (update.type == key.type);
		}) | rpl::filter([=](const SliceUpdate &update) {
			return builder->applyUpdate(update.data);
		}) | rpl::start_with_next(pushNextSnapshot, lifetime);

		using OneRemoved = Storage::SharedMediaRemoveOne;
		session->storage().sharedMediaOneRemoved(
		) | rpl::filter([=](const OneRemoved &update) {
			return (update.peerId == key.peerId)
				&& update.types.test(key.type);
		}) | rpl::filter([=](const OneRemoved &update) {
			return builder->removeOne(update.messageId);
		}) | rpl::start_with_next(pushNextSnapshot, lifetime);

		using AllRemoved = Storage::SharedMediaRemoveAll;
		session->storage().sharedMediaAllRemoved(
		) | rpl::filter([=](const AllRemoved &update) {
			return (update.peerId == key.peerId)
				&& (!update.topicRootId
					|| update.topicRootId == key.topicRootId)
				&& update.types.test(key.type);
		}) | rpl::filter([=] {
			return builder->removeAll();
		}) | rpl::start_with_next(pushNextSnapshot, lifetime);

		using InvalidateBottom = Storage::SharedMediaInvalidateBottom;
		session->storage().sharedMediaBottomInvalidated(
		) | rpl::filter([=](const InvalidateBottom &update) {
			return (update.peerId == key.peerId);
		}) | rpl::filter([=] {
			return builder->invalidateBottom();
		}) | rpl::start_with_next(pushNextSnapshot, lifetime);

		using Result = Storage::SharedMediaResult;
		session->storage().query(Storage::SharedMediaQuery(
			key,
			limitBefore,
			limitAfter
		)) | rpl::filter([=](const Result &result) {
			return builder->applyInitial(result);
		}) | rpl::start_with_next_done(
			pushNextSnapshot,
			[=] { builder->checkInsufficient(); },
			lifetime);

		return lifetime;
	};
}

rpl::producer<SparseIdsMergedSlice> SharedScheduledMediaViewer(
		not_null<Main::Session*> session,
		SharedMediaMergedKey key,
		int limitBefore,
		int limitAfter) {
	Expects(!IsServerMsgId(key.mergedKey.universalId));
	Expects((key.mergedKey.universalId != 0)
		|| (limitBefore == 0 && limitAfter == 0));

	const auto history = session->data().history(key.mergedKey.peerId);

	return rpl::single(rpl::empty) | rpl::then(
		session->data().scheduledMessages().updates(history)
	) | rpl::map([=] {
		const auto list = session->data().scheduledMessages().list(history);

		auto items = ranges::views::all(
			list.ids
		) | ranges::views::transform([=](const FullMsgId &fullId) {
			return session->data().message(fullId);
		}) | ranges::views::filter([=](HistoryItem *item) {
			return item
				? IsItemGoodForType(item, key.type)
				: false;
		}) | ranges::to_vector;

		ranges::sort(items, ranges::less(), &HistoryItem::position);

		auto finishMsgIds = ranges::views::all(
			items
		) | ranges::views::transform([=](not_null<HistoryItem*> item) {
			return item->fullId().msg;
		}) | ranges::to_vector;

		const auto fullCount = finishMsgIds.size();

		auto unsorted = SparseUnsortedIdsSlice(
			std::move(finishMsgIds),
			fullCount,
			list.skippedBefore,
			list.skippedAfter);
		return SparseIdsMergedSlice(
			key.mergedKey,
			std::move(unsorted));
	});
}

rpl::producer<SparseIdsMergedSlice> SharedMediaMergedViewer(
		not_null<Main::Session*> session,
		SharedMediaMergedKey key,
		int limitBefore,
		int limitAfter) {
	auto createSimpleViewer = [=](
			PeerId peerId,
			MsgId topicRootId,
			SparseIdsSlice::Key simpleKey,
			int limitBefore,
			int limitAfter) {
		return SharedMediaViewer(
			session,
			Storage::SharedMediaKey(
				peerId,
				topicRootId,
				key.type,
				simpleKey),
			limitBefore,
			limitAfter
		);
	};
	return SparseIdsMergedSlice::CreateViewer(
		key.mergedKey,
		limitBefore,
		limitAfter,
		std::move(createSimpleViewer));
}

SharedMediaWithLastSlice::SharedMediaWithLastSlice(
	not_null<Main::Session*> session,
	Key key)
: SharedMediaWithLastSlice(
	session,
	key,
	SparseIdsMergedSlice(ViewerKey(key)),
	EndingSlice(key)) {
}

SharedMediaWithLastSlice::SharedMediaWithLastSlice(
	not_null<Main::Session*> session,
	Key key,
	SparseIdsMergedSlice slice,
	std::optional<SparseIdsMergedSlice> ending)
: _session(session)
, _key(key)
, _slice(std::move(slice))
, _ending(std::move(ending))
, _lastPhotoId(LastPeerPhotoId(session, key.peerId))
, _isolatedLastPhoto(_key.type == Type::ChatPhoto
	? IsLastIsolated(session, _slice, _ending, _lastPhotoId)
	: false) {
}

std::optional<int> SharedMediaWithLastSlice::fullCount() const {
	return Add(
		_slice.fullCount(),
		_isolatedLastPhoto | [](bool isolated) { return isolated ? 1 : 0; });
}

std::optional<int> SharedMediaWithLastSlice::skippedBeforeImpl() const {
	return _slice.skippedBefore();
}

std::optional<int> SharedMediaWithLastSlice::skippedBefore() const {
	return _reversed ? skippedAfterImpl() : skippedBeforeImpl();
}

std::optional<int> SharedMediaWithLastSlice::skippedAfterImpl() const {
	return isolatedInSlice()
		? Add(
			_slice.skippedAfter(),
			lastPhotoSkip())
		: (lastPhotoSkip() | [](int) { return 0; });
}

std::optional<int> SharedMediaWithLastSlice::skippedAfter() const {
	return _reversed ? skippedBeforeImpl() : skippedAfterImpl();
}

std::optional<int> SharedMediaWithLastSlice::indexOfImpl(Value value) const {
	return std::get_if<FullMsgId>(&value)
		? _slice.indexOf(*std::get_if<FullMsgId>(&value))
		: (isolatedInSlice()
			|| !_lastPhotoId
			|| (*std::get_if<not_null<PhotoData*>>(&value))->id != *_lastPhotoId)
			? std::nullopt
			: Add(_slice.size() - 1, lastPhotoSkip());
}

std::optional<int> SharedMediaWithLastSlice::indexOf(Value value) const {
	const auto result = indexOfImpl(value);
	if (result && (*result < 0 || *result >= size())) {
		// Should not happen.
		auto info = QStringList();
		info.push_back("slice:" + QString::number(_slice.size()));
		info.push_back(_slice.fullCount()
			? QString::number(*_slice.fullCount())
			: QString("-"));
		info.push_back(_slice.skippedBefore()
			? QString::number(*_slice.skippedBefore())
			: QString("-"));
		info.push_back(_slice.skippedAfter()
			? QString::number(*_slice.skippedAfter())
			: QString("-"));
		info.push_back("ending:" + (_ending
			? QString::number(_ending->size())
			: QString("-")));
		info.push_back((_ending && _ending->fullCount())
			? QString::number(*_ending->fullCount())
			: QString("-"));
		info.push_back((_ending && _ending->skippedBefore())
			? QString::number(*_ending->skippedBefore())
			: QString("-"));
		info.push_back((_ending && _ending->skippedAfter())
			? QString::number(*_ending->skippedAfter())
			: QString("-"));
		if (const auto msgId = std::get_if<FullMsgId>(&value)) {
			info.push_back("value:" + QString::number(msgId->peer.value));
			info.push_back(QString::number(msgId->msg.bare));
			const auto index = _slice.indexOf(*std::get_if<FullMsgId>(&value));
			info.push_back("index:" + (index
				? QString::number(*index)
				: QString("-")));
		} else if (const auto photo = std::get_if<not_null<PhotoData*>>(&value)) {
			info.push_back("value:" + QString::number((*photo)->id));
		} else {
			info.push_back("value:bad");
		}
		info.push_back("isolated:" + QString(Logs::b(isolatedInSlice())));
		info.push_back("last:" + (_lastPhotoId
			? QString::number(*_lastPhotoId)
			: QString("-")));
		info.push_back("isolated_last:" + (_isolatedLastPhoto
			? QString(Logs::b(*_isolatedLastPhoto))
			: QString("-")));
		info.push_back("skip:" + (lastPhotoSkip()
			? QString::number(*lastPhotoSkip())
			: QString("-")));
		CrashReports::SetAnnotation("DebugInfo", info.join(','));
		Unexpected("Result in SharedMediaWithLastSlice::indexOf");
	}
	return _reversed
		? (result | func::negate | func::add(size() - 1))
		: result;
}

int SharedMediaWithLastSlice::size() const {
	return _slice.size()
		+ ((!isolatedInSlice() && lastPhotoSkip() == 1) ? 1 : 0);
}

SharedMediaWithLastSlice::Value SharedMediaWithLastSlice::operator[](int index) const {
	Expects(index >= 0 && index < size());

	if (_reversed) {
		index = size() - index - 1;
	}
	return (index < _slice.size())
		? Value(_slice[index])
		: Value(_session->data().photo(*_lastPhotoId));
}

std::optional<int> SharedMediaWithLastSlice::distance(
		const Key &a,
		const Key &b) const {
	if (auto i = indexOf(ComputeId(a))) {
		if (auto j = indexOf(ComputeId(b))) {
			return *j - *i;
		}
	}
	return std::nullopt;
}

void SharedMediaWithLastSlice::reverse() {
	_reversed = !_reversed;
}

std::optional<PhotoId> SharedMediaWithLastSlice::LastPeerPhotoId(
		not_null<Main::Session*> session,
		PeerId peerId) {
	if (const auto peer = session->data().peerLoaded(peerId)) {
		return peer->userpicPhotoUnknown()
			? std::nullopt
			: base::make_optional(peer->userpicPhotoId());
	}
	return std::nullopt;
}

std::optional<bool> SharedMediaWithLastSlice::IsLastIsolated(
		not_null<Main::Session*> session,
		const SparseIdsMergedSlice &slice,
		const std::optional<SparseIdsMergedSlice> &ending,
		std::optional<PhotoId> lastPeerPhotoId) {
	if (!lastPeerPhotoId) {
		return std::nullopt;
	} else if (!*lastPeerPhotoId) {
		return false;
	}
	return LastFullMsgId(ending ? *ending : slice)
		| [&](FullMsgId msgId) { return session->data().message(msgId); }
		| [](HistoryItem *item) { return item ? item->media() : nullptr; }
		| [](Data::Media *media) { return media ? media->photo() : nullptr; }
		| [](PhotoData *photo) { return photo ? photo->id : 0; }
		| [&](PhotoId photoId) { return *lastPeerPhotoId != photoId; };
}

std::optional<FullMsgId> SharedMediaWithLastSlice::LastFullMsgId(
		const SparseIdsMergedSlice &slice) {
	if (slice.fullCount() == 0) {
		return FullMsgId();
	} else if (slice.size() == 0 || slice.skippedAfter() != 0) {
		return std::nullopt;
	}
	return slice[slice.size() - 1];
}

rpl::producer<SharedMediaWithLastSlice> SharedMediaWithLastViewer(
		not_null<Main::Session*> session,
		SharedMediaWithLastSlice::Key key,
		int limitBefore,
		int limitAfter) {
	return [=](auto consumer) {
		auto viewerKey = SharedMediaMergedKey(
			SharedMediaWithLastSlice::ViewerKey(key),
			key.type);

		if (std::get_if<not_null<PhotoData*>>(&key.universalId)) {
			return SharedMediaMergedViewer(
				session,
				std::move(viewerKey),
				limitBefore,
				limitAfter
			) | rpl::start_with_next([=](SparseIdsMergedSlice &&update) {
				consumer.put_next(SharedMediaWithLastSlice(
					session,
					key,
					std::move(update),
					std::nullopt));
			});
		}

		if (key.topicRootId == SharedMediaWithLastSlice::kScheduledTopicId) {
			return SharedScheduledMediaViewer(
				session,
				std::move(viewerKey),
				limitBefore,
				limitAfter
			) | rpl::start_with_next([=](SparseIdsMergedSlice &&update) {
				consumer.put_next(SharedMediaWithLastSlice(
					session,
					key,
					std::move(update),
					std::nullopt));
			});
		}
		return rpl::combine(
			SharedMediaMergedViewer(
				session,
				std::move(viewerKey),
				limitBefore,
				limitAfter),
			SharedMediaMergedViewer(
				session,
				SharedMediaMergedKey(
					SharedMediaWithLastSlice::EndingKey(key),
					key.type),
				1,
				1)
		) | rpl::start_with_next([=](
				SparseIdsMergedSlice &&viewer,
				SparseIdsMergedSlice &&ending) {
			consumer.put_next(SharedMediaWithLastSlice(
				session,
				key,
				std::move(viewer),
				std::move(ending)));
		});
	};
}

rpl::producer<SharedMediaWithLastSlice> SharedMediaWithLastReversedViewer(
		not_null<Main::Session*> session,
		SharedMediaWithLastSlice::Key key,
		int limitBefore,
		int limitAfter) {
	return SharedMediaWithLastViewer(
		session,
		key,
		limitBefore,
		limitAfter
	) | rpl::map([](SharedMediaWithLastSlice &&slice) {
		slice.reverse();
		return std::move(slice);
	});
}