| 
									
										
										
										
											2017-08-18 22:14:31 +03:00
										 |  |  | /*
 | 
					
						
							|  |  |  | This file is part of Telegram Desktop, | 
					
						
							| 
									
										
										
										
											2018-01-03 13:23:14 +03:00
										 |  |  | the official desktop application for the Telegram messaging service. | 
					
						
							| 
									
										
										
										
											2017-08-18 22:14:31 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-03 13:23:14 +03:00
										 |  |  | For license and copyright information please follow this link: | 
					
						
							|  |  |  | https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 | 
					
						
							| 
									
										
										
										
											2017-08-18 22:14:31 +03:00
										 |  |  | */ | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "storage/storage_shared_media.h"
 | 
					
						
							| 
									
										
										
										
											2017-11-30 21:33:27 +04:00
										 |  |  | #include "base/weak_ptr.h"
 | 
					
						
							| 
									
										
										
										
											2017-11-06 11:31:18 +04:00
										 |  |  | #include "data/data_sparse_ids.h"
 | 
					
						
							| 
									
										
										
										
											2017-08-18 22:14:31 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-13 09:06:02 +03:00
										 |  |  | class History; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-08 13:06:50 +04:00
										 |  |  | namespace Main { | 
					
						
							|  |  |  | class Session; | 
					
						
							|  |  |  | } // namespace Main
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-21 19:28:46 +03:00
										 |  |  | std::optional<Storage::SharedMediaType> SharedMediaOverviewType( | 
					
						
							| 
									
										
										
										
											2017-08-18 22:14:31 +03:00
										 |  |  | 	Storage::SharedMediaType type); | 
					
						
							|  |  |  | void SharedMediaShowOverview( | 
					
						
							|  |  |  | 	Storage::SharedMediaType type, | 
					
						
							|  |  |  | 	not_null<History*> history); | 
					
						
							| 
									
										
										
										
											2017-10-30 23:24:20 +04:00
										 |  |  | bool SharedMediaAllowSearch(Storage::SharedMediaType type); | 
					
						
							| 
									
										
										
										
											2017-08-18 22:14:31 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-29 19:32:01 +04:00
										 |  |  | rpl::producer<SparseIdsSlice> SharedMediaViewer( | 
					
						
							| 
									
										
										
										
											2020-06-08 13:06:50 +04:00
										 |  |  | 	not_null<Main::Session*> session, | 
					
						
							| 
									
										
										
										
											2017-10-29 19:32:01 +04:00
										 |  |  | 	Storage::SharedMediaKey key, | 
					
						
							| 
									
										
										
										
											2017-09-04 22:14:44 +03:00
										 |  |  | 	int limitBefore, | 
					
						
							|  |  |  | 	int limitAfter); | 
					
						
							| 
									
										
										
										
											2017-08-27 14:59:08 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-29 19:32:01 +04:00
										 |  |  | struct SharedMediaMergedKey { | 
					
						
							| 
									
										
										
										
											2017-08-27 14:59:08 +03:00
										 |  |  | 	using Type = Storage::SharedMediaType; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-29 19:32:01 +04:00
										 |  |  | 	SharedMediaMergedKey( | 
					
						
							|  |  |  | 		SparseIdsMergedSlice::Key mergedKey, | 
					
						
							|  |  |  | 		Type type) | 
					
						
							|  |  |  | 	: mergedKey(mergedKey) | 
					
						
							|  |  |  | 	, type(type) { | 
					
						
							| 
									
										
										
										
											2017-08-29 18:15:54 +03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-09-04 22:14:44 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-29 19:32:01 +04:00
										 |  |  | 	bool operator==(const SharedMediaMergedKey &other) const { | 
					
						
							|  |  |  | 		return (mergedKey == other.mergedKey) | 
					
						
							|  |  |  | 			&& (type == other.type); | 
					
						
							| 
									
										
										
										
											2017-08-27 14:59:08 +03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-29 19:32:01 +04:00
										 |  |  | 	SparseIdsMergedSlice::Key mergedKey; | 
					
						
							|  |  |  | 	Type type = Type::kCount; | 
					
						
							| 
									
										
										
										
											2017-08-27 14:59:08 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-29 17:40:55 +03:00
										 |  |  | rpl::producer<SparseIdsMergedSlice> SharedScheduledMediaViewer( | 
					
						
							|  |  |  | 	not_null<Main::Session*> session, | 
					
						
							|  |  |  | 	SharedMediaMergedKey key, | 
					
						
							|  |  |  | 	int limitBefore, | 
					
						
							|  |  |  | 	int limitAfter); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-29 19:32:01 +04:00
										 |  |  | rpl::producer<SparseIdsMergedSlice> SharedMediaMergedViewer( | 
					
						
							| 
									
										
										
										
											2020-06-08 13:06:50 +04:00
										 |  |  | 	not_null<Main::Session*> session, | 
					
						
							| 
									
										
										
										
											2017-10-29 19:32:01 +04:00
										 |  |  | 	SharedMediaMergedKey key, | 
					
						
							| 
									
										
										
										
											2017-09-04 22:14:44 +03:00
										 |  |  | 	int limitBefore, | 
					
						
							|  |  |  | 	int limitAfter); | 
					
						
							| 
									
										
										
										
											2017-08-27 14:59:08 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-04 22:14:44 +03:00
										 |  |  | class SharedMediaWithLastSlice { | 
					
						
							| 
									
										
										
										
											2017-08-29 18:15:54 +03:00
										 |  |  | public: | 
					
						
							|  |  |  | 	using Type = Storage::SharedMediaType; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-31 12:14:53 +04:00
										 |  |  | 	using Value = std::variant<FullMsgId, not_null<PhotoData*>>; | 
					
						
							| 
									
										
										
										
											2017-10-29 19:32:01 +04:00
										 |  |  | 	using MessageId = SparseIdsMergedSlice::UniversalMsgId; | 
					
						
							| 
									
										
										
										
											2020-08-31 12:14:53 +04:00
										 |  |  | 	using UniversalMsgId = std::variant< | 
					
						
							| 
									
										
										
										
											2017-08-29 18:15:54 +03:00
										 |  |  | 		MessageId, | 
					
						
							|  |  |  | 		not_null<PhotoData*>>; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	struct Key { | 
					
						
							|  |  |  | 		Key( | 
					
						
							|  |  |  | 			PeerId peerId, | 
					
						
							|  |  |  | 			PeerId migratedPeerId, | 
					
						
							|  |  |  | 			Type type, | 
					
						
							| 
									
										
										
										
											2021-07-01 03:00:00 +03:00
										 |  |  | 			UniversalMsgId universalId, | 
					
						
							|  |  |  | 			bool scheduled = false) | 
					
						
							| 
									
										
										
										
											2017-10-29 19:32:01 +04:00
										 |  |  | 		: peerId(peerId) | 
					
						
							|  |  |  | 		, migratedPeerId(migratedPeerId) | 
					
						
							|  |  |  | 		, type(type) | 
					
						
							| 
									
										
										
										
											2021-07-01 03:00:00 +03:00
										 |  |  | 		, universalId(universalId) | 
					
						
							|  |  |  | 		, scheduled(scheduled) { | 
					
						
							| 
									
										
										
										
											2020-08-31 12:14:53 +04:00
										 |  |  | 			Expects(v::is<MessageId>(universalId) || type == Type::ChatPhoto); | 
					
						
							| 
									
										
										
										
											2017-08-29 18:15:54 +03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		bool operator==(const Key &other) const { | 
					
						
							|  |  |  | 			return (peerId == other.peerId) | 
					
						
							|  |  |  | 				&& (migratedPeerId == other.migratedPeerId) | 
					
						
							|  |  |  | 				&& (type == other.type) | 
					
						
							|  |  |  | 				&& (universalId == other.universalId); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-10-29 19:32:01 +04:00
										 |  |  | 		bool operator!=(const Key &other) const { | 
					
						
							|  |  |  | 			return !(*this == other); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-08-29 18:15:54 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		PeerId peerId = 0; | 
					
						
							|  |  |  | 		PeerId migratedPeerId = 0; | 
					
						
							|  |  |  | 		Type type = Type::kCount; | 
					
						
							|  |  |  | 		UniversalMsgId universalId; | 
					
						
							| 
									
										
										
										
											2021-07-01 03:00:00 +03:00
										 |  |  | 		bool scheduled = false; | 
					
						
							| 
									
										
										
										
											2017-08-29 18:15:54 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-04 22:14:44 +03:00
										 |  |  | 	SharedMediaWithLastSlice( | 
					
						
							| 
									
										
										
										
											2020-06-08 19:17:33 +04:00
										 |  |  | 		not_null<Main::Session*> session, | 
					
						
							|  |  |  | 		Key key); | 
					
						
							|  |  |  | 	SharedMediaWithLastSlice( | 
					
						
							|  |  |  | 		not_null<Main::Session*> session, | 
					
						
							| 
									
										
										
										
											2017-08-29 18:15:54 +03:00
										 |  |  | 		Key key, | 
					
						
							| 
									
										
										
										
											2017-10-29 19:32:01 +04:00
										 |  |  | 		SparseIdsMergedSlice slice, | 
					
						
							| 
									
										
										
										
											2018-09-21 19:28:46 +03:00
										 |  |  | 		std::optional<SparseIdsMergedSlice> ending); | 
					
						
							| 
									
										
										
										
											2017-08-29 18:15:54 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-21 19:28:46 +03:00
										 |  |  | 	std::optional<int> fullCount() const; | 
					
						
							|  |  |  | 	std::optional<int> skippedBefore() const; | 
					
						
							|  |  |  | 	std::optional<int> skippedAfter() const; | 
					
						
							|  |  |  | 	std::optional<int> indexOf(Value fullId) const; | 
					
						
							| 
									
										
										
										
											2017-08-29 18:15:54 +03:00
										 |  |  | 	int size() const; | 
					
						
							|  |  |  | 	Value operator[](int index) const; | 
					
						
							| 
									
										
										
										
											2018-09-21 19:28:46 +03:00
										 |  |  | 	std::optional<int> distance(const Key &a, const Key &b) const; | 
					
						
							| 
									
										
										
										
											2017-08-29 18:15:54 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-23 13:58:12 +04:00
										 |  |  | 	void reverse(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-29 19:32:01 +04:00
										 |  |  | 	static SparseIdsMergedSlice::Key ViewerKey(const Key &key) { | 
					
						
							| 
									
										
										
										
											2017-08-29 18:15:54 +03:00
										 |  |  | 		return { | 
					
						
							|  |  |  | 			key.peerId, | 
					
						
							|  |  |  | 			key.migratedPeerId, | 
					
						
							| 
									
										
										
										
											2020-08-31 12:14:53 +04:00
										 |  |  | 			v::is<MessageId>(key.universalId) | 
					
						
							| 
									
										
										
										
											2020-09-01 23:56:25 +04:00
										 |  |  | 				? v::get<MessageId>(key.universalId) | 
					
						
							| 
									
										
										
										
											2017-10-29 19:32:01 +04:00
										 |  |  | 				: ServerMaxMsgId - 1 | 
					
						
							| 
									
										
										
										
											2017-08-29 18:15:54 +03:00
										 |  |  | 		}; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-10-29 19:32:01 +04:00
										 |  |  | 	static SparseIdsMergedSlice::Key EndingKey(const Key &key) { | 
					
						
							| 
									
										
										
										
											2017-08-29 18:15:54 +03:00
										 |  |  | 		return { | 
					
						
							|  |  |  | 			key.peerId, | 
					
						
							|  |  |  | 			key.migratedPeerId, | 
					
						
							|  |  |  | 			ServerMaxMsgId - 1 | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-09-04 22:14:44 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							| 
									
										
										
										
											2018-09-21 19:28:46 +03:00
										 |  |  | 	static std::optional<SparseIdsMergedSlice> EndingSlice(const Key &key) { | 
					
						
							| 
									
										
										
										
											2020-08-31 12:14:53 +04:00
										 |  |  | 		return v::is<MessageId>(key.universalId) | 
					
						
							| 
									
										
										
										
											2017-10-29 19:32:01 +04:00
										 |  |  | 			? base::make_optional(SparseIdsMergedSlice(EndingKey(key))) | 
					
						
							| 
									
										
										
										
											2018-09-21 19:28:46 +03:00
										 |  |  | 			: std::nullopt; | 
					
						
							| 
									
										
										
										
											2017-08-29 18:15:54 +03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-08 19:17:33 +04:00
										 |  |  | 	static std::optional<PhotoId> LastPeerPhotoId( | 
					
						
							|  |  |  | 		not_null<Main::Session*> session, | 
					
						
							|  |  |  | 		PeerId peerId); | 
					
						
							| 
									
										
										
										
											2018-09-21 19:28:46 +03:00
										 |  |  | 	static std::optional<bool> IsLastIsolated( | 
					
						
							| 
									
										
										
										
											2020-06-08 19:17:33 +04:00
										 |  |  | 		not_null<Main::Session*> session, | 
					
						
							| 
									
										
										
										
											2017-10-29 19:32:01 +04:00
										 |  |  | 		const SparseIdsMergedSlice &slice, | 
					
						
							| 
									
										
										
										
											2018-09-21 19:28:46 +03:00
										 |  |  | 		const std::optional<SparseIdsMergedSlice> &ending, | 
					
						
							|  |  |  | 		std::optional<PhotoId> lastPeerPhotoId); | 
					
						
							|  |  |  | 	static std::optional<FullMsgId> LastFullMsgId( | 
					
						
							| 
									
										
										
										
											2017-10-29 19:32:01 +04:00
										 |  |  | 		const SparseIdsMergedSlice &slice); | 
					
						
							| 
									
										
										
										
											2018-09-21 19:28:46 +03:00
										 |  |  | 	static std::optional<int> Add( | 
					
						
							|  |  |  | 			const std::optional<int> &a, | 
					
						
							|  |  |  | 			const std::optional<int> &b) { | 
					
						
							|  |  |  | 		return (a && b) ? base::make_optional(*a + *b) : std::nullopt; | 
					
						
							| 
									
										
										
										
											2017-08-29 18:15:54 +03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	static Value ComputeId(PeerId peerId, MsgId msgId) { | 
					
						
							| 
									
										
										
										
											2021-04-02 01:04:10 +04:00
										 |  |  | 		return FullMsgId(peerToChannel(peerId), msgId); | 
					
						
							| 
									
										
										
										
											2017-08-29 18:15:54 +03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	static Value ComputeId(const Key &key) { | 
					
						
							| 
									
										
										
										
											2020-08-31 12:14:53 +04:00
										 |  |  | 		if (const auto messageId = std::get_if<MessageId>(&key.universalId)) { | 
					
						
							| 
									
										
										
										
											2017-10-05 16:35:52 +01:00
										 |  |  | 			return (*messageId >= 0) | 
					
						
							| 
									
										
										
										
											2017-08-29 18:15:54 +03:00
										 |  |  | 				? ComputeId(key.peerId, *messageId) | 
					
						
							| 
									
										
										
										
											2017-10-05 16:35:52 +01:00
										 |  |  | 				: ComputeId(key.migratedPeerId, ServerMaxMsgId + *messageId); | 
					
						
							| 
									
										
										
										
											2017-08-29 18:15:54 +03:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-09-01 23:56:25 +04:00
										 |  |  | 		return v::get<not_null<PhotoData*>>(key.universalId); | 
					
						
							| 
									
										
										
										
											2017-08-29 18:15:54 +03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bool isolatedInSlice() const { | 
					
						
							|  |  |  | 		return (_slice.skippedAfter() != 0); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-09-21 19:28:46 +03:00
										 |  |  | 	std::optional<int> lastPhotoSkip() const { | 
					
						
							| 
									
										
										
										
											2017-08-29 18:15:54 +03:00
										 |  |  | 		return _isolatedLastPhoto | 
					
						
							|  |  |  | 			| [](bool isolated) { return isolated ? 1 : 0; }; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-21 19:28:46 +03:00
										 |  |  | 	std::optional<int> skippedBeforeImpl() const; | 
					
						
							|  |  |  | 	std::optional<int> skippedAfterImpl() const; | 
					
						
							|  |  |  | 	std::optional<int> indexOfImpl(Value fullId) const; | 
					
						
							| 
									
										
										
										
											2017-11-23 13:58:12 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-08 19:17:33 +04:00
										 |  |  | 	not_null<Main::Session*> _session; | 
					
						
							| 
									
										
										
										
											2017-08-29 18:15:54 +03:00
										 |  |  | 	Key _key; | 
					
						
							| 
									
										
										
										
											2017-10-29 19:32:01 +04:00
										 |  |  | 	SparseIdsMergedSlice _slice; | 
					
						
							| 
									
										
										
										
											2018-09-21 19:28:46 +03:00
										 |  |  | 	std::optional<SparseIdsMergedSlice> _ending; | 
					
						
							|  |  |  | 	std::optional<PhotoId> _lastPhotoId; | 
					
						
							|  |  |  | 	std::optional<bool> _isolatedLastPhoto; | 
					
						
							| 
									
										
										
										
											2017-11-23 13:58:12 +04:00
										 |  |  | 	bool _reversed = false; | 
					
						
							| 
									
										
										
										
											2017-08-29 18:15:54 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-04 22:14:44 +03:00
										 |  |  | rpl::producer<SharedMediaWithLastSlice> SharedMediaWithLastViewer( | 
					
						
							| 
									
										
										
										
											2020-06-08 13:06:50 +04:00
										 |  |  | 	not_null<Main::Session*> session, | 
					
						
							| 
									
										
										
										
											2017-09-04 22:14:44 +03:00
										 |  |  | 	SharedMediaWithLastSlice::Key key, | 
					
						
							|  |  |  | 	int limitBefore, | 
					
						
							|  |  |  | 	int limitAfter); | 
					
						
							| 
									
										
										
										
											2017-11-23 13:58:12 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | rpl::producer<SharedMediaWithLastSlice> SharedMediaWithLastReversedViewer( | 
					
						
							| 
									
										
										
										
											2020-06-08 13:06:50 +04:00
										 |  |  | 	not_null<Main::Session*> session, | 
					
						
							| 
									
										
										
										
											2017-11-23 13:58:12 +04:00
										 |  |  | 	SharedMediaWithLastSlice::Key key, | 
					
						
							|  |  |  | 	int limitBefore, | 
					
						
							|  |  |  | 	int limitAfter); |