2
0
mirror of https://github.com/telegramdesktop/tdesktop synced 2025-08-31 06:26:18 +00:00

Update API scheme on layer 143 + transcribe.

This commit is contained in:
John Preston
2022-05-24 19:38:46 +04:00
parent 3fac522bbc
commit 0b2a5a22ba
14 changed files with 421 additions and 29 deletions

View File

@@ -0,0 +1,97 @@
/*
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 "api/api_transcribes.h"
#include "history/history_item.h"
#include "history/history.h"
#include "main/main_session.h"
#include "data/data_session.h"
#include "data/data_peer.h"
#include "apiwrap.h"
namespace Api {
Transcribes::Transcribes(not_null<ApiWrap*> api)
: _session(&api->session())
, _api(&api->instance()) {
}
void Transcribes::toggle(not_null<HistoryItem*> item) {
const auto id = item->fullId();
auto i = _map.find(id);
if (i == _map.end()) {
load(item);
//_session->data().requestItemRepaint(item);
_session->data().requestItemResize(item);
} else if (!i->second.requestId) {
i->second.shown = !i->second.shown;
_session->data().requestItemResize(item);
}
}
const Transcribes::Entry &Transcribes::entry(
not_null<HistoryItem*> item) const {
static auto empty = Entry();
const auto i = _map.find(item->fullId());
return (i != _map.end()) ? i->second : empty;
}
void Transcribes::apply(const MTPDupdateTranscribeAudio &update) {
const auto id = update.vtranscription_id().v;
const auto i = _ids.find(id);
if (i == _ids.end()) {
return;
}
const auto j = _map.find(i->second);
if (j == _map.end()) {
return;
}
const auto text = qs(update.vtext());
j->second.result = text;
j->second.pending = !update.is_final();
if (const auto item = _session->data().message(i->second)) {
_session->data().requestItemResize(item);
}
}
void Transcribes::load(not_null<HistoryItem*> item) {
if (!item->isHistoryEntry() || item->isLocal()) {
return;
}
const auto id = item->fullId();
const auto requestId = _api.request(MTPmessages_TranscribeAudio(
item->history()->peer->input,
MTP_int(item->id)
)).done([=](const MTPmessages_TranscribedAudio &result) {
result.match([&](const MTPDmessages_transcribedAudio &data) {
auto &entry = _map[id];
entry.requestId = 0;
entry.pending = data.is_pending();
entry.result = qs(data.vtext());
_ids.emplace(data.vtranscription_id().v, id);
if (const auto item = _session->data().message(id)) {
_session->data().requestItemResize(item);
}
});
}).fail([=] {
auto &entry = _map[id];
entry.requestId = 0;
entry.pending = false;
entry.failed = true;
if (const auto item = _session->data().message(id)) {
_session->data().requestItemResize(item);
}
}).send();
auto &entry = _map.emplace(id).first->second;
entry.requestId = requestId;
entry.shown = true;
entry.failed = false;
entry.pending = false;
}
} // namespace Api

View File

@@ -0,0 +1,48 @@
/*
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
*/
#pragma once
#include "mtproto/sender.h"
class ApiWrap;
namespace Main {
class Session;
} // namespace Main
namespace Api {
class Transcribes final {
public:
explicit Transcribes(not_null<ApiWrap*> api);
struct Entry {
QString result;
bool shown = false;
bool failed = false;
bool pending = false;
mtpRequestId requestId = 0;
};
void toggle(not_null<HistoryItem*> item);
[[nodiscard]] const Entry &entry(not_null<HistoryItem*> item) const;
void apply(const MTPDupdateTranscribeAudio &update);
private:
void load(not_null<HistoryItem*> item);
const not_null<Main::Session*> _session;
MTP::Sender _api;
base::flat_map<FullMsgId, Entry> _map;
base::flat_map<uint64, FullMsgId> _ids;
};
} // namespace Api

View File

@@ -13,6 +13,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "api/api_text_entities.h"
#include "api/api_user_privacy.h"
#include "api/api_unread_things.h"
#include "api/api_transcribes.h"
#include "main/main_session.h"
#include "main/main_account.h"
#include "mtproto/mtp_instance.h"
@@ -2387,6 +2388,11 @@ void Updates::feedUpdate(const MTPUpdate &update) {
session().api().ringtones().applyUpdate();
} break;
case mtpc_updateTranscribeAudio: {
const auto &data = update.c_updateTranscribeAudio();
_session->api().transcribes().apply(data);
}
}
}