2016-05-31 09:46:31 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2016-05-31 09:46:31 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2016-05-31 09:46:31 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2023-10-10 06:49:22 +00:00
|
|
|
#include "data/data_msg_id.h"
|
|
|
|
|
2016-11-15 11:56:49 +00:00
|
|
|
namespace Ui {
|
2018-05-21 21:31:46 +00:00
|
|
|
class InputField;
|
2016-11-15 11:56:49 +00:00
|
|
|
} // namespace Ui
|
|
|
|
|
2020-06-08 08:03:45 +00:00
|
|
|
namespace Main {
|
|
|
|
class Session;
|
|
|
|
} // namespace Main
|
|
|
|
|
2016-05-31 09:46:31 +00:00
|
|
|
namespace Data {
|
|
|
|
|
2020-06-08 08:03:45 +00:00
|
|
|
void ApplyPeerCloudDraft(
|
|
|
|
not_null<Main::Session*> session,
|
|
|
|
PeerId peerId,
|
2022-10-17 16:29:48 +00:00
|
|
|
MsgId topicRootId,
|
2020-06-08 08:03:45 +00:00
|
|
|
const MTPDdraftMessage &draft);
|
|
|
|
void ClearPeerCloudDraft(
|
|
|
|
not_null<Main::Session*> session,
|
|
|
|
PeerId peerId,
|
2022-10-17 16:29:48 +00:00
|
|
|
MsgId topicRootId,
|
2020-06-08 08:03:45 +00:00
|
|
|
TimeId date);
|
2016-06-09 14:31:10 +00:00
|
|
|
|
2023-10-19 14:20:27 +00:00
|
|
|
struct WebPageDraft {
|
|
|
|
[[nodiscard]] static WebPageDraft FromItem(not_null<HistoryItem*> item);
|
|
|
|
|
|
|
|
WebPageId id = 0;
|
|
|
|
QString url;
|
|
|
|
bool forceLargeMedia : 1 = false;
|
|
|
|
bool forceSmallMedia : 1 = false;
|
|
|
|
bool invert : 1 = false;
|
|
|
|
bool manual : 1 = false;
|
|
|
|
bool removed : 1 = false;
|
|
|
|
|
|
|
|
friend inline bool operator==(const WebPageDraft&, const WebPageDraft&)
|
|
|
|
= default;
|
2021-01-29 11:27:17 +00:00
|
|
|
};
|
|
|
|
|
2016-06-09 14:31:10 +00:00
|
|
|
struct Draft {
|
2018-02-03 19:52:35 +00:00
|
|
|
Draft() = default;
|
|
|
|
Draft(
|
|
|
|
const TextWithTags &textWithTags,
|
2023-10-10 06:49:22 +00:00
|
|
|
FullReplyTo reply,
|
2018-02-03 19:52:35 +00:00
|
|
|
const MessageCursor &cursor,
|
2023-10-19 14:20:27 +00:00
|
|
|
WebPageDraft webpage,
|
2018-02-03 19:52:35 +00:00
|
|
|
mtpRequestId saveRequestId = 0);
|
|
|
|
Draft(
|
2018-05-21 21:31:46 +00:00
|
|
|
not_null<const Ui::InputField*> field,
|
2023-10-10 06:49:22 +00:00
|
|
|
FullReplyTo reply,
|
2023-10-19 14:20:27 +00:00
|
|
|
WebPageDraft webpage,
|
2018-02-03 19:52:35 +00:00
|
|
|
mtpRequestId saveRequestId = 0);
|
2016-11-15 11:56:49 +00:00
|
|
|
|
2018-02-03 19:52:35 +00:00
|
|
|
TimeId date = 0;
|
2016-06-09 14:31:10 +00:00
|
|
|
TextWithTags textWithTags;
|
2023-10-10 06:49:22 +00:00
|
|
|
FullReplyTo reply; // reply.messageId.msg is editMsgId for edit draft.
|
2016-06-09 14:31:10 +00:00
|
|
|
MessageCursor cursor;
|
2023-10-19 14:20:27 +00:00
|
|
|
WebPageDraft webpage;
|
2016-06-09 14:31:10 +00:00
|
|
|
mtpRequestId saveRequestId = 0;
|
|
|
|
};
|
|
|
|
|
2020-11-13 17:27:08 +00:00
|
|
|
class DraftKey {
|
|
|
|
public:
|
2022-10-17 16:29:48 +00:00
|
|
|
[[nodiscard]] static constexpr DraftKey None() {
|
2020-11-13 17:27:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2022-10-17 16:29:48 +00:00
|
|
|
[[nodiscard]] static constexpr DraftKey Local(MsgId topicRootId) {
|
|
|
|
return (topicRootId < 0 || topicRootId >= ServerMaxMsgId)
|
|
|
|
? None()
|
|
|
|
: (topicRootId ? topicRootId.bare : kLocalDraftIndex);
|
|
|
|
}
|
|
|
|
[[nodiscard]] static constexpr DraftKey LocalEdit(MsgId topicRootId) {
|
|
|
|
return (topicRootId < 0 || topicRootId >= ServerMaxMsgId)
|
|
|
|
? None()
|
|
|
|
: ((topicRootId ? topicRootId.bare : kLocalDraftIndex)
|
|
|
|
+ kEditDraftShift);
|
|
|
|
}
|
|
|
|
[[nodiscard]] static constexpr DraftKey Cloud(MsgId topicRootId) {
|
|
|
|
return (topicRootId < 0 || topicRootId >= ServerMaxMsgId)
|
|
|
|
? None()
|
|
|
|
: topicRootId
|
|
|
|
? (kCloudDraftShift + topicRootId.bare)
|
|
|
|
: kCloudDraftIndex;
|
|
|
|
}
|
|
|
|
[[nodiscard]] static constexpr DraftKey Scheduled() {
|
2020-11-13 17:27:08 +00:00
|
|
|
return kScheduledDraftIndex;
|
|
|
|
}
|
2022-10-17 16:29:48 +00:00
|
|
|
[[nodiscard]] static constexpr DraftKey ScheduledEdit() {
|
2020-11-13 17:27:08 +00:00
|
|
|
return kScheduledDraftIndex + kEditDraftShift;
|
|
|
|
}
|
|
|
|
|
2022-10-17 16:29:48 +00:00
|
|
|
[[nodiscard]] static constexpr DraftKey FromSerialized(qint64 value) {
|
2020-11-13 17:27:08 +00:00
|
|
|
return value;
|
|
|
|
}
|
2022-10-17 16:29:48 +00:00
|
|
|
[[nodiscard]] constexpr qint64 serialize() const {
|
2020-11-13 17:27:08 +00:00
|
|
|
return _value;
|
|
|
|
}
|
|
|
|
|
2022-10-17 16:29:48 +00:00
|
|
|
[[nodiscard]] static constexpr DraftKey FromSerializedOld(int32 value) {
|
|
|
|
return !value
|
|
|
|
? None()
|
|
|
|
: (value == kLocalDraftIndex + kEditDraftShiftOld)
|
|
|
|
? LocalEdit(0)
|
|
|
|
: (value == kScheduledDraftIndex + kEditDraftShiftOld)
|
|
|
|
? ScheduledEdit()
|
|
|
|
: (value > 0 && value < 0x4000'0000)
|
|
|
|
? Local(MsgId(value))
|
|
|
|
: (value > kEditDraftShiftOld
|
|
|
|
&& value < kEditDraftShiftOld + 0x4000'000)
|
|
|
|
? LocalEdit(int64(value - kEditDraftShiftOld))
|
|
|
|
: None();
|
|
|
|
}
|
|
|
|
[[nodiscard]] constexpr bool isLocal() const {
|
|
|
|
return (_value == kLocalDraftIndex)
|
|
|
|
|| (_value > 0 && _value < ServerMaxMsgId.bare);
|
|
|
|
}
|
|
|
|
[[nodiscard]] constexpr bool isCloud() const {
|
|
|
|
return (_value == kCloudDraftIndex)
|
|
|
|
|| (_value > kCloudDraftShift
|
|
|
|
&& _value < kCloudDraftShift + ServerMaxMsgId.bare);
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr MsgId topicRootId() const {
|
|
|
|
const auto max = ServerMaxMsgId.bare;
|
|
|
|
if (_value > kCloudDraftShift && _value < kCloudDraftShift + max) {
|
|
|
|
return (_value - kCloudDraftShift);
|
|
|
|
} else if (_value > kEditDraftShift && _value < kEditDraftShift + max) {
|
|
|
|
return (_value - kEditDraftShift);
|
|
|
|
} else if (_value > 0 && _value < max) {
|
|
|
|
return _value;
|
|
|
|
}
|
|
|
|
return 0;
|
2020-11-13 17:27:08 +00:00
|
|
|
}
|
2022-10-17 16:29:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
friend inline constexpr auto operator<=>(DraftKey, DraftKey) = default;
|
|
|
|
|
2020-11-13 17:27:08 +00:00
|
|
|
inline explicit operator bool() const {
|
|
|
|
return _value != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2022-10-17 16:29:48 +00:00
|
|
|
constexpr DraftKey(int64 value) : _value(value) {
|
2020-11-13 17:27:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr auto kLocalDraftIndex = -1;
|
|
|
|
static constexpr auto kCloudDraftIndex = -2;
|
|
|
|
static constexpr auto kScheduledDraftIndex = -3;
|
2021-09-30 06:04:55 +00:00
|
|
|
static constexpr auto kEditDraftShift = ServerMaxMsgId.bare;
|
2022-10-17 16:29:48 +00:00
|
|
|
static constexpr auto kCloudDraftShift = 2 * ServerMaxMsgId.bare;
|
2021-09-30 06:04:55 +00:00
|
|
|
static constexpr auto kEditDraftShiftOld = 0x3FFF'FFFF;
|
2020-11-13 17:27:08 +00:00
|
|
|
|
2021-09-30 06:04:55 +00:00
|
|
|
int64 _value = 0;
|
2020-11-13 17:27:08 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
using HistoryDrafts = base::flat_map<DraftKey, std::unique_ptr<Draft>>;
|
|
|
|
|
2022-10-17 16:29:48 +00:00
|
|
|
[[nodiscard]] inline bool DraftStringIsEmpty(const QString &text) {
|
2021-09-08 10:53:54 +00:00
|
|
|
for (const auto &ch : text) {
|
2016-06-09 14:31:10 +00:00
|
|
|
if (!ch.isSpace()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-10-17 16:29:48 +00:00
|
|
|
[[nodiscard]] inline bool DraftIsNull(const Draft *draft) {
|
2018-11-15 15:36:04 +00:00
|
|
|
return !draft
|
2023-10-10 06:49:22 +00:00
|
|
|
|| (!draft->reply.messageId
|
|
|
|
&& DraftStringIsEmpty(draft->textWithTags.text));
|
2016-06-09 14:31:10 +00:00
|
|
|
}
|
|
|
|
|
2022-10-17 16:29:48 +00:00
|
|
|
[[nodiscard]] inline bool DraftsAreEqual(const Draft *a, const Draft *b) {
|
|
|
|
const auto aIsNull = DraftIsNull(a);
|
|
|
|
const auto bIsNull = DraftIsNull(b);
|
2016-06-09 14:31:10 +00:00
|
|
|
if (aIsNull) {
|
|
|
|
return bIsNull;
|
|
|
|
} else if (bIsNull) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-15 15:36:04 +00:00
|
|
|
return (a->textWithTags == b->textWithTags)
|
2023-10-10 06:49:22 +00:00
|
|
|
&& (a->reply == b->reply)
|
2023-10-19 14:20:27 +00:00
|
|
|
&& (a->webpage == b->webpage);
|
2016-06-09 14:31:10 +00:00
|
|
|
}
|
2016-05-31 09:46:31 +00:00
|
|
|
|
|
|
|
} // namespace Data
|