-
Notifications
You must be signed in to change notification settings - Fork 7.2k
Expand file tree
/
Copy pathdata_drafts.cpp
More file actions
221 lines (206 loc) · 6.38 KB
/
Copy pathdata_drafts.cpp
File metadata and controls
221 lines (206 loc) · 6.38 KB
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
/*
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_drafts.h"
#include "api/api_text_entities.h"
#include "ui/widgets/fields/input_field.h"
#include "chat_helpers/message_field.h"
#include "history/history.h"
#include "history/history_widget.h"
#include "history/history_item_components.h"
#include "iv/iv_rich_page.h"
#include "main/main_session.h"
#include "data/data_changes.h"
#include "data/data_session.h"
#include "data/data_web_page.h"
#include "mainwidget.h"
#include "storage/localstorage.h"
namespace Data {
WebPageDraft WebPageDraft::FromItem(not_null<HistoryItem*> item) {
const auto previewMedia = item->media();
const auto previewPage = previewMedia
? previewMedia->webpage()
: nullptr;
using PageFlag = MediaWebPageFlag;
const auto previewFlags = previewMedia
? previewMedia->webpageFlags()
: PageFlag();
return {
.id = previewPage ? previewPage->id : 0,
.url = previewPage ? previewPage->url : QString(),
.forceLargeMedia = !!(previewFlags & PageFlag::ForceLargeMedia),
.forceSmallMedia = !!(previewFlags & PageFlag::ForceSmallMedia),
.invert = item->invertMedia(),
.manual = !!(previewFlags & PageFlag::Manual),
.removed = !previewPage,
};
}
Draft::Draft(
const TextWithTags &textWithTags,
FullReplyTo reply,
SuggestOptions suggest,
const MessageCursor &cursor,
WebPageDraft webpage,
mtpRequestId saveRequestId)
: textWithTags(textWithTags)
, reply(std::move(reply))
, suggest(suggest)
, cursor(cursor)
, webpage(webpage)
, saveRequestId(saveRequestId) {
}
Draft::Draft(
not_null<const Ui::InputField*> field,
FullReplyTo reply,
SuggestOptions suggest,
WebPageDraft webpage,
mtpRequestId saveRequestId)
: textWithTags(field->getTextWithTags())
, reply(std::move(reply))
, suggest(suggest)
, cursor(field)
, webpage(webpage) {
}
bool DraftIsNull(const Draft *draft) {
return !draft
|| (!draft->hasRichMessage()
&& !draft->reply.messageId
&& !draft->suggest.exists
&& DraftStringIsEmpty(draft->textWithTags.text));
}
bool DraftsAreEqual(const Draft *a, const Draft *b) {
const auto aIsNull = DraftIsNull(a);
const auto bIsNull = DraftIsNull(b);
if (aIsNull) {
return bIsNull;
} else if (bIsNull) {
return false;
} else if (a->hasRichMessage() != b->hasRichMessage()) {
return false;
} else if (a->hasRichMessage()
&& !Iv::RichPagesEqual(*a->richMessage, *b->richMessage)) {
return false;
}
return (a->textWithTags == b->textWithTags)
&& (a->reply == b->reply)
&& (a->suggest == b->suggest)
&& (a->webpage == b->webpage);
}
void ApplyPeerCloudDraft(
not_null<Main::Session*> session,
PeerId peerId,
MsgId topicRootId,
PeerId monoforumPeerId,
const MTPDdraftMessage &draft) {
const auto history = session->data().history(peerId);
const auto date = draft.vdate().v;
if (history->skipCloudDraftUpdate(topicRootId, monoforumPeerId, date)) {
return;
}
const auto richMessage = draft.vrich_message()
? Iv::ParseRichPage(session, *draft.vrich_message())
: std::shared_ptr<const Iv::RichPage>();
const auto textWithTags = richMessage
? TextWithTags()
: TextWithTags{
qs(draft.vmessage()),
TextUtilities::ConvertEntitiesToTextTags(
Api::EntitiesFromMTP(
session,
draft.ventities().value_or_empty()))
};
auto replyTo = draft.vreply_to()
? ReplyToFromMTP(history, *draft.vreply_to())
: FullReplyTo();
replyTo.topicRootId = topicRootId;
replyTo.monoforumPeerId = monoforumPeerId;
auto webpage = WebPageDraft{
.invert = draft.is_invert_media(),
.removed = draft.is_no_webpage(),
};
const auto media = draft.vmedia();
if (!richMessage && media) {
media->match([&](const MTPDmessageMediaWebPage &data) {
const auto parsed = session->data().processWebpage(
data.vwebpage());
if (!parsed->failed) {
webpage.forceLargeMedia = data.is_force_large_media();
webpage.forceSmallMedia = data.is_force_small_media();
webpage.manual = data.is_manual();
webpage.url = parsed->url;
webpage.id = parsed->id;
}
}, [](const auto &) {});
}
auto suggest = SuggestOptions();
if (!history->suggestDraftAllowed()) {
// Don't apply suggest options in unsupported chats.
} else if (const auto suggested = draft.vsuggested_post()) {
const auto &data = suggested->data();
suggest.exists = 1;
suggest.date = data.vschedule_date().value_or_empty();
const auto price = CreditsAmountFromTL(data.vprice());
suggest.priceWhole = price.whole();
suggest.priceNano = price.nano();
suggest.ton = price.ton() ? 1 : 0;
}
auto cloudDraft = std::make_unique<Draft>(
textWithTags,
replyTo,
suggest,
MessageCursor(Ui::kQFixedMax, Ui::kQFixedMax, Ui::kQFixedMax),
std::move(webpage));
cloudDraft->date = date;
cloudDraft->richMessage = richMessage;
cloudDraft->richMessageSummary = Iv::FlattenRichPageSummary(richMessage);
history->setCloudDraft(std::move(cloudDraft));
history->applyCloudDraft(topicRootId, monoforumPeerId);
}
void ClearPeerCloudDraft(
not_null<Main::Session*> session,
PeerId peerId,
MsgId topicRootId,
PeerId monoforumPeerId,
TimeId date) {
const auto history = session->data().history(peerId);
if (history->skipCloudDraftUpdate(topicRootId, monoforumPeerId, date)) {
return;
}
history->clearCloudDraft(topicRootId, monoforumPeerId);
history->applyCloudDraft(topicRootId, monoforumPeerId);
}
void SetChatLinkDraft(not_null<PeerData*> peer, TextWithEntities draft) {
static const auto kInlineStart = QRegularExpression("^@[a-zA-Z0-9_]");
if (kInlineStart.match(draft.text).hasMatch()) {
draft = TextWithEntities().append(' ').append(std::move(draft));
}
const auto textWithTags = TextWithTags{
draft.text,
TextUtilities::ConvertEntitiesToTextTags(draft.entities)
};
const auto cursor = MessageCursor{
int(textWithTags.text.size()),
int(textWithTags.text.size()),
Ui::kQFixedMax
};
const auto history = peer->owner().history(peer->id);
const auto topicRootId = MsgId();
const auto monoforumPeerId = PeerId();
history->setLocalDraft(std::make_unique<Draft>(
textWithTags,
FullReplyTo{
.topicRootId = topicRootId,
.monoforumPeerId = monoforumPeerId,
},
SuggestOptions(),
cursor,
WebPageDraft()));
history->clearLocalEditDraft(topicRootId, monoforumPeerId);
history->session().changes().entryUpdated(
history,
EntryUpdate::Flag::LocalDraftSet);
}
} // namespace Data