2017-09-04 11:40:02 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2017-09-04 11:40:02 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2017-09-04 11:40:02 +00:00
|
|
|
*/
|
|
|
|
#include "storage/storage_shared_media.h"
|
|
|
|
|
2017-10-29 15:32:01 +00:00
|
|
|
#include <rpl/map.h>
|
2017-09-04 11:40:02 +00:00
|
|
|
|
|
|
|
namespace Storage {
|
|
|
|
|
2022-10-11 15:08:19 +00:00
|
|
|
auto SharedMedia::enforceLists(Key key)
|
|
|
|
-> std::map<Key, SharedMedia::Lists>::iterator {
|
|
|
|
auto result = _lists.find(key);
|
2017-08-18 19:14:31 +00:00
|
|
|
if (result != _lists.end()) {
|
|
|
|
return result;
|
2017-09-04 11:40:02 +00:00
|
|
|
}
|
2022-10-11 15:08:19 +00:00
|
|
|
result = _lists.emplace(key, Lists {}).first;
|
2017-08-18 19:14:31 +00:00
|
|
|
for (auto index = 0; index != kSharedMediaTypeCount; ++index) {
|
|
|
|
auto &list = result->second[index];
|
|
|
|
auto type = static_cast<SharedMediaType>(index);
|
2017-09-04 19:14:44 +00:00
|
|
|
|
2017-12-22 07:05:20 +00:00
|
|
|
list.sliceUpdated(
|
|
|
|
) | rpl::map([=](const SparseIdsSliceUpdate &update) {
|
|
|
|
return SharedMediaSliceUpdate(
|
2022-10-11 15:08:19 +00:00
|
|
|
key.peerId,
|
|
|
|
key.topicRootId,
|
2017-12-22 07:05:20 +00:00
|
|
|
type,
|
|
|
|
update);
|
|
|
|
}) | rpl::start_to_stream(_sliceUpdated, _lifetime);
|
2017-08-18 19:14:31 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SharedMedia::add(SharedMediaAddNew &&query) {
|
2022-10-11 15:08:19 +00:00
|
|
|
auto peerIt = enforceLists({ query.peerId, MsgId(0) });
|
|
|
|
while (peerIt != end(_lists) && peerIt->first.peerId == query.peerId) {
|
|
|
|
for (auto index = 0; index != kSharedMediaTypeCount; ++index) {
|
|
|
|
auto type = static_cast<SharedMediaType>(index);
|
|
|
|
if (query.types.test(type)) {
|
|
|
|
peerIt->second[index].addNew(query.messageId);
|
|
|
|
}
|
2017-09-04 11:40:02 +00:00
|
|
|
}
|
2022-10-11 15:08:19 +00:00
|
|
|
++peerIt;
|
2017-09-04 11:40:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SharedMedia::add(SharedMediaAddExisting &&query) {
|
2022-10-11 15:08:19 +00:00
|
|
|
auto peerIt = enforceLists({ query.peerId, query.topicRootId });
|
2017-09-04 11:40:02 +00:00
|
|
|
for (auto index = 0; index != kSharedMediaTypeCount; ++index) {
|
|
|
|
auto type = static_cast<SharedMediaType>(index);
|
|
|
|
if (query.types.test(type)) {
|
2022-10-11 15:08:19 +00:00
|
|
|
peerIt->second[index].addExisting(
|
|
|
|
query.messageId,
|
|
|
|
query.noSkipRange);
|
2017-09-04 11:40:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SharedMedia::add(SharedMediaAddSlice &&query) {
|
|
|
|
Expects(IsValidSharedMediaType(query.type));
|
2018-01-09 17:08:31 +00:00
|
|
|
|
2022-10-11 15:08:19 +00:00
|
|
|
auto peerIt = enforceLists({ query.peerId, query.topicRootId });
|
2017-09-04 11:40:02 +00:00
|
|
|
auto index = static_cast<int>(query.type);
|
2017-11-17 20:04:22 +00:00
|
|
|
peerIt->second[index].addSlice(
|
|
|
|
std::move(query.messageIds),
|
|
|
|
query.noSkipRange,
|
|
|
|
query.count);
|
2017-09-04 11:40:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SharedMedia::remove(SharedMediaRemoveOne &&query) {
|
2022-10-12 20:23:14 +00:00
|
|
|
auto peerIt = _lists.lower_bound({ query.peerId, MsgId(0) });
|
2022-10-11 15:08:19 +00:00
|
|
|
while (peerIt != end(_lists) && peerIt->first.peerId == query.peerId) {
|
2017-09-04 11:40:02 +00:00
|
|
|
for (auto index = 0; index != kSharedMediaTypeCount; ++index) {
|
|
|
|
auto type = static_cast<SharedMediaType>(index);
|
|
|
|
if (query.types.test(type)) {
|
|
|
|
peerIt->second[index].removeOne(query.messageId);
|
|
|
|
}
|
|
|
|
}
|
2022-10-11 15:08:19 +00:00
|
|
|
++peerIt;
|
2017-09-04 11:40:02 +00:00
|
|
|
}
|
2022-10-11 15:08:19 +00:00
|
|
|
_oneRemoved.fire(std::move(query));
|
2017-09-04 11:40:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SharedMedia::remove(SharedMediaRemoveAll &&query) {
|
2022-10-12 20:23:14 +00:00
|
|
|
auto peerIt = _lists.lower_bound({ query.peerId, MsgId(0) });
|
2022-10-11 15:08:19 +00:00
|
|
|
while (peerIt != end(_lists) && peerIt->first.peerId == query.peerId) {
|
2017-09-04 11:40:02 +00:00
|
|
|
for (auto index = 0; index != kSharedMediaTypeCount; ++index) {
|
2020-10-20 16:29:24 +00:00
|
|
|
auto type = static_cast<SharedMediaType>(index);
|
|
|
|
if (query.types.test(type)) {
|
|
|
|
peerIt->second[index].removeAll();
|
|
|
|
}
|
2017-09-04 11:40:02 +00:00
|
|
|
}
|
2022-10-11 15:08:19 +00:00
|
|
|
++peerIt;
|
2017-09-04 11:40:02 +00:00
|
|
|
}
|
2022-10-11 15:08:19 +00:00
|
|
|
_allRemoved.fire(std::move(query));
|
2017-09-04 11:40:02 +00:00
|
|
|
}
|
|
|
|
|
2018-02-05 14:37:22 +00:00
|
|
|
void SharedMedia::invalidate(SharedMediaInvalidateBottom &&query) {
|
2022-10-12 20:23:14 +00:00
|
|
|
auto peerIt = _lists.lower_bound({ query.peerId, MsgId(0) });
|
2022-10-11 15:08:19 +00:00
|
|
|
while (peerIt != end(_lists) && peerIt->first.peerId == query.peerId) {
|
2018-02-05 14:37:22 +00:00
|
|
|
for (auto index = 0; index != kSharedMediaTypeCount; ++index) {
|
|
|
|
peerIt->second[index].invalidateBottom();
|
|
|
|
}
|
2022-10-11 15:08:19 +00:00
|
|
|
++peerIt;
|
2018-02-05 14:37:22 +00:00
|
|
|
}
|
2022-10-11 15:08:19 +00:00
|
|
|
_bottomInvalidated.fire(std::move(query));
|
2018-02-05 14:37:22 +00:00
|
|
|
}
|
|
|
|
|
2017-09-04 19:14:44 +00:00
|
|
|
rpl::producer<SharedMediaResult> SharedMedia::query(SharedMediaQuery &&query) const {
|
2017-08-18 19:14:31 +00:00
|
|
|
Expects(IsValidSharedMediaType(query.key.type));
|
2020-10-20 16:29:24 +00:00
|
|
|
|
2022-10-11 15:08:19 +00:00
|
|
|
auto peerIt = _lists.find({ query.key.peerId, query.key.topicRootId });
|
2017-09-04 11:40:02 +00:00
|
|
|
if (peerIt != _lists.end()) {
|
2017-08-18 19:14:31 +00:00
|
|
|
auto index = static_cast<int>(query.key.type);
|
2017-10-29 15:32:01 +00:00
|
|
|
return peerIt->second[index].query(SparseIdsListQuery(
|
|
|
|
query.key.messageId,
|
|
|
|
query.limitBefore,
|
|
|
|
query.limitAfter));
|
2017-09-04 11:40:02 +00:00
|
|
|
}
|
2017-09-04 19:14:44 +00:00
|
|
|
return [](auto consumer) {
|
|
|
|
consumer.put_done();
|
|
|
|
return rpl::lifetime();
|
|
|
|
};
|
2017-09-04 11:40:02 +00:00
|
|
|
}
|
|
|
|
|
2020-10-20 16:29:24 +00:00
|
|
|
SharedMediaResult SharedMedia::snapshot(const SharedMediaQuery &query) const {
|
|
|
|
Expects(IsValidSharedMediaType(query.key.type));
|
|
|
|
|
2022-10-11 15:08:19 +00:00
|
|
|
auto peerIt = _lists.find({ query.key.peerId, query.key.topicRootId });
|
2020-10-20 16:29:24 +00:00
|
|
|
if (peerIt != _lists.end()) {
|
|
|
|
auto index = static_cast<int>(query.key.type);
|
|
|
|
return peerIt->second[index].snapshot(SparseIdsListQuery(
|
|
|
|
query.key.messageId,
|
|
|
|
query.limitBefore,
|
|
|
|
query.limitAfter));
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SharedMedia::empty(const SharedMediaKey &key) const {
|
|
|
|
Expects(IsValidSharedMediaType(key.type));
|
|
|
|
|
2022-10-11 15:08:19 +00:00
|
|
|
auto peerIt = _lists.find({ key.peerId, key.topicRootId });
|
2020-10-20 16:29:24 +00:00
|
|
|
if (peerIt != _lists.end()) {
|
|
|
|
auto index = static_cast<int>(key.type);
|
|
|
|
return peerIt->second[index].empty();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-11-21 09:20:56 +00:00
|
|
|
rpl::producer<SharedMediaSliceUpdate> SharedMedia::sliceUpdated() const {
|
|
|
|
return _sliceUpdated.events();
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<SharedMediaRemoveOne> SharedMedia::oneRemoved() const {
|
|
|
|
return _oneRemoved.events();
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<SharedMediaRemoveAll> SharedMedia::allRemoved() const {
|
|
|
|
return _allRemoved.events();
|
|
|
|
}
|
|
|
|
|
2018-02-05 14:37:22 +00:00
|
|
|
rpl::producer<SharedMediaInvalidateBottom> SharedMedia::bottomInvalidated() const {
|
|
|
|
return _bottomInvalidated.events();
|
|
|
|
}
|
|
|
|
|
2017-08-18 19:14:31 +00:00
|
|
|
} // namespace Storage
|