-
Notifications
You must be signed in to change notification settings - Fork 7.2k
Expand file tree
/
Copy pathdata_changes.cpp
More file actions
394 lines (333 loc) · 10.2 KB
/
Copy pathdata_changes.cpp
File metadata and controls
394 lines (333 loc) · 10.2 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
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_changes.h"
#include "main/main_session.h"
namespace Data {
template <typename DataType, typename UpdateType>
void Changes::Manager<DataType, UpdateType>::updated(
not_null<DataType*> data,
Flags flags,
bool dropScheduled) {
sendRealtimeNotifications(data, flags);
if (dropScheduled) {
const auto i = _updates.find(data);
if (i != _updates.end()) {
flags |= i->second;
_updates.erase(i);
}
_stream.fire({ data, flags });
} else {
_updates[data] |= flags;
}
}
template <typename DataType, typename UpdateType>
void Changes::Manager<DataType, UpdateType>::sendRealtimeNotifications(
not_null<DataType*> data,
Flags flags) {
for (auto i = 0; i != kCount; ++i) {
const auto flag = static_cast<Flag>(1U << i);
if (flags & flag) {
_realtimeStreams[i].fire({ data, flags });
}
}
}
template <typename DataType, typename UpdateType>
rpl::producer<UpdateType> Changes::Manager<DataType, UpdateType>::updates(
Flags flags) const {
return _stream.events(
) | rpl::filter([=](const UpdateType &update) {
return (update.flags & flags);
});
}
template <typename DataType, typename UpdateType>
rpl::producer<UpdateType> Changes::Manager<DataType, UpdateType>::updates(
not_null<DataType*> data,
Flags flags) const {
return _stream.events(
) | rpl::filter([=](const UpdateType &update) {
const auto &[updateData, updateFlags] = update;
return (updateData == data) && (updateFlags & flags);
});
}
template <typename DataType, typename UpdateType>
auto Changes::Manager<DataType, UpdateType>::realtimeUpdates(Flag flag) const
-> rpl::producer<UpdateType> {
return _realtimeStreams[details::CountBit(flag)].events();
}
template <typename DataType, typename UpdateType>
rpl::producer<UpdateType> Changes::Manager<DataType, UpdateType>::flagsValue(
not_null<DataType*> data,
Flags flags) const {
return rpl::single(
UpdateType{ data, flags }
) | rpl::then(updates(data, flags));
}
template <typename DataType, typename UpdateType>
void Changes::Manager<DataType, UpdateType>::drop(not_null<DataType*> data) {
_updates.remove(data);
}
template <typename DataType, typename UpdateType>
void Changes::Manager<DataType, UpdateType>::sendNotifications() {
for (const auto &[data, flags] : base::take(_updates)) {
_stream.fire({ data, flags });
}
}
Changes::Changes(not_null<Main::Session*> session) : _session(session) {
}
Main::Session &Changes::session() const {
return *_session;
}
void Changes::nameUpdated(
not_null<PeerData*> peer,
base::flat_set<QChar> oldFirstLetters) {
_nameStream.fire({ peer, std::move(oldFirstLetters) });
}
rpl::producer<NameUpdate> Changes::realtimeNameUpdates() const {
return _nameStream.events();
}
rpl::producer<NameUpdate> Changes::realtimeNameUpdates(
not_null<PeerData*> peer) const {
return _nameStream.events() | rpl::filter([=](const NameUpdate &update) {
return (update.peer == peer);
});
}
void Changes::peerUpdated(not_null<PeerData*> peer, PeerUpdate::Flags flags) {
_peerChanges.updated(peer, flags);
scheduleNotifications();
}
rpl::producer<PeerUpdate> Changes::peerUpdates(
PeerUpdate::Flags flags) const {
return _peerChanges.updates(flags);
}
rpl::producer<PeerUpdate> Changes::peerUpdates(
not_null<PeerData*> peer,
PeerUpdate::Flags flags) const {
return _peerChanges.updates(peer, flags);
}
rpl::producer<PeerUpdate> Changes::peerFlagsValue(
not_null<PeerData*> peer,
PeerUpdate::Flags flags) const {
return _peerChanges.flagsValue(peer, flags);
}
rpl::producer<PeerUpdate> Changes::realtimePeerUpdates(
PeerUpdate::Flag flag) const {
return _peerChanges.realtimeUpdates(flag);
}
void Changes::historyUpdated(
not_null<History*> history,
HistoryUpdate::Flags flags) {
_historyChanges.updated(history, flags);
scheduleNotifications();
}
rpl::producer<HistoryUpdate> Changes::historyUpdates(
HistoryUpdate::Flags flags) const {
return _historyChanges.updates(flags);
}
rpl::producer<HistoryUpdate> Changes::historyUpdates(
not_null<History*> history,
HistoryUpdate::Flags flags) const {
return _historyChanges.updates(history, flags);
}
rpl::producer<HistoryUpdate> Changes::historyFlagsValue(
not_null<History*> history,
HistoryUpdate::Flags flags) const {
return _historyChanges.flagsValue(history, flags);
}
rpl::producer<HistoryUpdate> Changes::realtimeHistoryUpdates(
HistoryUpdate::Flag flag) const {
return _historyChanges.realtimeUpdates(flag);
}
void Changes::topicUpdated(
not_null<ForumTopic*> topic,
TopicUpdate::Flags flags) {
const auto drop = (flags & TopicUpdate::Flag::Destroyed);
_topicChanges.updated(topic, flags, drop);
if (!drop) {
scheduleNotifications();
}
}
rpl::producer<TopicUpdate> Changes::topicUpdates(
TopicUpdate::Flags flags) const {
return _topicChanges.updates(flags);
}
rpl::producer<TopicUpdate> Changes::topicUpdates(
not_null<ForumTopic*> topic,
TopicUpdate::Flags flags) const {
return _topicChanges.updates(topic, flags);
}
rpl::producer<TopicUpdate> Changes::topicFlagsValue(
not_null<ForumTopic*> topic,
TopicUpdate::Flags flags) const {
return _topicChanges.flagsValue(topic, flags);
}
rpl::producer<TopicUpdate> Changes::realtimeTopicUpdates(
TopicUpdate::Flag flag) const {
return _topicChanges.realtimeUpdates(flag);
}
void Changes::topicRemoved(not_null<ForumTopic*> topic) {
_topicChanges.drop(topic);
}
void Changes::sublistUpdated(
not_null<SavedSublist*> sublist,
SublistUpdate::Flags flags) {
const auto drop = (flags & SublistUpdate::Flag::Destroyed);
_sublistChanges.updated(sublist, flags, drop);
if (!drop) {
scheduleNotifications();
}
}
rpl::producer<SublistUpdate> Changes::sublistUpdates(
SublistUpdate::Flags flags) const {
return _sublistChanges.updates(flags);
}
rpl::producer<SublistUpdate> Changes::sublistUpdates(
not_null<SavedSublist*> sublist,
SublistUpdate::Flags flags) const {
return _sublistChanges.updates(sublist, flags);
}
rpl::producer<SublistUpdate> Changes::sublistFlagsValue(
not_null<SavedSublist*> sublist,
SublistUpdate::Flags flags) const {
return _sublistChanges.flagsValue(sublist, flags);
}
rpl::producer<SublistUpdate> Changes::realtimeSublistUpdates(
SublistUpdate::Flag flag) const {
return _sublistChanges.realtimeUpdates(flag);
}
void Changes::sublistRemoved(not_null<SavedSublist*> sublist) {
_sublistChanges.drop(sublist);
}
void Changes::messageUpdated(
not_null<HistoryItem*> item,
MessageUpdate::Flags flags) {
const auto drop = (flags & MessageUpdate::Flag::Destroyed);
_messageChanges.updated(item, flags, drop);
if (!drop) {
scheduleNotifications();
}
}
rpl::producer<MessageUpdate> Changes::messageUpdates(
MessageUpdate::Flags flags) const {
return _messageChanges.updates(flags);
}
rpl::producer<MessageUpdate> Changes::messageUpdates(
not_null<HistoryItem*> item,
MessageUpdate::Flags flags) const {
return _messageChanges.updates(item, flags);
}
rpl::producer<MessageUpdate> Changes::messageFlagsValue(
not_null<HistoryItem*> item,
MessageUpdate::Flags flags) const {
return _messageChanges.flagsValue(item, flags);
}
rpl::producer<MessageUpdate> Changes::realtimeMessageUpdates(
MessageUpdate::Flag flag) const {
return _messageChanges.realtimeUpdates(flag);
}
void Changes::entryUpdated(
not_null<Dialogs::Entry*> entry,
EntryUpdate::Flags flags) {
const auto drop = (flags & EntryUpdate::Flag::Destroyed);
_entryChanges.updated(entry, flags, drop);
if (!drop) {
scheduleNotifications();
}
}
rpl::producer<EntryUpdate> Changes::entryUpdates(
EntryUpdate::Flags flags) const {
return _entryChanges.updates(flags);
}
rpl::producer<EntryUpdate> Changes::entryUpdates(
not_null<Dialogs::Entry*> entry,
EntryUpdate::Flags flags) const {
return _entryChanges.updates(entry, flags);
}
rpl::producer<EntryUpdate> Changes::entryFlagsValue(
not_null<Dialogs::Entry*> entry,
EntryUpdate::Flags flags) const {
return _entryChanges.flagsValue(entry, flags);
}
rpl::producer<EntryUpdate> Changes::realtimeEntryUpdates(
EntryUpdate::Flag flag) const {
return _entryChanges.realtimeUpdates(flag);
}
void Changes::entryRemoved(not_null<Dialogs::Entry*> entry) {
_entryChanges.drop(entry);
}
void Changes::storyUpdated(
not_null<Story*> story,
StoryUpdate::Flags flags) {
const auto drop = (flags & StoryUpdate::Flag::Destroyed);
_storyChanges.updated(story, flags, drop);
if (!drop) {
scheduleNotifications();
}
}
rpl::producer<StoryUpdate> Changes::storyUpdates(
StoryUpdate::Flags flags) const {
return _storyChanges.updates(flags);
}
rpl::producer<StoryUpdate> Changes::storyUpdates(
not_null<Story*> story,
StoryUpdate::Flags flags) const {
return _storyChanges.updates(story, flags);
}
rpl::producer<StoryUpdate> Changes::storyFlagsValue(
not_null<Story*> story,
StoryUpdate::Flags flags) const {
return _storyChanges.flagsValue(story, flags);
}
rpl::producer<StoryUpdate> Changes::realtimeStoryUpdates(
StoryUpdate::Flag flag) const {
return _storyChanges.realtimeUpdates(flag);
}
void Changes::chatAdminChanged(
not_null<PeerData*> peer,
not_null<UserData*> user,
ChatAdminRights rights,
QString rank) {
_chatAdminChanges.fire({
.peer = peer,
.user = user,
.rights = rights,
.rank = std::move(rank),
});
}
rpl::producer<ChatAdminChange> Changes::chatAdminChanges() const {
return _chatAdminChanges.events();
}
void Changes::chatMemberRankChanged(
not_null<PeerData*> peer,
not_null<UserData*> user,
QString rank) {
_chatMemberRankChanges.fire({ peer, user, std::move(rank) });
}
rpl::producer<ChatMemberRankChange> Changes::chatMemberRankChanges() const {
return _chatMemberRankChanges.events();
}
void Changes::scheduleNotifications() {
if (!_notify) {
_notify = true;
crl::on_main(&session(), [=] {
sendNotifications();
});
}
}
void Changes::sendNotifications() {
if (!_notify) {
return;
}
_notify = false;
_peerChanges.sendNotifications();
_historyChanges.sendNotifications();
_messageChanges.sendNotifications();
_entryChanges.sendNotifications();
_topicChanges.sendNotifications();
_sublistChanges.sendNotifications();
_storyChanges.sendNotifications();
}
} // namespace Data