2017-08-18 19:14:31 +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-08-18 19:14:31 +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-08-18 19:14:31 +00:00
|
|
|
*/
|
2017-11-06 07:31:18 +00:00
|
|
|
#include "data/data_shared_media.h"
|
2017-08-18 19:14:31 +00:00
|
|
|
|
2017-10-05 17:32:34 +00:00
|
|
|
#include <rpl/combine.h>
|
2019-07-24 11:45:24 +00:00
|
|
|
#include "main/main_session.h"
|
2017-08-18 19:14:31 +00:00
|
|
|
#include "apiwrap.h"
|
|
|
|
#include "storage/storage_facade.h"
|
2018-01-13 12:45:11 +00:00
|
|
|
#include "history/history.h"
|
|
|
|
#include "history/history_item.h"
|
2024-04-13 15:16:07 +00:00
|
|
|
#include "data/components/scheduled_messages.h"
|
2021-06-29 14:40:55 +00:00
|
|
|
#include "data/data_document.h"
|
2018-01-14 16:02:25 +00:00
|
|
|
#include "data/data_media_types.h"
|
2018-12-18 10:45:06 +00:00
|
|
|
#include "data/data_photo.h"
|
2018-01-17 16:21:01 +00:00
|
|
|
#include "data/data_session.h"
|
2018-01-03 08:23:34 +00:00
|
|
|
#include "core/crash_reports.h"
|
2017-08-18 19:14:31 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2017-10-29 15:32:01 +00:00
|
|
|
using Type = Storage::SharedMediaType;
|
2017-08-18 19:14:31 +00:00
|
|
|
|
2021-06-29 14:40:55 +00:00
|
|
|
bool IsItemGoodForType(const not_null<HistoryItem*> item, Type type) {
|
|
|
|
const auto media = item->media();
|
2021-07-01 00:00:00 +00:00
|
|
|
if (!media || media->webpage()) {
|
2021-06-29 14:40:55 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const auto photo = media->photo();
|
|
|
|
const auto photoType = (type == Type::Photo);
|
2021-07-01 00:00:00 +00:00
|
|
|
const auto photoVideoType = (type == Type::PhotoVideo);
|
|
|
|
if ((photoType || photoVideoType) && photo) {
|
2021-06-29 14:40:55 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto document = media->document();
|
|
|
|
if (!document) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-07-01 00:00:00 +00:00
|
|
|
const auto voiceType = (type == Type::VoiceFile);
|
|
|
|
const auto voiceDoc = document->isVoiceMessage();
|
|
|
|
|
|
|
|
const auto roundType = (type == Type::RoundFile);
|
|
|
|
const auto roundDoc = document->isVideoMessage();
|
2021-06-29 14:40:55 +00:00
|
|
|
|
|
|
|
const auto audioType = (type == Type::MusicFile);
|
|
|
|
const auto audioDoc = document->isAudioFile();
|
|
|
|
|
|
|
|
const auto gifType = (type == Type::GIF);
|
|
|
|
const auto gifDoc = document->isGifv();
|
|
|
|
|
|
|
|
const auto videoType = (type == Type::Video);
|
|
|
|
const auto videoDoc = document->isVideoFile();
|
|
|
|
|
2021-07-01 00:00:00 +00:00
|
|
|
const auto voiceRoundType = (type == Type::RoundVoiceFile);
|
|
|
|
const auto fileType = (type == Type::File);
|
|
|
|
|
2021-06-29 14:40:55 +00:00
|
|
|
return (audioType && audioDoc)
|
|
|
|
|| (voiceType && voiceDoc)
|
2021-07-01 00:00:00 +00:00
|
|
|
|| (roundType && roundDoc)
|
|
|
|
|| (voiceRoundType && (roundDoc || voiceDoc))
|
2021-06-29 14:40:55 +00:00
|
|
|
|| (gifType && gifDoc)
|
2021-07-01 00:00:00 +00:00
|
|
|
|| ((videoType || photoVideoType) && videoDoc)
|
|
|
|
|| (fileType && (document->isTheme()
|
|
|
|
|| document->isImage()
|
2021-11-05 18:32:26 +00:00
|
|
|
|| !document->canBeStreamed(item)));
|
2021-06-29 14:40:55 +00:00
|
|
|
}
|
|
|
|
|
2017-08-18 19:14:31 +00:00
|
|
|
} // namespace
|
|
|
|
|
2018-09-21 16:28:46 +00:00
|
|
|
std::optional<Storage::SharedMediaType> SharedMediaOverviewType(
|
2017-10-30 19:24:20 +00:00
|
|
|
Storage::SharedMediaType type) {
|
2017-12-08 18:27:28 +00:00
|
|
|
switch (type) {
|
|
|
|
case Type::Photo:
|
|
|
|
case Type::Video:
|
|
|
|
case Type::MusicFile:
|
|
|
|
case Type::File:
|
2018-11-05 08:22:05 +00:00
|
|
|
case Type::RoundVoiceFile:
|
2017-12-08 18:27:28 +00:00
|
|
|
case Type::Link: return type;
|
2017-08-18 19:14:31 +00:00
|
|
|
}
|
2018-09-21 16:28:46 +00:00
|
|
|
return std::nullopt;
|
2017-08-18 19:14:31 +00:00
|
|
|
}
|
|
|
|
|
2017-10-30 19:24:20 +00:00
|
|
|
bool SharedMediaAllowSearch(Storage::SharedMediaType type) {
|
|
|
|
switch (type) {
|
|
|
|
case Type::MusicFile:
|
|
|
|
case Type::File:
|
|
|
|
case Type::Link: return true;
|
|
|
|
default: return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-29 15:32:01 +00:00
|
|
|
rpl::producer<SparseIdsSlice> SharedMediaViewer(
|
2020-06-08 09:06:50 +00:00
|
|
|
not_null<Main::Session*> session,
|
2017-10-29 15:32:01 +00:00
|
|
|
Storage::SharedMediaKey key,
|
2017-09-04 19:14:44 +00:00
|
|
|
int limitBefore,
|
|
|
|
int limitAfter) {
|
|
|
|
Expects(IsServerMsgId(key.messageId) || (key.messageId == 0));
|
|
|
|
Expects((key.messageId != 0) || (limitBefore == 0 && limitAfter == 0));
|
|
|
|
|
|
|
|
return [=](auto consumer) {
|
|
|
|
auto lifetime = rpl::lifetime();
|
2017-10-29 15:32:01 +00:00
|
|
|
auto builder = lifetime.make_state<SparseIdsSliceBuilder>(
|
|
|
|
key.messageId,
|
2017-09-04 19:14:44 +00:00
|
|
|
limitBefore,
|
|
|
|
limitAfter);
|
|
|
|
auto requestMediaAround = [
|
2020-06-08 09:06:50 +00:00
|
|
|
peer = session->data().peer(key.peerId),
|
2022-10-11 15:08:19 +00:00
|
|
|
topicRootId = key.topicRootId,
|
2017-09-04 19:14:44 +00:00
|
|
|
type = key.type
|
2017-10-29 15:32:01 +00:00
|
|
|
](const SparseIdsSliceBuilder::AroundData &data) {
|
2020-06-08 09:06:50 +00:00
|
|
|
peer->session().api().requestSharedMedia(
|
2017-09-04 19:14:44 +00:00
|
|
|
peer,
|
2022-10-11 15:08:19 +00:00
|
|
|
topicRootId,
|
2017-09-04 19:14:44 +00:00
|
|
|
type,
|
2017-10-30 19:24:20 +00:00
|
|
|
data.aroundId,
|
|
|
|
data.direction);
|
2017-09-04 19:14:44 +00:00
|
|
|
};
|
2017-12-22 07:05:20 +00:00
|
|
|
builder->insufficientAround(
|
|
|
|
) | rpl::start_with_next(requestMediaAround, lifetime);
|
2017-09-04 19:14:44 +00:00
|
|
|
|
2017-10-29 15:32:01 +00:00
|
|
|
auto pushNextSnapshot = [=] {
|
|
|
|
consumer.put_next(builder->snapshot());
|
|
|
|
};
|
|
|
|
|
|
|
|
using SliceUpdate = Storage::SharedMediaSliceUpdate;
|
2020-06-08 09:06:50 +00:00
|
|
|
session->storage().sharedMediaSliceUpdated(
|
2017-12-22 07:05:20 +00:00
|
|
|
) | rpl::filter([=](const SliceUpdate &update) {
|
|
|
|
return (update.peerId == key.peerId)
|
2022-10-11 15:08:19 +00:00
|
|
|
&& (update.topicRootId == key.topicRootId)
|
2017-12-22 07:05:20 +00:00
|
|
|
&& (update.type == key.type);
|
|
|
|
}) | rpl::filter([=](const SliceUpdate &update) {
|
|
|
|
return builder->applyUpdate(update.data);
|
|
|
|
}) | rpl::start_with_next(pushNextSnapshot, lifetime);
|
2017-10-29 15:32:01 +00:00
|
|
|
|
|
|
|
using OneRemoved = Storage::SharedMediaRemoveOne;
|
2020-06-08 09:06:50 +00:00
|
|
|
session->storage().sharedMediaOneRemoved(
|
2017-12-22 07:05:20 +00:00
|
|
|
) | rpl::filter([=](const OneRemoved &update) {
|
|
|
|
return (update.peerId == key.peerId)
|
|
|
|
&& update.types.test(key.type);
|
|
|
|
}) | rpl::filter([=](const OneRemoved &update) {
|
|
|
|
return builder->removeOne(update.messageId);
|
|
|
|
}) | rpl::start_with_next(pushNextSnapshot, lifetime);
|
2017-10-29 15:32:01 +00:00
|
|
|
|
|
|
|
using AllRemoved = Storage::SharedMediaRemoveAll;
|
2020-06-08 09:06:50 +00:00
|
|
|
session->storage().sharedMediaAllRemoved(
|
2017-12-22 07:05:20 +00:00
|
|
|
) | rpl::filter([=](const AllRemoved &update) {
|
2020-10-20 16:29:24 +00:00
|
|
|
return (update.peerId == key.peerId)
|
2022-11-01 05:22:21 +00:00
|
|
|
&& (!update.topicRootId
|
|
|
|
|| update.topicRootId == key.topicRootId)
|
2022-10-11 15:08:19 +00:00
|
|
|
&& update.types.test(key.type);
|
2017-12-22 07:05:20 +00:00
|
|
|
}) | rpl::filter([=] {
|
|
|
|
return builder->removeAll();
|
|
|
|
}) | rpl::start_with_next(pushNextSnapshot, lifetime);
|
2017-10-29 15:32:01 +00:00
|
|
|
|
2018-02-05 14:37:22 +00:00
|
|
|
using InvalidateBottom = Storage::SharedMediaInvalidateBottom;
|
2020-06-08 09:06:50 +00:00
|
|
|
session->storage().sharedMediaBottomInvalidated(
|
2018-02-05 14:37:22 +00:00
|
|
|
) | rpl::filter([=](const InvalidateBottom &update) {
|
|
|
|
return (update.peerId == key.peerId);
|
|
|
|
}) | rpl::filter([=] {
|
|
|
|
return builder->invalidateBottom();
|
|
|
|
}) | rpl::start_with_next(pushNextSnapshot, lifetime);
|
|
|
|
|
2017-10-29 15:32:01 +00:00
|
|
|
using Result = Storage::SharedMediaResult;
|
2020-06-08 09:06:50 +00:00
|
|
|
session->storage().query(Storage::SharedMediaQuery(
|
2017-12-22 07:05:20 +00:00
|
|
|
key,
|
|
|
|
limitBefore,
|
|
|
|
limitAfter
|
|
|
|
)) | rpl::filter([=](const Result &result) {
|
|
|
|
return builder->applyInitial(result);
|
|
|
|
}) | rpl::start_with_next_done(
|
|
|
|
pushNextSnapshot,
|
|
|
|
[=] { builder->checkInsufficient(); },
|
|
|
|
lifetime);
|
2017-09-04 19:14:44 +00:00
|
|
|
|
|
|
|
return lifetime;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-06-29 14:40:55 +00:00
|
|
|
rpl::producer<SparseIdsMergedSlice> SharedScheduledMediaViewer(
|
|
|
|
not_null<Main::Session*> session,
|
|
|
|
SharedMediaMergedKey key,
|
|
|
|
int limitBefore,
|
|
|
|
int limitAfter) {
|
2023-07-27 16:51:53 +00:00
|
|
|
Expects(!key.mergedKey.universalId
|
|
|
|
|| Data::IsScheduledMsgId(key.mergedKey.universalId));
|
2021-06-29 14:40:55 +00:00
|
|
|
Expects((key.mergedKey.universalId != 0)
|
|
|
|
|| (limitBefore == 0 && limitAfter == 0));
|
|
|
|
|
|
|
|
const auto history = session->data().history(key.mergedKey.peerId);
|
|
|
|
|
2022-03-11 05:55:21 +00:00
|
|
|
return rpl::single(rpl::empty) | rpl::then(
|
2024-04-13 15:16:07 +00:00
|
|
|
session->scheduledMessages().updates(history)
|
2021-06-29 14:40:55 +00:00
|
|
|
) | rpl::map([=] {
|
2024-04-13 15:16:07 +00:00
|
|
|
const auto list = session->scheduledMessages().list(history);
|
2021-06-29 14:40:55 +00:00
|
|
|
|
|
|
|
auto items = ranges::views::all(
|
|
|
|
list.ids
|
|
|
|
) | ranges::views::transform([=](const FullMsgId &fullId) {
|
|
|
|
return session->data().message(fullId);
|
|
|
|
}) | ranges::views::filter([=](HistoryItem *item) {
|
|
|
|
return item
|
|
|
|
? IsItemGoodForType(item, key.type)
|
|
|
|
: false;
|
|
|
|
}) | ranges::to_vector;
|
|
|
|
|
|
|
|
ranges::sort(items, ranges::less(), &HistoryItem::position);
|
|
|
|
|
|
|
|
auto finishMsgIds = ranges::views::all(
|
|
|
|
items
|
|
|
|
) | ranges::views::transform([=](not_null<HistoryItem*> item) {
|
|
|
|
return item->fullId().msg;
|
|
|
|
}) | ranges::to_vector;
|
|
|
|
|
|
|
|
const auto fullCount = finishMsgIds.size();
|
|
|
|
|
|
|
|
auto unsorted = SparseUnsortedIdsSlice(
|
|
|
|
std::move(finishMsgIds),
|
|
|
|
fullCount,
|
|
|
|
list.skippedBefore,
|
|
|
|
list.skippedAfter);
|
|
|
|
return SparseIdsMergedSlice(
|
|
|
|
key.mergedKey,
|
|
|
|
std::move(unsorted));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-10-29 15:32:01 +00:00
|
|
|
rpl::producer<SparseIdsMergedSlice> SharedMediaMergedViewer(
|
2020-06-08 09:06:50 +00:00
|
|
|
not_null<Main::Session*> session,
|
2017-10-29 15:32:01 +00:00
|
|
|
SharedMediaMergedKey key,
|
2017-09-04 19:14:44 +00:00
|
|
|
int limitBefore,
|
|
|
|
int limitAfter) {
|
2017-10-30 19:24:20 +00:00
|
|
|
auto createSimpleViewer = [=](
|
|
|
|
PeerId peerId,
|
2022-10-11 15:08:19 +00:00
|
|
|
MsgId topicRootId,
|
2017-10-30 19:24:20 +00:00
|
|
|
SparseIdsSlice::Key simpleKey,
|
|
|
|
int limitBefore,
|
|
|
|
int limitAfter) {
|
|
|
|
return SharedMediaViewer(
|
2020-06-08 09:06:50 +00:00
|
|
|
session,
|
2017-10-30 19:24:20 +00:00
|
|
|
Storage::SharedMediaKey(
|
|
|
|
peerId,
|
2022-10-11 15:08:19 +00:00
|
|
|
topicRootId,
|
2017-10-30 19:24:20 +00:00
|
|
|
key.type,
|
|
|
|
simpleKey),
|
|
|
|
limitBefore,
|
|
|
|
limitAfter
|
|
|
|
);
|
2017-09-04 19:14:44 +00:00
|
|
|
};
|
2017-10-30 19:24:20 +00:00
|
|
|
return SparseIdsMergedSlice::CreateViewer(
|
|
|
|
key.mergedKey,
|
|
|
|
limitBefore,
|
|
|
|
limitAfter,
|
|
|
|
std::move(createSimpleViewer));
|
2017-08-27 11:59:08 +00:00
|
|
|
}
|
2017-08-29 15:15:54 +00:00
|
|
|
|
2020-06-08 15:17:33 +00:00
|
|
|
SharedMediaWithLastSlice::SharedMediaWithLastSlice(
|
|
|
|
not_null<Main::Session*> session,
|
|
|
|
Key key)
|
2017-10-09 21:55:30 +00:00
|
|
|
: SharedMediaWithLastSlice(
|
2020-06-08 15:17:33 +00:00
|
|
|
session,
|
2017-08-29 15:15:54 +00:00
|
|
|
key,
|
2017-10-29 15:32:01 +00:00
|
|
|
SparseIdsMergedSlice(ViewerKey(key)),
|
2017-08-29 15:15:54 +00:00
|
|
|
EndingSlice(key)) {
|
|
|
|
}
|
|
|
|
|
2017-09-04 19:14:44 +00:00
|
|
|
SharedMediaWithLastSlice::SharedMediaWithLastSlice(
|
2020-06-08 15:17:33 +00:00
|
|
|
not_null<Main::Session*> session,
|
2017-08-29 15:15:54 +00:00
|
|
|
Key key,
|
2017-10-29 15:32:01 +00:00
|
|
|
SparseIdsMergedSlice slice,
|
2018-09-21 16:28:46 +00:00
|
|
|
std::optional<SparseIdsMergedSlice> ending)
|
2020-06-08 15:17:33 +00:00
|
|
|
: _session(session)
|
|
|
|
, _key(key)
|
2017-10-29 15:32:01 +00:00
|
|
|
, _slice(std::move(slice))
|
|
|
|
, _ending(std::move(ending))
|
2020-06-08 15:17:33 +00:00
|
|
|
, _lastPhotoId(LastPeerPhotoId(session, key.peerId))
|
2017-10-29 15:32:01 +00:00
|
|
|
, _isolatedLastPhoto(_key.type == Type::ChatPhoto
|
2020-06-08 15:17:33 +00:00
|
|
|
? IsLastIsolated(session, _slice, _ending, _lastPhotoId)
|
2017-10-29 15:32:01 +00:00
|
|
|
: false) {
|
2017-08-29 15:15:54 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 16:28:46 +00:00
|
|
|
std::optional<int> SharedMediaWithLastSlice::fullCount() const {
|
2017-08-29 15:15:54 +00:00
|
|
|
return Add(
|
|
|
|
_slice.fullCount(),
|
|
|
|
_isolatedLastPhoto | [](bool isolated) { return isolated ? 1 : 0; });
|
|
|
|
}
|
|
|
|
|
2018-09-21 16:28:46 +00:00
|
|
|
std::optional<int> SharedMediaWithLastSlice::skippedBeforeImpl() const {
|
2017-08-29 15:15:54 +00:00
|
|
|
return _slice.skippedBefore();
|
|
|
|
}
|
|
|
|
|
2018-09-21 16:28:46 +00:00
|
|
|
std::optional<int> SharedMediaWithLastSlice::skippedBefore() const {
|
2017-11-23 09:58:12 +00:00
|
|
|
return _reversed ? skippedAfterImpl() : skippedBeforeImpl();
|
|
|
|
}
|
|
|
|
|
2018-09-21 16:28:46 +00:00
|
|
|
std::optional<int> SharedMediaWithLastSlice::skippedAfterImpl() const {
|
2017-08-29 15:15:54 +00:00
|
|
|
return isolatedInSlice()
|
|
|
|
? Add(
|
|
|
|
_slice.skippedAfter(),
|
|
|
|
lastPhotoSkip())
|
|
|
|
: (lastPhotoSkip() | [](int) { return 0; });
|
|
|
|
}
|
|
|
|
|
2018-09-21 16:28:46 +00:00
|
|
|
std::optional<int> SharedMediaWithLastSlice::skippedAfter() const {
|
2017-11-23 09:58:12 +00:00
|
|
|
return _reversed ? skippedBeforeImpl() : skippedAfterImpl();
|
|
|
|
}
|
|
|
|
|
2018-09-21 16:28:46 +00:00
|
|
|
std::optional<int> SharedMediaWithLastSlice::indexOfImpl(Value value) const {
|
2020-08-31 08:14:53 +00:00
|
|
|
return std::get_if<FullMsgId>(&value)
|
|
|
|
? _slice.indexOf(*std::get_if<FullMsgId>(&value))
|
2017-08-29 15:15:54 +00:00
|
|
|
: (isolatedInSlice()
|
2017-12-05 06:41:43 +00:00
|
|
|
|| !_lastPhotoId
|
2020-08-31 08:14:53 +00:00
|
|
|
|| (*std::get_if<not_null<PhotoData*>>(&value))->id != *_lastPhotoId)
|
2018-09-21 16:28:46 +00:00
|
|
|
? std::nullopt
|
2017-08-29 15:15:54 +00:00
|
|
|
: Add(_slice.size() - 1, lastPhotoSkip());
|
|
|
|
}
|
|
|
|
|
2018-09-21 16:28:46 +00:00
|
|
|
std::optional<int> SharedMediaWithLastSlice::indexOf(Value value) const {
|
2018-01-03 08:23:34 +00:00
|
|
|
const auto result = indexOfImpl(value);
|
|
|
|
if (result && (*result < 0 || *result >= size())) {
|
|
|
|
// Should not happen.
|
|
|
|
auto info = QStringList();
|
|
|
|
info.push_back("slice:" + QString::number(_slice.size()));
|
|
|
|
info.push_back(_slice.fullCount()
|
|
|
|
? QString::number(*_slice.fullCount())
|
|
|
|
: QString("-"));
|
|
|
|
info.push_back(_slice.skippedBefore()
|
|
|
|
? QString::number(*_slice.skippedBefore())
|
|
|
|
: QString("-"));
|
|
|
|
info.push_back(_slice.skippedAfter()
|
|
|
|
? QString::number(*_slice.skippedAfter())
|
|
|
|
: QString("-"));
|
|
|
|
info.push_back("ending:" + (_ending
|
|
|
|
? QString::number(_ending->size())
|
|
|
|
: QString("-")));
|
|
|
|
info.push_back((_ending && _ending->fullCount())
|
|
|
|
? QString::number(*_ending->fullCount())
|
|
|
|
: QString("-"));
|
|
|
|
info.push_back((_ending && _ending->skippedBefore())
|
|
|
|
? QString::number(*_ending->skippedBefore())
|
|
|
|
: QString("-"));
|
|
|
|
info.push_back((_ending && _ending->skippedAfter())
|
|
|
|
? QString::number(*_ending->skippedAfter())
|
|
|
|
: QString("-"));
|
2020-08-31 08:14:53 +00:00
|
|
|
if (const auto msgId = std::get_if<FullMsgId>(&value)) {
|
2021-12-09 07:32:54 +00:00
|
|
|
info.push_back("value:" + QString::number(msgId->peer.value));
|
2021-09-30 06:04:55 +00:00
|
|
|
info.push_back(QString::number(msgId->msg.bare));
|
2020-08-31 08:14:53 +00:00
|
|
|
const auto index = _slice.indexOf(*std::get_if<FullMsgId>(&value));
|
2018-01-03 08:23:34 +00:00
|
|
|
info.push_back("index:" + (index
|
|
|
|
? QString::number(*index)
|
|
|
|
: QString("-")));
|
2020-08-31 08:14:53 +00:00
|
|
|
} else if (const auto photo = std::get_if<not_null<PhotoData*>>(&value)) {
|
2018-01-03 08:23:34 +00:00
|
|
|
info.push_back("value:" + QString::number((*photo)->id));
|
|
|
|
} else {
|
|
|
|
info.push_back("value:bad");
|
|
|
|
}
|
|
|
|
info.push_back("isolated:" + QString(Logs::b(isolatedInSlice())));
|
|
|
|
info.push_back("last:" + (_lastPhotoId
|
|
|
|
? QString::number(*_lastPhotoId)
|
|
|
|
: QString("-")));
|
|
|
|
info.push_back("isolated_last:" + (_isolatedLastPhoto
|
|
|
|
? QString(Logs::b(*_isolatedLastPhoto))
|
|
|
|
: QString("-")));
|
|
|
|
info.push_back("skip:" + (lastPhotoSkip()
|
|
|
|
? QString::number(*lastPhotoSkip())
|
|
|
|
: QString("-")));
|
|
|
|
CrashReports::SetAnnotation("DebugInfo", info.join(','));
|
|
|
|
Unexpected("Result in SharedMediaWithLastSlice::indexOf");
|
|
|
|
}
|
2017-11-23 09:58:12 +00:00
|
|
|
return _reversed
|
|
|
|
? (result | func::negate | func::add(size() - 1))
|
|
|
|
: result;
|
|
|
|
}
|
|
|
|
|
2017-09-04 19:14:44 +00:00
|
|
|
int SharedMediaWithLastSlice::size() const {
|
2017-08-29 15:15:54 +00:00
|
|
|
return _slice.size()
|
|
|
|
+ ((!isolatedInSlice() && lastPhotoSkip() == 1) ? 1 : 0);
|
|
|
|
}
|
|
|
|
|
2017-09-04 19:14:44 +00:00
|
|
|
SharedMediaWithLastSlice::Value SharedMediaWithLastSlice::operator[](int index) const {
|
2017-08-29 15:15:54 +00:00
|
|
|
Expects(index >= 0 && index < size());
|
|
|
|
|
2017-11-23 09:58:12 +00:00
|
|
|
if (_reversed) {
|
|
|
|
index = size() - index - 1;
|
|
|
|
}
|
2017-08-29 15:15:54 +00:00
|
|
|
return (index < _slice.size())
|
|
|
|
? Value(_slice[index])
|
2020-06-08 15:17:33 +00:00
|
|
|
: Value(_session->data().photo(*_lastPhotoId));
|
2017-08-29 15:15:54 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 16:28:46 +00:00
|
|
|
std::optional<int> SharedMediaWithLastSlice::distance(
|
2017-10-29 15:32:01 +00:00
|
|
|
const Key &a,
|
|
|
|
const Key &b) const {
|
2017-08-29 15:15:54 +00:00
|
|
|
if (auto i = indexOf(ComputeId(a))) {
|
|
|
|
if (auto j = indexOf(ComputeId(b))) {
|
|
|
|
return *j - *i;
|
|
|
|
}
|
|
|
|
}
|
2018-09-21 16:28:46 +00:00
|
|
|
return std::nullopt;
|
2017-08-29 15:15:54 +00:00
|
|
|
}
|
|
|
|
|
2017-11-23 09:58:12 +00:00
|
|
|
void SharedMediaWithLastSlice::reverse() {
|
|
|
|
_reversed = !_reversed;
|
|
|
|
}
|
|
|
|
|
2018-09-21 16:28:46 +00:00
|
|
|
std::optional<PhotoId> SharedMediaWithLastSlice::LastPeerPhotoId(
|
2020-06-08 15:17:33 +00:00
|
|
|
not_null<Main::Session*> session,
|
2017-12-05 06:41:43 +00:00
|
|
|
PeerId peerId) {
|
2020-06-08 15:17:33 +00:00
|
|
|
if (const auto peer = session->data().peerLoaded(peerId)) {
|
2017-12-05 06:41:43 +00:00
|
|
|
return peer->userpicPhotoUnknown()
|
2018-09-21 16:28:46 +00:00
|
|
|
? std::nullopt
|
2017-12-05 06:41:43 +00:00
|
|
|
: base::make_optional(peer->userpicPhotoId());
|
2017-08-29 15:15:54 +00:00
|
|
|
}
|
2018-09-21 16:28:46 +00:00
|
|
|
return std::nullopt;
|
2017-08-29 15:15:54 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 16:28:46 +00:00
|
|
|
std::optional<bool> SharedMediaWithLastSlice::IsLastIsolated(
|
2020-06-08 15:17:33 +00:00
|
|
|
not_null<Main::Session*> session,
|
2017-10-29 15:32:01 +00:00
|
|
|
const SparseIdsMergedSlice &slice,
|
2018-09-21 16:28:46 +00:00
|
|
|
const std::optional<SparseIdsMergedSlice> &ending,
|
|
|
|
std::optional<PhotoId> lastPeerPhotoId) {
|
2017-12-05 06:41:43 +00:00
|
|
|
if (!lastPeerPhotoId) {
|
2018-09-21 16:28:46 +00:00
|
|
|
return std::nullopt;
|
2017-12-05 06:41:43 +00:00
|
|
|
} else if (!*lastPeerPhotoId) {
|
2017-08-29 15:15:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return LastFullMsgId(ending ? *ending : slice)
|
2020-06-08 15:17:33 +00:00
|
|
|
| [&](FullMsgId msgId) { return session->data().message(msgId); }
|
2018-01-14 16:02:25 +00:00
|
|
|
| [](HistoryItem *item) { return item ? item->media() : nullptr; }
|
|
|
|
| [](Data::Media *media) { return media ? media->photo() : nullptr; }
|
2017-08-29 15:15:54 +00:00
|
|
|
| [](PhotoData *photo) { return photo ? photo->id : 0; }
|
2017-12-05 06:41:43 +00:00
|
|
|
| [&](PhotoId photoId) { return *lastPeerPhotoId != photoId; };
|
2017-08-29 15:15:54 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 16:28:46 +00:00
|
|
|
std::optional<FullMsgId> SharedMediaWithLastSlice::LastFullMsgId(
|
2017-10-29 15:32:01 +00:00
|
|
|
const SparseIdsMergedSlice &slice) {
|
2017-08-29 15:15:54 +00:00
|
|
|
if (slice.fullCount() == 0) {
|
|
|
|
return FullMsgId();
|
|
|
|
} else if (slice.size() == 0 || slice.skippedAfter() != 0) {
|
2018-09-21 16:28:46 +00:00
|
|
|
return std::nullopt;
|
2017-08-29 15:15:54 +00:00
|
|
|
}
|
|
|
|
return slice[slice.size() - 1];
|
|
|
|
}
|
|
|
|
|
2017-09-04 19:14:44 +00:00
|
|
|
rpl::producer<SharedMediaWithLastSlice> SharedMediaWithLastViewer(
|
2020-06-08 09:06:50 +00:00
|
|
|
not_null<Main::Session*> session,
|
2017-09-04 19:14:44 +00:00
|
|
|
SharedMediaWithLastSlice::Key key,
|
|
|
|
int limitBefore,
|
|
|
|
int limitAfter) {
|
|
|
|
return [=](auto consumer) {
|
2021-07-01 00:00:00 +00:00
|
|
|
auto viewerKey = SharedMediaMergedKey(
|
|
|
|
SharedMediaWithLastSlice::ViewerKey(key),
|
|
|
|
key.type);
|
|
|
|
|
2020-08-31 08:14:53 +00:00
|
|
|
if (std::get_if<not_null<PhotoData*>>(&key.universalId)) {
|
2017-10-09 21:55:30 +00:00
|
|
|
return SharedMediaMergedViewer(
|
2020-06-08 09:06:50 +00:00
|
|
|
session,
|
2021-07-01 00:00:00 +00:00
|
|
|
std::move(viewerKey),
|
|
|
|
limitBefore,
|
|
|
|
limitAfter
|
|
|
|
) | rpl::start_with_next([=](SparseIdsMergedSlice &&update) {
|
|
|
|
consumer.put_next(SharedMediaWithLastSlice(
|
|
|
|
session,
|
|
|
|
key,
|
|
|
|
std::move(update),
|
|
|
|
std::nullopt));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-10-11 15:08:19 +00:00
|
|
|
if (key.topicRootId == SharedMediaWithLastSlice::kScheduledTopicId) {
|
2021-07-01 00:00:00 +00:00
|
|
|
return SharedScheduledMediaViewer(
|
|
|
|
session,
|
|
|
|
std::move(viewerKey),
|
2017-10-09 21:55:30 +00:00
|
|
|
limitBefore,
|
|
|
|
limitAfter
|
2017-10-29 15:32:01 +00:00
|
|
|
) | rpl::start_with_next([=](SparseIdsMergedSlice &&update) {
|
2017-10-09 21:55:30 +00:00
|
|
|
consumer.put_next(SharedMediaWithLastSlice(
|
2020-06-08 15:17:33 +00:00
|
|
|
session,
|
2017-10-09 21:55:30 +00:00
|
|
|
key,
|
|
|
|
std::move(update),
|
2018-09-21 16:28:46 +00:00
|
|
|
std::nullopt));
|
2017-10-09 21:55:30 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return rpl::combine(
|
|
|
|
SharedMediaMergedViewer(
|
2020-06-08 09:06:50 +00:00
|
|
|
session,
|
2021-07-01 00:00:00 +00:00
|
|
|
std::move(viewerKey),
|
2017-10-09 21:55:30 +00:00
|
|
|
limitBefore,
|
|
|
|
limitAfter),
|
2017-09-04 19:14:44 +00:00
|
|
|
SharedMediaMergedViewer(
|
2020-06-08 09:06:50 +00:00
|
|
|
session,
|
2017-10-29 15:32:01 +00:00
|
|
|
SharedMediaMergedKey(
|
|
|
|
SharedMediaWithLastSlice::EndingKey(key),
|
|
|
|
key.type),
|
2017-09-04 19:14:44 +00:00
|
|
|
1,
|
2017-10-09 21:55:30 +00:00
|
|
|
1)
|
|
|
|
) | rpl::start_with_next([=](
|
2017-10-29 15:32:01 +00:00
|
|
|
SparseIdsMergedSlice &&viewer,
|
|
|
|
SparseIdsMergedSlice &&ending) {
|
2017-10-09 21:55:30 +00:00
|
|
|
consumer.put_next(SharedMediaWithLastSlice(
|
2020-06-08 15:17:33 +00:00
|
|
|
session,
|
2017-10-09 21:55:30 +00:00
|
|
|
key,
|
|
|
|
std::move(viewer),
|
|
|
|
std::move(ending)));
|
|
|
|
});
|
2017-09-04 19:14:44 +00:00
|
|
|
};
|
|
|
|
}
|
2017-11-23 09:58:12 +00:00
|
|
|
|
|
|
|
rpl::producer<SharedMediaWithLastSlice> SharedMediaWithLastReversedViewer(
|
2020-06-08 09:06:50 +00:00
|
|
|
not_null<Main::Session*> session,
|
2017-11-23 09:58:12 +00:00
|
|
|
SharedMediaWithLastSlice::Key key,
|
|
|
|
int limitBefore,
|
|
|
|
int limitAfter) {
|
2017-12-22 07:05:20 +00:00
|
|
|
return SharedMediaWithLastViewer(
|
2020-06-08 09:06:50 +00:00
|
|
|
session,
|
2017-12-22 07:05:20 +00:00
|
|
|
key,
|
|
|
|
limitBefore,
|
|
|
|
limitAfter
|
|
|
|
) | rpl::map([](SharedMediaWithLastSlice &&slice) {
|
|
|
|
slice.reverse();
|
|
|
|
return std::move(slice);
|
|
|
|
});
|
2017-11-23 09:58:12 +00:00
|
|
|
}
|