Handle t.me/addtheme links.

This commit is contained in:
John Preston 2019-09-06 17:24:22 +03:00
parent e38123cc48
commit 469c6770fb
4 changed files with 61 additions and 12 deletions

View File

@ -413,7 +413,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
"lng_theme_reverting#other" = "Reverting to the old theme in {count} seconds.";
"lng_theme_keep_changes" = "Keep changes";
"lng_theme_revert" = "Revert";
"lng_theme_no_desktop_version" = "Sorry, this theme doesn't include a version for Telegram Desktop.\n\n(Also, Telegram Desktop doesn't support cloud themes yet.)";
"lng_theme_no_desktop" = "Sorry, this theme doesn't include a version for Telegram Desktop.";
"lng_background_header" = "Background preview";
"lng_background_text1" = "Ah, you kids today with techno music! You should enjoy the classics, like Hasselhoff!";
"lng_background_text2" = "I can't even take you seriously right now.";

View File

@ -22,6 +22,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "passport/passport_form_controller.h"
#include "window/window_session_controller.h"
#include "data/data_session.h"
#include "data/data_cloud_themes.h"
#include "data/data_channel.h"
#include "mainwindow.h"
#include "mainwidget.h"
@ -85,8 +86,11 @@ bool ShowTheme(
if (!session) {
return false;
}
const auto clickFromMessageId = context.value<FullMsgId>();
Core::App().hideMediaView();
Ui::show(Box<InformBox>(tr::lng_theme_no_desktop_version(tr::now)));
session->data().cloudThemes().resolve(
match->captured(1),
clickFromMessageId);
return true;
}

View File

@ -9,11 +9,16 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "window/themes/window_theme.h"
#include "window/themes/window_theme_preview.h"
#include "window/themes/window_theme_editor_box.h"
#include "window/window_controller.h"
#include "data/data_session.h"
#include "data/data_document.h"
#include "data/data_file_origin.h"
#include "main/main_session.h"
#include "boxes/confirm_box.h"
#include "lang/lang_keys.h"
#include "apiwrap.h"
#include "mainwindow.h"
namespace Data {
namespace {
@ -121,19 +126,55 @@ void CloudThemes::applyUpdate(const MTPTheme &theme) {
const auto cloud = CloudTheme::Parse(_session, data);
const auto &object = Window::Theme::Background()->themeObject();
if ((cloud.id != object.cloud.id)
|| (cloud.documentId == object.cloud.documentId)) {
|| (cloud.documentId == object.cloud.documentId)
|| !cloud.documentId) {
return;
}
if (const auto updated = data.vdocument()) {
updateFromDocument(
cloud,
_session->data().processDocument(*updated));
}
updateFromDocument(
cloud,
_session->data().document(cloud.documentId));
}, [&](const MTPDthemeDocumentNotModified &data) {
});
scheduleReload();
}
void CloudThemes::resolve(
const QString &slug,
const FullMsgId &clickFromMessageId) {
_session->api().request(_resolveRequestId).cancel();
_resolveRequestId = _session->api().request(MTPaccount_GetTheme(
MTP_string(Format()),
MTP_inputThemeSlug(MTP_string(slug)),
MTP_long(0)
)).done([=](const MTPTheme &result) {
result.match([&](const MTPDtheme &data) {
const auto cloud = CloudTheme::Parse(_session, data);
if (cloud.documentId) {
const auto document = _session->data().document(
cloud.documentId);
DocumentOpenClickHandler::Open(
Data::FileOrigin(),
document,
_session->data().message(clickFromMessageId));
} else if (cloud.createdBy == _session->userId()) {
Ui::show(Box(
Window::Theme::CreateForExistingBox,
&App::wnd()->controller(),
cloud));
} else {
Ui::show(Box<InformBox>(
tr::lng_theme_no_desktop(tr::now)));
}
}, [&](const MTPDthemeDocumentNotModified &data) {
});
}).fail([=](const RPCError &error) {
if (error.type() == qstr("THEME_FORMAT_INVALID")) {
Ui::show(Box<InformBox>(
tr::lng_theme_no_desktop(tr::now)));
}
}).send();
}
void CloudThemes::updateFromDocument(
const CloudTheme &cloud,
not_null<DocumentData*> document) {
@ -169,13 +210,14 @@ void CloudThemes::scheduleReload() {
}
void CloudThemes::refresh() {
if (_requestId) {
if (_refreshRquestId) {
return;
}
_requestId = _session->api().request(MTPaccount_GetThemes(
_refreshRquestId = _session->api().request(MTPaccount_GetThemes(
MTP_string(Format()),
MTP_int(_hash)
)).done([=](const MTPaccount_Themes &result) {
_refreshRquestId = 0;
result.match([&](const MTPDaccount_themes &data) {
_hash = data.vhash().v;
parseThemes(data.vthemes().v);
@ -183,7 +225,7 @@ void CloudThemes::refresh() {
}, [](const MTPDaccount_themesNotModified &) {
});
}).fail([=](const RPCError &error) {
_requestId = 0;
_refreshRquestId = 0;
}).send();
}

View File

@ -43,6 +43,8 @@ public:
void applyUpdate(const MTPTheme &theme);
void resolve(const QString &slug, const FullMsgId &clickFromMessageId);
private:
void parseThemes(const QVector<MTPTheme> &list);
@ -57,7 +59,8 @@ private:
const not_null<Main::Session*> _session;
int32 _hash = 0;
mtpRequestId _requestId = 0;
mtpRequestId _refreshRquestId = 0;
mtpRequestId _resolveRequestId = 0;
std::vector<CloudTheme> _list;
rpl::event_stream<> _updates;