2023-05-03 18:30:37 +00:00
|
|
|
/*
|
|
|
|
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_stories.h"
|
|
|
|
|
2023-06-22 15:29:11 +00:00
|
|
|
#include "api/api_report.h"
|
2023-06-05 15:23:49 +00:00
|
|
|
#include "base/unixtime.h"
|
2023-05-26 09:27:34 +00:00
|
|
|
#include "apiwrap.h"
|
2023-05-29 15:09:36 +00:00
|
|
|
#include "core/application.h"
|
2023-05-26 09:27:34 +00:00
|
|
|
#include "data/data_changes.h"
|
2023-08-31 08:58:34 +00:00
|
|
|
#include "data/data_channel.h"
|
2023-05-03 18:30:37 +00:00
|
|
|
#include "data/data_document.h"
|
2023-07-18 08:15:19 +00:00
|
|
|
#include "data/data_folder.h"
|
2023-05-03 18:30:37 +00:00
|
|
|
#include "data/data_photo.h"
|
2023-06-15 16:28:53 +00:00
|
|
|
#include "data/data_user.h"
|
2023-05-03 18:30:37 +00:00
|
|
|
#include "data/data_session.h"
|
2023-06-15 16:28:53 +00:00
|
|
|
#include "history/history.h"
|
|
|
|
#include "history/history_item.h"
|
2023-05-26 09:27:34 +00:00
|
|
|
#include "lang/lang_keys.h"
|
2024-04-16 15:32:33 +00:00
|
|
|
#include "main/main_app_config.h"
|
2023-05-03 18:30:37 +00:00
|
|
|
#include "main/main_session.h"
|
2023-06-15 16:28:53 +00:00
|
|
|
#include "ui/layers/show.h"
|
2023-05-26 09:27:34 +00:00
|
|
|
#include "ui/text/text_utilities.h"
|
2023-05-03 18:30:37 +00:00
|
|
|
|
|
|
|
namespace Data {
|
|
|
|
namespace {
|
|
|
|
|
2023-05-26 09:27:34 +00:00
|
|
|
constexpr auto kMaxResolveTogether = 100;
|
2023-05-29 12:03:23 +00:00
|
|
|
constexpr auto kIgnorePreloadAroundIfLoaded = 15;
|
|
|
|
constexpr auto kPreloadAroundCount = 30;
|
2023-05-29 15:09:36 +00:00
|
|
|
constexpr auto kMarkAsReadDelay = 3 * crl::time(1000);
|
2023-06-09 14:49:09 +00:00
|
|
|
constexpr auto kIncrementViewsDelay = 5 * crl::time(1000);
|
2023-06-07 15:56:38 +00:00
|
|
|
constexpr auto kArchiveFirstPerPage = 30;
|
|
|
|
constexpr auto kArchivePerPage = 100;
|
2023-06-05 16:31:15 +00:00
|
|
|
constexpr auto kSavedFirstPerPage = 30;
|
|
|
|
constexpr auto kSavedPerPage = 100;
|
2023-06-26 16:22:56 +00:00
|
|
|
constexpr auto kMaxPreloadSources = 10;
|
|
|
|
constexpr auto kStillPreloadFromFirst = 3;
|
2023-07-03 20:05:11 +00:00
|
|
|
constexpr auto kMaxSegmentsCount = 180;
|
2023-07-05 15:52:22 +00:00
|
|
|
constexpr auto kPollingIntervalChat = 5 * TimeId(60);
|
|
|
|
constexpr auto kPollingIntervalViewer = 1 * TimeId(60);
|
2023-07-05 17:02:57 +00:00
|
|
|
constexpr auto kPollViewsInterval = 10 * crl::time(1000);
|
|
|
|
constexpr auto kPollingViewsPerPage = Story::kRecentViewersMax;
|
2023-05-26 09:27:34 +00:00
|
|
|
|
|
|
|
using UpdateFlag = StoryUpdate::Flag;
|
|
|
|
|
2023-06-02 14:26:39 +00:00
|
|
|
[[nodiscard]] std::optional<StoryMedia> ParseMedia(
|
2023-06-13 18:07:29 +00:00
|
|
|
not_null<Session*> owner,
|
|
|
|
const MTPMessageMedia &media) {
|
2023-05-26 15:18:10 +00:00
|
|
|
return media.match([&](const MTPDmessageMediaPhoto &data)
|
2023-05-30 17:12:15 +00:00
|
|
|
-> std::optional<StoryMedia> {
|
2023-05-26 15:18:10 +00:00
|
|
|
if (const auto photo = data.vphoto()) {
|
|
|
|
const auto result = owner->processPhoto(*photo);
|
|
|
|
if (!result->isNull()) {
|
|
|
|
return StoryMedia{ result };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}, [&](const MTPDmessageMediaDocument &data)
|
2023-05-30 17:12:15 +00:00
|
|
|
-> std::optional<StoryMedia> {
|
2023-05-26 15:18:10 +00:00
|
|
|
if (const auto document = data.vdocument()) {
|
|
|
|
const auto result = owner->processDocument(*document);
|
|
|
|
if (!result->isNull()
|
|
|
|
&& (result->isGifv() || result->isVideoFile())) {
|
2023-06-16 12:44:32 +00:00
|
|
|
result->setStoryMedia(true);
|
2023-05-26 15:18:10 +00:00
|
|
|
return StoryMedia{ result };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {};
|
2023-06-16 09:34:08 +00:00
|
|
|
}, [&](const MTPDmessageMediaUnsupported &data) {
|
|
|
|
return std::make_optional(StoryMedia{ v::null });
|
2023-05-26 15:18:10 +00:00
|
|
|
}, [](const auto &) { return std::optional<StoryMedia>(); });
|
|
|
|
}
|
|
|
|
|
2023-05-03 18:30:37 +00:00
|
|
|
} // namespace
|
|
|
|
|
2024-04-18 05:46:06 +00:00
|
|
|
std::vector<StoryId> RespectingPinned(const StoriesIds &ids) {
|
2024-04-16 15:32:33 +00:00
|
|
|
if (ids.pinnedToTop.empty()) {
|
2024-04-18 05:46:06 +00:00
|
|
|
return ids.list | ranges::to_vector;
|
2024-04-16 15:32:33 +00:00
|
|
|
}
|
2024-04-18 05:46:06 +00:00
|
|
|
auto result = std::vector<StoryId>();
|
|
|
|
result.reserve(ids.list.size());
|
|
|
|
result.insert(end(result), begin(ids.pinnedToTop), end(ids.pinnedToTop));
|
|
|
|
for (const auto &id : ids.list) {
|
|
|
|
if (!ranges::contains(ids.pinnedToTop, id)) {
|
|
|
|
result.push_back(id);
|
2024-04-16 15:32:33 +00:00
|
|
|
}
|
|
|
|
}
|
2024-04-18 05:46:06 +00:00
|
|
|
return result;
|
2024-04-16 15:32:33 +00:00
|
|
|
}
|
|
|
|
|
2023-06-02 14:26:39 +00:00
|
|
|
StoriesSourceInfo StoriesSource::info() const {
|
|
|
|
return {
|
2023-08-31 08:58:34 +00:00
|
|
|
.id = peer->id,
|
2023-06-02 14:26:39 +00:00
|
|
|
.last = ids.empty() ? 0 : ids.back().date,
|
2023-07-05 07:55:16 +00:00
|
|
|
.count = uint32(std::min(int(ids.size()), kMaxSegmentsCount)),
|
|
|
|
.unreadCount = uint32(std::min(unreadCount(), kMaxSegmentsCount)),
|
2023-08-31 08:58:34 +00:00
|
|
|
.premium = (peer->isUser() && peer->asUser()->isPremium()) ? 1U : 0,
|
2023-06-02 14:26:39 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-07-03 20:05:11 +00:00
|
|
|
int StoriesSource::unreadCount() const {
|
|
|
|
const auto i = ids.lower_bound(StoryIdDates{ .id = readTill + 1 });
|
|
|
|
return int(end(ids) - i);
|
2023-05-26 07:21:19 +00:00
|
|
|
}
|
|
|
|
|
2023-06-26 16:22:56 +00:00
|
|
|
StoryIdDates StoriesSource::toOpen() const {
|
|
|
|
if (ids.empty()) {
|
|
|
|
return {};
|
2023-06-14 17:31:23 +00:00
|
|
|
}
|
2023-06-26 16:22:56 +00:00
|
|
|
const auto i = ids.lower_bound(StoryIdDates{ readTill + 1 });
|
|
|
|
return (i != end(ids)) ? *i : ids.front();
|
2023-05-03 18:30:37 +00:00
|
|
|
}
|
|
|
|
|
2023-05-29 15:09:36 +00:00
|
|
|
Stories::Stories(not_null<Session*> owner)
|
|
|
|
: _owner(owner)
|
2023-06-05 15:23:49 +00:00
|
|
|
, _expireTimer([=] { processExpired(); })
|
2023-06-09 14:49:09 +00:00
|
|
|
, _markReadTimer([=] { sendMarkAsReadRequests(); })
|
2023-07-05 15:52:22 +00:00
|
|
|
, _incrementViewsTimer([=] { sendIncrementViewsRequests(); })
|
2023-07-05 17:02:57 +00:00
|
|
|
, _pollingTimer([=] { sendPollingRequests(); })
|
|
|
|
, _pollingViewsTimer([=] { sendPollingViewsRequests(); }) {
|
2023-09-05 07:07:32 +00:00
|
|
|
crl::on_main(this, [=] {
|
|
|
|
session().changes().peerUpdates(
|
|
|
|
Data::PeerUpdate::Flag::Rights
|
|
|
|
) | rpl::start_with_next([=](const Data::PeerUpdate &update) {
|
|
|
|
const auto channel = update.peer->asChannel();
|
|
|
|
if (!channel) {
|
|
|
|
return;
|
|
|
|
} else if (!channel->canEditStories()) {
|
|
|
|
const auto peerId = channel->id;
|
|
|
|
const auto i = _peersWithDeletedStories.find(peerId);
|
|
|
|
if (i != end(_peersWithDeletedStories)) {
|
|
|
|
_peersWithDeletedStories.erase(i);
|
|
|
|
for (auto j = begin(_deleted); j != end(_deleted);) {
|
|
|
|
if (j->peer == peerId) {
|
|
|
|
j = _deleted.erase(j);
|
|
|
|
} else {
|
|
|
|
++j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
clearArchive(channel);
|
|
|
|
}
|
|
|
|
}, _lifetime);
|
|
|
|
});
|
2023-05-03 18:30:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Stories::~Stories() {
|
2023-07-05 15:52:22 +00:00
|
|
|
Expects(_pollingSettings.empty());
|
2023-07-05 17:02:57 +00:00
|
|
|
Expects(_pollingViews.empty());
|
2023-05-03 18:30:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Session &Stories::owner() const {
|
|
|
|
return *_owner;
|
|
|
|
}
|
|
|
|
|
2023-05-26 09:27:34 +00:00
|
|
|
Main::Session &Stories::session() const {
|
|
|
|
return _owner->session();
|
|
|
|
}
|
|
|
|
|
2023-06-02 07:46:19 +00:00
|
|
|
void Stories::apply(const MTPDupdateStory &data) {
|
2023-08-31 08:58:34 +00:00
|
|
|
const auto peerId = peerFromMTP(data.vpeer());
|
|
|
|
const auto peer = _owner->peer(peerId);
|
2023-06-05 15:23:49 +00:00
|
|
|
const auto now = base::unixtime::now();
|
2023-08-31 08:58:34 +00:00
|
|
|
const auto idDates = parseAndApply(peer, data.vstory(), now);
|
2023-06-05 15:23:49 +00:00
|
|
|
if (!idDates) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto expired = (idDates.expires <= now);
|
|
|
|
if (expired) {
|
|
|
|
applyExpired({ peerId, idDates.id });
|
2023-06-02 14:26:39 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto i = _all.find(peerId);
|
|
|
|
if (i == end(_all)) {
|
2023-08-31 08:58:34 +00:00
|
|
|
requestPeerStories(peer);
|
2023-06-02 14:26:39 +00:00
|
|
|
return;
|
2023-06-05 15:23:49 +00:00
|
|
|
} else if (i->second.ids.contains(idDates)) {
|
2023-06-02 14:26:39 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-06-05 15:23:49 +00:00
|
|
|
const auto wasInfo = i->second.info();
|
|
|
|
i->second.ids.emplace(idDates);
|
|
|
|
const auto nowInfo = i->second.info();
|
2023-08-31 08:58:34 +00:00
|
|
|
if (peer->isSelf() && i->second.readTill < idDates.id) {
|
|
|
|
_readTill[peerId] = i->second.readTill = idDates.id;
|
2023-06-07 15:56:38 +00:00
|
|
|
}
|
2023-06-05 15:23:49 +00:00
|
|
|
if (wasInfo == nowInfo) {
|
2023-06-02 14:26:39 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto refreshInList = [&](StorySourcesList list) {
|
|
|
|
auto &sources = _sources[static_cast<int>(list)];
|
|
|
|
const auto i = ranges::find(
|
|
|
|
sources,
|
|
|
|
peerId,
|
|
|
|
&StoriesSourceInfo::id);
|
|
|
|
if (i != end(sources)) {
|
2023-06-05 15:23:49 +00:00
|
|
|
*i = nowInfo;
|
2023-06-02 14:26:39 +00:00
|
|
|
sort(list);
|
2023-06-02 07:46:19 +00:00
|
|
|
}
|
2023-06-02 14:26:39 +00:00
|
|
|
};
|
2023-08-31 08:58:34 +00:00
|
|
|
if (peer->hasStoriesHidden()) {
|
2023-06-21 06:12:28 +00:00
|
|
|
refreshInList(StorySourcesList::Hidden);
|
|
|
|
} else {
|
2023-06-02 14:26:39 +00:00
|
|
|
refreshInList(StorySourcesList::NotHidden);
|
2023-06-02 07:46:19 +00:00
|
|
|
}
|
2023-06-19 17:00:34 +00:00
|
|
|
_sourceChanged.fire_copy(peerId);
|
2023-08-31 08:58:34 +00:00
|
|
|
updatePeerStoriesState(peer);
|
2023-07-04 16:13:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Stories::apply(const MTPDupdateReadStories &data) {
|
2023-08-31 08:58:34 +00:00
|
|
|
bumpReadTill(peerFromMTP(data.vpeer()), data.vmax_id().v);
|
2023-05-03 18:30:37 +00:00
|
|
|
}
|
|
|
|
|
2023-08-01 17:09:09 +00:00
|
|
|
void Stories::apply(const MTPStoriesStealthMode &stealthMode) {
|
|
|
|
const auto &data = stealthMode.data();
|
|
|
|
_stealthMode = StealthMode{
|
|
|
|
.enabledTill = data.vactive_until_date().value_or_empty(),
|
|
|
|
.cooldownTill = data.vcooldown_until_date().value_or_empty(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-08-31 08:58:34 +00:00
|
|
|
void Stories::apply(not_null<PeerData*> peer, const MTPPeerStories *data) {
|
2023-06-05 16:31:15 +00:00
|
|
|
if (!data) {
|
2023-06-21 06:12:28 +00:00
|
|
|
applyDeletedFromSources(peer->id, StorySourcesList::NotHidden);
|
|
|
|
applyDeletedFromSources(peer->id, StorySourcesList::Hidden);
|
2023-06-05 16:31:15 +00:00
|
|
|
_all.erase(peer->id);
|
|
|
|
_sourceChanged.fire_copy(peer->id);
|
2023-08-31 08:58:34 +00:00
|
|
|
updatePeerStoriesState(peer);
|
2023-06-05 16:31:15 +00:00
|
|
|
} else {
|
|
|
|
parseAndApply(*data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-14 22:59:50 +00:00
|
|
|
Story *Stories::applySingle(PeerId peerId, const MTPstoryItem &story) {
|
2023-06-23 12:04:53 +00:00
|
|
|
const auto idDates = parseAndApply(
|
|
|
|
_owner->peer(peerId),
|
|
|
|
story,
|
|
|
|
base::unixtime::now());
|
|
|
|
const auto value = idDates
|
|
|
|
? lookup({ peerId, idDates.id })
|
|
|
|
: base::make_unexpected(NoStory::Deleted);
|
|
|
|
return value ? value->get() : nullptr;
|
|
|
|
}
|
|
|
|
|
2023-08-31 08:58:34 +00:00
|
|
|
void Stories::requestPeerStories(
|
|
|
|
not_null<PeerData*> peer,
|
2023-07-14 08:02:04 +00:00
|
|
|
Fn<void()> done) {
|
2024-01-09 17:32:18 +00:00
|
|
|
const auto &[i, ok] = _requestingPeerStories.emplace(peer);
|
2023-07-14 08:02:04 +00:00
|
|
|
if (done) {
|
|
|
|
i->second.push_back(std::move(done));
|
|
|
|
}
|
|
|
|
if (!ok) {
|
2023-06-02 14:26:39 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-07-14 08:02:04 +00:00
|
|
|
const auto finish = [=] {
|
2023-08-31 08:58:34 +00:00
|
|
|
if (const auto callbacks = _requestingPeerStories.take(peer)) {
|
2023-07-14 08:02:04 +00:00
|
|
|
for (const auto &callback : *callbacks) {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2023-08-31 08:58:34 +00:00
|
|
|
_owner->session().api().request(MTPstories_GetPeerStories(
|
|
|
|
peer->input
|
|
|
|
)).done([=](const MTPstories_PeerStories &result) {
|
2023-06-02 14:26:39 +00:00
|
|
|
const auto &data = result.data();
|
|
|
|
_owner->processUsers(data.vusers());
|
2023-08-31 08:58:34 +00:00
|
|
|
_owner->processChats(data.vchats());
|
2023-06-02 14:26:39 +00:00
|
|
|
parseAndApply(data.vstories());
|
2023-07-14 08:02:04 +00:00
|
|
|
finish();
|
2023-06-02 14:26:39 +00:00
|
|
|
}).fail([=] {
|
2023-08-31 08:58:34 +00:00
|
|
|
applyDeletedFromSources(peer->id, StorySourcesList::NotHidden);
|
|
|
|
applyDeletedFromSources(peer->id, StorySourcesList::Hidden);
|
2023-07-14 08:02:04 +00:00
|
|
|
finish();
|
2023-06-02 14:26:39 +00:00
|
|
|
}).send();
|
2023-06-05 15:23:49 +00:00
|
|
|
}
|
2023-06-02 14:26:39 +00:00
|
|
|
|
2023-06-05 15:23:49 +00:00
|
|
|
void Stories::registerExpiring(TimeId expires, FullStoryId id) {
|
|
|
|
for (auto i = _expiring.findFirst(expires)
|
|
|
|
; (i != end(_expiring)) && (i->first == expires)
|
|
|
|
; ++i) {
|
|
|
|
if (i->second == id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const auto reschedule = _expiring.empty()
|
|
|
|
|| (_expiring.front().first > expires);
|
|
|
|
_expiring.emplace(expires, id);
|
|
|
|
if (reschedule) {
|
|
|
|
scheduleExpireTimer();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stories::scheduleExpireTimer() {
|
|
|
|
if (_expireSchedulePosted) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_expireSchedulePosted = true;
|
|
|
|
crl::on_main(this, [=] {
|
|
|
|
if (!_expireSchedulePosted) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_expireSchedulePosted = false;
|
|
|
|
if (_expiring.empty()) {
|
|
|
|
_expireTimer.cancel();
|
|
|
|
} else {
|
|
|
|
const auto nearest = _expiring.front().first;
|
|
|
|
const auto now = base::unixtime::now();
|
|
|
|
const auto delay = (nearest > now)
|
|
|
|
? (nearest - now)
|
|
|
|
: 0;
|
|
|
|
_expireTimer.callOnce(delay * crl::time(1000));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stories::processExpired() {
|
|
|
|
const auto now = base::unixtime::now();
|
|
|
|
auto expired = base::flat_set<FullStoryId>();
|
|
|
|
auto i = begin(_expiring);
|
|
|
|
for (; i != end(_expiring) && i->first <= now; ++i) {
|
|
|
|
expired.emplace(i->second);
|
|
|
|
}
|
|
|
|
_expiring.erase(begin(_expiring), i);
|
|
|
|
for (const auto &id : expired) {
|
|
|
|
applyExpired(id);
|
|
|
|
}
|
|
|
|
if (!_expiring.empty()) {
|
|
|
|
scheduleExpireTimer();
|
|
|
|
}
|
2023-06-02 14:26:39 +00:00
|
|
|
}
|
|
|
|
|
2023-09-05 07:07:32 +00:00
|
|
|
Stories::Set *Stories::lookupArchive(not_null<PeerData*> peer) {
|
|
|
|
const auto peerId = peer->id;
|
|
|
|
if (hasArchive(peer)) {
|
|
|
|
const auto i = _archive.find(peerId);
|
|
|
|
return (i != end(_archive))
|
|
|
|
? &i->second
|
|
|
|
: &_archive.emplace(peerId, Set()).first->second;
|
|
|
|
}
|
|
|
|
clearArchive(peer);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stories::clearArchive(not_null<PeerData*> peer) {
|
|
|
|
const auto peerId = peer->id;
|
|
|
|
const auto i = _archive.find(peerId);
|
|
|
|
if (i == end(_archive)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto archive = base::take(i->second);
|
|
|
|
_archive.erase(i);
|
|
|
|
for (const auto &id : archive.ids.list) {
|
|
|
|
if (const auto story = lookup({ peerId, id })) {
|
2024-04-16 10:23:53 +00:00
|
|
|
if ((*story)->expired() && !(*story)->inProfile()) {
|
2023-09-05 07:07:32 +00:00
|
|
|
applyDeleted(peer, id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_archiveChanged.fire_copy(peerId);
|
|
|
|
}
|
|
|
|
|
2023-08-31 08:58:34 +00:00
|
|
|
void Stories::parseAndApply(const MTPPeerStories &stories) {
|
2023-05-03 18:30:37 +00:00
|
|
|
const auto &data = stories.data();
|
2023-08-31 08:58:34 +00:00
|
|
|
const auto peerId = peerFromMTP(data.vpeer());
|
2023-07-19 14:34:04 +00:00
|
|
|
const auto already = _readTill.find(peerId);
|
|
|
|
const auto readTill = std::max(
|
|
|
|
data.vmax_read_id().value_or_empty(),
|
|
|
|
(already != end(_readTill) ? already->second : 0));
|
2023-08-31 08:58:34 +00:00
|
|
|
const auto peer = _owner->peer(peerId);
|
2023-06-02 14:26:39 +00:00
|
|
|
auto result = StoriesSource{
|
2023-08-31 08:58:34 +00:00
|
|
|
.peer = peer,
|
2023-05-03 18:30:37 +00:00
|
|
|
.readTill = readTill,
|
2023-08-31 08:58:34 +00:00
|
|
|
.hidden = peer->hasStoriesHidden(),
|
2023-05-03 18:30:37 +00:00
|
|
|
};
|
|
|
|
const auto &list = data.vstories().v;
|
2023-06-05 15:23:49 +00:00
|
|
|
const auto now = base::unixtime::now();
|
2023-05-26 07:21:19 +00:00
|
|
|
result.ids.reserve(list.size());
|
2023-05-03 18:30:37 +00:00
|
|
|
for (const auto &story : list) {
|
2023-08-31 08:58:34 +00:00
|
|
|
if (const auto id = parseAndApply(result.peer, story, now)) {
|
2023-06-02 07:46:19 +00:00
|
|
|
result.ids.emplace(id);
|
2023-06-02 14:26:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (result.ids.empty()) {
|
2023-06-21 06:12:28 +00:00
|
|
|
applyDeletedFromSources(peerId, StorySourcesList::NotHidden);
|
|
|
|
applyDeletedFromSources(peerId, StorySourcesList::Hidden);
|
2023-08-31 08:58:34 +00:00
|
|
|
peer->setStoriesState(PeerData::StoriesState::None);
|
2023-06-02 14:26:39 +00:00
|
|
|
return;
|
2023-08-31 08:58:34 +00:00
|
|
|
} else if (peer->isSelf()) {
|
2023-06-07 15:56:38 +00:00
|
|
|
result.readTill = result.ids.back().id;
|
2023-06-02 14:26:39 +00:00
|
|
|
}
|
2023-07-04 16:13:56 +00:00
|
|
|
_readTill[peerId] = result.readTill;
|
2023-06-02 14:26:39 +00:00
|
|
|
const auto info = result.info();
|
|
|
|
const auto i = _all.find(peerId);
|
|
|
|
if (i != end(_all)) {
|
|
|
|
if (i->second != result) {
|
|
|
|
i->second = std::move(result);
|
|
|
|
}
|
|
|
|
} else {
|
2023-06-09 17:25:26 +00:00
|
|
|
_all.emplace(peerId, std::move(result));
|
2023-06-02 14:26:39 +00:00
|
|
|
}
|
|
|
|
const auto add = [&](StorySourcesList list) {
|
|
|
|
auto &sources = _sources[static_cast<int>(list)];
|
|
|
|
const auto i = ranges::find(
|
|
|
|
sources,
|
|
|
|
peerId,
|
|
|
|
&StoriesSourceInfo::id);
|
|
|
|
if (i == end(sources)) {
|
|
|
|
sources.push_back(info);
|
|
|
|
} else if (*i == info) {
|
|
|
|
return;
|
2023-06-02 07:46:19 +00:00
|
|
|
} else {
|
2023-06-02 14:26:39 +00:00
|
|
|
*i = info;
|
2023-06-02 07:46:19 +00:00
|
|
|
}
|
2023-06-02 14:26:39 +00:00
|
|
|
sort(list);
|
|
|
|
};
|
2023-08-31 08:58:34 +00:00
|
|
|
if (result.peer->isSelf()
|
|
|
|
|| (result.peer->isChannel() && result.peer->asChannel()->amIn())
|
|
|
|
|| (result.peer->isUser()
|
|
|
|
&& (result.peer->asUser()->isBot()
|
|
|
|
|| result.peer->asUser()->isContact()))
|
|
|
|
|| result.peer->isServiceUser()) {
|
|
|
|
const auto hidden = result.peer->hasStoriesHidden();
|
2023-06-21 06:12:28 +00:00
|
|
|
using List = StorySourcesList;
|
|
|
|
add(hidden ? List::Hidden : List::NotHidden);
|
|
|
|
applyDeletedFromSources(
|
|
|
|
peerId,
|
|
|
|
hidden ? List::NotHidden : List::Hidden);
|
2023-06-02 14:26:39 +00:00
|
|
|
} else {
|
2023-06-21 06:12:28 +00:00
|
|
|
applyDeletedFromSources(peerId, StorySourcesList::NotHidden);
|
|
|
|
applyDeletedFromSources(peerId, StorySourcesList::Hidden);
|
2023-05-03 18:30:37 +00:00
|
|
|
}
|
2023-06-05 16:31:15 +00:00
|
|
|
_sourceChanged.fire_copy(peerId);
|
2023-08-31 08:58:34 +00:00
|
|
|
updatePeerStoriesState(result.peer);
|
2023-05-03 18:30:37 +00:00
|
|
|
}
|
|
|
|
|
2023-05-26 15:18:10 +00:00
|
|
|
Story *Stories::parseAndApply(
|
|
|
|
not_null<PeerData*> peer,
|
2023-06-05 15:23:49 +00:00
|
|
|
const MTPDstoryItem &data,
|
|
|
|
TimeId now) {
|
2023-05-26 15:18:10 +00:00
|
|
|
const auto media = ParseMedia(_owner, data.vmedia());
|
|
|
|
if (!media) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2023-06-05 15:23:49 +00:00
|
|
|
const auto expires = data.vexpire_date().v;
|
2023-06-08 07:11:07 +00:00
|
|
|
const auto expired = (expires <= now);
|
2023-09-05 07:07:32 +00:00
|
|
|
if (expired && !data.is_pinned() && !hasArchive(peer)) {
|
2023-06-05 15:23:49 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2023-05-03 18:30:37 +00:00
|
|
|
const auto id = data.vid().v;
|
2023-06-30 07:48:18 +00:00
|
|
|
const auto fullId = FullStoryId{ peer->id, id };
|
2023-05-26 07:21:19 +00:00
|
|
|
auto &stories = _stories[peer->id];
|
|
|
|
const auto i = stories.find(id);
|
|
|
|
if (i != end(stories)) {
|
2023-06-07 15:56:38 +00:00
|
|
|
const auto result = i->second.get();
|
2023-06-26 16:22:56 +00:00
|
|
|
const auto mediaChanged = (result->media() != *media);
|
2023-07-05 17:02:57 +00:00
|
|
|
result->applyChanges(*media, data, now);
|
2023-07-05 15:52:22 +00:00
|
|
|
const auto j = _pollingSettings.find(result);
|
|
|
|
if (j != end(_pollingSettings)) {
|
|
|
|
maybeSchedulePolling(result, j->second, now);
|
2023-05-03 18:30:37 +00:00
|
|
|
}
|
2023-06-26 16:22:56 +00:00
|
|
|
if (mediaChanged) {
|
|
|
|
_preloaded.remove(fullId);
|
|
|
|
if (_preloading && _preloading->id() == fullId) {
|
|
|
|
_preloading = nullptr;
|
|
|
|
rebuildPreloadSources(StorySourcesList::NotHidden);
|
|
|
|
rebuildPreloadSources(StorySourcesList::Hidden);
|
|
|
|
continuePreloading();
|
|
|
|
}
|
2023-06-30 07:48:18 +00:00
|
|
|
_owner->refreshStoryItemViews(fullId);
|
2023-06-26 16:22:56 +00:00
|
|
|
}
|
2023-06-07 15:56:38 +00:00
|
|
|
return result;
|
2023-05-03 18:30:37 +00:00
|
|
|
}
|
2023-06-30 07:48:18 +00:00
|
|
|
const auto wasDeleted = _deleted.remove(fullId);
|
2023-05-26 07:21:19 +00:00
|
|
|
const auto result = stories.emplace(id, std::make_unique<Story>(
|
|
|
|
id,
|
|
|
|
peer,
|
|
|
|
StoryMedia{ *media },
|
2023-07-05 17:02:57 +00:00
|
|
|
data,
|
|
|
|
now
|
|
|
|
)).first->second.get();
|
2023-06-07 15:56:38 +00:00
|
|
|
|
2023-09-05 07:07:32 +00:00
|
|
|
if (const auto archive = lookupArchive(peer)) {
|
|
|
|
const auto added = archive->ids.list.emplace(id).second;
|
2023-06-07 15:56:38 +00:00
|
|
|
if (added) {
|
2023-09-05 07:07:32 +00:00
|
|
|
if (archive->total >= 0 && id > archive->lastId) {
|
|
|
|
++archive->total;
|
2023-06-07 15:56:38 +00:00
|
|
|
}
|
2023-09-05 07:07:32 +00:00
|
|
|
_archiveChanged.fire_copy(peer->id);
|
2023-06-07 15:56:38 +00:00
|
|
|
}
|
|
|
|
}
|
2023-06-05 15:23:49 +00:00
|
|
|
|
|
|
|
if (expired) {
|
2023-06-30 07:48:18 +00:00
|
|
|
_expiring.remove(expires, fullId);
|
|
|
|
applyExpired(fullId);
|
2023-06-05 15:23:49 +00:00
|
|
|
} else {
|
2023-06-30 07:48:18 +00:00
|
|
|
registerExpiring(expires, fullId);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wasDeleted) {
|
|
|
|
_owner->refreshStoryItemViews(fullId);
|
2023-06-05 15:23:49 +00:00
|
|
|
}
|
|
|
|
|
2023-05-26 07:21:19 +00:00
|
|
|
return result;
|
2023-05-03 18:30:37 +00:00
|
|
|
}
|
|
|
|
|
2023-06-05 15:23:49 +00:00
|
|
|
StoryIdDates Stories::parseAndApply(
|
2023-06-02 07:46:19 +00:00
|
|
|
not_null<PeerData*> peer,
|
2023-06-05 15:23:49 +00:00
|
|
|
const MTPstoryItem &story,
|
|
|
|
TimeId now) {
|
2023-06-02 07:46:19 +00:00
|
|
|
return story.match([&](const MTPDstoryItem &data) {
|
2023-06-05 15:23:49 +00:00
|
|
|
if (const auto story = parseAndApply(peer, data, now)) {
|
|
|
|
return story->idDates();
|
2023-06-02 07:46:19 +00:00
|
|
|
}
|
2023-09-05 07:07:32 +00:00
|
|
|
applyDeleted(peer, data.vid().v);
|
2023-06-05 15:23:49 +00:00
|
|
|
return StoryIdDates();
|
2023-06-02 07:46:19 +00:00
|
|
|
}, [&](const MTPDstoryItemSkipped &data) {
|
2023-06-05 15:23:49 +00:00
|
|
|
const auto expires = data.vexpire_date().v;
|
2023-06-08 07:11:07 +00:00
|
|
|
const auto expired = (expires <= now);
|
2023-06-05 15:23:49 +00:00
|
|
|
const auto fullId = FullStoryId{ peer->id, data.vid().v };
|
|
|
|
if (!expired) {
|
|
|
|
registerExpiring(expires, fullId);
|
2023-09-05 07:07:32 +00:00
|
|
|
} else if (!hasArchive(peer)) {
|
|
|
|
applyDeleted(peer, data.vid().v);
|
2023-06-05 15:23:49 +00:00
|
|
|
return StoryIdDates();
|
|
|
|
} else {
|
|
|
|
_expiring.remove(expires, fullId);
|
|
|
|
applyExpired(fullId);
|
|
|
|
}
|
|
|
|
return StoryIdDates{
|
|
|
|
data.vid().v,
|
|
|
|
data.vdate().v,
|
|
|
|
data.vexpire_date().v,
|
|
|
|
};
|
2023-06-02 07:46:19 +00:00
|
|
|
}, [&](const MTPDstoryItemDeleted &data) {
|
2023-09-05 07:07:32 +00:00
|
|
|
applyDeleted(peer, data.vid().v);
|
2023-06-05 15:23:49 +00:00
|
|
|
return StoryIdDates();
|
2023-06-02 07:46:19 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-05-26 09:27:34 +00:00
|
|
|
void Stories::updateDependentMessages(not_null<Data::Story*> story) {
|
|
|
|
const auto i = _dependentMessages.find(story);
|
|
|
|
if (i != end(_dependentMessages)) {
|
|
|
|
for (const auto &dependent : i->second) {
|
|
|
|
dependent->updateDependencyItem();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
session().changes().storyUpdated(
|
|
|
|
story,
|
|
|
|
Data::StoryUpdate::Flag::Edited);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stories::registerDependentMessage(
|
|
|
|
not_null<HistoryItem*> dependent,
|
|
|
|
not_null<Data::Story*> dependency) {
|
|
|
|
_dependentMessages[dependency].emplace(dependent);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stories::unregisterDependentMessage(
|
|
|
|
not_null<HistoryItem*> dependent,
|
|
|
|
not_null<Data::Story*> dependency) {
|
|
|
|
const auto i = _dependentMessages.find(dependency);
|
|
|
|
if (i != end(_dependentMessages)) {
|
|
|
|
if (i->second.remove(dependent) && i->second.empty()) {
|
|
|
|
_dependentMessages.erase(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-05 17:02:57 +00:00
|
|
|
void Stories::savedStateChanged(not_null<Story*> story) {
|
2023-06-07 15:56:38 +00:00
|
|
|
const auto id = story->id();
|
|
|
|
const auto peer = story->peer()->id;
|
2024-04-16 10:23:53 +00:00
|
|
|
const auto inProfile = story->inProfile();
|
|
|
|
if (inProfile) {
|
2023-06-07 15:56:38 +00:00
|
|
|
auto &saved = _saved[peer];
|
|
|
|
const auto added = saved.ids.list.emplace(id).second;
|
|
|
|
if (added) {
|
|
|
|
if (saved.total >= 0 && id > saved.lastId) {
|
|
|
|
++saved.total;
|
|
|
|
}
|
|
|
|
_savedChanged.fire_copy(peer);
|
|
|
|
}
|
|
|
|
} else if (const auto i = _saved.find(peer); i != end(_saved)) {
|
|
|
|
auto &saved = i->second;
|
|
|
|
if (saved.ids.list.remove(id)) {
|
|
|
|
if (saved.total > 0) {
|
|
|
|
--saved.total;
|
|
|
|
}
|
|
|
|
_savedChanged.fire_copy(peer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-02 14:26:39 +00:00
|
|
|
void Stories::loadMore(StorySourcesList list) {
|
|
|
|
const auto index = static_cast<int>(list);
|
|
|
|
if (_loadMoreRequestId[index] || _sourcesLoaded[index]) {
|
2023-05-03 18:30:37 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-06-21 06:12:28 +00:00
|
|
|
const auto hidden = (list == StorySourcesList::Hidden);
|
2023-05-03 18:30:37 +00:00
|
|
|
const auto api = &_owner->session().api();
|
|
|
|
using Flag = MTPstories_GetAllStories::Flag;
|
2023-06-02 14:26:39 +00:00
|
|
|
_loadMoreRequestId[index] = api->request(MTPstories_GetAllStories(
|
2023-06-21 06:12:28 +00:00
|
|
|
MTP_flags((hidden ? Flag::f_hidden : Flag())
|
2023-06-02 14:26:39 +00:00
|
|
|
| (_sourcesStates[index].isEmpty()
|
|
|
|
? Flag(0)
|
|
|
|
: (Flag::f_next | Flag::f_state))),
|
|
|
|
MTP_string(_sourcesStates[index])
|
2023-05-03 18:30:37 +00:00
|
|
|
)).done([=](const MTPstories_AllStories &result) {
|
2023-06-02 14:26:39 +00:00
|
|
|
_loadMoreRequestId[index] = 0;
|
2023-05-03 18:30:37 +00:00
|
|
|
|
|
|
|
result.match([&](const MTPDstories_allStories &data) {
|
|
|
|
_owner->processUsers(data.vusers());
|
2023-08-31 08:58:34 +00:00
|
|
|
_owner->processChats(data.vchats());
|
2023-06-02 14:26:39 +00:00
|
|
|
_sourcesStates[index] = qs(data.vstate());
|
|
|
|
_sourcesLoaded[index] = !data.is_has_more();
|
2023-08-31 08:58:34 +00:00
|
|
|
for (const auto &single : data.vpeer_stories().v) {
|
2023-06-02 14:26:39 +00:00
|
|
|
parseAndApply(single);
|
2023-05-03 18:30:37 +00:00
|
|
|
}
|
|
|
|
}, [](const MTPDstories_allStoriesNotModified &) {
|
|
|
|
});
|
2023-07-17 16:47:44 +00:00
|
|
|
|
2023-08-01 17:09:09 +00:00
|
|
|
result.match([&](const auto &data) {
|
|
|
|
apply(data.vstealth_mode());
|
|
|
|
});
|
|
|
|
|
2023-07-17 16:47:44 +00:00
|
|
|
preloadListsMore();
|
2023-05-03 18:30:37 +00:00
|
|
|
}).fail([=] {
|
2023-06-02 14:26:39 +00:00
|
|
|
_loadMoreRequestId[index] = 0;
|
2023-05-03 18:30:37 +00:00
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
2023-07-17 16:47:44 +00:00
|
|
|
void Stories::preloadListsMore() {
|
|
|
|
if (_loadMoreRequestId[static_cast<int>(StorySourcesList::NotHidden)]
|
|
|
|
|| _loadMoreRequestId[static_cast<int>(StorySourcesList::Hidden)]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto loading = [&](StorySourcesList list) {
|
|
|
|
return _loadMoreRequestId[static_cast<int>(list)] != 0;
|
|
|
|
};
|
|
|
|
const auto countLoaded = [&](StorySourcesList list) {
|
|
|
|
const auto index = static_cast<int>(list);
|
|
|
|
return _sourcesLoaded[index] || !_sourcesStates[index].isEmpty();
|
|
|
|
};
|
|
|
|
if (loading(StorySourcesList::NotHidden)
|
|
|
|
|| loading(StorySourcesList::Hidden)) {
|
|
|
|
return;
|
|
|
|
} else if (!countLoaded(StorySourcesList::NotHidden)) {
|
|
|
|
loadMore(StorySourcesList::NotHidden);
|
|
|
|
} else if (!countLoaded(StorySourcesList::Hidden)) {
|
|
|
|
loadMore(StorySourcesList::Hidden);
|
2023-09-05 07:07:32 +00:00
|
|
|
} else if (!archiveCountKnown(_owner->session().userPeerId())) {
|
|
|
|
archiveLoadMore(_owner->session().userPeerId());
|
2023-07-17 16:47:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-18 08:15:19 +00:00
|
|
|
void Stories::notifySourcesChanged(StorySourcesList list) {
|
|
|
|
_sourcesChanged[static_cast<int>(list)].fire({});
|
|
|
|
if (list == StorySourcesList::Hidden) {
|
|
|
|
pushHiddenCountsToFolder();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stories::pushHiddenCountsToFolder() {
|
|
|
|
const auto &list = sources(StorySourcesList::Hidden);
|
|
|
|
if (list.empty()) {
|
|
|
|
if (_folderForHidden) {
|
|
|
|
_folderForHidden->updateStoriesCount(0, 0);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!_folderForHidden) {
|
|
|
|
_folderForHidden = _owner->folder(Folder::kId);
|
|
|
|
}
|
|
|
|
const auto count = int(list.size());
|
|
|
|
const auto unread = ranges::count_if(
|
|
|
|
list,
|
|
|
|
[](const StoriesSourceInfo &info) { return info.unreadCount > 0; });
|
|
|
|
_folderForHidden->updateStoriesCount(count, unread);
|
|
|
|
}
|
|
|
|
|
2023-05-26 09:27:34 +00:00
|
|
|
void Stories::sendResolveRequests() {
|
2023-05-29 12:03:23 +00:00
|
|
|
if (!_resolveSent.empty()) {
|
2023-05-26 09:27:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto leftToSend = kMaxResolveTogether;
|
2023-05-29 12:03:23 +00:00
|
|
|
auto byPeer = base::flat_map<PeerId, QVector<MTPint>>();
|
|
|
|
for (auto i = begin(_resolvePending); i != end(_resolvePending);) {
|
2023-06-13 09:36:17 +00:00
|
|
|
const auto peerId = i->first;
|
|
|
|
auto &ids = i->second;
|
2023-05-29 12:03:23 +00:00
|
|
|
auto &sent = _resolveSent[peerId];
|
|
|
|
if (ids.size() <= leftToSend) {
|
|
|
|
sent = base::take(ids);
|
2023-06-13 09:36:17 +00:00
|
|
|
i = _resolvePending.erase(i); // Invalidates `ids`.
|
2023-05-29 12:03:23 +00:00
|
|
|
leftToSend -= int(sent.size());
|
|
|
|
} else {
|
|
|
|
sent = {
|
|
|
|
std::make_move_iterator(begin(ids)),
|
|
|
|
std::make_move_iterator(begin(ids) + leftToSend)
|
|
|
|
};
|
|
|
|
ids.erase(begin(ids), begin(ids) + leftToSend);
|
|
|
|
leftToSend = 0;
|
|
|
|
}
|
2023-05-26 09:27:34 +00:00
|
|
|
auto &prepared = byPeer[peerId];
|
2023-05-29 12:03:23 +00:00
|
|
|
for (auto &[storyId, callbacks] : sent) {
|
|
|
|
prepared.push_back(MTP_int(storyId));
|
2023-05-26 09:27:34 +00:00
|
|
|
}
|
2023-05-29 12:03:23 +00:00
|
|
|
if (!leftToSend) {
|
2023-05-26 09:27:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const auto api = &_owner->session().api();
|
|
|
|
for (auto &entry : byPeer) {
|
|
|
|
const auto peerId = entry.first;
|
|
|
|
auto &prepared = entry.second;
|
2023-05-29 12:03:23 +00:00
|
|
|
const auto finish = [=](PeerId peerId) {
|
|
|
|
const auto sent = _resolveSent.take(peerId);
|
|
|
|
Assert(sent.has_value());
|
|
|
|
for (const auto &[storyId, list] : *sent) {
|
|
|
|
finalizeResolve({ peerId, storyId });
|
|
|
|
for (const auto &callback : list) {
|
2023-05-26 09:27:34 +00:00
|
|
|
callback();
|
|
|
|
}
|
|
|
|
}
|
2023-05-29 12:03:23 +00:00
|
|
|
_itemsChanged.fire_copy(peerId);
|
|
|
|
if (_resolveSent.empty() && !_resolvePending.empty()) {
|
2023-05-26 09:27:34 +00:00
|
|
|
crl::on_main(&session(), [=] { sendResolveRequests(); });
|
|
|
|
}
|
|
|
|
};
|
2023-08-31 08:58:34 +00:00
|
|
|
const auto peer = _owner->session().data().peer(peerId);
|
2023-07-11 18:22:40 +00:00
|
|
|
api->request(MTPstories_GetStoriesByID(
|
2023-08-31 08:58:34 +00:00
|
|
|
peer->input,
|
2023-05-29 12:03:23 +00:00
|
|
|
MTP_vector<MTPint>(prepared)
|
|
|
|
)).done([=](const MTPstories_Stories &result) {
|
2023-05-26 09:27:34 +00:00
|
|
|
owner().processUsers(result.data().vusers());
|
2023-08-31 08:58:34 +00:00
|
|
|
owner().processChats(result.data().vchats());
|
|
|
|
processResolvedStories(peer, result.data().vstories().v);
|
|
|
|
finish(peer->id);
|
2023-05-29 12:03:23 +00:00
|
|
|
}).fail([=] {
|
|
|
|
finish(peerId);
|
2023-05-26 09:27:34 +00:00
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stories::processResolvedStories(
|
|
|
|
not_null<PeerData*> peer,
|
|
|
|
const QVector<MTPStoryItem> &list) {
|
2023-06-05 15:23:49 +00:00
|
|
|
const auto now = base::unixtime::now();
|
2023-05-26 09:27:34 +00:00
|
|
|
for (const auto &item : list) {
|
|
|
|
item.match([&](const MTPDstoryItem &data) {
|
2023-06-05 15:23:49 +00:00
|
|
|
if (!parseAndApply(peer, data, now)) {
|
2023-09-05 07:07:32 +00:00
|
|
|
applyDeleted(peer, data.vid().v);
|
2023-05-26 15:18:10 +00:00
|
|
|
}
|
2023-05-26 09:27:34 +00:00
|
|
|
}, [&](const MTPDstoryItemSkipped &data) {
|
|
|
|
LOG(("API Error: Unexpected storyItemSkipped in resolve."));
|
|
|
|
}, [&](const MTPDstoryItemDeleted &data) {
|
2023-09-05 07:07:32 +00:00
|
|
|
applyDeleted(peer, data.vid().v);
|
2023-05-26 09:27:34 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stories::finalizeResolve(FullStoryId id) {
|
|
|
|
const auto already = lookup(id);
|
|
|
|
if (!already.has_value() && already.error() == NoStory::Unknown) {
|
|
|
|
LOG(("API Error: Could not resolve story %1_%2"
|
|
|
|
).arg(id.peer.value
|
|
|
|
).arg(id.story));
|
2023-09-05 07:07:32 +00:00
|
|
|
applyDeleted(_owner->peer(id.peer), id.story);
|
2023-05-26 09:27:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-05 07:07:32 +00:00
|
|
|
void Stories::applyDeleted(not_null<PeerData*> peer, StoryId id) {
|
|
|
|
const auto fullId = FullStoryId{ peer->id, id };
|
|
|
|
applyRemovedFromActive(fullId);
|
2023-06-05 15:23:49 +00:00
|
|
|
|
2023-09-05 07:07:32 +00:00
|
|
|
if (const auto channel = peer->asChannel()) {
|
|
|
|
if (!hasArchive(channel)) {
|
|
|
|
_peersWithDeletedStories.emplace(channel->id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_deleted.emplace(fullId);
|
|
|
|
const auto peerId = peer->id;
|
|
|
|
const auto i = _stories.find(peerId);
|
2023-06-07 15:56:38 +00:00
|
|
|
if (i != end(_stories)) {
|
2023-09-05 07:07:32 +00:00
|
|
|
const auto j = i->second.find(id);
|
2023-06-07 15:56:38 +00:00
|
|
|
if (j != end(i->second)) {
|
2023-09-05 07:07:32 +00:00
|
|
|
const auto &story
|
|
|
|
= _deletingStories[fullId]
|
|
|
|
= std::move(j->second);
|
2023-06-05 15:23:49 +00:00
|
|
|
_expiring.remove(story->expires(), story->fullId());
|
2023-06-07 15:56:38 +00:00
|
|
|
i->second.erase(j);
|
2023-07-21 09:44:43 +00:00
|
|
|
|
2023-06-05 15:23:49 +00:00
|
|
|
session().changes().storyUpdated(
|
|
|
|
story.get(),
|
|
|
|
UpdateFlag::Destroyed);
|
|
|
|
removeDependencyStory(story.get());
|
2023-09-05 07:07:32 +00:00
|
|
|
if (hasArchive(story->peer())) {
|
|
|
|
if (const auto k = _archive.find(peerId)
|
|
|
|
; k != end(_archive)) {
|
|
|
|
const auto archive = &k->second;
|
|
|
|
if (archive->ids.list.remove(id)) {
|
|
|
|
if (archive->total > 0) {
|
|
|
|
--archive->total;
|
|
|
|
}
|
|
|
|
_archiveChanged.fire_copy(peerId);
|
|
|
|
}
|
2023-06-07 15:56:38 +00:00
|
|
|
}
|
|
|
|
}
|
2024-04-16 10:23:53 +00:00
|
|
|
if (story->inProfile()) {
|
2023-09-05 07:07:32 +00:00
|
|
|
if (const auto k = _saved.find(peerId); k != end(_saved)) {
|
2023-06-07 15:56:38 +00:00
|
|
|
const auto saved = &k->second;
|
2023-09-05 07:07:32 +00:00
|
|
|
if (saved->ids.list.remove(id)) {
|
2023-06-07 15:56:38 +00:00
|
|
|
if (saved->total > 0) {
|
|
|
|
--saved->total;
|
|
|
|
}
|
2023-09-05 07:07:32 +00:00
|
|
|
_savedChanged.fire_copy(peerId);
|
2023-06-07 15:56:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-05 07:07:32 +00:00
|
|
|
if (_preloading && _preloading->id() == fullId) {
|
2023-08-01 17:09:09 +00:00
|
|
|
_preloading = nullptr;
|
2023-09-05 07:07:32 +00:00
|
|
|
preloadFinished(fullId);
|
2023-06-26 16:22:56 +00:00
|
|
|
}
|
2023-09-05 07:07:32 +00:00
|
|
|
_owner->refreshStoryItemViews(fullId);
|
2023-07-05 15:52:22 +00:00
|
|
|
Assert(!_pollingSettings.contains(story.get()));
|
2023-09-05 07:07:32 +00:00
|
|
|
if (const auto j = _items.find(peerId); j != end(_items)) {
|
|
|
|
const auto k = j->second.find(id);
|
2023-07-21 08:18:16 +00:00
|
|
|
if (k != end(j->second)) {
|
|
|
|
Assert(!k->second.lock());
|
|
|
|
j->second.erase(k);
|
|
|
|
if (j->second.empty()) {
|
|
|
|
_items.erase(j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-06-07 15:56:38 +00:00
|
|
|
if (i->second.empty()) {
|
|
|
|
_stories.erase(i);
|
2023-06-05 15:23:49 +00:00
|
|
|
}
|
2023-09-05 07:07:32 +00:00
|
|
|
_deletingStories.remove(fullId);
|
2023-06-05 15:23:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stories::applyExpired(FullStoryId id) {
|
|
|
|
if (const auto maybeStory = lookup(id)) {
|
|
|
|
const auto story = *maybeStory;
|
2024-04-16 10:23:53 +00:00
|
|
|
if (!hasArchive(story->peer()) && !story->inProfile()) {
|
2023-09-05 07:07:32 +00:00
|
|
|
applyDeleted(story->peer(), id.story);
|
2023-06-05 15:23:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
applyRemovedFromActive(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stories::applyRemovedFromActive(FullStoryId id) {
|
2023-06-02 14:26:39 +00:00
|
|
|
const auto removeFromList = [&](StorySourcesList list) {
|
|
|
|
const auto index = static_cast<int>(list);
|
|
|
|
auto &sources = _sources[index];
|
|
|
|
const auto i = ranges::find(
|
|
|
|
sources,
|
|
|
|
id.peer,
|
|
|
|
&StoriesSourceInfo::id);
|
|
|
|
if (i != end(sources)) {
|
|
|
|
sources.erase(i);
|
2023-07-18 08:15:19 +00:00
|
|
|
notifySourcesChanged(list);
|
2023-06-02 14:26:39 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
const auto i = _all.find(id.peer);
|
|
|
|
if (i != end(_all)) {
|
2023-06-05 15:23:49 +00:00
|
|
|
const auto j = i->second.ids.lower_bound(StoryIdDates{ id.story });
|
2023-06-02 14:26:39 +00:00
|
|
|
if (j != end(i->second.ids) && j->id == id.story) {
|
|
|
|
i->second.ids.erase(j);
|
2023-08-31 08:58:34 +00:00
|
|
|
const auto peer = i->second.peer;
|
2023-06-02 14:26:39 +00:00
|
|
|
if (i->second.ids.empty()) {
|
|
|
|
_all.erase(i);
|
|
|
|
removeFromList(StorySourcesList::NotHidden);
|
2023-06-21 06:12:28 +00:00
|
|
|
removeFromList(StorySourcesList::Hidden);
|
2023-06-02 14:26:39 +00:00
|
|
|
}
|
2023-06-05 16:31:15 +00:00
|
|
|
_sourceChanged.fire_copy(id.peer);
|
2023-08-31 08:58:34 +00:00
|
|
|
updatePeerStoriesState(peer);
|
2023-06-02 14:26:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stories::applyDeletedFromSources(PeerId id, StorySourcesList list) {
|
2023-06-21 06:12:28 +00:00
|
|
|
auto &sources = _sources[static_cast<int>(list)];
|
|
|
|
const auto i = ranges::find(
|
|
|
|
sources,
|
|
|
|
id,
|
|
|
|
&StoriesSourceInfo::id);
|
|
|
|
if (i != end(sources)) {
|
|
|
|
sources.erase(i);
|
2023-05-26 14:48:33 +00:00
|
|
|
}
|
2023-07-18 08:15:19 +00:00
|
|
|
notifySourcesChanged(list);
|
2023-05-26 09:27:34 +00:00
|
|
|
}
|
|
|
|
|
2023-05-26 14:48:33 +00:00
|
|
|
void Stories::removeDependencyStory(not_null<Story*> story) {
|
|
|
|
const auto i = _dependentMessages.find(story);
|
|
|
|
if (i != end(_dependentMessages)) {
|
|
|
|
const auto items = std::move(i->second);
|
|
|
|
_dependentMessages.erase(i);
|
|
|
|
|
|
|
|
for (const auto &dependent : items) {
|
|
|
|
dependent->dependencyStoryRemoved(story);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-02 14:26:39 +00:00
|
|
|
void Stories::sort(StorySourcesList list) {
|
|
|
|
const auto index = static_cast<int>(list);
|
|
|
|
auto &sources = _sources[index];
|
2023-06-23 09:23:50 +00:00
|
|
|
const auto self = _owner->session().userPeerId();
|
|
|
|
const auto changelogSenderId = UserData::kServiceNotificationsId;
|
2023-06-02 14:26:39 +00:00
|
|
|
const auto proj = [&](const StoriesSourceInfo &info) {
|
|
|
|
const auto key = int64(info.last)
|
2023-06-23 09:23:50 +00:00
|
|
|
+ (info.premium ? (int64(1) << 47) : 0)
|
|
|
|
+ ((info.id == changelogSenderId) ? (int64(1) << 47) : 0)
|
2023-07-03 20:05:11 +00:00
|
|
|
+ ((info.unreadCount > 0) ? (int64(1) << 49) : 0)
|
2023-06-02 14:26:39 +00:00
|
|
|
+ ((info.id == self) ? (int64(1) << 50) : 0);
|
|
|
|
return std::make_pair(key, info.id);
|
|
|
|
};
|
|
|
|
ranges::sort(sources, ranges::greater(), proj);
|
2023-07-18 08:15:19 +00:00
|
|
|
notifySourcesChanged(list);
|
2023-06-26 16:22:56 +00:00
|
|
|
preloadSourcesChanged(list);
|
2023-06-02 14:26:39 +00:00
|
|
|
}
|
|
|
|
|
2023-06-05 16:31:15 +00:00
|
|
|
std::shared_ptr<HistoryItem> Stories::lookupItem(not_null<Story*> story) {
|
|
|
|
const auto i = _items.find(story->peer()->id);
|
|
|
|
if (i == end(_items)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
const auto j = i->second.find(story->id());
|
|
|
|
if (j == end(i->second)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return j->second.lock();
|
|
|
|
}
|
|
|
|
|
2023-08-01 17:09:09 +00:00
|
|
|
StealthMode Stories::stealthMode() const {
|
|
|
|
return _stealthMode.current();
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<StealthMode> Stories::stealthModeValue() const {
|
|
|
|
return _stealthMode.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stories::activateStealthMode(Fn<void()> done) {
|
|
|
|
const auto api = &session().api();
|
|
|
|
using Flag = MTPstories_ActivateStealthMode::Flag;
|
|
|
|
api->request(MTPstories_ActivateStealthMode(
|
|
|
|
MTP_flags(Flag::f_past | Flag::f_future)
|
2023-08-04 14:49:58 +00:00
|
|
|
)).done([=](const MTPUpdates &result) {
|
|
|
|
api->applyUpdates(result);
|
2023-08-01 17:09:09 +00:00
|
|
|
if (done) done();
|
|
|
|
}).fail([=] {
|
|
|
|
if (done) done();
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
2023-08-08 08:55:12 +00:00
|
|
|
void Stories::sendReaction(FullStoryId id, Data::ReactionId reaction) {
|
|
|
|
if (const auto maybeStory = lookup(id)) {
|
|
|
|
const auto story = *maybeStory;
|
|
|
|
story->setReactionId(reaction);
|
|
|
|
|
|
|
|
const auto api = &session().api();
|
|
|
|
api->request(MTPstories_SendReaction(
|
|
|
|
MTP_flags(0),
|
2023-08-31 08:58:34 +00:00
|
|
|
story->peer()->input,
|
2023-08-08 08:55:12 +00:00
|
|
|
MTP_int(id.story),
|
|
|
|
ReactionToMTP(reaction)
|
|
|
|
)).send();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-05 16:31:15 +00:00
|
|
|
std::shared_ptr<HistoryItem> Stories::resolveItem(not_null<Story*> story) {
|
|
|
|
auto &items = _items[story->peer()->id];
|
|
|
|
auto i = items.find(story->id());
|
|
|
|
if (i == end(items)) {
|
|
|
|
i = items.emplace(story->id()).first;
|
|
|
|
} else if (const auto result = i->second.lock()) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
const auto history = _owner->history(story->peer());
|
|
|
|
auto result = std::shared_ptr<HistoryItem>(
|
2024-02-26 18:24:00 +00:00
|
|
|
history->makeMessage(StoryIdToMsgId(story->id()), story).get(),
|
2023-06-05 16:31:15 +00:00
|
|
|
HistoryItem::Destroyer());
|
|
|
|
i->second = result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<HistoryItem> Stories::resolveItem(FullStoryId id) {
|
|
|
|
const auto story = lookup(id);
|
|
|
|
return story ? resolveItem(*story) : std::shared_ptr<HistoryItem>();
|
|
|
|
}
|
|
|
|
|
|
|
|
const StoriesSource *Stories::source(PeerId id) const {
|
|
|
|
const auto i = _all.find(id);
|
|
|
|
return (i != end(_all)) ? &i->second : nullptr;
|
2023-05-03 18:30:37 +00:00
|
|
|
}
|
|
|
|
|
2023-06-02 14:26:39 +00:00
|
|
|
const std::vector<StoriesSourceInfo> &Stories::sources(
|
|
|
|
StorySourcesList list) const {
|
|
|
|
return _sources[static_cast<int>(list)];
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Stories::sourcesLoaded(StorySourcesList list) const {
|
|
|
|
return _sourcesLoaded[static_cast<int>(list)];
|
2023-05-03 18:30:37 +00:00
|
|
|
}
|
|
|
|
|
2023-06-02 14:26:39 +00:00
|
|
|
rpl::producer<> Stories::sourcesChanged(StorySourcesList list) const {
|
|
|
|
return _sourcesChanged[static_cast<int>(list)].events();
|
2023-05-22 15:59:16 +00:00
|
|
|
}
|
|
|
|
|
2023-06-05 16:31:15 +00:00
|
|
|
rpl::producer<PeerId> Stories::sourceChanged() const {
|
|
|
|
return _sourceChanged.events();
|
|
|
|
}
|
|
|
|
|
2023-05-29 12:03:23 +00:00
|
|
|
rpl::producer<PeerId> Stories::itemsChanged() const {
|
|
|
|
return _itemsChanged.events();
|
|
|
|
}
|
|
|
|
|
2023-05-26 07:21:19 +00:00
|
|
|
base::expected<not_null<Story*>, NoStory> Stories::lookup(
|
|
|
|
FullStoryId id) const {
|
|
|
|
const auto i = _stories.find(id.peer);
|
|
|
|
if (i != end(_stories)) {
|
|
|
|
const auto j = i->second.find(id.story);
|
|
|
|
if (j != end(i->second)) {
|
|
|
|
return j->second.get();
|
2023-05-03 18:30:37 +00:00
|
|
|
}
|
2023-05-26 07:21:19 +00:00
|
|
|
}
|
|
|
|
return base::make_unexpected(
|
|
|
|
_deleted.contains(id) ? NoStory::Deleted : NoStory::Unknown);
|
|
|
|
}
|
|
|
|
|
2023-07-05 15:52:22 +00:00
|
|
|
void Stories::resolve(FullStoryId id, Fn<void()> done, bool force) {
|
|
|
|
if (!force) {
|
|
|
|
const auto already = lookup(id);
|
|
|
|
if (already.has_value() || already.error() != NoStory::Unknown) {
|
|
|
|
if (done) {
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
return;
|
2023-05-29 12:03:23 +00:00
|
|
|
}
|
2023-05-26 09:27:34 +00:00
|
|
|
}
|
2023-05-29 12:03:23 +00:00
|
|
|
if (const auto i = _resolveSent.find(id.peer); i != end(_resolveSent)) {
|
|
|
|
if (const auto j = i->second.find(id.story); j != end(i->second)) {
|
|
|
|
if (done) {
|
|
|
|
j->second.push_back(std::move(done));
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auto &ids = _resolvePending[id.peer];
|
2023-05-26 09:27:34 +00:00
|
|
|
if (ids.empty()) {
|
|
|
|
crl::on_main(&session(), [=] {
|
|
|
|
sendResolveRequests();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
auto &callbacks = ids[id.story];
|
|
|
|
if (done) {
|
|
|
|
callbacks.push_back(std::move(done));
|
|
|
|
}
|
2023-05-03 18:30:37 +00:00
|
|
|
}
|
|
|
|
|
2023-06-05 12:10:34 +00:00
|
|
|
void Stories::loadAround(FullStoryId id, StoriesContext context) {
|
|
|
|
if (v::is<StoriesContextSingle>(context.data)) {
|
|
|
|
return;
|
|
|
|
} else if (v::is<StoriesContextSaved>(context.data)
|
|
|
|
|| v::is<StoriesContextArchive>(context.data)) {
|
|
|
|
return;
|
|
|
|
}
|
2023-06-02 14:26:39 +00:00
|
|
|
const auto i = _all.find(id.peer);
|
2023-05-29 12:03:23 +00:00
|
|
|
if (i == end(_all)) {
|
|
|
|
return;
|
|
|
|
}
|
2023-06-05 15:23:49 +00:00
|
|
|
const auto j = i->second.ids.lower_bound(StoryIdDates{ id.story });
|
2023-06-02 14:26:39 +00:00
|
|
|
if (j == end(i->second.ids) || j->id != id.story) {
|
2023-05-29 12:03:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto ignore = [&] {
|
|
|
|
const auto side = kIgnorePreloadAroundIfLoaded;
|
2023-06-02 14:26:39 +00:00
|
|
|
const auto left = ranges::min(int(j - begin(i->second.ids)), side);
|
|
|
|
const auto right = ranges::min(int(end(i->second.ids) - j), side);
|
2023-05-29 12:03:23 +00:00
|
|
|
for (auto k = j - left; k != j + right; ++k) {
|
2023-06-02 14:26:39 +00:00
|
|
|
const auto maybeStory = lookup({ id.peer, k->id });
|
2023-05-29 12:03:23 +00:00
|
|
|
if (!maybeStory && maybeStory.error() == NoStory::Unknown) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}();
|
|
|
|
if (ignore) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto side = kPreloadAroundCount;
|
2023-06-02 14:26:39 +00:00
|
|
|
const auto left = ranges::min(int(j - begin(i->second.ids)), side);
|
|
|
|
const auto right = ranges::min(int(end(i->second.ids) - j), side);
|
2023-05-29 12:03:23 +00:00
|
|
|
const auto from = j - left;
|
|
|
|
const auto till = j + right;
|
|
|
|
for (auto k = from; k != till; ++k) {
|
2023-06-02 14:26:39 +00:00
|
|
|
resolve({ id.peer, k->id }, nullptr);
|
2023-05-29 12:03:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-30 13:57:11 +00:00
|
|
|
void Stories::markAsRead(FullStoryId id, bool viewed) {
|
2023-06-09 14:49:09 +00:00
|
|
|
if (id.peer == _owner->session().userPeerId()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto maybeStory = lookup(id);
|
|
|
|
if (!maybeStory) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto story = *maybeStory;
|
2024-04-16 10:23:53 +00:00
|
|
|
if (story->expired() && story->inProfile()) {
|
2023-06-09 14:49:09 +00:00
|
|
|
_incrementViewsPending[id.peer].emplace(id.story);
|
|
|
|
if (!_incrementViewsTimer.isActive()) {
|
|
|
|
_incrementViewsTimer.callOnce(kIncrementViewsDelay);
|
|
|
|
}
|
|
|
|
}
|
2023-07-04 16:13:56 +00:00
|
|
|
if (!bumpReadTill(id.peer, id.story)) {
|
2023-05-29 15:09:36 +00:00
|
|
|
return;
|
2023-07-04 16:13:56 +00:00
|
|
|
}
|
|
|
|
if (!_markReadPending.contains(id.peer)) {
|
2023-05-29 15:09:36 +00:00
|
|
|
sendMarkAsReadRequests();
|
|
|
|
}
|
|
|
|
_markReadPending.emplace(id.peer);
|
2023-07-04 16:13:56 +00:00
|
|
|
_markReadTimer.callOnce(kMarkAsReadDelay);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Stories::bumpReadTill(PeerId peerId, StoryId maxReadTill) {
|
|
|
|
auto &till = _readTill[peerId];
|
2023-07-05 07:55:16 +00:00
|
|
|
auto refreshItems = std::vector<StoryId>();
|
|
|
|
const auto guard = gsl::finally([&] {
|
|
|
|
for (const auto id : refreshItems) {
|
|
|
|
_owner->refreshStoryItemViews({ peerId, id });
|
|
|
|
}
|
|
|
|
});
|
2023-07-04 16:13:56 +00:00
|
|
|
if (till < maxReadTill) {
|
2023-07-05 07:55:16 +00:00
|
|
|
const auto from = till;
|
2023-07-04 16:13:56 +00:00
|
|
|
till = maxReadTill;
|
2023-08-31 08:58:34 +00:00
|
|
|
updatePeerStoriesState(_owner->peer(peerId));
|
2023-07-05 07:55:16 +00:00
|
|
|
const auto i = _stories.find(peerId);
|
|
|
|
if (i != end(_stories)) {
|
|
|
|
refreshItems = ranges::make_subrange(
|
|
|
|
i->second.lower_bound(from + 1),
|
|
|
|
i->second.lower_bound(till + 1)
|
2023-07-14 07:24:13 +00:00
|
|
|
) | ranges::views::transform([=](const auto &pair) {
|
|
|
|
_owner->session().changes().storyUpdated(
|
|
|
|
pair.second.get(),
|
|
|
|
StoryUpdate::Flag::MarkRead);
|
2023-07-05 07:55:16 +00:00
|
|
|
return pair.first;
|
|
|
|
}) | ranges::to_vector;
|
|
|
|
}
|
2023-07-04 16:13:56 +00:00
|
|
|
}
|
|
|
|
const auto i = _all.find(peerId);
|
|
|
|
if (i == end(_all) || i->second.readTill >= maxReadTill) {
|
|
|
|
return false;
|
|
|
|
}
|
2023-07-03 20:05:11 +00:00
|
|
|
const auto wasUnreadCount = i->second.unreadCount();
|
2023-07-04 16:13:56 +00:00
|
|
|
i->second.readTill = maxReadTill;
|
2023-07-03 20:05:11 +00:00
|
|
|
const auto nowUnreadCount = i->second.unreadCount();
|
|
|
|
if (wasUnreadCount != nowUnreadCount) {
|
2023-06-02 14:26:39 +00:00
|
|
|
const auto refreshInList = [&](StorySourcesList list) {
|
|
|
|
auto &sources = _sources[static_cast<int>(list)];
|
|
|
|
const auto i = ranges::find(
|
|
|
|
sources,
|
2023-07-04 16:13:56 +00:00
|
|
|
peerId,
|
2023-06-02 14:26:39 +00:00
|
|
|
&StoriesSourceInfo::id);
|
|
|
|
if (i != end(sources)) {
|
2023-07-03 20:05:11 +00:00
|
|
|
i->unreadCount = nowUnreadCount;
|
2023-06-02 14:26:39 +00:00
|
|
|
sort(list);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
refreshInList(StorySourcesList::NotHidden);
|
2023-06-21 06:12:28 +00:00
|
|
|
refreshInList(StorySourcesList::Hidden);
|
2023-06-02 14:26:39 +00:00
|
|
|
}
|
2023-07-04 16:13:56 +00:00
|
|
|
return true;
|
2023-05-29 15:09:36 +00:00
|
|
|
}
|
|
|
|
|
2023-06-15 16:28:53 +00:00
|
|
|
void Stories::toggleHidden(
|
|
|
|
PeerId peerId,
|
|
|
|
bool hidden,
|
|
|
|
std::shared_ptr<Ui::Show> show) {
|
2023-08-31 08:58:34 +00:00
|
|
|
const auto peer = _owner->peer(peerId);
|
2023-09-25 13:39:18 +00:00
|
|
|
const auto justRemove = peer->isServiceUser() && hidden;
|
2023-08-31 08:58:34 +00:00
|
|
|
if (peer->hasStoriesHidden() != hidden) {
|
2023-09-25 13:39:18 +00:00
|
|
|
if (!justRemove) {
|
|
|
|
peer->setStoriesHidden(hidden);
|
|
|
|
}
|
2023-08-31 08:58:34 +00:00
|
|
|
session().api().request(MTPstories_TogglePeerStoriesHidden(
|
|
|
|
peer->input,
|
2023-06-02 17:14:15 +00:00
|
|
|
MTP_bool(hidden)
|
|
|
|
)).send();
|
|
|
|
}
|
|
|
|
|
2023-08-31 08:58:34 +00:00
|
|
|
const auto name = peer->shortName();
|
2023-06-15 16:28:53 +00:00
|
|
|
const auto guard = gsl::finally([&] {
|
2023-09-28 19:28:45 +00:00
|
|
|
if (show && !justRemove) {
|
2023-06-23 16:39:57 +00:00
|
|
|
const auto phrase = hidden
|
|
|
|
? tr::lng_stories_hidden_to_contacts
|
|
|
|
: tr::lng_stories_shown_in_chats;
|
|
|
|
show->showToast(phrase(
|
|
|
|
tr::now,
|
|
|
|
lt_user,
|
|
|
|
Ui::Text::Bold(name),
|
2023-06-30 08:48:42 +00:00
|
|
|
Ui::Text::RichLangValue));
|
2023-06-15 16:28:53 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-09-25 13:39:18 +00:00
|
|
|
if (justRemove) {
|
|
|
|
apply(peer, nullptr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-06-02 17:14:15 +00:00
|
|
|
const auto i = _all.find(peerId);
|
|
|
|
if (i == end(_all)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
i->second.hidden = hidden;
|
2023-06-21 06:12:28 +00:00
|
|
|
const auto info = i->second.info();
|
2023-06-02 17:14:15 +00:00
|
|
|
const auto main = static_cast<int>(StorySourcesList::NotHidden);
|
2023-06-21 06:12:28 +00:00
|
|
|
const auto other = static_cast<int>(StorySourcesList::Hidden);
|
|
|
|
const auto proj = &StoriesSourceInfo::id;
|
2023-06-02 17:14:15 +00:00
|
|
|
if (hidden) {
|
2023-06-21 06:12:28 +00:00
|
|
|
const auto i = ranges::find(_sources[main], peerId, proj);
|
2023-06-02 17:14:15 +00:00
|
|
|
if (i != end(_sources[main])) {
|
|
|
|
_sources[main].erase(i);
|
2023-07-18 08:15:19 +00:00
|
|
|
notifySourcesChanged(StorySourcesList::NotHidden);
|
2023-06-26 16:22:56 +00:00
|
|
|
preloadSourcesChanged(StorySourcesList::NotHidden);
|
2023-06-02 17:14:15 +00:00
|
|
|
}
|
2023-06-21 06:12:28 +00:00
|
|
|
const auto j = ranges::find(_sources[other], peerId, proj);
|
|
|
|
if (j == end(_sources[other])) {
|
|
|
|
_sources[other].push_back(info);
|
|
|
|
} else {
|
|
|
|
*j = info;
|
2023-06-02 17:14:15 +00:00
|
|
|
}
|
2023-06-21 06:12:28 +00:00
|
|
|
sort(StorySourcesList::Hidden);
|
2023-06-02 17:14:15 +00:00
|
|
|
} else {
|
2023-06-21 06:12:28 +00:00
|
|
|
const auto i = ranges::find(_sources[other], peerId, proj);
|
|
|
|
if (i != end(_sources[other])) {
|
|
|
|
_sources[other].erase(i);
|
2023-07-18 08:15:19 +00:00
|
|
|
notifySourcesChanged(StorySourcesList::Hidden);
|
2023-06-26 16:22:56 +00:00
|
|
|
preloadSourcesChanged(StorySourcesList::Hidden);
|
2023-06-21 06:12:28 +00:00
|
|
|
}
|
|
|
|
const auto j = ranges::find(_sources[main], peerId, proj);
|
|
|
|
if (j == end(_sources[main])) {
|
|
|
|
_sources[main].push_back(info);
|
|
|
|
} else {
|
|
|
|
*j = info;
|
2023-06-02 17:14:15 +00:00
|
|
|
}
|
2023-06-21 06:12:28 +00:00
|
|
|
sort(StorySourcesList::NotHidden);
|
2023-06-02 17:14:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-29 15:09:36 +00:00
|
|
|
void Stories::sendMarkAsReadRequest(
|
|
|
|
not_null<PeerData*> peer,
|
|
|
|
StoryId tillId) {
|
|
|
|
const auto peerId = peer->id;
|
|
|
|
_markReadRequests.emplace(peerId);
|
|
|
|
const auto finish = [=] {
|
|
|
|
_markReadRequests.remove(peerId);
|
|
|
|
if (!_markReadTimer.isActive()
|
|
|
|
&& _markReadPending.contains(peerId)) {
|
|
|
|
sendMarkAsReadRequests();
|
|
|
|
}
|
2023-06-09 14:49:09 +00:00
|
|
|
checkQuitPreventFinished();
|
2023-05-29 15:09:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const auto api = &_owner->session().api();
|
|
|
|
api->request(MTPstories_ReadStories(
|
2023-08-31 08:58:34 +00:00
|
|
|
peer->input,
|
2023-05-29 15:09:36 +00:00
|
|
|
MTP_int(tillId)
|
|
|
|
)).done(finish).fail(finish).send();
|
|
|
|
}
|
|
|
|
|
2023-06-09 14:49:09 +00:00
|
|
|
void Stories::checkQuitPreventFinished() {
|
|
|
|
if (_markReadRequests.empty() && _incrementViewsRequests.empty()) {
|
|
|
|
if (Core::Quitting()) {
|
|
|
|
LOG(("Stories doesn't prevent quit any more."));
|
|
|
|
}
|
|
|
|
Core::App().quitPreventFinished();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-29 15:09:36 +00:00
|
|
|
void Stories::sendMarkAsReadRequests() {
|
|
|
|
_markReadTimer.cancel();
|
|
|
|
for (auto i = begin(_markReadPending); i != end(_markReadPending);) {
|
|
|
|
const auto peerId = *i;
|
|
|
|
if (_markReadRequests.contains(peerId)) {
|
|
|
|
++i;
|
|
|
|
continue;
|
|
|
|
}
|
2023-06-02 14:26:39 +00:00
|
|
|
const auto j = _all.find(peerId);
|
2023-05-29 15:09:36 +00:00
|
|
|
if (j != end(_all)) {
|
2023-08-31 08:58:34 +00:00
|
|
|
sendMarkAsReadRequest(j->second.peer, j->second.readTill);
|
2023-05-29 15:09:36 +00:00
|
|
|
}
|
|
|
|
i = _markReadPending.erase(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-09 14:49:09 +00:00
|
|
|
void Stories::sendIncrementViewsRequests() {
|
|
|
|
if (_incrementViewsPending.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
struct Prepared {
|
|
|
|
PeerId peer = 0;
|
|
|
|
QVector<MTPint> ids;
|
|
|
|
};
|
|
|
|
auto prepared = std::vector<Prepared>();
|
|
|
|
for (const auto &[peer, ids] : _incrementViewsPending) {
|
|
|
|
if (_incrementViewsRequests.contains(peer)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
prepared.push_back({ .peer = peer });
|
|
|
|
for (const auto &id : ids) {
|
|
|
|
prepared.back().ids.push_back(MTP_int(id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto api = &_owner->session().api();
|
|
|
|
for (auto &[peer, ids] : prepared) {
|
|
|
|
_incrementViewsRequests.emplace(peer);
|
2023-06-09 17:25:26 +00:00
|
|
|
const auto finish = [=, peer = peer] {
|
2023-06-09 14:49:09 +00:00
|
|
|
_incrementViewsRequests.remove(peer);
|
|
|
|
if (!_incrementViewsTimer.isActive()
|
|
|
|
&& _incrementViewsPending.contains(peer)) {
|
|
|
|
sendIncrementViewsRequests();
|
|
|
|
}
|
|
|
|
checkQuitPreventFinished();
|
|
|
|
};
|
|
|
|
api->request(MTPstories_IncrementStoryViews(
|
2023-08-31 08:58:34 +00:00
|
|
|
_owner->peer(peer)->input,
|
2023-06-09 14:49:09 +00:00
|
|
|
MTP_vector<MTPint>(std::move(ids))
|
|
|
|
)).done(finish).fail(finish).send();
|
|
|
|
_incrementViewsPending.remove(peer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-01 16:01:29 +00:00
|
|
|
void Stories::loadViewsSlice(
|
2023-09-15 16:41:36 +00:00
|
|
|
not_null<PeerData*> peer,
|
2023-06-01 16:01:29 +00:00
|
|
|
StoryId id,
|
2023-08-04 14:49:58 +00:00
|
|
|
QString offset,
|
|
|
|
Fn<void(StoryViews)> done) {
|
2023-09-15 16:41:36 +00:00
|
|
|
Expects(peer->isSelf() || !done);
|
|
|
|
|
|
|
|
if (_viewsStoryPeer == peer
|
|
|
|
&& _viewsStoryId == id
|
2023-07-05 17:02:57 +00:00
|
|
|
&& _viewsOffset == offset
|
2023-08-04 14:49:58 +00:00
|
|
|
&& (!offset.isEmpty() || _viewsRequestId)) {
|
2023-07-05 17:02:57 +00:00
|
|
|
if (_viewsRequestId) {
|
|
|
|
_viewsDone = std::move(done);
|
|
|
|
}
|
2023-06-01 16:01:29 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-09-15 16:41:36 +00:00
|
|
|
_viewsStoryPeer = peer;
|
2023-06-01 16:01:29 +00:00
|
|
|
_viewsStoryId = id;
|
|
|
|
_viewsOffset = offset;
|
2023-07-05 17:02:57 +00:00
|
|
|
_viewsDone = std::move(done);
|
2023-06-01 16:01:29 +00:00
|
|
|
|
2023-09-15 16:41:36 +00:00
|
|
|
if (peer->isSelf()) {
|
|
|
|
sendViewsSliceRequest();
|
|
|
|
} else {
|
|
|
|
sendViewsCountsRequest();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-15 00:01:44 +00:00
|
|
|
void Stories::loadReactionsSlice(
|
|
|
|
not_null<PeerData*> peer,
|
|
|
|
StoryId id,
|
|
|
|
QString offset,
|
|
|
|
Fn<void(StoryViews)> done) {
|
|
|
|
Expects(peer->isChannel());
|
|
|
|
|
|
|
|
if (_reactionsStoryPeer == peer
|
|
|
|
&& _reactionsStoryId == id
|
|
|
|
&& _reactionsOffset == offset) {
|
|
|
|
if (_reactionsRequestId) {
|
|
|
|
_reactionsDone = std::move(done);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_reactionsStoryPeer = peer;
|
|
|
|
_reactionsStoryId = id;
|
|
|
|
_reactionsOffset = offset;
|
|
|
|
_reactionsDone = std::move(done);
|
|
|
|
|
|
|
|
using Flag = MTPstories_GetStoryReactionsList::Flag;
|
|
|
|
const auto api = &_owner->session().api();
|
|
|
|
_owner->session().api().request(_reactionsRequestId).cancel();
|
|
|
|
_reactionsRequestId = api->request(MTPstories_GetStoryReactionsList(
|
|
|
|
MTP_flags(offset.isEmpty() ? Flag() : Flag::f_offset),
|
|
|
|
_reactionsStoryPeer->input,
|
|
|
|
MTP_int(_reactionsStoryId),
|
|
|
|
MTPReaction(),
|
|
|
|
MTP_string(_reactionsOffset),
|
|
|
|
MTP_int(kViewsPerPage)
|
|
|
|
)).done([=](const MTPstories_StoryReactionsList &result) {
|
|
|
|
_reactionsRequestId = 0;
|
|
|
|
|
|
|
|
const auto &data = result.data();
|
|
|
|
auto slice = StoryViews{
|
|
|
|
.nextOffset = data.vnext_offset().value_or_empty(),
|
|
|
|
.reactions = data.vcount().v,
|
|
|
|
.total = data.vcount().v,
|
|
|
|
};
|
|
|
|
_owner->processUsers(data.vusers());
|
|
|
|
_owner->processChats(data.vchats());
|
|
|
|
slice.list.reserve(data.vreactions().v.size());
|
|
|
|
for (const auto &reaction : data.vreactions().v) {
|
|
|
|
reaction.match([&](const MTPDstoryReaction &data) {
|
|
|
|
slice.list.push_back({
|
|
|
|
.peer = _owner->peer(peerFromMTP(data.vpeer_id())),
|
|
|
|
.reaction = ReactionFromMTP(data.vreaction()),
|
|
|
|
.date = data.vdate().v,
|
|
|
|
});
|
|
|
|
}, [&](const MTPDstoryReactionPublicRepost &data) {
|
|
|
|
const auto story = applySingle(
|
|
|
|
peerFromMTP(data.vpeer_id()),
|
|
|
|
data.vstory());
|
|
|
|
if (story) {
|
|
|
|
slice.list.push_back({
|
|
|
|
.peer = story->peer(),
|
|
|
|
.repostId = story->id(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, [&](const MTPDstoryReactionPublicForward &data) {
|
|
|
|
const auto item = _owner->addNewMessage(
|
|
|
|
data.vmessage(),
|
|
|
|
{},
|
|
|
|
NewMessageType::Existing);
|
|
|
|
if (item) {
|
|
|
|
slice.list.push_back({
|
|
|
|
.peer = item->history()->peer,
|
|
|
|
.forwardId = item->id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
const auto fullId = FullStoryId{
|
|
|
|
.peer = _reactionsStoryPeer->id,
|
|
|
|
.story = _reactionsStoryId,
|
|
|
|
};
|
|
|
|
if (const auto story = lookup(fullId)) {
|
|
|
|
(*story)->applyChannelReactionsSlice(_reactionsOffset, slice);
|
|
|
|
}
|
|
|
|
if (const auto done = base::take(_reactionsDone)) {
|
|
|
|
done(std::move(slice));
|
|
|
|
}
|
|
|
|
}).fail([=] {
|
|
|
|
_reactionsRequestId = 0;
|
|
|
|
if (const auto done = base::take(_reactionsDone)) {
|
|
|
|
done({});
|
|
|
|
}
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
2023-09-15 16:41:36 +00:00
|
|
|
void Stories::sendViewsSliceRequest() {
|
|
|
|
Expects(_viewsStoryPeer != nullptr);
|
|
|
|
Expects(_viewsStoryPeer->isSelf());
|
|
|
|
|
2023-08-09 08:35:11 +00:00
|
|
|
using Flag = MTPstories_GetStoryViewsList::Flag;
|
2023-09-15 16:41:36 +00:00
|
|
|
const auto api = &_owner->session().api();
|
|
|
|
_owner->session().api().request(_viewsRequestId).cancel();
|
2023-06-01 16:01:29 +00:00
|
|
|
_viewsRequestId = api->request(MTPstories_GetStoryViewsList(
|
2023-08-09 08:35:11 +00:00
|
|
|
MTP_flags(Flag::f_reactions_first),
|
2023-09-15 16:41:36 +00:00
|
|
|
_viewsStoryPeer->input,
|
2023-08-04 14:49:58 +00:00
|
|
|
MTPstring(), // q
|
2023-09-15 16:41:36 +00:00
|
|
|
MTP_int(_viewsStoryId),
|
|
|
|
MTP_string(_viewsOffset),
|
|
|
|
MTP_int(_viewsDone ? kViewsPerPage : kPollingViewsPerPage)
|
2023-06-01 16:01:29 +00:00
|
|
|
)).done([=](const MTPstories_StoryViewsList &result) {
|
|
|
|
_viewsRequestId = 0;
|
|
|
|
|
|
|
|
const auto &data = result.data();
|
2023-08-04 14:49:58 +00:00
|
|
|
auto slice = StoryViews{
|
|
|
|
.nextOffset = data.vnext_offset().value_or_empty(),
|
2023-08-04 15:30:49 +00:00
|
|
|
.reactions = data.vreactions_count().v,
|
2023-12-14 22:59:50 +00:00
|
|
|
.forwards = data.vforwards_count().v,
|
|
|
|
.views = data.vviews_count().v,
|
2023-08-04 14:49:58 +00:00
|
|
|
.total = data.vcount().v,
|
|
|
|
};
|
2023-06-01 16:01:29 +00:00
|
|
|
_owner->processUsers(data.vusers());
|
2023-12-15 00:01:44 +00:00
|
|
|
_owner->processChats(data.vchats());
|
2023-08-04 14:49:58 +00:00
|
|
|
slice.list.reserve(data.vviews().v.size());
|
2023-06-01 16:01:29 +00:00
|
|
|
for (const auto &view : data.vviews().v) {
|
2023-12-13 21:30:17 +00:00
|
|
|
view.match([&](const MTPDstoryView &data) {
|
|
|
|
slice.list.push_back({
|
|
|
|
.peer = _owner->peer(peerFromUser(data.vuser_id())),
|
|
|
|
.reaction = (data.vreaction()
|
|
|
|
? ReactionFromMTP(*data.vreaction())
|
|
|
|
: Data::ReactionId()),
|
|
|
|
.date = data.vdate().v,
|
|
|
|
});
|
2023-12-14 22:59:50 +00:00
|
|
|
}, [&](const MTPDstoryViewPublicRepost &data) {
|
|
|
|
const auto story = applySingle(
|
|
|
|
peerFromMTP(data.vpeer_id()),
|
|
|
|
data.vstory());
|
|
|
|
if (story) {
|
|
|
|
slice.list.push_back({
|
|
|
|
.peer = story->peer(),
|
|
|
|
.repostId = story->id(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, [&](const MTPDstoryViewPublicForward &data) {
|
|
|
|
const auto item = _owner->addNewMessage(
|
|
|
|
data.vmessage(),
|
|
|
|
{},
|
|
|
|
NewMessageType::Existing);
|
|
|
|
if (item) {
|
|
|
|
slice.list.push_back({
|
|
|
|
.peer = item->history()->peer,
|
|
|
|
.forwardId = item->id,
|
|
|
|
});
|
|
|
|
}
|
2023-06-01 16:01:29 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
const auto fullId = FullStoryId{
|
|
|
|
.peer = _owner->session().userPeerId(),
|
|
|
|
.story = _viewsStoryId,
|
|
|
|
};
|
|
|
|
if (const auto story = lookup(fullId)) {
|
2023-08-04 14:49:58 +00:00
|
|
|
(*story)->applyViewsSlice(_viewsOffset, slice);
|
2023-06-01 16:01:29 +00:00
|
|
|
}
|
|
|
|
if (const auto done = base::take(_viewsDone)) {
|
|
|
|
done(std::move(slice));
|
|
|
|
}
|
|
|
|
}).fail([=] {
|
|
|
|
_viewsRequestId = 0;
|
|
|
|
if (const auto done = base::take(_viewsDone)) {
|
|
|
|
done({});
|
|
|
|
}
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
2023-09-15 16:41:36 +00:00
|
|
|
void Stories::sendViewsCountsRequest() {
|
|
|
|
Expects(_viewsStoryPeer != nullptr);
|
|
|
|
Expects(!_viewsDone);
|
|
|
|
|
|
|
|
const auto api = &_owner->session().api();
|
|
|
|
_owner->session().api().request(_viewsRequestId).cancel();
|
|
|
|
_viewsRequestId = api->request(MTPstories_GetStoriesViews(
|
|
|
|
_viewsStoryPeer->input,
|
|
|
|
MTP_vector<MTPint>(1, MTP_int(_viewsStoryId))
|
|
|
|
)).done([=](const MTPstories_StoryViews &result) {
|
|
|
|
_viewsRequestId = 0;
|
|
|
|
|
|
|
|
const auto &data = result.data();
|
|
|
|
_owner->processUsers(data.vusers());
|
|
|
|
if (data.vviews().v.size() == 1) {
|
|
|
|
const auto fullId = FullStoryId{
|
|
|
|
_viewsStoryPeer->id,
|
|
|
|
_viewsStoryId,
|
|
|
|
};
|
|
|
|
if (const auto story = lookup(fullId)) {
|
|
|
|
(*story)->applyViewsCounts(data.vviews().v.front().data());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).fail([=] {
|
|
|
|
_viewsRequestId = 0;
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
2023-09-05 07:07:32 +00:00
|
|
|
bool Stories::hasArchive(not_null<PeerData*> peer) const {
|
|
|
|
if (peer->isSelf()) {
|
|
|
|
return true;
|
|
|
|
} else if (const auto channel = peer->asChannel()) {
|
|
|
|
return channel->canEditStories();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const StoriesIds &Stories::archive(PeerId peerId) const {
|
|
|
|
static const auto empty = StoriesIds();
|
|
|
|
const auto i = _archive.find(peerId);
|
|
|
|
return (i != end(_archive)) ? i->second.ids : empty;
|
2023-06-05 15:23:49 +00:00
|
|
|
}
|
|
|
|
|
2023-09-05 07:07:32 +00:00
|
|
|
rpl::producer<PeerId> Stories::archiveChanged() const {
|
2023-06-07 15:56:38 +00:00
|
|
|
return _archiveChanged.events();
|
2023-06-05 15:23:49 +00:00
|
|
|
}
|
|
|
|
|
2023-09-05 07:07:32 +00:00
|
|
|
int Stories::archiveCount(PeerId peerId) const {
|
|
|
|
const auto i = _archive.find(peerId);
|
|
|
|
return (i != end(_archive)) ? i->second.total : 0;
|
2023-06-05 15:23:49 +00:00
|
|
|
}
|
|
|
|
|
2023-09-05 07:07:32 +00:00
|
|
|
bool Stories::archiveCountKnown(PeerId peerId) const {
|
|
|
|
const auto i = _archive.find(peerId);
|
|
|
|
return (i != end(_archive)) && (i->second.total >= 0);
|
2023-06-05 15:23:49 +00:00
|
|
|
}
|
|
|
|
|
2023-09-05 07:07:32 +00:00
|
|
|
bool Stories::archiveLoaded(PeerId peerId) const {
|
|
|
|
const auto i = _archive.find(peerId);
|
|
|
|
return (i != end(_archive)) && i->second.loaded;
|
2023-06-05 15:23:49 +00:00
|
|
|
}
|
|
|
|
|
2023-09-05 07:07:32 +00:00
|
|
|
const StoriesIds &Stories::saved(PeerId peerId) const {
|
|
|
|
static const auto empty = StoriesIds();
|
2023-06-05 16:31:15 +00:00
|
|
|
const auto i = _saved.find(peerId);
|
2023-09-05 07:07:32 +00:00
|
|
|
return (i != end(_saved)) ? i->second.ids : empty;
|
2023-06-05 16:31:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<PeerId> Stories::savedChanged() const {
|
|
|
|
return _savedChanged.events();
|
|
|
|
}
|
|
|
|
|
|
|
|
int Stories::savedCount(PeerId peerId) const {
|
|
|
|
const auto i = _saved.find(peerId);
|
|
|
|
return (i != end(_saved)) ? i->second.total : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Stories::savedCountKnown(PeerId peerId) const {
|
|
|
|
const auto i = _saved.find(peerId);
|
|
|
|
return (i != end(_saved)) && (i->second.total >= 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Stories::savedLoaded(PeerId peerId) const {
|
|
|
|
const auto i = _saved.find(peerId);
|
|
|
|
return (i != end(_saved)) && i->second.loaded;
|
|
|
|
}
|
|
|
|
|
2023-09-05 07:07:32 +00:00
|
|
|
void Stories::archiveLoadMore(PeerId peerId) {
|
|
|
|
const auto peer = _owner->peer(peerId);
|
|
|
|
const auto archive = lookupArchive(peer);
|
|
|
|
if (!archive || archive->requestId || archive->loaded) {
|
2023-06-05 15:23:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto api = &_owner->session().api();
|
2023-09-05 07:07:32 +00:00
|
|
|
archive->requestId = api->request(MTPstories_GetStoriesArchive(
|
|
|
|
peer->input,
|
|
|
|
MTP_int(archive->lastId),
|
|
|
|
MTP_int(archive->lastId ? kArchivePerPage : kArchiveFirstPerPage)
|
2023-06-05 15:23:49 +00:00
|
|
|
)).done([=](const MTPstories_Stories &result) {
|
2023-09-05 07:07:32 +00:00
|
|
|
const auto archive = lookupArchive(peer);
|
|
|
|
if (!archive) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
archive->requestId = 0;
|
2023-06-05 15:23:49 +00:00
|
|
|
|
|
|
|
const auto &data = result.data();
|
|
|
|
const auto now = base::unixtime::now();
|
2023-09-05 07:07:32 +00:00
|
|
|
archive->total = data.vcount().v;
|
2023-06-05 15:23:49 +00:00
|
|
|
for (const auto &story : data.vstories().v) {
|
|
|
|
const auto id = story.match([&](const auto &id) {
|
|
|
|
return id.vid().v;
|
|
|
|
});
|
2023-09-05 07:07:32 +00:00
|
|
|
archive->ids.list.emplace(id);
|
|
|
|
archive->lastId = id;
|
|
|
|
if (!parseAndApply(peer, story, now)) {
|
|
|
|
archive->ids.list.remove(id);
|
|
|
|
if (archive->total > 0) {
|
|
|
|
--archive->total;
|
2023-06-05 15:23:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-05 07:07:32 +00:00
|
|
|
const auto ids = int(archive->ids.list.size());
|
|
|
|
archive->loaded = data.vstories().v.empty();
|
|
|
|
archive->total = archive->loaded ? ids : std::max(archive->total, ids);
|
|
|
|
_archiveChanged.fire_copy(peerId);
|
2023-06-05 15:23:49 +00:00
|
|
|
}).fail([=] {
|
2023-09-05 07:07:32 +00:00
|
|
|
const auto archive = lookupArchive(peer);
|
|
|
|
if (!archive) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
archive->requestId = 0;
|
|
|
|
archive->loaded = true;
|
|
|
|
archive->total = int(archive->ids.list.size());
|
|
|
|
_archiveChanged.fire_copy(peerId);
|
2023-06-05 16:31:15 +00:00
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stories::savedLoadMore(PeerId peerId) {
|
|
|
|
auto &saved = _saved[peerId];
|
2023-06-07 15:56:38 +00:00
|
|
|
if (saved.requestId || saved.loaded) {
|
2023-06-05 16:31:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto api = &_owner->session().api();
|
|
|
|
const auto peer = _owner->peer(peerId);
|
|
|
|
saved.requestId = api->request(MTPstories_GetPinnedStories(
|
2023-08-31 08:58:34 +00:00
|
|
|
peer->input,
|
2023-06-05 16:31:15 +00:00
|
|
|
MTP_int(saved.lastId),
|
|
|
|
MTP_int(saved.lastId ? kSavedPerPage : kSavedFirstPerPage)
|
|
|
|
)).done([=](const MTPstories_Stories &result) {
|
|
|
|
auto &saved = _saved[peerId];
|
|
|
|
saved.requestId = 0;
|
|
|
|
|
|
|
|
const auto &data = result.data();
|
|
|
|
const auto now = base::unixtime::now();
|
2024-04-16 15:32:33 +00:00
|
|
|
auto pinnedToTopIds = data.vpinned_to_top().value_or_empty();
|
|
|
|
auto pinnedToTop = pinnedToTopIds
|
|
|
|
| ranges::views::transform(&MTPint::v)
|
|
|
|
| ranges::to_vector;
|
2023-06-05 16:31:15 +00:00
|
|
|
saved.total = data.vcount().v;
|
|
|
|
for (const auto &story : data.vstories().v) {
|
|
|
|
const auto id = story.match([&](const auto &id) {
|
|
|
|
return id.vid().v;
|
|
|
|
});
|
|
|
|
saved.ids.list.emplace(id);
|
|
|
|
saved.lastId = id;
|
|
|
|
if (!parseAndApply(peer, story, now)) {
|
|
|
|
saved.ids.list.remove(id);
|
|
|
|
if (saved.total > 0) {
|
|
|
|
--saved.total;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-06-07 15:56:38 +00:00
|
|
|
const auto ids = int(saved.ids.list.size());
|
2023-06-05 16:31:15 +00:00
|
|
|
saved.loaded = data.vstories().v.empty();
|
2023-06-07 15:56:38 +00:00
|
|
|
saved.total = saved.loaded ? ids : std::max(saved.total, ids);
|
2024-04-16 15:32:33 +00:00
|
|
|
setPinnedToTop(peerId, std::move(pinnedToTop));
|
2023-06-05 16:31:15 +00:00
|
|
|
_savedChanged.fire_copy(peerId);
|
|
|
|
}).fail([=] {
|
|
|
|
auto &saved = _saved[peerId];
|
|
|
|
saved.requestId = 0;
|
|
|
|
saved.loaded = true;
|
|
|
|
saved.total = int(saved.ids.list.size());
|
|
|
|
_savedChanged.fire_copy(peerId);
|
2023-06-05 15:23:49 +00:00
|
|
|
}).send();
|
2023-06-22 15:29:11 +00:00
|
|
|
}
|
2023-06-05 16:31:15 +00:00
|
|
|
|
2024-04-16 15:32:33 +00:00
|
|
|
void Stories::setPinnedToTop(
|
|
|
|
PeerId peerId,
|
|
|
|
std::vector<StoryId> &&pinnedToTop) {
|
|
|
|
const auto i = _saved.find(peerId);
|
|
|
|
if (i == end(_saved) && pinnedToTop.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto &saved = (i == end(_saved)) ? _saved[peerId] : i->second;
|
|
|
|
if (saved.ids.pinnedToTop != pinnedToTop) {
|
|
|
|
for (const auto id : saved.ids.pinnedToTop) {
|
|
|
|
if (!ranges::contains(pinnedToTop, id)) {
|
|
|
|
if (const auto maybeStory = lookup({ peerId, id })) {
|
|
|
|
(*maybeStory)->setPinnedToTop(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const auto id : pinnedToTop) {
|
|
|
|
if (!ranges::contains(saved.ids.pinnedToTop, id)) {
|
|
|
|
if (const auto maybeStory = lookup({ peerId, id })) {
|
|
|
|
(*maybeStory)->setPinnedToTop(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
saved.ids.pinnedToTop = std::move(pinnedToTop);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-22 15:29:11 +00:00
|
|
|
void Stories::deleteList(const std::vector<FullStoryId> &ids) {
|
2023-09-05 14:17:19 +00:00
|
|
|
if (ids.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto peer = session().data().peer(ids.front().peer);
|
2023-06-22 15:29:11 +00:00
|
|
|
auto list = QVector<MTPint>();
|
|
|
|
list.reserve(ids.size());
|
|
|
|
for (const auto &id : ids) {
|
2023-09-05 14:17:19 +00:00
|
|
|
if (id.peer == peer->id) {
|
2023-06-22 15:29:11 +00:00
|
|
|
list.push_back(MTP_int(id.story));
|
|
|
|
}
|
|
|
|
}
|
2023-09-05 14:17:19 +00:00
|
|
|
const auto api = &_owner->session().api();
|
|
|
|
api->request(MTPstories_DeleteStories(
|
|
|
|
peer->input,
|
|
|
|
MTP_vector<MTPint>(list)
|
|
|
|
)).done([=](const MTPVector<MTPint> &result) {
|
|
|
|
for (const auto &id : result.v) {
|
|
|
|
applyDeleted(peer, id.v);
|
|
|
|
}
|
|
|
|
}).send();
|
2023-06-22 15:29:11 +00:00
|
|
|
}
|
|
|
|
|
2024-04-16 10:23:53 +00:00
|
|
|
void Stories::toggleInProfileList(
|
2023-06-30 11:24:59 +00:00
|
|
|
const std::vector<FullStoryId> &ids,
|
2024-04-16 10:23:53 +00:00
|
|
|
bool inProfile) {
|
2023-09-05 14:17:19 +00:00
|
|
|
if (ids.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto peer = session().data().peer(ids.front().peer);
|
2023-06-30 11:24:59 +00:00
|
|
|
auto list = QVector<MTPint>();
|
|
|
|
list.reserve(ids.size());
|
|
|
|
for (const auto &id : ids) {
|
2023-09-05 14:17:19 +00:00
|
|
|
if (id.peer == peer->id) {
|
2023-06-30 11:24:59 +00:00
|
|
|
list.push_back(MTP_int(id.story));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (list.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto api = &_owner->session().api();
|
|
|
|
api->request(MTPstories_TogglePinned(
|
2023-09-05 14:17:19 +00:00
|
|
|
peer->input,
|
2023-06-30 11:24:59 +00:00
|
|
|
MTP_vector<MTPint>(list),
|
2024-04-16 10:23:53 +00:00
|
|
|
MTP_bool(inProfile)
|
2023-06-30 11:24:59 +00:00
|
|
|
)).done([=](const MTPVector<MTPint> &result) {
|
2023-09-05 14:17:19 +00:00
|
|
|
const auto peerId = peer->id;
|
|
|
|
auto &saved = _saved[peerId];
|
2023-06-30 11:24:59 +00:00
|
|
|
const auto loaded = saved.loaded;
|
|
|
|
const auto lastId = !saved.ids.list.empty()
|
|
|
|
? saved.ids.list.back()
|
2023-06-30 12:45:41 +00:00
|
|
|
: saved.lastId
|
|
|
|
? saved.lastId
|
2023-06-30 11:24:59 +00:00
|
|
|
: std::numeric_limits<StoryId>::max();
|
|
|
|
auto dirty = false;
|
|
|
|
for (const auto &id : result.v) {
|
2023-09-05 14:17:19 +00:00
|
|
|
if (const auto maybeStory = lookup({ peerId, id.v })) {
|
2023-06-30 11:24:59 +00:00
|
|
|
const auto story = *maybeStory;
|
2024-04-16 10:23:53 +00:00
|
|
|
story->setInProfile(inProfile);
|
|
|
|
if (inProfile) {
|
2023-06-30 11:24:59 +00:00
|
|
|
const auto add = loaded || (id.v >= lastId);
|
|
|
|
if (!add) {
|
|
|
|
dirty = true;
|
|
|
|
} else if (saved.ids.list.emplace(id.v).second) {
|
|
|
|
if (saved.total >= 0) {
|
|
|
|
++saved.total;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (saved.ids.list.remove(id.v)) {
|
|
|
|
if (saved.total > 0) {
|
|
|
|
--saved.total;
|
|
|
|
}
|
|
|
|
} else if (!loaded) {
|
|
|
|
dirty = true;
|
|
|
|
}
|
|
|
|
} else if (!loaded) {
|
|
|
|
dirty = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (dirty) {
|
2023-09-05 14:17:19 +00:00
|
|
|
savedLoadMore(peerId);
|
2023-06-30 11:24:59 +00:00
|
|
|
} else {
|
2023-09-05 14:17:19 +00:00
|
|
|
_savedChanged.fire_copy(peerId);
|
2023-06-30 11:24:59 +00:00
|
|
|
}
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
2024-04-16 15:32:33 +00:00
|
|
|
bool Stories::canTogglePinnedList(
|
|
|
|
const std::vector<FullStoryId> &ids,
|
|
|
|
bool pin) const {
|
|
|
|
Expects(!ids.empty());
|
|
|
|
|
|
|
|
if (!pin) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto peerId = ids.front().peer;
|
|
|
|
const auto i = _saved.find(peerId);
|
|
|
|
if (i == end(_saved)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto &already = i->second.ids.pinnedToTop;
|
|
|
|
auto count = int(already.size());
|
|
|
|
for (const auto &id : ids) {
|
|
|
|
if (!ranges::contains(already, id.story)) {
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count <= maxPinnedCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
int Stories::maxPinnedCount() const {
|
|
|
|
const auto appConfig = &_owner->session().appConfig();
|
|
|
|
return appConfig->get<int>(u"stories_pinned_to_top_count_max"_q, 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stories::togglePinnedList(
|
|
|
|
const std::vector<FullStoryId> &ids,
|
|
|
|
bool pin) {
|
|
|
|
if (ids.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto peerId = ids.front().peer;
|
|
|
|
auto &saved = _saved[peerId];
|
|
|
|
auto list = QVector<MTPint>();
|
|
|
|
list.reserve(maxPinnedCount());
|
|
|
|
for (const auto &id : saved.ids.pinnedToTop) {
|
|
|
|
if (pin || !ranges::contains(ids, FullStoryId{ peerId, id })) {
|
|
|
|
list.push_back(MTP_int(id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (pin) {
|
|
|
|
auto copy = ids;
|
|
|
|
ranges::sort(copy, ranges::greater());
|
|
|
|
for (const auto &id : copy) {
|
|
|
|
if (id.peer == peerId
|
|
|
|
&& !ranges::contains(saved.ids.pinnedToTop, id.story)) {
|
|
|
|
list.push_back(MTP_int(id.story));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const auto api = &_owner->session().api();
|
|
|
|
const auto peer = session().data().peer(peerId);
|
|
|
|
api->request(MTPstories_TogglePinnedToTop(
|
|
|
|
peer->input,
|
|
|
|
MTP_vector<MTPint>(list)
|
|
|
|
)).done([=] {
|
|
|
|
setPinnedToTop(peerId, list
|
|
|
|
| ranges::views::transform(&MTPint::v)
|
|
|
|
| ranges::to_vector);
|
|
|
|
_savedChanged.fire_copy(peerId);
|
|
|
|
}).send();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-06-22 15:29:11 +00:00
|
|
|
void Stories::report(
|
|
|
|
std::shared_ptr<Ui::Show> show,
|
|
|
|
FullStoryId id,
|
|
|
|
Ui::ReportReason reason,
|
|
|
|
QString text) {
|
|
|
|
if (const auto maybeStory = lookup(id)) {
|
|
|
|
const auto story = *maybeStory;
|
|
|
|
Api::SendReport(show, story->peer(), reason, text, story->id());
|
|
|
|
}
|
2023-06-05 15:23:49 +00:00
|
|
|
}
|
|
|
|
|
2023-05-29 15:09:36 +00:00
|
|
|
bool Stories::isQuitPrevent() {
|
|
|
|
if (!_markReadPending.empty()) {
|
|
|
|
sendMarkAsReadRequests();
|
|
|
|
}
|
2023-06-09 14:49:09 +00:00
|
|
|
if (!_incrementViewsPending.empty()) {
|
|
|
|
sendIncrementViewsRequests();
|
|
|
|
}
|
|
|
|
if (_markReadRequests.empty() && _incrementViewsRequests.empty()) {
|
2023-05-29 15:09:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
LOG(("Stories prevents quit, marking as read..."));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-06-27 08:38:02 +00:00
|
|
|
void Stories::incrementPreloadingMainSources() {
|
|
|
|
Expects(_preloadingMainSourcesCounter >= 0);
|
|
|
|
|
|
|
|
if (++_preloadingMainSourcesCounter == 1
|
|
|
|
&& rebuildPreloadSources(StorySourcesList::NotHidden)) {
|
|
|
|
continuePreloading();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stories::decrementPreloadingMainSources() {
|
|
|
|
Expects(_preloadingMainSourcesCounter > 0);
|
|
|
|
|
|
|
|
if (!--_preloadingMainSourcesCounter
|
|
|
|
&& rebuildPreloadSources(StorySourcesList::NotHidden)) {
|
|
|
|
continuePreloading();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-26 16:22:56 +00:00
|
|
|
void Stories::incrementPreloadingHiddenSources() {
|
|
|
|
Expects(_preloadingHiddenSourcesCounter >= 0);
|
|
|
|
|
|
|
|
if (++_preloadingHiddenSourcesCounter == 1
|
|
|
|
&& rebuildPreloadSources(StorySourcesList::Hidden)) {
|
|
|
|
continuePreloading();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stories::decrementPreloadingHiddenSources() {
|
|
|
|
Expects(_preloadingHiddenSourcesCounter > 0);
|
|
|
|
|
|
|
|
if (!--_preloadingHiddenSourcesCounter
|
|
|
|
&& rebuildPreloadSources(StorySourcesList::Hidden)) {
|
|
|
|
continuePreloading();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stories::setPreloadingInViewer(std::vector<FullStoryId> ids) {
|
|
|
|
ids.erase(ranges::remove_if(ids, [&](FullStoryId id) {
|
|
|
|
return _preloaded.contains(id);
|
|
|
|
}), end(ids));
|
|
|
|
if (_toPreloadViewer != ids) {
|
|
|
|
_toPreloadViewer = std::move(ids);
|
|
|
|
continuePreloading();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-04 16:13:56 +00:00
|
|
|
std::optional<Stories::PeerSourceState> Stories::peerSourceState(
|
|
|
|
not_null<PeerData*> peer,
|
|
|
|
StoryId storyMaxId) {
|
|
|
|
const auto i = _readTill.find(peer->id);
|
|
|
|
if (_readTillReceived || (i != end(_readTill))) {
|
|
|
|
return PeerSourceState{
|
|
|
|
.maxId = storyMaxId,
|
|
|
|
.readTill = std::min(
|
|
|
|
storyMaxId,
|
|
|
|
(i != end(_readTill)) ? i->second : 0),
|
|
|
|
};
|
|
|
|
}
|
2023-07-05 07:55:16 +00:00
|
|
|
requestReadTills();
|
2023-08-31 08:58:34 +00:00
|
|
|
_pendingPeerStateMaxId[peer] = storyMaxId;
|
2023-07-04 16:13:56 +00:00
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
2023-07-05 07:55:16 +00:00
|
|
|
void Stories::requestReadTills() {
|
|
|
|
if (_readTillReceived || _readTillsRequestId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto api = &_owner->session().api();
|
2023-08-31 08:58:34 +00:00
|
|
|
_readTillsRequestId = api->request(MTPstories_GetAllReadPeerStories(
|
2023-07-05 07:55:16 +00:00
|
|
|
)).done([=](const MTPUpdates &result) {
|
|
|
|
_readTillReceived = true;
|
|
|
|
api->applyUpdates(result);
|
2023-08-31 08:58:34 +00:00
|
|
|
for (auto &[peer, maxId] : base::take(_pendingPeerStateMaxId)) {
|
|
|
|
updatePeerStoriesState(peer);
|
2023-07-05 07:55:16 +00:00
|
|
|
}
|
|
|
|
for (const auto &storyId : base::take(_pendingReadTillItems)) {
|
|
|
|
_owner->refreshStoryItemViews(storyId);
|
|
|
|
}
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Stories::isUnread(not_null<Story*> story) {
|
|
|
|
const auto till = _readTill.find(story->peer()->id);
|
|
|
|
if (till == end(_readTill) && !_readTillReceived) {
|
|
|
|
requestReadTills();
|
|
|
|
_pendingReadTillItems.emplace(story->fullId());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const auto readTill = (till != end(_readTill)) ? till->second : 0;
|
|
|
|
return (story->id() > readTill);
|
|
|
|
}
|
|
|
|
|
2023-07-05 15:52:22 +00:00
|
|
|
void Stories::registerPolling(not_null<Story*> story, Polling polling) {
|
|
|
|
auto &settings = _pollingSettings[story];
|
|
|
|
switch (polling) {
|
|
|
|
case Polling::Chat: ++settings.chat; break;
|
2023-07-05 17:02:57 +00:00
|
|
|
case Polling::Viewer:
|
|
|
|
++settings.viewer;
|
2023-09-15 16:41:36 +00:00
|
|
|
if ((story->peer()->isSelf() || story->peer()->isChannel())
|
2023-07-05 17:02:57 +00:00
|
|
|
&& _pollingViews.emplace(story).second) {
|
|
|
|
sendPollingViewsRequests();
|
|
|
|
}
|
|
|
|
break;
|
2023-07-05 15:52:22 +00:00
|
|
|
}
|
|
|
|
maybeSchedulePolling(story, settings, base::unixtime::now());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stories::unregisterPolling(not_null<Story*> story, Polling polling) {
|
|
|
|
const auto i = _pollingSettings.find(story);
|
|
|
|
Assert(i != end(_pollingSettings));
|
|
|
|
|
|
|
|
switch (polling) {
|
|
|
|
case Polling::Chat:
|
|
|
|
Assert(i->second.chat > 0);
|
|
|
|
--i->second.chat;
|
|
|
|
break;
|
|
|
|
case Polling::Viewer:
|
|
|
|
Assert(i->second.viewer > 0);
|
2023-07-05 17:02:57 +00:00
|
|
|
if (!--i->second.viewer) {
|
|
|
|
_pollingViews.remove(story);
|
|
|
|
if (_pollingViews.empty()) {
|
|
|
|
_pollingViewsTimer.cancel();
|
|
|
|
}
|
|
|
|
}
|
2023-07-05 15:52:22 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!i->second.chat && !i->second.viewer) {
|
|
|
|
_pollingSettings.erase(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Stories::registerPolling(FullStoryId id, Polling polling) {
|
|
|
|
if (const auto maybeStory = lookup(id)) {
|
|
|
|
registerPolling(*maybeStory, polling);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stories::unregisterPolling(FullStoryId id, Polling polling) {
|
2023-07-21 09:44:43 +00:00
|
|
|
if (const auto maybeStory = lookup(id)) {
|
|
|
|
unregisterPolling(*maybeStory, polling);
|
|
|
|
} else if (const auto i = _deletingStories.find(id)
|
|
|
|
; i != end(_deletingStories)) {
|
|
|
|
unregisterPolling(i->second.get(), polling);
|
|
|
|
} else {
|
|
|
|
Unexpected("Couldn't find story for unregistering polling.");
|
|
|
|
}
|
2023-07-05 15:52:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int Stories::pollingInterval(const PollingSettings &settings) const {
|
|
|
|
return settings.viewer ? kPollingIntervalViewer : kPollingIntervalChat;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stories::maybeSchedulePolling(
|
|
|
|
not_null<Story*> story,
|
|
|
|
const PollingSettings &settings,
|
|
|
|
TimeId now) {
|
|
|
|
const auto last = story->lastUpdateTime();
|
|
|
|
const auto next = last + pollingInterval(settings);
|
|
|
|
const auto left = std::max(next - now, 0) * crl::time(1000) + 1;
|
|
|
|
if (!_pollingTimer.isActive() || _pollingTimer.remainingTime() > left) {
|
|
|
|
_pollingTimer.callOnce(left);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stories::sendPollingRequests() {
|
|
|
|
auto min = 0;
|
|
|
|
const auto now = base::unixtime::now();
|
|
|
|
for (const auto &[story, settings] : _pollingSettings) {
|
|
|
|
const auto last = story->lastUpdateTime();
|
|
|
|
const auto next = last + pollingInterval(settings);
|
|
|
|
if (now >= next) {
|
|
|
|
resolve(story->fullId(), nullptr, true);
|
|
|
|
} else {
|
|
|
|
const auto left = (next - now) * crl::time(1000) + 1;
|
|
|
|
if (!min || left < min) {
|
|
|
|
min = left;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (min > 0) {
|
|
|
|
_pollingTimer.callOnce(min);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-05 17:02:57 +00:00
|
|
|
void Stories::sendPollingViewsRequests() {
|
|
|
|
if (_pollingViews.empty()) {
|
|
|
|
return;
|
|
|
|
} else if (!_viewsRequestId) {
|
|
|
|
Assert(_viewsDone == nullptr);
|
2023-09-15 16:41:36 +00:00
|
|
|
const auto story = _pollingViews.front();
|
|
|
|
loadViewsSlice(story->peer(), story->id(), QString(), nullptr);
|
2023-07-05 17:02:57 +00:00
|
|
|
}
|
|
|
|
_pollingViewsTimer.callOnce(kPollViewsInterval);
|
|
|
|
}
|
|
|
|
|
2023-08-31 08:58:34 +00:00
|
|
|
void Stories::updatePeerStoriesState(not_null<PeerData*> peer) {
|
2023-07-04 16:13:56 +00:00
|
|
|
const auto till = _readTill.find(peer->id);
|
|
|
|
const auto readTill = (till != end(_readTill)) ? till->second : 0;
|
|
|
|
const auto pendingMaxId = [&] {
|
2023-08-31 08:58:34 +00:00
|
|
|
const auto j = _pendingPeerStateMaxId.find(peer);
|
|
|
|
return (j != end(_pendingPeerStateMaxId)) ? j->second : 0;
|
2023-07-04 16:13:56 +00:00
|
|
|
};
|
|
|
|
const auto i = _all.find(peer->id);
|
|
|
|
const auto max = (i != end(_all))
|
|
|
|
? (i->second.ids.empty() ? 0 : i->second.ids.back().id)
|
|
|
|
: pendingMaxId();
|
2023-08-31 08:58:34 +00:00
|
|
|
peer->setStoriesState(!max
|
|
|
|
? PeerData::StoriesState::None
|
|
|
|
: (max <= readTill)
|
|
|
|
? PeerData::StoriesState::HasRead
|
|
|
|
: PeerData::StoriesState::HasUnread);
|
2023-07-04 16:13:56 +00:00
|
|
|
}
|
|
|
|
|
2023-06-26 16:22:56 +00:00
|
|
|
void Stories::preloadSourcesChanged(StorySourcesList list) {
|
|
|
|
if (rebuildPreloadSources(list)) {
|
|
|
|
continuePreloading();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Stories::rebuildPreloadSources(StorySourcesList list) {
|
|
|
|
const auto index = static_cast<int>(list);
|
2023-06-27 08:38:02 +00:00
|
|
|
const auto &counter = (list == StorySourcesList::Hidden)
|
|
|
|
? _preloadingHiddenSourcesCounter
|
|
|
|
: _preloadingMainSourcesCounter;
|
|
|
|
if (!counter) {
|
2023-06-26 16:22:56 +00:00
|
|
|
return !base::take(_toPreloadSources[index]).empty();
|
|
|
|
}
|
|
|
|
auto now = std::vector<FullStoryId>();
|
|
|
|
auto processed = 0;
|
|
|
|
for (const auto &source : _sources[index]) {
|
|
|
|
const auto i = _all.find(source.id);
|
|
|
|
if (i != end(_all)) {
|
|
|
|
if (const auto id = i->second.toOpen().id) {
|
|
|
|
const auto fullId = FullStoryId{ source.id, id };
|
|
|
|
if (!_preloaded.contains(fullId)) {
|
|
|
|
now.push_back(fullId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (++processed >= kMaxPreloadSources) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (now != _toPreloadSources[index]) {
|
|
|
|
_toPreloadSources[index] = std::move(now);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stories::continuePreloading() {
|
|
|
|
const auto now = _preloading ? _preloading->id() : FullStoryId();
|
|
|
|
if (now) {
|
|
|
|
if (shouldContinuePreload(now)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_preloading = nullptr;
|
|
|
|
}
|
|
|
|
const auto id = nextPreloadId();
|
|
|
|
if (!id) {
|
|
|
|
return;
|
|
|
|
} else if (const auto maybeStory = lookup(id)) {
|
|
|
|
startPreloading(*maybeStory);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Stories::shouldContinuePreload(FullStoryId id) const {
|
|
|
|
const auto first = ranges::views::concat(
|
|
|
|
_toPreloadViewer,
|
|
|
|
_toPreloadSources[static_cast<int>(StorySourcesList::Hidden)],
|
|
|
|
_toPreloadSources[static_cast<int>(StorySourcesList::NotHidden)]
|
|
|
|
) | ranges::views::take(kStillPreloadFromFirst);
|
|
|
|
return ranges::contains(first, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
FullStoryId Stories::nextPreloadId() const {
|
|
|
|
const auto hidden = static_cast<int>(StorySourcesList::Hidden);
|
|
|
|
const auto main = static_cast<int>(StorySourcesList::NotHidden);
|
|
|
|
const auto result = !_toPreloadViewer.empty()
|
|
|
|
? _toPreloadViewer.front()
|
|
|
|
: !_toPreloadSources[hidden].empty()
|
|
|
|
? _toPreloadSources[hidden].front()
|
|
|
|
: !_toPreloadSources[main].empty()
|
|
|
|
? _toPreloadSources[main].front()
|
|
|
|
: FullStoryId();
|
|
|
|
|
|
|
|
Ensures(!_preloaded.contains(result));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stories::startPreloading(not_null<Story*> story) {
|
|
|
|
Expects(!_preloaded.contains(story->fullId()));
|
|
|
|
|
|
|
|
const auto id = story->fullId();
|
2023-06-27 08:38:02 +00:00
|
|
|
auto preloading = std::make_unique<StoryPreload>(story, [=] {
|
|
|
|
_preloading = nullptr;
|
|
|
|
preloadFinished(id, true);
|
2023-06-26 16:22:56 +00:00
|
|
|
});
|
2023-06-27 08:38:02 +00:00
|
|
|
if (!_preloaded.contains(id)) {
|
|
|
|
_preloading = std::move(preloading);
|
|
|
|
}
|
2023-06-26 16:22:56 +00:00
|
|
|
}
|
|
|
|
|
2023-06-27 08:38:02 +00:00
|
|
|
void Stories::preloadFinished(FullStoryId id, bool markAsPreloaded) {
|
2023-06-26 16:22:56 +00:00
|
|
|
for (auto &sources : _toPreloadSources) {
|
|
|
|
sources.erase(ranges::remove(sources, id), end(sources));
|
|
|
|
}
|
|
|
|
_toPreloadViewer.erase(
|
|
|
|
ranges::remove(_toPreloadViewer, id),
|
|
|
|
end(_toPreloadViewer));
|
|
|
|
if (markAsPreloaded) {
|
|
|
|
_preloaded.emplace(id);
|
|
|
|
}
|
|
|
|
crl::on_main(this, [=] {
|
|
|
|
continuePreloading();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-06-09 17:25:26 +00:00
|
|
|
} // namespace Data
|