2020-07-06 09:58:18 +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 "api/api_send_progress.h"
|
|
|
|
|
|
|
|
#include "main/main_session.h"
|
|
|
|
#include "history/history.h"
|
|
|
|
#include "data/data_peer.h"
|
2020-10-23 14:28:11 +00:00
|
|
|
#include "data/data_user.h"
|
|
|
|
#include "base/unixtime.h"
|
|
|
|
#include "data/data_peer_values.h"
|
2020-07-06 09:58:18 +00:00
|
|
|
#include "apiwrap.h"
|
|
|
|
|
|
|
|
namespace Api {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr auto kCancelTypingActionTimeout = crl::time(5000);
|
2020-09-18 11:50:47 +00:00
|
|
|
constexpr auto kSetMyActionForMs = 10 * crl::time(1000);
|
2020-10-23 15:25:55 +00:00
|
|
|
constexpr auto kSendTypingsToOfflineFor = TimeId(30);
|
2020-07-06 09:58:18 +00:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
SendProgressManager::SendProgressManager(not_null<Main::Session*> session)
|
|
|
|
: _session(session)
|
|
|
|
, _stopTypingTimer([=] { cancelTyping(base::take(_stopTypingHistory)); }) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void SendProgressManager::cancel(
|
|
|
|
not_null<History*> history,
|
|
|
|
SendProgressType type) {
|
2020-09-18 11:50:47 +00:00
|
|
|
cancel(history, 0, type);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SendProgressManager::cancel(
|
|
|
|
not_null<History*> history,
|
|
|
|
MsgId topMsgId,
|
|
|
|
SendProgressType type) {
|
|
|
|
const auto i = _requests.find(Key{ history, topMsgId, type });
|
2020-07-06 09:58:18 +00:00
|
|
|
if (i != _requests.end()) {
|
|
|
|
_session->api().request(i->second).cancel();
|
|
|
|
_requests.erase(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SendProgressManager::cancelTyping(not_null<History*> history) {
|
|
|
|
_stopTypingTimer.cancel();
|
|
|
|
cancel(history, SendProgressType::Typing);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SendProgressManager::update(
|
|
|
|
not_null<History*> history,
|
|
|
|
SendProgressType type,
|
2020-09-18 11:50:47 +00:00
|
|
|
int progress) {
|
|
|
|
update(history, 0, type, progress);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SendProgressManager::update(
|
|
|
|
not_null<History*> history,
|
|
|
|
MsgId topMsgId,
|
|
|
|
SendProgressType type,
|
|
|
|
int progress) {
|
2020-07-06 09:58:18 +00:00
|
|
|
const auto peer = history->peer;
|
|
|
|
if (peer->isSelf() || (peer->isChannel() && !peer->isMegagroup())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto doing = (progress >= 0);
|
2020-09-18 11:50:47 +00:00
|
|
|
const auto key = Key{ history, topMsgId, type };
|
|
|
|
if (updated(key, doing)) {
|
|
|
|
cancel(history, topMsgId, type);
|
2020-07-06 09:58:18 +00:00
|
|
|
if (doing) {
|
2020-09-18 11:50:47 +00:00
|
|
|
send(key, progress);
|
2020-07-06 09:58:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-18 11:50:47 +00:00
|
|
|
bool SendProgressManager::updated(const Key &key, bool doing) {
|
|
|
|
const auto now = crl::now();
|
|
|
|
const auto i = _updated.find(key);
|
|
|
|
if (doing) {
|
|
|
|
if (i == end(_updated)) {
|
|
|
|
_updated.emplace(key, now + kSetMyActionForMs);
|
|
|
|
} else if (i->second > now + (kSetMyActionForMs / 2)) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
i->second = now + kSetMyActionForMs;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (i == end(_updated)) {
|
|
|
|
return false;
|
|
|
|
} else if (i->second <= now) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
_updated.erase(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SendProgressManager::send(const Key &key, int progress) {
|
2020-10-23 14:28:11 +00:00
|
|
|
if (skipRequest(key)) {
|
|
|
|
return;
|
|
|
|
}
|
2020-07-06 09:58:18 +00:00
|
|
|
using Type = SendProgressType;
|
2020-07-14 16:23:14 +00:00
|
|
|
const auto action = [&]() -> MTPsendMessageAction {
|
|
|
|
const auto p = MTP_int(progress);
|
2020-09-18 11:50:47 +00:00
|
|
|
switch (key.type) {
|
2020-07-14 16:23:14 +00:00
|
|
|
case Type::Typing: return MTP_sendMessageTypingAction();
|
|
|
|
case Type::RecordVideo: return MTP_sendMessageRecordVideoAction();
|
|
|
|
case Type::UploadVideo: return MTP_sendMessageUploadVideoAction(p);
|
|
|
|
case Type::RecordVoice: return MTP_sendMessageRecordAudioAction();
|
|
|
|
case Type::UploadVoice: return MTP_sendMessageUploadAudioAction(p);
|
|
|
|
case Type::RecordRound: return MTP_sendMessageRecordRoundAction();
|
|
|
|
case Type::UploadRound: return MTP_sendMessageUploadRoundAction(p);
|
|
|
|
case Type::UploadPhoto: return MTP_sendMessageUploadPhotoAction(p);
|
|
|
|
case Type::UploadFile: return MTP_sendMessageUploadDocumentAction(p);
|
|
|
|
case Type::ChooseLocation: return MTP_sendMessageGeoLocationAction();
|
|
|
|
case Type::ChooseContact: return MTP_sendMessageChooseContactAction();
|
|
|
|
case Type::PlayGame: return MTP_sendMessageGamePlayAction();
|
|
|
|
default: return MTP_sendMessageTypingAction();
|
|
|
|
}
|
|
|
|
}();
|
2020-07-06 09:58:18 +00:00
|
|
|
const auto requestId = _session->api().request(MTPmessages_SetTyping(
|
2020-09-18 11:50:47 +00:00
|
|
|
MTP_flags(key.topMsgId
|
|
|
|
? MTPmessages_SetTyping::Flag::f_top_msg_id
|
|
|
|
: MTPmessages_SetTyping::Flag(0)),
|
|
|
|
key.history->peer->input,
|
|
|
|
MTP_int(key.topMsgId),
|
2020-07-06 09:58:18 +00:00
|
|
|
action
|
|
|
|
)).done([=](const MTPBool &result, mtpRequestId requestId) {
|
|
|
|
done(result, requestId);
|
|
|
|
}).send();
|
2020-09-18 11:50:47 +00:00
|
|
|
_requests.emplace(key, requestId);
|
2020-07-06 09:58:18 +00:00
|
|
|
|
2020-09-18 11:50:47 +00:00
|
|
|
if (key.type == Type::Typing) {
|
|
|
|
_stopTypingHistory = key.history;
|
2020-07-06 09:58:18 +00:00
|
|
|
_stopTypingTimer.callOnce(kCancelTypingActionTimeout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-23 14:28:11 +00:00
|
|
|
bool SendProgressManager::skipRequest(const Key &key) const {
|
|
|
|
const auto user = key.history->peer->asUser();
|
|
|
|
if (!user) {
|
|
|
|
return false;
|
|
|
|
} else if (user->isSelf()) {
|
|
|
|
return true;
|
|
|
|
} else if (user->isBot() && !user->isSupport()) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-10-23 15:25:55 +00:00
|
|
|
const auto recently = base::unixtime::now() - kSendTypingsToOfflineFor;
|
|
|
|
return !Data::OnlineTextActive(user->onlineTill, recently);
|
2020-10-23 14:28:11 +00:00
|
|
|
}
|
|
|
|
|
2020-07-06 09:58:18 +00:00
|
|
|
void SendProgressManager::done(
|
|
|
|
const MTPBool &result,
|
|
|
|
mtpRequestId requestId) {
|
|
|
|
for (auto i = _requests.begin(), e = _requests.end(); i != e; ++i) {
|
|
|
|
if (i->second == requestId) {
|
|
|
|
_requests.erase(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Api
|