Sort scheduled messages on an update.

This commit is contained in:
John Preston 2019-08-09 14:12:03 +01:00
parent 99037d3d46
commit 956bb876f6
2 changed files with 14 additions and 15 deletions

View File

@ -102,7 +102,9 @@ void ScheduledMessages::apply(const MTPDupdateNewScheduledMessage &update) {
if (!history) { if (!history) {
return; return;
} }
append(history, _data[history], message); auto &list = _data[history];
append(history, list, message);
sort(list);
_updates.fire_copy(history); _updates.fire_copy(history);
} }
@ -163,7 +165,7 @@ Data::MessagesSlice ScheduledMessages::list(not_null<History*> history) {
list list
) | ranges::view::transform( ) | ranges::view::transform(
&HistoryItem::fullId &HistoryItem::fullId
) | ranges::view::reverse | ranges::to_vector; ) | ranges::to_vector;
return result; return result;
} }
@ -203,7 +205,9 @@ void ScheduledMessages::parse(
for (const auto &message : messages) { for (const auto &message : messages) {
append(history, list, message); append(history, list, message);
} }
if (list.items.empty()) { if (!list.items.empty()) {
sort(list);
} else {
_data.erase(element); _data.erase(element);
element = end(_data); element = end(_data);
} }
@ -225,17 +229,7 @@ void ScheduledMessages::append(
const auto id = message.match([&](const auto &data) { const auto id = message.match([&](const auto &data) {
return data.vid().v; return data.vid().v;
}); });
const auto i = list.itemById.find(id); if (list.itemById.find(id) != end(list.itemById)) {
if (i != end(list.itemById)) {
const auto j = ranges::find(
list.items,
i->second.get(),
&OwnedItem::get);
const auto e = end(list.items);
Assert(j != e);
if (j + 1 != e) {
std::rotate(j, j + 1, e);
}
return; return;
} }
@ -252,6 +246,10 @@ void ScheduledMessages::append(
list.idByItem.emplace(item, id); list.idByItem.emplace(item, id);
} }
void ScheduledMessages::sort(List &list) {
ranges::sort(list.items, ranges::less(), &HistoryItem::position);
}
void ScheduledMessages::remove(not_null<const HistoryItem*> item) { void ScheduledMessages::remove(not_null<const HistoryItem*> item) {
const auto history = item->history(); const auto history = item->history();
const auto i = _data.find(history); const auto i = _data.find(history);
@ -278,7 +276,7 @@ int32 ScheduledMessages::countListHash(const List &list) const {
using namespace Api; using namespace Api;
auto hash = HashInit(); auto hash = HashInit();
for (const auto &item : list.items) { for (const auto &item : list.items | ranges::view::reverse) {
const auto j = list.idByItem.find(item.get()); const auto j = list.idByItem.find(item.get());
HashUpdate(hash, j->second); HashUpdate(hash, j->second);
if (const auto edited = item->Get<HistoryMessageEdited>()) { if (const auto edited = item->Get<HistoryMessageEdited>()) {

View File

@ -56,6 +56,7 @@ private:
not_null<History*> history, not_null<History*> history,
List &list, List &list,
const MTPMessage &message); const MTPMessage &message);
void sort(List &list);
void remove(not_null<const HistoryItem*> item); void remove(not_null<const HistoryItem*> item);
[[nodiscard]] int32 countListHash(const List &list) const; [[nodiscard]] int32 countListHash(const List &list) const;