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 {
|
|
|
|
|
2017-08-18 19:14:31 +00:00
|
|
|
std::map<PeerId, SharedMedia::Lists>::iterator
|
|
|
|
SharedMedia::enforceLists(PeerId peer) {
|
|
|
|
auto result = _lists.find(peer);
|
|
|
|
if (result != _lists.end()) {
|
|
|
|
return result;
|
2017-09-04 11:40:02 +00:00
|
|
|
}
|
2017-08-18 19:14:31 +00:00
|
|
|
result = _lists.emplace(peer, Lists {}).first;
|
|
|
|
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(
|
|
|
|
peer,
|
|
|
|
type,
|
|
|
|
update);
|
|
|
|
}) | rpl::start_to_stream(_sliceUpdated, _lifetime);
|
2017-08-18 19:14:31 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SharedMedia::add(SharedMediaAddNew &&query) {
|
|
|
|
auto peer = query.peerId;
|
|
|
|
auto peerIt = enforceLists(peer);
|
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].addNew(query.messageId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SharedMedia::add(SharedMediaAddExisting &&query) {
|
2017-08-18 19:14:31 +00:00
|
|
|
auto peerIt = enforceLists(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].addExisting(query.messageId, query.noSkipRange);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SharedMedia::add(SharedMediaAddSlice &&query) {
|
|
|
|
Expects(IsValidSharedMediaType(query.type));
|
2018-01-09 17:08:31 +00:00
|
|
|
|
2017-08-18 19:14:31 +00:00
|
|
|
auto peerIt = enforceLists(query.peerId);
|
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) {
|
|
|
|
auto peerIt = _lists.find(query.peerId);
|
|
|
|
if (peerIt != _lists.end()) {
|
|
|
|
for (auto index = 0; index != kSharedMediaTypeCount; ++index) {
|
|
|
|
auto type = static_cast<SharedMediaType>(index);
|
|
|
|
if (query.types.test(type)) {
|
|
|
|
peerIt->second[index].removeOne(query.messageId);
|
|
|
|
}
|
|
|
|
}
|
2017-12-21 12:11:33 +00:00
|
|
|
_oneRemoved.fire(std::move(query));
|
2017-09-04 11:40:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SharedMedia::remove(SharedMediaRemoveAll &&query) {
|
|
|
|
auto peerIt = _lists.find(query.peerId);
|
|
|
|
if (peerIt != _lists.end()) {
|
|
|
|
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
|
|
|
}
|
2017-09-04 19:14:44 +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) {
|
|
|
|
auto peerIt = _lists.find(query.peerId);
|
|
|
|
if (peerIt != _lists.end()) {
|
|
|
|
for (auto index = 0; index != kSharedMediaTypeCount; ++index) {
|
|
|
|
peerIt->second[index].invalidateBottom();
|
|
|
|
}
|
|
|
|
_bottomInvalidated.fire(std::move(query));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2017-08-18 19:14:31 +00:00
|
|
|
auto peerIt = _lists.find(query.key.peerId);
|
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));
|
|
|
|
|
|
|
|
auto peerIt = _lists.find(query.key.peerId);
|
|
|
|
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));
|
|
|
|
|
|
|
|
auto peerIt = _lists.find(key.peerId);
|
|
|
|
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
|