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-05-03 18:30:37 +00:00
|
|
|
#include "data/data_document.h"
|
|
|
|
#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"
|
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-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
|
|
|
|
|
2023-06-02 14:26:39 +00:00
|
|
|
StoriesSourceInfo StoriesSource::info() const {
|
|
|
|
return {
|
|
|
|
.id = user->id,
|
|
|
|
.last = ids.empty() ? 0 : ids.back().date,
|
|
|
|
.unread = unread(),
|
|
|
|
.premium = user->isPremium(),
|
|
|
|
.hidden = hidden,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StoriesSource::unread() const {
|
|
|
|
return !ids.empty() && readTill < ids.back().id;
|
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(); })
|
|
|
|
, _incrementViewsTimer([=] { sendIncrementViewsRequests(); }) {
|
2023-05-03 18:30:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Stories::~Stories() {
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
const auto peerId = peerFromUser(data.vuser_id());
|
2023-06-02 14:26:39 +00:00
|
|
|
const auto user = not_null(_owner->peer(peerId)->asUser());
|
2023-06-05 15:23:49 +00:00
|
|
|
const auto now = base::unixtime::now();
|
|
|
|
const auto idDates = parseAndApply(user, data.vstory(), now);
|
|
|
|
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)) {
|
|
|
|
requestUserStories(user);
|
|
|
|
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-06-07 15:56:38 +00:00
|
|
|
if (user->isSelf() && i->second.readTill < idDates.id) {
|
|
|
|
i->second.readTill = idDates.id;
|
|
|
|
}
|
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-06-21 06:12:28 +00:00
|
|
|
if (user->hasStoriesHidden()) {
|
|
|
|
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-05-03 18:30:37 +00:00
|
|
|
}
|
|
|
|
|
2023-06-05 16:31:15 +00:00
|
|
|
void Stories::apply(not_null<PeerData*> peer, const MTPUserStories *data) {
|
|
|
|
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);
|
|
|
|
} else {
|
|
|
|
parseAndApply(*data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-23 12:04:53 +00:00
|
|
|
Story *Stories::applyFromWebpage(PeerId peerId, const MTPstoryItem &story) {
|
|
|
|
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-06-02 14:26:39 +00:00
|
|
|
void Stories::requestUserStories(not_null<UserData*> user) {
|
|
|
|
if (!_requestingUserStories.emplace(user).second) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_owner->session().api().request(MTPstories_GetUserStories(
|
|
|
|
user->inputUser
|
|
|
|
)).done([=](const MTPstories_UserStories &result) {
|
|
|
|
_requestingUserStories.remove(user);
|
|
|
|
const auto &data = result.data();
|
|
|
|
_owner->processUsers(data.vusers());
|
|
|
|
parseAndApply(data.vstories());
|
|
|
|
}).fail([=] {
|
|
|
|
_requestingUserStories.remove(user);
|
2023-06-21 06:12:28 +00:00
|
|
|
applyDeletedFromSources(user->id, StorySourcesList::NotHidden);
|
|
|
|
applyDeletedFromSources(user->id, StorySourcesList::Hidden);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void Stories::parseAndApply(const MTPUserStories &stories) {
|
2023-05-03 18:30:37 +00:00
|
|
|
const auto &data = stories.data();
|
2023-06-02 14:26:39 +00:00
|
|
|
const auto peerId = peerFromUser(data.vuser_id());
|
2023-05-03 18:30:37 +00:00
|
|
|
const auto readTill = data.vmax_read_id().value_or_empty();
|
|
|
|
const auto count = int(data.vstories().v.size());
|
2023-06-02 17:14:15 +00:00
|
|
|
const auto user = _owner->peer(peerId)->asUser();
|
2023-06-02 14:26:39 +00:00
|
|
|
auto result = StoriesSource{
|
2023-06-02 17:14:15 +00:00
|
|
|
.user = user,
|
2023-05-03 18:30:37 +00:00
|
|
|
.readTill = readTill,
|
2023-06-02 17:14:15 +00:00
|
|
|
.hidden = user->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-06-05 15:23:49 +00:00
|
|
|
if (const auto id = parseAndApply(result.user, 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-06-02 14:26:39 +00:00
|
|
|
return;
|
2023-06-07 15:56:38 +00:00
|
|
|
} else if (user->isSelf()) {
|
|
|
|
result.readTill = result.ids.back().id;
|
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-06-05 15:23:49 +00:00
|
|
|
if (result.user->isContact()) {
|
2023-06-21 06:12:28 +00:00
|
|
|
const auto hidden = result.user->hasStoriesHidden();
|
|
|
|
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-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-06-05 15:23:49 +00:00
|
|
|
if (expired && !data.is_pinned() && !peer->isSelf()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2023-05-03 18:30:37 +00:00
|
|
|
const auto id = data.vid().v;
|
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();
|
|
|
|
const auto pinned = result->pinned();
|
2023-06-26 16:22:56 +00:00
|
|
|
const auto mediaChanged = (result->media() != *media);
|
2023-06-07 15:56:38 +00:00
|
|
|
if (result->applyChanges(*media, data)) {
|
|
|
|
if (result->pinned() != pinned) {
|
|
|
|
savedStateUpdated(result);
|
|
|
|
}
|
2023-05-26 15:18:10 +00:00
|
|
|
session().changes().storyUpdated(
|
2023-06-07 15:56:38 +00:00
|
|
|
result,
|
2023-05-26 15:18:10 +00:00
|
|
|
UpdateFlag::Edited);
|
2023-06-07 15:56:38 +00:00
|
|
|
if (const auto item = lookupItem(result)) {
|
|
|
|
item->applyChanges(result);
|
2023-06-05 16:31:15 +00:00
|
|
|
}
|
2023-05-03 18:30:37 +00:00
|
|
|
}
|
2023-06-26 16:22:56 +00:00
|
|
|
if (mediaChanged) {
|
|
|
|
const auto fullId = result->fullId();
|
|
|
|
_preloaded.remove(fullId);
|
|
|
|
if (_preloading && _preloading->id() == fullId) {
|
|
|
|
_preloading = nullptr;
|
|
|
|
rebuildPreloadSources(StorySourcesList::NotHidden);
|
|
|
|
rebuildPreloadSources(StorySourcesList::Hidden);
|
|
|
|
continuePreloading();
|
|
|
|
}
|
|
|
|
}
|
2023-06-07 15:56:38 +00:00
|
|
|
return result;
|
2023-05-03 18:30:37 +00:00
|
|
|
}
|
2023-05-26 07:21:19 +00:00
|
|
|
const auto result = stories.emplace(id, std::make_unique<Story>(
|
|
|
|
id,
|
|
|
|
peer,
|
|
|
|
StoryMedia{ *media },
|
2023-06-05 15:23:49 +00:00
|
|
|
data.vdate().v,
|
|
|
|
data.vexpire_date().v)).first->second.get();
|
2023-05-26 15:18:10 +00:00
|
|
|
result->applyChanges(*media, data);
|
2023-06-07 15:56:38 +00:00
|
|
|
if (result->pinned()) {
|
|
|
|
savedStateUpdated(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (peer->isSelf()) {
|
|
|
|
const auto added = _archive.list.emplace(id).second;
|
|
|
|
if (added) {
|
|
|
|
if (_archiveTotal >= 0 && id > _archiveLastId) {
|
|
|
|
++_archiveTotal;
|
|
|
|
}
|
|
|
|
_archiveChanged.fire({});
|
|
|
|
}
|
|
|
|
}
|
2023-06-05 15:23:49 +00:00
|
|
|
|
|
|
|
if (expired) {
|
|
|
|
_expiring.remove(expires, result->fullId());
|
|
|
|
applyExpired(result->fullId());
|
|
|
|
} else {
|
|
|
|
registerExpiring(expires, result->fullId());
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
applyDeleted({ peer->id, 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);
|
|
|
|
} else if (!peer->isSelf()) {
|
|
|
|
applyDeleted(fullId);
|
|
|
|
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) {
|
|
|
|
applyDeleted({ peer->id, 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-06-07 15:56:38 +00:00
|
|
|
void Stories::savedStateUpdated(not_null<Story*> story) {
|
|
|
|
const auto id = story->id();
|
|
|
|
const auto peer = story->peer()->id;
|
|
|
|
const auto pinned = story->pinned();
|
|
|
|
if (pinned) {
|
|
|
|
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-06-02 14:26:39 +00:00
|
|
|
_sourcesStates[index] = qs(data.vstate());
|
|
|
|
_sourcesLoaded[index] = !data.is_has_more();
|
2023-05-03 18:30:37 +00:00
|
|
|
for (const auto &single : data.vuser_stories().v) {
|
2023-06-02 14:26:39 +00:00
|
|
|
parseAndApply(single);
|
2023-05-03 18:30:37 +00:00
|
|
|
}
|
|
|
|
}, [](const MTPDstories_allStoriesNotModified &) {
|
|
|
|
});
|
|
|
|
}).fail([=] {
|
2023-06-02 14:26:39 +00:00
|
|
|
_loadMoreRequestId[index] = 0;
|
2023-05-03 18:30:37 +00:00
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
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(); });
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const auto user = _owner->session().data().peer(peerId)->asUser();
|
|
|
|
if (!user) {
|
2023-05-29 12:03:23 +00:00
|
|
|
finish(peerId);
|
2023-05-26 09:27:34 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const auto requestId = api->request(MTPstories_GetStoriesByID(
|
|
|
|
user->inputUser,
|
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());
|
|
|
|
processResolvedStories(user, result.data().vstories().v);
|
2023-05-29 12:03:23 +00:00
|
|
|
finish(user->id);
|
|
|
|
}).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-05-26 15:18:10 +00:00
|
|
|
applyDeleted({ peer->id, data.vid().v });
|
|
|
|
}
|
2023-05-26 09:27:34 +00:00
|
|
|
}, [&](const MTPDstoryItemSkipped &data) {
|
|
|
|
LOG(("API Error: Unexpected storyItemSkipped in resolve."));
|
|
|
|
}, [&](const MTPDstoryItemDeleted &data) {
|
|
|
|
applyDeleted({ peer->id, data.vid().v });
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
|
|
|
applyDeleted(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stories::applyDeleted(FullStoryId id) {
|
2023-06-05 15:23:49 +00:00
|
|
|
applyRemovedFromActive(id);
|
|
|
|
|
|
|
|
_deleted.emplace(id);
|
2023-06-07 15:56:38 +00:00
|
|
|
const auto i = _stories.find(id.peer);
|
|
|
|
if (i != end(_stories)) {
|
|
|
|
const auto j = i->second.find(id.story);
|
|
|
|
if (j != end(i->second)) {
|
2023-06-05 16:31:15 +00:00
|
|
|
// Duplicated in Stories::apply(peer, const MTPUserStories*).
|
2023-06-07 15:56:38 +00:00
|
|
|
auto story = 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-06-05 15:23:49 +00:00
|
|
|
session().changes().storyUpdated(
|
|
|
|
story.get(),
|
|
|
|
UpdateFlag::Destroyed);
|
|
|
|
removeDependencyStory(story.get());
|
2023-06-07 15:56:38 +00:00
|
|
|
if (id.peer == session().userPeerId()
|
|
|
|
&& _archive.list.remove(id.story)) {
|
|
|
|
if (_archiveTotal > 0) {
|
|
|
|
--_archiveTotal;
|
|
|
|
}
|
|
|
|
_archiveChanged.fire({});
|
|
|
|
}
|
|
|
|
if (story->pinned()) {
|
|
|
|
if (const auto k = _saved.find(id.peer); k != end(_saved)) {
|
|
|
|
const auto saved = &k->second;
|
|
|
|
if (saved->ids.list.remove(id.story)) {
|
|
|
|
if (saved->total > 0) {
|
|
|
|
--saved->total;
|
|
|
|
}
|
|
|
|
_savedChanged.fire_copy(id.peer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-06-26 16:22:56 +00:00
|
|
|
if (_preloading && _preloading->id() == id) {
|
2023-06-27 08:38:02 +00:00
|
|
|
preloadFinished(id);
|
2023-06-26 16:22:56 +00:00
|
|
|
}
|
2023-06-07 15:56:38 +00:00
|
|
|
if (i->second.empty()) {
|
|
|
|
_stories.erase(i);
|
2023-06-05 15:23:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stories::applyExpired(FullStoryId id) {
|
|
|
|
if (const auto maybeStory = lookup(id)) {
|
|
|
|
const auto story = *maybeStory;
|
2023-06-07 15:56:38 +00:00
|
|
|
if (!story->peer()->isSelf() && !story->pinned()) {
|
2023-06-05 15:23:49 +00:00
|
|
|
applyDeleted(id);
|
|
|
|
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);
|
|
|
|
_sourcesChanged[index].fire({});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
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);
|
|
|
|
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-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-06-21 06:12:28 +00:00
|
|
|
_sourcesChanged[static_cast<int>(list)].fire({});
|
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-06-02 14:26:39 +00:00
|
|
|
+ (info.unread ? (int64(1) << 49) : 0)
|
|
|
|
+ ((info.id == self) ? (int64(1) << 50) : 0);
|
|
|
|
return std::make_pair(key, info.id);
|
|
|
|
};
|
|
|
|
ranges::sort(sources, ranges::greater(), proj);
|
|
|
|
_sourcesChanged[index].fire({});
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
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>(
|
|
|
|
history->makeMessage(story).get(),
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stories::resolve(FullStoryId id, Fn<void()> done) {
|
2023-05-26 09:27:34 +00:00
|
|
|
const auto already = lookup(id);
|
|
|
|
if (already.has_value() || already.error() != NoStory::Unknown) {
|
2023-05-29 12:03:23 +00:00
|
|
|
if (done) {
|
|
|
|
done();
|
|
|
|
}
|
2023-05-26 09:27:34 +00:00
|
|
|
return;
|
|
|
|
}
|
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;
|
|
|
|
if (story->expired() && story->pinned()) {
|
|
|
|
_incrementViewsPending[id.peer].emplace(id.story);
|
|
|
|
if (!_incrementViewsTimer.isActive()) {
|
|
|
|
_incrementViewsTimer.callOnce(kIncrementViewsDelay);
|
|
|
|
}
|
|
|
|
}
|
2023-06-02 14:26:39 +00:00
|
|
|
const auto i = _all.find(id.peer);
|
2023-06-13 15:51:08 +00:00
|
|
|
if (i == end(_all) || i->second.readTill >= id.story) {
|
2023-05-29 15:09:36 +00:00
|
|
|
return;
|
|
|
|
} else if (!_markReadPending.contains(id.peer)) {
|
|
|
|
sendMarkAsReadRequests();
|
|
|
|
}
|
|
|
|
_markReadPending.emplace(id.peer);
|
2023-06-02 14:26:39 +00:00
|
|
|
const auto wasUnread = i->second.unread();
|
|
|
|
i->second.readTill = id.story;
|
|
|
|
const auto nowUnread = i->second.unread();
|
|
|
|
if (wasUnread != nowUnread) {
|
|
|
|
const auto refreshInList = [&](StorySourcesList list) {
|
|
|
|
auto &sources = _sources[static_cast<int>(list)];
|
|
|
|
const auto i = ranges::find(
|
|
|
|
sources,
|
|
|
|
id.peer,
|
|
|
|
&StoriesSourceInfo::id);
|
|
|
|
if (i != end(sources)) {
|
|
|
|
i->unread = nowUnread;
|
|
|
|
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-05-29 15:09:36 +00:00
|
|
|
_markReadTimer.callOnce(kMarkAsReadDelay);
|
|
|
|
}
|
|
|
|
|
2023-06-15 16:28:53 +00:00
|
|
|
void Stories::toggleHidden(
|
|
|
|
PeerId peerId,
|
|
|
|
bool hidden,
|
|
|
|
std::shared_ptr<Ui::Show> show) {
|
2023-06-02 17:14:15 +00:00
|
|
|
const auto user = _owner->peer(peerId)->asUser();
|
|
|
|
Assert(user != nullptr);
|
|
|
|
if (user->hasStoriesHidden() != hidden) {
|
|
|
|
user->setFlags(hidden
|
|
|
|
? (user->flags() | UserDataFlag::StoriesHidden)
|
|
|
|
: (user->flags() & ~UserDataFlag::StoriesHidden));
|
|
|
|
session().api().request(MTPcontacts_ToggleStoriesHidden(
|
|
|
|
user->inputUser,
|
|
|
|
MTP_bool(hidden)
|
|
|
|
)).send();
|
|
|
|
}
|
|
|
|
|
2023-06-23 16:39:57 +00:00
|
|
|
const auto name = user->shortName();
|
2023-06-15 16:28:53 +00:00
|
|
|
const auto guard = gsl::finally([&] {
|
|
|
|
if (show) {
|
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),
|
|
|
|
Ui::Text::WithEntities));
|
2023-06-15 16:28:53 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
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);
|
|
|
|
_sourcesChanged[main].fire({});
|
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);
|
|
|
|
_sourcesChanged[other].fire({});
|
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) {
|
|
|
|
Expects(peer->isUser());
|
|
|
|
|
|
|
|
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(
|
|
|
|
peer->asUser()->inputUser,
|
|
|
|
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-06-02 14:26:39 +00:00
|
|
|
sendMarkAsReadRequest(j->second.user, 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;
|
|
|
|
}
|
|
|
|
auto ids = QVector<MTPint>();
|
|
|
|
auto peer = PeerId();
|
|
|
|
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(
|
|
|
|
_owner->peer(peer)->asUser()->inputUser,
|
|
|
|
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(
|
|
|
|
StoryId id,
|
|
|
|
std::optional<StoryView> offset,
|
|
|
|
Fn<void(std::vector<StoryView>)> done) {
|
|
|
|
_viewsDone = std::move(done);
|
|
|
|
if (_viewsStoryId == id && _viewsOffset == offset) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_viewsStoryId = id;
|
|
|
|
_viewsOffset = offset;
|
|
|
|
|
|
|
|
const auto api = &_owner->session().api();
|
|
|
|
api->request(_viewsRequestId).cancel();
|
|
|
|
_viewsRequestId = api->request(MTPstories_GetStoryViewsList(
|
|
|
|
MTP_int(id),
|
|
|
|
MTP_int(offset ? offset->date : 0),
|
|
|
|
MTP_long(offset ? peerToUser(offset->peer->id).bare : 0),
|
2023-06-08 15:54:21 +00:00
|
|
|
MTP_int(kViewsPerPage)
|
2023-06-01 16:01:29 +00:00
|
|
|
)).done([=](const MTPstories_StoryViewsList &result) {
|
|
|
|
_viewsRequestId = 0;
|
|
|
|
|
|
|
|
auto slice = std::vector<StoryView>();
|
|
|
|
|
|
|
|
const auto &data = result.data();
|
|
|
|
_owner->processUsers(data.vusers());
|
|
|
|
slice.reserve(data.vviews().v.size());
|
|
|
|
for (const auto &view : data.vviews().v) {
|
|
|
|
slice.push_back({
|
|
|
|
.peer = _owner->peer(peerFromUser(view.data().vuser_id())),
|
|
|
|
.date = view.data().vdate().v,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
const auto fullId = FullStoryId{
|
|
|
|
.peer = _owner->session().userPeerId(),
|
|
|
|
.story = _viewsStoryId,
|
|
|
|
};
|
|
|
|
if (const auto story = lookup(fullId)) {
|
|
|
|
(*story)->applyViewsSlice(_viewsOffset, slice, data.vcount().v);
|
|
|
|
}
|
|
|
|
if (const auto done = base::take(_viewsDone)) {
|
|
|
|
done(std::move(slice));
|
|
|
|
}
|
|
|
|
}).fail([=] {
|
|
|
|
_viewsRequestId = 0;
|
|
|
|
if (const auto done = base::take(_viewsDone)) {
|
|
|
|
done({});
|
|
|
|
}
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
2023-06-07 15:56:38 +00:00
|
|
|
const StoriesIds &Stories::archive() const {
|
|
|
|
return _archive;
|
2023-06-05 15:23:49 +00:00
|
|
|
}
|
|
|
|
|
2023-06-07 15:56:38 +00:00
|
|
|
rpl::producer<> Stories::archiveChanged() const {
|
|
|
|
return _archiveChanged.events();
|
2023-06-05 15:23:49 +00:00
|
|
|
}
|
|
|
|
|
2023-06-07 15:56:38 +00:00
|
|
|
int Stories::archiveCount() const {
|
|
|
|
return std::max(_archiveTotal, 0);
|
2023-06-05 15:23:49 +00:00
|
|
|
}
|
|
|
|
|
2023-06-07 15:56:38 +00:00
|
|
|
bool Stories::archiveCountKnown() const {
|
|
|
|
return _archiveTotal >= 0;
|
2023-06-05 15:23:49 +00:00
|
|
|
}
|
|
|
|
|
2023-06-07 15:56:38 +00:00
|
|
|
bool Stories::archiveLoaded() const {
|
|
|
|
return _archiveLoaded;
|
2023-06-05 15:23:49 +00:00
|
|
|
}
|
|
|
|
|
2023-06-05 16:31:15 +00:00
|
|
|
const StoriesIds *Stories::saved(PeerId peerId) const {
|
|
|
|
const auto i = _saved.find(peerId);
|
|
|
|
return (i != end(_saved)) ? &i->second.ids : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-06-07 15:56:38 +00:00
|
|
|
void Stories::archiveLoadMore() {
|
|
|
|
if (_archiveRequestId || _archiveLoaded) {
|
2023-06-05 15:23:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto api = &_owner->session().api();
|
2023-06-07 15:56:38 +00:00
|
|
|
_archiveRequestId = api->request(MTPstories_GetStoriesArchive(
|
|
|
|
MTP_int(_archiveLastId),
|
|
|
|
MTP_int(_archiveLastId ? kArchivePerPage : kArchiveFirstPerPage)
|
2023-06-05 15:23:49 +00:00
|
|
|
)).done([=](const MTPstories_Stories &result) {
|
2023-06-07 15:56:38 +00:00
|
|
|
_archiveRequestId = 0;
|
2023-06-05 15:23:49 +00:00
|
|
|
|
|
|
|
const auto &data = result.data();
|
|
|
|
const auto self = _owner->session().user();
|
|
|
|
const auto now = base::unixtime::now();
|
2023-06-07 15:56:38 +00:00
|
|
|
_archiveTotal = 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-06-07 15:56:38 +00:00
|
|
|
_archive.list.emplace(id);
|
|
|
|
_archiveLastId = id;
|
2023-06-05 15:23:49 +00:00
|
|
|
if (!parseAndApply(self, story, now)) {
|
2023-06-07 15:56:38 +00:00
|
|
|
_archive.list.remove(id);
|
|
|
|
if (_archiveTotal > 0) {
|
|
|
|
--_archiveTotal;
|
2023-06-05 15:23:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-06-07 15:56:38 +00:00
|
|
|
const auto ids = int(_archive.list.size());
|
|
|
|
_archiveLoaded = data.vstories().v.empty();
|
|
|
|
_archiveTotal = _archiveLoaded ? ids : std::max(_archiveTotal, ids);
|
|
|
|
_archiveChanged.fire({});
|
2023-06-05 15:23:49 +00:00
|
|
|
}).fail([=] {
|
2023-06-07 15:56:38 +00:00
|
|
|
_archiveRequestId = 0;
|
|
|
|
_archiveLoaded = true;
|
|
|
|
_archiveTotal = int(_archive.list.size());
|
|
|
|
_archiveChanged.fire({});
|
2023-06-05 16:31:15 +00:00
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stories::savedLoadMore(PeerId peerId) {
|
|
|
|
Expects(peerIsUser(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(
|
|
|
|
peer->asUser()->inputUser,
|
|
|
|
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();
|
|
|
|
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);
|
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
|
|
|
|
2023-06-22 15:29:11 +00:00
|
|
|
void Stories::deleteList(const std::vector<FullStoryId> &ids) {
|
|
|
|
auto list = QVector<MTPint>();
|
|
|
|
list.reserve(ids.size());
|
|
|
|
const auto selfId = session().userPeerId();
|
|
|
|
for (const auto &id : ids) {
|
|
|
|
if (id.peer == selfId) {
|
|
|
|
list.push_back(MTP_int(id.story));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!list.empty()) {
|
|
|
|
const auto api = &_owner->session().api();
|
|
|
|
api->request(MTPstories_DeleteStories(
|
|
|
|
MTP_vector<MTPint>(list)
|
|
|
|
)).done([=](const MTPVector<MTPint> &result) {
|
|
|
|
for (const auto &id : result.v) {
|
|
|
|
applyDeleted({ selfId, id.v });
|
|
|
|
}
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|