2019-08-08 14:13:26 +00:00
|
|
|
/*
|
|
|
|
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_scheduled_messages.h"
|
|
|
|
|
|
|
|
#include "data/data_peer.h"
|
|
|
|
#include "data/data_session.h"
|
|
|
|
#include "api/api_hash.h"
|
2019-09-16 11:14:06 +00:00
|
|
|
#include "api/api_text_entities.h"
|
2019-08-08 14:13:26 +00:00
|
|
|
#include "main/main_session.h"
|
|
|
|
#include "history/history.h"
|
|
|
|
#include "history/history_item_components.h"
|
2020-01-25 22:41:02 +00:00
|
|
|
#include "history/history_message.h"
|
2019-08-08 14:13:26 +00:00
|
|
|
#include "apiwrap.h"
|
|
|
|
|
|
|
|
namespace Data {
|
|
|
|
namespace {
|
|
|
|
|
2019-08-29 17:16:37 +00:00
|
|
|
constexpr auto kRequestTimeLimit = 60 * crl::time(1000);
|
|
|
|
|
|
|
|
[[nodiscard]] bool TooEarlyForRequest(crl::time received) {
|
|
|
|
return (received > 0) && (received + kRequestTimeLimit > crl::now());
|
|
|
|
}
|
|
|
|
|
2019-08-08 14:13:26 +00:00
|
|
|
MTPMessage PrepareMessage(const MTPMessage &message, MsgId id) {
|
|
|
|
return message.match([&](const MTPDmessageEmpty &) {
|
|
|
|
return MTP_messageEmpty(MTP_int(id));
|
|
|
|
}, [&](const MTPDmessageService &data) {
|
|
|
|
return MTP_messageService(
|
|
|
|
MTP_flags(data.vflags().v
|
|
|
|
| MTPDmessageService::Flag(
|
|
|
|
MTPDmessage::Flag::f_from_scheduled)),
|
|
|
|
MTP_int(id),
|
|
|
|
MTP_int(data.vfrom_id().value_or_empty()),
|
|
|
|
data.vto_id(),
|
|
|
|
MTP_int(data.vreply_to_msg_id().value_or_empty()),
|
|
|
|
data.vdate(),
|
|
|
|
data.vaction());
|
|
|
|
}, [&](const MTPDmessage &data) {
|
|
|
|
const auto fwdFrom = data.vfwd_from();
|
|
|
|
const auto media = data.vmedia();
|
|
|
|
const auto markup = data.vreply_markup();
|
|
|
|
const auto entities = data.ventities();
|
|
|
|
return MTP_message(
|
|
|
|
MTP_flags(data.vflags().v | MTPDmessage::Flag::f_from_scheduled),
|
|
|
|
MTP_int(id),
|
|
|
|
MTP_int(data.vfrom_id().value_or_empty()),
|
|
|
|
data.vto_id(),
|
|
|
|
fwdFrom ? *fwdFrom : MTPMessageFwdHeader(),
|
|
|
|
MTP_int(data.vvia_bot_id().value_or_empty()),
|
|
|
|
MTP_int(data.vreply_to_msg_id().value_or_empty()),
|
|
|
|
data.vdate(),
|
|
|
|
data.vmessage(),
|
|
|
|
media ? *media : MTPMessageMedia(),
|
|
|
|
markup ? *markup : MTPReplyMarkup(),
|
|
|
|
entities ? *entities : MTPVector<MTPMessageEntity>(),
|
|
|
|
MTP_int(data.vviews().value_or_empty()),
|
|
|
|
MTP_int(data.vedit_date().value_or_empty()),
|
|
|
|
MTP_bytes(data.vpost_author().value_or_empty()),
|
|
|
|
MTP_long(data.vgrouped_id().value_or_empty()),
|
|
|
|
//MTPMessageReactions(),
|
|
|
|
MTPVector<MTPRestrictionReason>());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
ScheduledMessages::ScheduledMessages(not_null<Session*> owner)
|
2019-08-29 17:16:37 +00:00
|
|
|
: _session(&owner->session())
|
|
|
|
, _clearTimer([=] { clearOldRequests(); }) {
|
2019-08-08 14:13:26 +00:00
|
|
|
owner->itemRemoved(
|
|
|
|
) | rpl::filter([](not_null<const HistoryItem*> item) {
|
|
|
|
return item->isScheduled();
|
|
|
|
}) | rpl::start_with_next([=](not_null<const HistoryItem*> item) {
|
|
|
|
remove(item);
|
|
|
|
}, _lifetime);
|
|
|
|
}
|
|
|
|
|
|
|
|
ScheduledMessages::~ScheduledMessages() {
|
|
|
|
for (const auto &request : _requests) {
|
|
|
|
_session->api().request(request.second.requestId).cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-29 17:16:37 +00:00
|
|
|
void ScheduledMessages::clearOldRequests() {
|
|
|
|
const auto now = crl::now();
|
|
|
|
while (true) {
|
|
|
|
const auto i = ranges::find_if(_requests, [&](const auto &value) {
|
|
|
|
const auto &request = value.second;
|
|
|
|
return !request.requestId
|
|
|
|
&& (request.lastReceived + kRequestTimeLimit <= now);
|
|
|
|
});
|
|
|
|
if (i == end(_requests)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
_requests.erase(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-08 17:07:55 +00:00
|
|
|
MsgId ScheduledMessages::lookupId(not_null<HistoryItem*> item) const {
|
2019-08-09 17:58:58 +00:00
|
|
|
Expects(item->isScheduled());
|
|
|
|
|
|
|
|
const auto i = _data.find(item->history());
|
|
|
|
Assert(i != end(_data));
|
|
|
|
const auto &list = i->second;
|
|
|
|
const auto j = list.idByItem.find(item);
|
|
|
|
Assert(j != end(list.idByItem));
|
|
|
|
return j->second;
|
2019-08-08 14:13:26 +00:00
|
|
|
}
|
|
|
|
|
2020-01-26 18:32:19 +00:00
|
|
|
HistoryItem *ScheduledMessages::lookupItem(PeerId peer, MsgId msg) const {
|
|
|
|
const auto history = _session->data().historyLoaded(peer);
|
|
|
|
if (!history) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto i = _data.find(history);
|
|
|
|
if (i == end(_data)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto &items = i->second.items;
|
|
|
|
const auto j = ranges::find_if(items, [&](auto &item) {
|
|
|
|
return item->id == msg;
|
|
|
|
});
|
|
|
|
if (j == end(items)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return (*j).get();
|
|
|
|
}
|
|
|
|
|
2019-08-08 17:07:55 +00:00
|
|
|
int ScheduledMessages::count(not_null<History*> history) const {
|
|
|
|
const auto i = _data.find(history);
|
|
|
|
return (i != end(_data)) ? i->second.items.size() : 0;
|
2019-08-08 14:13:26 +00:00
|
|
|
}
|
|
|
|
|
2020-01-25 22:41:02 +00:00
|
|
|
void ScheduledMessages::sendNowSimpleMessage(
|
2020-03-09 11:17:56 +00:00
|
|
|
const MTPDupdateShortSentMessage &update,
|
|
|
|
not_null<HistoryItem*> local) {
|
2020-01-25 22:41:02 +00:00
|
|
|
Expects(local->isSending());
|
|
|
|
Expects(local->isScheduled());
|
|
|
|
Expects(local->date() == kScheduledUntilOnlineTimestamp);
|
|
|
|
|
|
|
|
// When the user sends a text message scheduled until online
|
|
|
|
// while the recipient is already online, the server sends
|
|
|
|
// updateShortSentMessage to the client and the client calls this method.
|
|
|
|
// Since such messages can only be sent to recipients,
|
|
|
|
// we know for sure that a message can't have fields such as the author,
|
|
|
|
// views count, etc.
|
|
|
|
|
2020-06-08 08:03:45 +00:00
|
|
|
const auto history = local->history();
|
2020-01-25 22:41:02 +00:00
|
|
|
auto flags = NewMessageFlags(history->peer)
|
|
|
|
| MTPDmessage::Flag::f_entities
|
|
|
|
| MTPDmessage::Flag::f_from_id
|
|
|
|
| (local->replyToId()
|
|
|
|
? MTPDmessage::Flag::f_reply_to_msg_id
|
|
|
|
: MTPDmessage::Flag(0));
|
|
|
|
auto clientFlags = NewMessageClientFlags()
|
|
|
|
| MTPDmessage_ClientFlag::f_local_history_entry;
|
|
|
|
|
|
|
|
history->addNewMessage(
|
|
|
|
MTP_message(
|
|
|
|
MTP_flags(flags),
|
|
|
|
update.vid(),
|
|
|
|
MTP_int(_session->userId()),
|
|
|
|
peerToMTP(history->peer->id),
|
|
|
|
MTPMessageFwdHeader(),
|
|
|
|
MTPint(),
|
|
|
|
MTP_int(local->replyToId()),
|
|
|
|
update.vdate(),
|
|
|
|
MTP_string(local->originalText().text),
|
|
|
|
MTP_messageMediaEmpty(),
|
|
|
|
MTPReplyMarkup(),
|
2020-06-08 08:03:45 +00:00
|
|
|
Api::EntitiesToMTP(
|
|
|
|
&history->session(),
|
|
|
|
local->originalText().entities),
|
2020-01-25 22:41:02 +00:00
|
|
|
MTP_int(1),
|
|
|
|
MTPint(),
|
|
|
|
MTP_string(),
|
|
|
|
MTPlong(),
|
|
|
|
//MTPMessageReactions(),
|
|
|
|
MTPVector<MTPRestrictionReason>()),
|
|
|
|
clientFlags,
|
|
|
|
NewMessageType::Unread);
|
|
|
|
|
|
|
|
local->destroy();
|
|
|
|
}
|
|
|
|
|
2019-08-08 14:13:26 +00:00
|
|
|
void ScheduledMessages::apply(const MTPDupdateNewScheduledMessage &update) {
|
|
|
|
const auto &message = update.vmessage();
|
|
|
|
const auto peer = PeerFromMessage(message);
|
|
|
|
if (!peer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto history = _session->data().historyLoaded(peer);
|
|
|
|
if (!history) {
|
|
|
|
return;
|
|
|
|
}
|
2019-08-09 13:12:03 +00:00
|
|
|
auto &list = _data[history];
|
|
|
|
append(history, list, message);
|
|
|
|
sort(list);
|
2019-08-08 14:13:26 +00:00
|
|
|
_updates.fire_copy(history);
|
|
|
|
}
|
|
|
|
|
2020-01-25 19:19:20 +00:00
|
|
|
void ScheduledMessages::checkEntitiesAndUpdate(const MTPDmessage &data) {
|
|
|
|
// When the user sends a message with a media scheduled until online
|
|
|
|
// while the recipient is already online, the server sends
|
|
|
|
// updateNewMessage to the client and the client calls this method.
|
|
|
|
|
|
|
|
const auto peer = peerFromMTP(data.vto_id());
|
|
|
|
if (!peerIsUser(peer)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto history = _session->data().historyLoaded(peer);
|
|
|
|
if (!history) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto i = _data.find(history);
|
|
|
|
if (i == end(_data)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto &itemMap = i->second.itemById;
|
|
|
|
const auto j = itemMap.find(data.vid().v);
|
|
|
|
if (j == end(itemMap)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto existing = j->second;
|
|
|
|
Assert(existing->date() == kScheduledUntilOnlineTimestamp);
|
|
|
|
existing->updateSentContent({
|
|
|
|
qs(data.vmessage()),
|
2020-06-08 08:03:45 +00:00
|
|
|
Api::EntitiesFromMTP(_session, data.ventities().value_or_empty())
|
2020-01-25 19:19:20 +00:00
|
|
|
}, data.vmedia());
|
|
|
|
existing->updateReplyMarkup(data.vreply_markup());
|
|
|
|
existing->updateForwardedInfo(data.vfwd_from());
|
|
|
|
_session->data().requestItemTextRefresh(existing);
|
|
|
|
|
|
|
|
existing->destroy();
|
|
|
|
}
|
|
|
|
|
2019-08-08 14:13:26 +00:00
|
|
|
void ScheduledMessages::apply(
|
|
|
|
const MTPDupdateDeleteScheduledMessages &update) {
|
|
|
|
const auto peer = peerFromMTP(update.vpeer());
|
|
|
|
if (!peer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto history = _session->data().historyLoaded(peer);
|
|
|
|
if (!history) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto i = _data.find(history);
|
|
|
|
if (i == end(_data)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (const auto &id : update.vmessages().v) {
|
|
|
|
const auto &list = i->second;
|
|
|
|
const auto j = list.itemById.find(id.v);
|
|
|
|
if (j != end(list.itemById)) {
|
|
|
|
j->second->destroy();
|
|
|
|
i = _data.find(history);
|
|
|
|
if (i == end(_data)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_updates.fire_copy(history);
|
|
|
|
}
|
|
|
|
|
2019-08-12 16:33:36 +00:00
|
|
|
void ScheduledMessages::apply(
|
|
|
|
const MTPDupdateMessageID &update,
|
|
|
|
not_null<HistoryItem*> local) {
|
|
|
|
const auto id = update.vid().v;
|
|
|
|
const auto i = _data.find(local->history());
|
|
|
|
Assert(i != end(_data));
|
|
|
|
auto &list = i->second;
|
|
|
|
const auto j = list.itemById.find(id);
|
|
|
|
if (j != end(list.itemById)) {
|
|
|
|
local->destroy();
|
|
|
|
} else {
|
|
|
|
Assert(!list.itemById.contains(local->id));
|
|
|
|
Assert(!list.idByItem.contains(local));
|
|
|
|
local->setRealId(local->history()->nextNonHistoryEntryId());
|
|
|
|
list.idByItem.emplace(local, id);
|
|
|
|
list.itemById.emplace(id, local);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScheduledMessages::appendSending(not_null<HistoryItem*> item) {
|
|
|
|
Expects(item->isSending());
|
|
|
|
Expects(item->isScheduled());
|
|
|
|
|
|
|
|
const auto history = item->history();
|
|
|
|
auto &list = _data[history];
|
|
|
|
list.items.emplace_back(item);
|
|
|
|
sort(list);
|
|
|
|
_updates.fire_copy(history);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScheduledMessages::removeSending(not_null<HistoryItem*> item) {
|
|
|
|
Expects(item->isSending() || item->hasFailed());
|
|
|
|
Expects(item->isScheduled());
|
|
|
|
|
2019-09-27 15:49:49 +00:00
|
|
|
item->destroy();
|
2019-08-12 16:33:36 +00:00
|
|
|
}
|
|
|
|
|
2019-08-08 14:13:26 +00:00
|
|
|
rpl::producer<> ScheduledMessages::updates(not_null<History*> history) {
|
|
|
|
request(history);
|
|
|
|
|
|
|
|
return _updates.events(
|
|
|
|
) | rpl::filter([=](not_null<History*> value) {
|
|
|
|
return (value == history);
|
2020-06-21 16:25:29 +00:00
|
|
|
}) | rpl::to_empty;
|
2019-08-08 14:13:26 +00:00
|
|
|
}
|
|
|
|
|
2019-08-08 17:07:55 +00:00
|
|
|
Data::MessagesSlice ScheduledMessages::list(not_null<History*> history) {
|
|
|
|
auto result = Data::MessagesSlice();
|
|
|
|
const auto i = _data.find(history);
|
|
|
|
if (i == end(_data)) {
|
|
|
|
const auto i = _requests.find(history);
|
|
|
|
if (i == end(_requests)) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
result.fullCount = result.skippedAfter = result.skippedBefore = 0;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
const auto &list = i->second.items;
|
|
|
|
result.skippedAfter = result.skippedBefore = 0;
|
|
|
|
result.fullCount = int(list.size());
|
|
|
|
result.ids = ranges::view::all(
|
|
|
|
list
|
|
|
|
) | ranges::view::transform(
|
|
|
|
&HistoryItem::fullId
|
2019-08-09 13:12:03 +00:00
|
|
|
) | ranges::to_vector;
|
2019-08-08 17:07:55 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-08-08 14:13:26 +00:00
|
|
|
void ScheduledMessages::request(not_null<History*> history) {
|
|
|
|
auto &request = _requests[history];
|
2019-08-29 17:16:37 +00:00
|
|
|
if (request.requestId || TooEarlyForRequest(request.lastReceived)) {
|
2019-08-08 14:13:26 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-08-12 16:33:36 +00:00
|
|
|
const auto i = _data.find(history);
|
|
|
|
const auto hash = (i != end(_data)) ? countListHash(i->second) : 0;
|
2019-08-08 14:13:26 +00:00
|
|
|
request.requestId = _session->api().request(
|
|
|
|
MTPmessages_GetScheduledHistory(
|
|
|
|
history->peer->input,
|
2019-08-12 16:33:36 +00:00
|
|
|
MTP_int(hash))
|
2019-08-08 14:13:26 +00:00
|
|
|
).done([=](const MTPmessages_Messages &result) {
|
|
|
|
parse(history, result);
|
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
_requests.remove(history);
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScheduledMessages::parse(
|
|
|
|
not_null<History*> history,
|
|
|
|
const MTPmessages_Messages &list) {
|
|
|
|
auto &request = _requests[history];
|
2019-08-29 17:16:37 +00:00
|
|
|
request.lastReceived = crl::now();
|
2019-08-08 14:13:26 +00:00
|
|
|
request.requestId = 0;
|
2019-08-29 17:16:37 +00:00
|
|
|
if (!_clearTimer.isActive()) {
|
|
|
|
_clearTimer.callOnce(kRequestTimeLimit * 2);
|
|
|
|
}
|
2019-08-08 14:13:26 +00:00
|
|
|
|
|
|
|
list.match([&](const MTPDmessages_messagesNotModified &data) {
|
|
|
|
}, [&](const auto &data) {
|
|
|
|
_session->data().processUsers(data.vusers());
|
|
|
|
_session->data().processChats(data.vchats());
|
2019-08-12 16:33:36 +00:00
|
|
|
|
2019-08-08 14:13:26 +00:00
|
|
|
const auto &messages = data.vmessages().v;
|
|
|
|
if (messages.isEmpty()) {
|
2019-08-20 13:21:10 +00:00
|
|
|
clearNotSending(history);
|
2019-08-08 14:13:26 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-08-12 16:33:36 +00:00
|
|
|
auto received = base::flat_set<not_null<HistoryItem*>>();
|
2019-08-20 13:21:10 +00:00
|
|
|
auto clear = base::flat_set<not_null<HistoryItem*>>();
|
|
|
|
auto &list = _data.emplace(history, List()).first->second;
|
2019-08-08 14:13:26 +00:00
|
|
|
for (const auto &message : messages) {
|
2019-08-12 16:33:36 +00:00
|
|
|
if (const auto item = append(history, list, message)) {
|
|
|
|
received.emplace(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const auto &owned : list.items) {
|
|
|
|
const auto item = owned.get();
|
|
|
|
if (!item->isSending() && !received.contains(item)) {
|
|
|
|
clear.emplace(item);
|
|
|
|
}
|
|
|
|
}
|
2019-08-20 13:21:10 +00:00
|
|
|
updated(history, received, clear);
|
2019-08-08 14:13:26 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-08-12 16:33:36 +00:00
|
|
|
HistoryItem *ScheduledMessages::append(
|
2019-08-08 14:13:26 +00:00
|
|
|
not_null<History*> history,
|
|
|
|
List &list,
|
|
|
|
const MTPMessage &message) {
|
|
|
|
const auto id = message.match([&](const auto &data) {
|
|
|
|
return data.vid().v;
|
|
|
|
});
|
2019-08-12 16:33:36 +00:00
|
|
|
const auto i = list.itemById.find(id);
|
|
|
|
if (i != end(list.itemById)) {
|
2019-08-13 15:35:49 +00:00
|
|
|
const auto existing = i->second;
|
|
|
|
message.match([&](const MTPDmessage &data) {
|
|
|
|
existing->updateSentContent({
|
|
|
|
qs(data.vmessage()),
|
2020-06-08 08:03:45 +00:00
|
|
|
Api::EntitiesFromMTP(
|
|
|
|
_session,
|
|
|
|
data.ventities().value_or_empty())
|
2019-08-13 15:35:49 +00:00
|
|
|
}, data.vmedia());
|
|
|
|
existing->updateReplyMarkup(data.vreply_markup());
|
|
|
|
existing->updateForwardedInfo(data.vfwd_from());
|
2020-05-09 20:34:57 +00:00
|
|
|
existing->updateDate(data.vdate().v);
|
2019-08-13 15:35:49 +00:00
|
|
|
history->owner().requestItemTextRefresh(existing);
|
|
|
|
}, [&](const auto &data) {});
|
|
|
|
return existing;
|
2019-08-08 14:13:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const auto item = _session->data().addNewMessage(
|
|
|
|
PrepareMessage(message, history->nextNonHistoryEntryId()),
|
|
|
|
MTPDmessage_ClientFlags(),
|
|
|
|
NewMessageType::Existing);
|
|
|
|
if (!item || item->history() != history) {
|
|
|
|
LOG(("API Error: Bad data received in scheduled messages."));
|
2019-08-12 16:33:36 +00:00
|
|
|
return nullptr;
|
2019-08-08 14:13:26 +00:00
|
|
|
}
|
|
|
|
list.items.emplace_back(item);
|
|
|
|
list.itemById.emplace(id, item);
|
|
|
|
list.idByItem.emplace(item, id);
|
2019-08-12 16:33:36 +00:00
|
|
|
return item;
|
2019-08-08 14:13:26 +00:00
|
|
|
}
|
|
|
|
|
2019-08-20 13:21:10 +00:00
|
|
|
void ScheduledMessages::clearNotSending(not_null<History*> history) {
|
|
|
|
const auto i = _data.find(history);
|
|
|
|
if (i == end(_data)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto clear = base::flat_set<not_null<HistoryItem*>>();
|
|
|
|
for (const auto &owned : i->second.items) {
|
2019-09-06 11:46:03 +00:00
|
|
|
if (!owned->isSending() && !owned->hasFailed()) {
|
2019-08-20 13:21:10 +00:00
|
|
|
clear.emplace(owned.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
updated(history, {}, clear);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScheduledMessages::updated(
|
|
|
|
not_null<History*> history,
|
|
|
|
const base::flat_set<not_null<HistoryItem*>> &added,
|
|
|
|
const base::flat_set<not_null<HistoryItem*>> &clear) {
|
|
|
|
if (!clear.empty()) {
|
|
|
|
for (const auto item : clear) {
|
|
|
|
item->destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const auto i = _data.find(history);
|
|
|
|
if (i != end(_data)) {
|
|
|
|
sort(i->second);
|
|
|
|
}
|
|
|
|
if (!added.empty() || !clear.empty()) {
|
|
|
|
_updates.fire_copy(history);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-09 13:12:03 +00:00
|
|
|
void ScheduledMessages::sort(List &list) {
|
|
|
|
ranges::sort(list.items, ranges::less(), &HistoryItem::position);
|
|
|
|
}
|
|
|
|
|
2019-08-08 14:13:26 +00:00
|
|
|
void ScheduledMessages::remove(not_null<const HistoryItem*> item) {
|
|
|
|
const auto history = item->history();
|
|
|
|
const auto i = _data.find(history);
|
|
|
|
Assert(i != end(_data));
|
|
|
|
auto &list = i->second;
|
|
|
|
|
2019-09-06 11:46:03 +00:00
|
|
|
if (!item->isSending() && !item->hasFailed()) {
|
2019-08-20 13:21:10 +00:00
|
|
|
const auto j = list.idByItem.find(item);
|
|
|
|
Assert(j != end(list.idByItem));
|
|
|
|
list.itemById.remove(j->second);
|
|
|
|
list.idByItem.erase(j);
|
|
|
|
}
|
2019-08-08 14:13:26 +00:00
|
|
|
const auto k = ranges::find(list.items, item, &OwnedItem::get);
|
|
|
|
Assert(k != list.items.end());
|
|
|
|
k->release();
|
|
|
|
list.items.erase(k);
|
|
|
|
|
|
|
|
if (list.items.empty()) {
|
|
|
|
_data.erase(i);
|
|
|
|
}
|
|
|
|
_updates.fire_copy(history);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32 ScheduledMessages::countListHash(const List &list) const {
|
|
|
|
using namespace Api;
|
|
|
|
|
|
|
|
auto hash = HashInit();
|
2019-08-12 16:33:36 +00:00
|
|
|
auto &&serverside = ranges::view::all(
|
|
|
|
list.items
|
|
|
|
) | ranges::view::filter([](const OwnedItem &item) {
|
|
|
|
return !item->isSending() && !item->hasFailed();
|
|
|
|
}) | ranges::view::reverse;
|
|
|
|
for (const auto &item : serverside) {
|
2019-08-08 14:13:26 +00:00
|
|
|
const auto j = list.idByItem.find(item.get());
|
|
|
|
HashUpdate(hash, j->second);
|
|
|
|
if (const auto edited = item->Get<HistoryMessageEdited>()) {
|
2019-08-12 10:37:18 +00:00
|
|
|
HashUpdate(hash, edited->date);
|
2019-08-08 14:13:26 +00:00
|
|
|
} else {
|
2019-08-12 10:37:18 +00:00
|
|
|
HashUpdate(hash, int32(0));
|
2019-08-08 14:13:26 +00:00
|
|
|
}
|
2019-08-12 10:37:18 +00:00
|
|
|
HashUpdate(hash, item->date());
|
2019-08-08 14:13:26 +00:00
|
|
|
}
|
|
|
|
return HashFinalize(hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Data
|