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
|
|
|
|
|
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,
|
|
|
|
const MTPDdraftMessage &draft);
|
|
|
|
void ClearPeerCloudDraft(
|
|
|
|
not_null<Main::Session*> session,
|
|
|
|
PeerId peerId,
|
|
|
|
TimeId date);
|
2016-06-09 14:31:10 +00:00
|
|
|
|
2021-01-29 11:27:17 +00:00
|
|
|
enum class PreviewState : char {
|
|
|
|
Allowed,
|
|
|
|
Cancelled,
|
|
|
|
EmptyOnEdit,
|
|
|
|
};
|
|
|
|
|
2016-06-09 14:31:10 +00:00
|
|
|
struct Draft {
|
2018-02-03 19:52:35 +00:00
|
|
|
Draft() = default;
|
|
|
|
Draft(
|
|
|
|
const TextWithTags &textWithTags,
|
|
|
|
MsgId msgId,
|
|
|
|
const MessageCursor &cursor,
|
2021-01-29 11:27:17 +00:00
|
|
|
PreviewState previewState,
|
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,
|
2018-02-03 19:52:35 +00:00
|
|
|
MsgId msgId,
|
2021-01-29 11:27:17 +00:00
|
|
|
PreviewState previewState,
|
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;
|
|
|
|
MsgId msgId = 0; // replyToId for message draft, editMsgId for edit draft
|
|
|
|
MessageCursor cursor;
|
2021-01-29 11:27:17 +00:00
|
|
|
PreviewState previewState = PreviewState::Allowed;
|
2016-06-09 14:31:10 +00:00
|
|
|
mtpRequestId saveRequestId = 0;
|
|
|
|
};
|
|
|
|
|
2020-11-13 17:27:08 +00: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 14:31:10 +00:00
|
|
|
inline bool draftStringIsEmpty(const QString &text) {
|
|
|
|
for_const (auto ch, text) {
|
|
|
|
if (!ch.isSpace()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-15 15:36:04 +00:00
|
|
|
inline bool draftIsNull(const Draft *draft) {
|
|
|
|
return !draft
|
|
|
|
|| (draftStringIsEmpty(draft->textWithTags.text) && !draft->msgId);
|
2016-06-09 14:31:10 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 15:36:04 +00:00
|
|
|
inline bool draftsAreEqual(const Draft *a, const Draft *b) {
|
2016-06-09 14:31:10 +00:00
|
|
|
bool aIsNull = draftIsNull(a);
|
|
|
|
bool bIsNull = draftIsNull(b);
|
|
|
|
if (aIsNull) {
|
|
|
|
return bIsNull;
|
|
|
|
} else if (bIsNull) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-11-15 15:36:04 +00:00
|
|
|
return (a->textWithTags == b->textWithTags)
|
|
|
|
&& (a->msgId == b->msgId)
|
2021-01-29 11:27:17 +00:00
|
|
|
&& (a->previewState == b->previewState);
|
2016-06-09 14:31:10 +00:00
|
|
|
}
|
2016-05-31 09:46:31 +00:00
|
|
|
|
|
|
|
} // namespace Data
|