File: data_groups.cpp

package info (click to toggle)
telegram-desktop 4.6.5%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 53,300 kB
  • sloc: cpp: 605,857; python: 3,978; ansic: 1,636; sh: 965; makefile: 841; objc: 652; javascript: 187; xml: 165
file content (176 lines) | stat: -rw-r--r-- 4,144 bytes parent folder | download
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
/*
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.h"
#include "history/history_item.h"
#include "dialogs/ui/dialogs_message_view.h"
#include "data/data_media_types.h"
#include "data/data_session.h"
#include "data/data_forum.h"
#include "data/data_forum_topic.h"

namespace Data {
namespace {

constexpr auto kMaxItemsInGroup = 10;

} // namespace

Groups::Groups(not_null<Session*> data) : _data(data) {
}

bool Groups::isGrouped(not_null<const HistoryItem*> item) const {
	if (!item->groupId()) {
		return false;
	}
	const auto media = item->media();
	return media && media->canBeGrouped();
}

bool Groups::isGroupOfOne(not_null<const HistoryItem*> item) const {
	if (const auto groupId = item->groupId()) {
		const auto i = _groups.find(groupId);
		return (i != _groups.end()) && (i->second.items.size() == 1);
	}
	return false;
}

void Groups::registerMessage(not_null<HistoryItem*> item) {
	if (!isGrouped(item)) {
		return;
	}
	const auto i = _groups.emplace(item->groupId(), Group()).first;
	auto &items = i->second.items;
	if (items.size() < kMaxItemsInGroup) {
		items.insert(findPositionForItem(items, item), item);
		if (items.size() > 1) {
			refreshViews(items);
		}
	}
}

void Groups::unregisterMessage(not_null<const HistoryItem*> item) {
	const auto groupId = item->groupId();
	if (!groupId) {
		return;
	}
	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);
			}
		}
	}
}

void Groups::refreshMessage(
		not_null<HistoryItem*> item,
		bool justRefreshViews) {
	if (!isGrouped(item)) {
		unregisterMessage(item);
		return;
	}
	if (!item->isRegular() && !item->isScheduled()) {
		return;
	}
	const auto groupId = item->groupId();
	const auto i = _groups.find(groupId);
	if (i == end(_groups)) {
		registerMessage(item);
		return;
	}
	auto &items = i->second.items;

	if (justRefreshViews) {
		refreshViews(items);
		return;
	}

	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 last = end(group);
	const auto itemId = item->id;
	for (auto result = begin(group); result != last; ++result) {
		if ((*result)->id > itemId) {
			return result;
		}
	}
	return last;
}

const Group *Groups::find(not_null<const HistoryItem*> item) const {
	const auto groupId = item->groupId();
	if (!groupId) {
		return nullptr;
	}
	const auto i = _groups.find(groupId);
	if (i != _groups.end()) {
		const auto &result = i->second;
		if (result.items.size() > 1) {
			return &result;
		}
	}
	return nullptr;
}

void Groups::refreshViews(const HistoryItemsList &items) {
	if (items.empty()) {
		return;
	}
	for (const auto &item : items) {
		_data->requestItemViewRefresh(item);
		item->invalidateChatListEntry();
	}
}

not_null<HistoryItem*> Groups::findItemToEdit(
		not_null<HistoryItem*> item) const {
	const auto group = find(item);
	if (!group) {
		return item;
	}
	const auto &list = group->items;
	const auto it = ranges::find_if(
		list,
		ranges::not_fn(&HistoryItem::emptyText));
	if (it == end(list)) {
		return list.front();
	}
	return (*it);
}

} // namespace Data