tdesktop/Telegram/SourceFiles/storage/storage_shared_media.cpp

171 lines
4.9 KiB
C++
Raw Normal View History

/*
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 "storage/storage_shared_media.h"
#include <rpl/map.h>
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);
if (result != _lists.end()) {
return result;
}
2022-10-11 15:08:19 +00:00
result = _lists.emplace(key, Lists {}).first;
for (auto index = 0; index != kSharedMediaTypeCount; ++index) {
auto &list = result->second[index];
auto type = static_cast<SharedMediaType>(index);
list.sliceUpdated(
) | rpl::map([=](const SparseIdsSliceUpdate &update) {
return SharedMediaSliceUpdate(
2022-10-11 15:08:19 +00:00
key.peerId,
key.topicRootId,
type,
update);
}) | rpl::start_to_stream(_sliceUpdated, _lifetime);
}
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);
}
}
2022-10-11 15:08:19 +00:00
++peerIt;
}
}
void SharedMedia::add(SharedMediaAddExisting &&query) {
2022-10-11 15:08:19 +00:00
auto peerIt = enforceLists({ query.peerId, query.topicRootId });
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);
}
}
}
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 });
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);
}
void SharedMedia::remove(SharedMediaRemoveOne &&query) {
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) {
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;
}
2022-10-11 15:08:19 +00:00
_oneRemoved.fire(std::move(query));
}
void SharedMedia::remove(SharedMediaRemoveAll &&query) {
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) {
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();
}
}
2022-10-11 15:08:19 +00:00
++peerIt;
}
2022-10-11 15:08:19 +00:00
_allRemoved.fire(std::move(query));
}
void SharedMedia::invalidate(SharedMediaInvalidateBottom &&query) {
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) {
for (auto index = 0; index != kSharedMediaTypeCount; ++index) {
peerIt->second[index].invalidateBottom();
}
2022-10-11 15:08:19 +00:00
++peerIt;
}
2022-10-11 15:08:19 +00:00
_bottomInvalidated.fire(std::move(query));
}
rpl::producer<SharedMediaResult> SharedMedia::query(SharedMediaQuery &&query) const {
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 });
if (peerIt != _lists.end()) {
auto index = static_cast<int>(query.key.type);
return peerIt->second[index].query(SparseIdsListQuery(
query.key.messageId,
query.limitBefore,
query.limitAfter));
}
return [](auto consumer) {
consumer.put_done();
return rpl::lifetime();
};
}
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();
}
rpl::producer<SharedMediaInvalidateBottom> SharedMedia::bottomInvalidated() const {
return _bottomInvalidated.events();
}
} // namespace Storage