feat: rework sendOfflinePacketAfterOnline

This commit is contained in:
ZavaruKitsu 2024-02-04 04:00:24 +03:00
parent a3f4185b45
commit 84c62470bb
7 changed files with 154 additions and 18 deletions

View file

@ -96,6 +96,8 @@ nice_target_sources(Telegram ${src_loc}
PRIVATE
${style_files}
ayu/ayu_worker.cpp
ayu/ayu_worker.h
ayu/ayu_url_handlers.cpp
ayu/ayu_url_handlers.h
ayu/ayu_state.cpp

View file

@ -85,6 +85,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
// AyuGram includes
#include "ayu/ayu_settings.h"
#include "ayu/ayu_worker.h"
namespace {
@ -3781,6 +3782,8 @@ void ApiWrap::sendMessage(MessageToSend &&message) {
draftTopicRootId,
UnixtimeFromMsgId(response.outerMsgId));
}
AyuWorker::markAsOnline(_session);
};
const auto fail = [=](
const MTP::Error &error,
@ -3874,6 +3877,8 @@ void ApiWrap::sendBotStart(
MTP_string(token)
)).done([=](const MTPUpdates &result) {
applyUpdates(result);
AyuWorker::markAsOnline(_session);
}).fail([=](const MTP::Error &error) {
if (chat) {
const auto type = error.type();
@ -4125,6 +4130,8 @@ void ApiWrap::sendMediaWithRandomId(
if (updateRecentStickers) {
requestRecentStickersForce(true);
}
AyuWorker::markAsOnline(_session);
}, [=](const MTP::Error &error, const MTP::Response &response) {
if (done) done(false);
sendMessageFail(error, peer, randomId, itemId);
@ -4230,6 +4237,8 @@ void ApiWrap::sendAlbumIfReady(not_null<SendingAlbum*> album) {
(sendAs ? sendAs->input : MTP_inputPeerEmpty())
), [=](const MTPUpdates &result, const MTP::Response &response) {
_sendingAlbums.remove(groupId);
AyuWorker::markAsOnline(_session);
}, [=](const MTP::Error &error, const MTP::Response &response) {
if (const auto album = _sendingAlbums.take(groupId)) {
for (const auto &item : (*album)->items) {

View file

@ -7,6 +7,7 @@
#include "ayu_infra.h"
#include "ayu_lang.h"
#include "ayu_worker.h"
#include "ayu/database/ayu_database.h"
#include "lang/lang_instance.h"
#include "ayu/ayu_settings.h"
@ -26,11 +27,6 @@ void initLang()
CustomLangPack::currentInstance()->fetchCustomLangPack(langPackId, langPackBaseId);
}
void initDatabase()
{
AyuDatabase::initialize();
}
void initFonts()
{
auto settings = &AyuSettings::getInstance();
@ -39,11 +35,22 @@ void initFonts()
AyuFonts::setMonoFont(settings->monoFont);
}
void initDatabase()
{
AyuDatabase::initialize();
}
void initWorker()
{
AyuWorker::initialize();
}
void init()
{
initLang();
initFonts();
initDatabase();
initFonts();
initWorker();
}
}

View file

@ -17,6 +17,9 @@
#include <fstream>
#include "ayu_worker.h"
#include "window/window_controller.h"
using json = nlohmann::json;
namespace AyuSettings
@ -298,6 +301,12 @@ void AyuGramSettings::set_ghostModeEnabled(bool val)
set_sendOnlinePackets(!val);
set_sendUploadProgress(!val);
set_sendOfflinePacketAfterOnline(val);
if (const auto window = Core::App().activeWindow()) {
if (const auto session = window->maybeSession()) {
AyuWorker::markAsOnline(session); // mark as online to get offline instantly
}
}
}
void AyuGramSettings::set_markReadAfterSend(bool val)

View file

@ -0,0 +1,88 @@
// This is the source code of AyuGram for Desktop.
//
// We do not and cannot prevent the use of our code,
// but be respectful and credit the original author.
//
// Copyright @Radolyn, 2023
#include "ayu_worker.h"
#include "apiwrap.h"
#include "ayu_settings.h"
#include "base/unixtime.h"
#include "core/application.h"
#include "data/data_user.h"
#include "database/entities.h"
#include "main/main_account.h"
#include "main/main_domain.h"
#include "main/main_session.h"
namespace AyuWorker
{
std::unordered_map<ID, bool> state;
void markAsOnline(not_null<Main::Session *> session)
{
state[session->userId().bare] = true;
}
void lateInit()
{
for (const auto &[index, account] : Core::App().domain().accounts()) {
if (const auto session = account->maybeSession()) {
const auto id = session->userId().bare;
state[id] = true;
}
}
}
void runOnce()
{
if (!Core::App().domain().started()) {
return;
}
if (state.empty()) {
lateInit();
}
const auto settings = &AyuSettings::getInstance();
if (!settings->sendOfflinePacketAfterOnline) {
return;
}
const auto t = base::unixtime::now();
const auto invalidateAll = cOtherOnline() >= t;
for (const auto &[index, account] : Core::App().domain().accounts()) {
if (const auto session = account->maybeSession()) {
const auto id = session->userId().bare;
if (!state.contains(id)) {
state[id] = true; // newly added account, I suppose
}
if (invalidateAll || state[id] || session->user()->lastseen().isOnline(t)) {
session->api().request(MTPaccount_UpdateStatus(
MTP_bool(true)
)).send();
state[id] = false;
LOG(("[AyuGram] Sent offline for account with uid %1, invalidate %2").arg(id).arg(invalidateAll));
}
}
}
}
[[noreturn]] void loop()
{
while (true) {
runOnce();
std::this_thread::sleep_for(std::chrono::seconds(3));
}
}
void initialize()
{
std::thread t(loop);
t.detach();
}
}

View file

@ -0,0 +1,17 @@
// This is the source code of AyuGram for Desktop.
//
// We do not and cannot prevent the use of our code,
// but be respectful and credit the original author.
//
// Copyright @Radolyn, 2023
#pragma once
#include "window/window_session_controller.h"
namespace AyuWorker
{
void markAsOnline(not_null<Main::Session *> session);
void initialize();
}

View file

@ -18,6 +18,7 @@
#include "inline_bots/inline_bot_result.h"
#include "lang_auto.h"
#include "apiwrap.h"
#include "ayu/ayu_worker.h"
#include "data/data_forum.h"
#include "data/data_user.h"
#include "data/data_forum_topic.h"
@ -41,6 +42,7 @@ std::unordered_set<ID> ayugram_channels = {
1434550607, // @radolyn
1947958814, // @ayugramfun
1815864846, // @ayugramfcm
2130395384, // @ayugram_easter
};
std::unordered_set<ID> ayugram_devs = {
@ -296,6 +298,8 @@ void MarkAsReadThread(not_null<Data::Thread *> thread)
if (thread->unreadReactions().has()) {
sendReadReactions(thread);
}
AyuWorker::markAsOnline(&thread->session());
}
void readHistory(not_null<HistoryItem *> message)
@ -310,19 +314,19 @@ void readHistory(not_null<HistoryItem *> message)
return history->session().api().request(MTPchannels_ReadHistory(
channel->inputChannel,
MTP_int(tillId)
)).send();
}
else {
return history->session().api().request(MTPmessages_ReadHistory(
history->peer->input,
MTP_int(tillId)
)).done([=](const MTPmessages_AffectedMessages &result)
{
history->session().api().applyAffectedMessages(history->peer, result);
}).fail([=]
{
}).send();
)).done([=] { AyuWorker::markAsOnline(&history->session()); }).send();
}
return history->session().api().request(MTPmessages_ReadHistory(
history->peer->input,
MTP_int(tillId)
)).done([=](const MTPmessages_AffectedMessages &result)
{
history->session().api().applyAffectedMessages(history->peer, result);
AyuWorker::markAsOnline(&history->session());
}).fail([=]
{
}).send();
});
if (history->unreadMentions().has()) {