| 
									
										
										
										
											2016-05-31 12:46: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. | 
					
						
							| 
									
										
										
										
											2016-05-31 12:46: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
 | 
					
						
							| 
									
										
										
										
											2016-05-31 12:46:31 +03:00
										 |  |  | */ | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-15 14:56:49 +03:00
										 |  |  | namespace Ui { | 
					
						
							| 
									
										
										
										
											2018-05-22 00:31:46 +03:00
										 |  |  | class InputField; | 
					
						
							| 
									
										
										
										
											2016-11-15 14:56:49 +03:00
										 |  |  | } // namespace Ui
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-08 12:03:45 +04:00
										 |  |  | namespace Main { | 
					
						
							|  |  |  | class Session; | 
					
						
							|  |  |  | } // namespace Main
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-31 12:46:31 +03:00
										 |  |  | namespace Data { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-08 12:03:45 +04:00
										 |  |  | void ApplyPeerCloudDraft( | 
					
						
							|  |  |  | 	not_null<Main::Session*> session, | 
					
						
							|  |  |  | 	PeerId peerId, | 
					
						
							|  |  |  | 	const MTPDdraftMessage &draft); | 
					
						
							|  |  |  | void ClearPeerCloudDraft( | 
					
						
							|  |  |  | 	not_null<Main::Session*> session, | 
					
						
							|  |  |  | 	PeerId peerId, | 
					
						
							|  |  |  | 	TimeId date); | 
					
						
							| 
									
										
										
										
											2016-06-09 17:31:10 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-29 15:27:17 +04:00
										 |  |  | enum class PreviewState : char { | 
					
						
							|  |  |  | 	Allowed, | 
					
						
							|  |  |  | 	Cancelled, | 
					
						
							|  |  |  | 	EmptyOnEdit, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-09 17:31:10 +03:00
										 |  |  | struct Draft { | 
					
						
							| 
									
										
										
										
											2018-02-03 22:52:35 +03:00
										 |  |  | 	Draft() = default; | 
					
						
							|  |  |  | 	Draft( | 
					
						
							|  |  |  | 		const TextWithTags &textWithTags, | 
					
						
							|  |  |  | 		MsgId msgId, | 
					
						
							|  |  |  | 		const MessageCursor &cursor, | 
					
						
							| 
									
										
										
										
											2021-01-29 15:27:17 +04:00
										 |  |  | 		PreviewState previewState, | 
					
						
							| 
									
										
										
										
											2018-02-03 22:52:35 +03:00
										 |  |  | 		mtpRequestId saveRequestId = 0); | 
					
						
							|  |  |  | 	Draft( | 
					
						
							| 
									
										
										
										
											2018-05-22 00:31:46 +03:00
										 |  |  | 		not_null<const Ui::InputField*> field, | 
					
						
							| 
									
										
										
										
											2018-02-03 22:52:35 +03:00
										 |  |  | 		MsgId msgId, | 
					
						
							| 
									
										
										
										
											2021-01-29 15:27:17 +04:00
										 |  |  | 		PreviewState previewState, | 
					
						
							| 
									
										
										
										
											2018-02-03 22:52:35 +03:00
										 |  |  | 		mtpRequestId saveRequestId = 0); | 
					
						
							| 
									
										
										
										
											2016-11-15 14:56:49 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-03 22:52:35 +03:00
										 |  |  | 	TimeId date = 0; | 
					
						
							| 
									
										
										
										
											2016-06-09 17:31:10 +03:00
										 |  |  | 	TextWithTags textWithTags; | 
					
						
							|  |  |  | 	MsgId msgId = 0; // replyToId for message draft, editMsgId for edit draft
 | 
					
						
							|  |  |  | 	MessageCursor cursor; | 
					
						
							| 
									
										
										
										
											2021-01-29 15:27:17 +04:00
										 |  |  | 	PreviewState previewState = PreviewState::Allowed; | 
					
						
							| 
									
										
										
										
											2016-06-09 17:31:10 +03:00
										 |  |  | 	mtpRequestId saveRequestId = 0; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 20:27:08 +03:00
										 |  |  | class DraftKey { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  | 	[[nodiscard]] static DraftKey None() { | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	[[nodiscard]] static DraftKey Local() { | 
					
						
							|  |  |  | 		return kLocalDraftIndex; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	[[nodiscard]] static DraftKey LocalEdit() { | 
					
						
							|  |  |  | 		return kLocalDraftIndex + kEditDraftShift; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	[[nodiscard]] static DraftKey Cloud() { | 
					
						
							|  |  |  | 		return kCloudDraftIndex; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	[[nodiscard]] static DraftKey Scheduled() { | 
					
						
							|  |  |  | 		return kScheduledDraftIndex; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	[[nodiscard]] static DraftKey ScheduledEdit() { | 
					
						
							|  |  |  | 		return kScheduledDraftIndex + kEditDraftShift; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	[[nodiscard]] static DraftKey Replies(MsgId rootId) { | 
					
						
							|  |  |  | 		return rootId; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	[[nodiscard]] static DraftKey RepliesEdit(MsgId rootId) { | 
					
						
							|  |  |  | 		return rootId + kEditDraftShift; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	[[nodiscard]] static DraftKey FromSerialized(int32 value) { | 
					
						
							|  |  |  | 		return value; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	[[nodiscard]] int32 serialize() const { | 
					
						
							|  |  |  | 		return _value; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	inline bool operator<(const DraftKey &other) const { | 
					
						
							|  |  |  | 		return _value < other._value; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	inline bool operator==(const DraftKey &other) const { | 
					
						
							|  |  |  | 		return _value == other._value; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	inline bool operator>(const DraftKey &other) const { | 
					
						
							|  |  |  | 		return (other < *this); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	inline bool operator<=(const DraftKey &other) const { | 
					
						
							|  |  |  | 		return !(other < *this); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	inline bool operator>=(const DraftKey &other) const { | 
					
						
							|  |  |  | 		return !(*this < other); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	inline bool operator!=(const DraftKey &other) const { | 
					
						
							|  |  |  | 		return !(*this == other); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	inline explicit operator bool() const { | 
					
						
							|  |  |  | 		return _value != 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  | 	DraftKey(int value) : _value(value) { | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	static constexpr auto kLocalDraftIndex = -1; | 
					
						
							|  |  |  | 	static constexpr auto kCloudDraftIndex = -2; | 
					
						
							|  |  |  | 	static constexpr auto kScheduledDraftIndex = -3; | 
					
						
							|  |  |  | 	static constexpr auto kEditDraftShift = ServerMaxMsgId; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	int _value = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | using HistoryDrafts = base::flat_map<DraftKey, std::unique_ptr<Draft>>; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-09 17:31:10 +03:00
										 |  |  | inline bool draftStringIsEmpty(const QString &text) { | 
					
						
							|  |  |  | 	for_const (auto ch, text) { | 
					
						
							|  |  |  | 		if (!ch.isSpace()) { | 
					
						
							|  |  |  | 			return false; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-15 19:36:04 +04:00
										 |  |  | inline bool draftIsNull(const Draft *draft) { | 
					
						
							|  |  |  | 	return !draft | 
					
						
							|  |  |  | 		|| (draftStringIsEmpty(draft->textWithTags.text) && !draft->msgId); | 
					
						
							| 
									
										
										
										
											2016-06-09 17:31:10 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-15 19:36:04 +04:00
										 |  |  | inline bool draftsAreEqual(const Draft *a, const Draft *b) { | 
					
						
							| 
									
										
										
										
											2016-06-09 17:31:10 +03:00
										 |  |  | 	bool aIsNull = draftIsNull(a); | 
					
						
							|  |  |  | 	bool bIsNull = draftIsNull(b); | 
					
						
							|  |  |  | 	if (aIsNull) { | 
					
						
							|  |  |  | 		return bIsNull; | 
					
						
							|  |  |  | 	} else if (bIsNull) { | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-15 19:36:04 +04:00
										 |  |  | 	return (a->textWithTags == b->textWithTags) | 
					
						
							|  |  |  | 		&& (a->msgId == b->msgId) | 
					
						
							| 
									
										
										
										
											2021-01-29 15:27:17 +04:00
										 |  |  | 		&& (a->previewState == b->previewState); | 
					
						
							| 
									
										
										
										
											2016-06-09 17:31:10 +03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-05-31 12:46:31 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | } // namespace Data
 |