2018-01-14 16:02:25 +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_groups.h"
|
|
|
|
|
|
|
|
#include "history/history_item.h"
|
2018-01-17 16:21:01 +00:00
|
|
|
#include "data/data_media_types.h"
|
|
|
|
#include "data/data_session.h"
|
2018-01-14 16:02:25 +00:00
|
|
|
|
|
|
|
namespace Data {
|
2018-02-14 19:38:01 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr auto kMaxItemsInGroup = 10;
|
|
|
|
|
|
|
|
} // namespace
|
2018-01-14 16:02:25 +00:00
|
|
|
|
2018-01-17 16:21:01 +00:00
|
|
|
Groups::Groups(not_null<Session*> data) : _data(data) {
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Groups::isGrouped(not_null<HistoryItem*> item) const {
|
|
|
|
if (!item->groupId()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const auto media = item->media();
|
|
|
|
return media && media->canBeGrouped();
|
|
|
|
}
|
|
|
|
|
2018-01-14 16:02:25 +00:00
|
|
|
void Groups::registerMessage(not_null<HistoryItem*> item) {
|
2018-01-17 16:21:01 +00:00
|
|
|
if (!isGrouped(item)) {
|
2018-01-14 16:02:25 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-01-17 16:21:01 +00:00
|
|
|
const auto i = _groups.emplace(item->groupId(), Group()).first;
|
|
|
|
auto &items = i->second.items;
|
2018-02-14 19:38:01 +00:00
|
|
|
if (items.size() < kMaxItemsInGroup) {
|
|
|
|
items.insert(findPositionForItem(items, item), item);
|
|
|
|
if (items.size() > 1) {
|
|
|
|
refreshViews(items);
|
|
|
|
}
|
2018-01-17 16:21:01 +00:00
|
|
|
}
|
2018-01-14 16:02:25 +00:00
|
|
|
}
|
|
|
|
|
2018-01-21 09:05:30 +00:00
|
|
|
void Groups::unregisterMessage(not_null<const HistoryItem*> item) {
|
2018-01-14 16:02:25 +00:00
|
|
|
const auto groupId = item->groupId();
|
|
|
|
if (!groupId) {
|
|
|
|
return;
|
|
|
|
}
|
2018-01-17 16:21:01 +00:00
|
|
|
const auto i = _groups.find(groupId);
|
|
|
|
if (i != end(_groups)) {
|
|
|
|
auto &items = i->second.items;
|
|
|
|
const auto removed = ranges::remove(items, item);
|
|
|
|
const auto last = end(items);
|
|
|
|
if (removed != last) {
|
|
|
|
items.erase(removed, last);
|
|
|
|
if (!items.empty()) {
|
|
|
|
refreshViews(items);
|
|
|
|
} else {
|
|
|
|
_groups.erase(i);
|
|
|
|
}
|
2018-01-14 16:02:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-02 10:38:53 +00:00
|
|
|
void Groups::refreshMessage(
|
|
|
|
not_null<HistoryItem*> item,
|
|
|
|
bool justRefreshViews) {
|
2018-01-17 16:21:01 +00:00
|
|
|
if (!isGrouped(item)) {
|
|
|
|
unregisterMessage(item);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!IsServerMsgId(item->id)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto groupId = item->groupId();
|
|
|
|
const auto i = _groups.find(groupId);
|
|
|
|
if (i == end(_groups)) {
|
|
|
|
registerMessage(item);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto &items = i->second.items;
|
2019-04-01 10:58:51 +00:00
|
|
|
|
2019-04-02 10:38:53 +00:00
|
|
|
if (justRefreshViews) {
|
2019-04-01 10:58:51 +00:00
|
|
|
refreshViews(items);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-17 16:21:01 +00:00
|
|
|
const auto position = findPositionForItem(items, item);
|
|
|
|
auto current = ranges::find(items, item);
|
|
|
|
if (current == end(items)) {
|
|
|
|
items.insert(position, item);
|
|
|
|
} else if (position == current + 1) {
|
|
|
|
return;
|
|
|
|
} else if (position > current + 1) {
|
|
|
|
for (++current; current != position; ++current) {
|
|
|
|
std::swap(*(current - 1), *current);
|
|
|
|
}
|
|
|
|
} else if (position < current) {
|
|
|
|
for (; current != position; --current) {
|
|
|
|
std::swap(*(current - 1), *current);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Unexpected("Position of item in Groups::refreshMessage().");
|
|
|
|
}
|
|
|
|
refreshViews(items);
|
|
|
|
}
|
|
|
|
|
|
|
|
HistoryItemsList::const_iterator Groups::findPositionForItem(
|
|
|
|
const HistoryItemsList &group,
|
|
|
|
not_null<HistoryItem*> item) {
|
|
|
|
const auto itemId = item->id;
|
|
|
|
const auto last = end(group);
|
|
|
|
if (!IsServerMsgId(itemId)) {
|
|
|
|
return last;
|
|
|
|
}
|
2018-01-18 13:59:22 +00:00
|
|
|
for (auto result = begin(group); result != last; ++result) {
|
2018-01-17 16:21:01 +00:00
|
|
|
const auto alreadyId = (*result)->id;
|
|
|
|
if (IsServerMsgId(alreadyId) && alreadyId > itemId) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return last;
|
|
|
|
}
|
|
|
|
|
2018-01-14 16:02:25 +00:00
|
|
|
const Group *Groups::find(not_null<HistoryItem*> item) const {
|
|
|
|
const auto groupId = item->groupId();
|
|
|
|
if (!groupId) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-01-17 16:21:01 +00:00
|
|
|
const auto i = _groups.find(groupId);
|
2018-01-21 09:05:30 +00:00
|
|
|
if (i != _groups.end()) {
|
|
|
|
const auto &result = i->second;
|
|
|
|
if (result.items.size() > 1) {
|
|
|
|
return &result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
2018-01-17 16:21:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Groups::refreshViews(const HistoryItemsList &items) {
|
|
|
|
for (const auto item : items) {
|
|
|
|
_data->requestItemViewRefresh(item);
|
|
|
|
}
|
2018-01-14 16:02:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Data
|