Check fields before uploading theme.

This commit is contained in:
John Preston 2019-09-06 15:21:33 +03:00
parent 9c86f0e0a5
commit 1cda90c3c5
1 changed files with 48 additions and 19 deletions

View File

@ -571,6 +571,7 @@ Fn<void()> SavePreparedTheme(
session->uploader().uploadMedia(state->id, media); session->uploader().uploadMedia(state->id, media);
}; };
const auto save = [=] {
state->generating = true; state->generating = true;
crl::async([=] { crl::async([=] {
crl::on_main([=, ready = PrepareTheme(palette, background)]{ crl::on_main([=, ready = PrepareTheme(palette, background)]{
@ -581,15 +582,35 @@ Fn<void()> SavePreparedTheme(
uploadFile(ready); uploadFile(ready);
}); });
}); });
};
const auto checkFields = [=] {
state->requestId = api->request(MTPaccount_CreateTheme(
MTP_string(fields.slug),
MTP_string(fields.title),
MTP_inputDocumentEmpty()
)).done([=](const MTPTheme &result) {
save();
}).fail([=](const RPCError &error) {
if (error.type() == qstr("THEME_FILE_INVALID")) {
save();
} else {
fail(SaveErrorType::Other, error.type());
}
}).send();
};
if (creating) {
checkFields();
} else {
save();
}
return [=] { return [=] {
if (state->generating) {
state->generating = false; state->generating = false;
} else {
api->request(base::take(state->requestId)).cancel(); api->request(base::take(state->requestId)).cancel();
session->uploader().cancel(state->id); session->uploader().cancel(state->id);
state->lifetime.destroy(); state->lifetime.destroy();
}
}; };
} }
@ -797,10 +818,18 @@ void SaveThemeBox(
}); });
const auto fail = crl::guard(box, [=]( const auto fail = crl::guard(box, [=](
SaveErrorType type, SaveErrorType type,
const QString &text) { const QString &error) {
*saving = false; *saving = false;
if (!text.isEmpty()) { if (error == qstr("THEME_TITLE_INVALID")) {
Ui::Toast::Show(text); type = SaveErrorType::Name;
} else if (error == qstr("THEME_SLUG_INVALID")) {
type = SaveErrorType::Link;
} else if (error == qstr("THEME_SLUG_OCCUPIED")) {
Ui::Toast::Show(
tr::lng_create_channel_link_occupied(tr::now));
type = SaveErrorType::Link;
} else {
Ui::Toast::Show(error);
} }
if (type == SaveErrorType::Name) { if (type == SaveErrorType::Name) {
name->showError(); name->showError();