2
0
mirror of https://github.com/kotatogram/kotatogram-desktop synced 2025-08-31 14:45:14 +00:00

Move shareContact and readServerHistory to ApiWrap.

Also allow non-confirming contact info sharing to Saved Messages.
This commit is contained in:
John Preston
2017-12-07 17:02:24 +04:00
parent f0a03223e8
commit 5bc47e5203
15 changed files with 551 additions and 431 deletions

View File

@@ -31,7 +31,8 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
#include "mainwindow.h"
#include "messenger.h"
#include "mainwidget.h"
#include "history/history_widget.h"
#include "boxes/add_contact_box.h"
#include "history/history_message.h"
#include "storage/localstorage.h"
#include "auth_session.h"
#include "boxes/confirm_box.h"
@@ -171,7 +172,7 @@ void ApiWrap::resolveMessageDatas() {
gotMessageDatas(nullptr, result, requestId);
}).fail([this](const RPCError &error, mtpRequestId requestId) {
finalizeMessageDataRequest(nullptr, requestId);
}).after(kSmallDelayMs).send();
}).afterDelay(kSmallDelayMs).send();
for (auto &request : _messageDataRequests) {
if (request.requestId > 0) continue;
request.requestId = requestId;
@@ -192,7 +193,7 @@ void ApiWrap::resolveMessageDatas() {
gotMessageDatas(channel, result, requestId);
}).fail([=](const RPCError &error, mtpRequestId requestId) {
finalizeMessageDataRequest(channel, requestId);
}).after(kSmallDelayMs).send();
}).afterDelay(kSmallDelayMs).send();
for (auto &request : *j) {
if (request.requestId > 0) continue;
@@ -839,7 +840,7 @@ void ApiWrap::requestSelfParticipant(ChannelData *channel) {
if (error.type() == qstr("USER_NOT_PARTICIPANT")) {
channel->inviter = -1;
}
}).after(kSmallDelayMs).send();
}).afterDelay(kSmallDelayMs).send();
_selfParticipantRequests.insert(channel, requestId);
}
@@ -933,7 +934,7 @@ void ApiWrap::requestStickerSets() {
gotStickerSet(setId, result);
}).fail([this, setId = i.key()](const RPCError &error) {
_stickerSetRequests.remove(setId);
}).after(waitMs).send();
}).afterDelay(waitMs).send();
}
}
@@ -987,7 +988,7 @@ void ApiWrap::saveStickerSets(const Stickers::Order &localOrder, const Stickers:
stickerSetDisenabled(requestId);
}).fail([this](const RPCError &error, mtpRequestId requestId) {
stickerSetDisenabled(requestId);
}).after(kSmallDelayMs).send();
}).afterDelay(kSmallDelayMs).send();
_stickerSetDisenableRequests.insert(requestId);
@@ -1024,7 +1025,7 @@ void ApiWrap::saveStickerSets(const Stickers::Order &localOrder, const Stickers:
stickerSetDisenabled(requestId);
}).fail([this](const RPCError &error, mtpRequestId requestId) {
stickerSetDisenabled(requestId);
}).after(kSmallDelayMs).send();
}).afterDelay(kSmallDelayMs).send();
_stickerSetDisenableRequests.insert(requestId);
@@ -1336,13 +1337,30 @@ void ApiWrap::clearHistory(not_null<PeerData*> peer) {
int ApiWrap::applyAffectedHistory(
not_null<PeerData*> peer,
const MTPmessages_AffectedHistory &result) {
auto &d = result.c_messages_affectedHistory();
if (peer && peer->isChannel()) {
peer->asChannel()->ptsUpdateAndApply(d.vpts.v, d.vpts_count.v);
const auto &data = result.c_messages_affectedHistory();
if (const auto channel = peer->asChannel()) {
channel->ptsUpdateAndApply(data.vpts.v, data.vpts_count.v);
} else {
App::main()->ptsUpdateAndApply(d.vpts.v, d.vpts_count.v);
App::main()->ptsUpdateAndApply(data.vpts.v, data.vpts_count.v);
}
return d.voffset.v;
return data.voffset.v;
}
void ApiWrap::applyAffectedMessages(
not_null<PeerData*> peer,
const MTPmessages_AffectedMessages &result) {
const auto &data = result.c_messages_affectedMessages();
if (const auto channel = peer->asChannel()) {
channel->ptsUpdateAndApply(data.vpts.v, data.vpts_count.v);
} else {
applyAffectedMessages(result);
}
}
void ApiWrap::applyAffectedMessages(
const MTPmessages_AffectedMessages &result) {
const auto &data = result.c_messages_affectedMessages();
App::main()->ptsUpdateAndApply(data.vpts.v, data.vpts_count.v);
}
void ApiWrap::saveDraftsToCloud() {
@@ -1522,13 +1540,13 @@ void ApiWrap::resolveWebPages() {
if (!ids.isEmpty()) {
requestId = request(MTPmessages_GetMessages(MTP_vector<MTPint>(ids))).done([this](const MTPmessages_Messages &result, mtpRequestId requestId) {
gotWebPages(nullptr, result, requestId);
}).after(kSmallDelayMs).send();
}).afterDelay(kSmallDelayMs).send();
}
QVector<mtpRequestId> reqsByIndex(idsByChannel.size(), 0);
for (auto i = idsByChannel.cbegin(), e = idsByChannel.cend(); i != e; ++i) {
reqsByIndex[i.value().first] = request(MTPchannels_GetMessages(i.key()->inputChannel, MTP_vector<MTPint>(i.value().second))).done([this, channel = i.key()](const MTPmessages_Messages &result, mtpRequestId requestId) {
gotWebPages(channel, result, requestId);
}).after(kSmallDelayMs).send();
}).afterDelay(kSmallDelayMs).send();
}
if (requestId || !reqsByIndex.isEmpty()) {
for (auto &pendingRequestId : _webPagesPending) {
@@ -2242,7 +2260,7 @@ void ApiWrap::sendSaveChatAdminsRequests(not_null<ChatData*> chat) {
if (error.type() == qstr("USER_RESTRICTED")) {
Ui::show(Box<InformBox>(lang(lng_cant_do_this)));
}
}).canWait(5).send();
}).afterDelay(kSmallDelayMs).send();
_chatAdminsSaveRequests[chat].insert(requestId);
};
@@ -2388,6 +2406,12 @@ void ApiWrap::userPhotosDone(
));
}
void ApiWrap::sendAction(const SendOptions &options) {
readServerHistory(options.history);
options.history->getReadyFor(ShowAtTheEndMsgId);
_sendActions.fire_copy(options);
}
void ApiWrap::forwardMessages(
HistoryItemsList &&items,
const SendOptions &options,
@@ -2409,7 +2433,7 @@ void ApiWrap::forwardMessages(
const auto genClientSideMessage = options.generateLocal && (count < 2);
const auto history = options.history;
App::main()->readServerHistory(history);
readServerHistory(history);
const auto channelPost = history->peer->isChannel()
&& !history->peer->isMegagroup();
@@ -2450,7 +2474,7 @@ void ApiWrap::forwardMessages(
if (shared && !--shared->requestsLeft) {
shared->callback();
}
}).after(
}).afterRequest(
history->sendRequestId
).send();
@@ -2494,4 +2518,187 @@ void ApiWrap::forwardMessages(
sendAccumulated();
}
void ApiWrap::shareContact(
const QString &phone,
const QString &firstName,
const QString &lastName,
const SendOptions &options) {
const auto userId = UserId(0);
sendSharedContact(phone, firstName, lastName, userId, options);
}
void ApiWrap::shareContact(
not_null<UserData*> user,
const SendOptions &options) {
const auto userId = peerToUser(user->id);
const auto phone = user->phone().isEmpty()
? App::phoneFromSharedContact(userId)
: user->phone();
if (phone.isEmpty()) {
return;
}
sendSharedContact(
phone,
user->firstName,
user->lastName,
userId,
options);
}
void ApiWrap::sendSharedContact(
const QString &phone,
const QString &firstName,
const QString &lastName,
UserId userId,
const SendOptions &options) {
sendAction(options);
const auto history = options.history;
const auto peer = history->peer;
const auto randomId = rand_value<uint64>();
const auto newId = FullMsgId(history->channelId(), clientMsgId());
const auto channelPost = peer->isChannel() && !peer->isMegagroup();
const auto silentPost = channelPost && options.silent;
auto flags = NewMessageFlags(peer) | MTPDmessage::Flag::f_media;
auto sendFlags = MTPmessages_SendMedia::Flags(0);
if (options.replyTo) {
flags |= MTPDmessage::Flag::f_reply_to_msg_id;
sendFlags |= MTPmessages_SendMedia::Flag::f_reply_to_msg_id;
}
if (channelPost) {
flags |= MTPDmessage::Flag::f_views;
flags |= MTPDmessage::Flag::f_post;
if (peer->asChannel()->addsSignature()) {
flags |= MTPDmessage::Flag::f_post_author;
}
} else {
flags |= MTPDmessage::Flag::f_from_id;
}
if (silentPost) {
sendFlags |= MTPmessages_SendMedia::Flag::f_silent;
}
const auto messageFromId = channelPost ? 0 : Auth().userId();
const auto messagePostAuthor = channelPost
? (Auth().user()->firstName + ' ' + Auth().user()->lastName)
: QString();
history->addNewMessage(
MTP_message(
MTP_flags(flags),
MTP_int(newId.msg),
MTP_int(messageFromId),
peerToMTP(peer->id),
MTPnullFwdHeader,
MTPint(),
MTP_int(options.replyTo),
MTP_int(unixtime()),
MTP_string(""),
MTP_messageMediaContact(
MTP_string(phone),
MTP_string(firstName),
MTP_string(lastName),
MTP_int(userId)),
MTPnullMarkup,
MTPnullEntities,
MTP_int(1),
MTPint(),
MTP_string(messagePostAuthor),
MTPlong()),
NewMessageUnread);
history->sendRequestId = request(MTPmessages_SendMedia(
MTP_flags(sendFlags),
peer->input,
MTP_int(options.replyTo),
MTP_inputMediaContact(
MTP_string(phone),
MTP_string(firstName),
MTP_string(lastName)),
MTP_long(randomId),
MTPnullMarkup
)).done([=](const MTPUpdates &result) {
applyUpdates(result);
}).fail([](const RPCError &error) {
if (error.type() == qstr("PEER_FLOOD")) {
Ui::show(Box<InformBox>(
PeerFloodErrorText(PeerFloodType::Send)));
} else if (error.type() == qstr("USER_BANNED_IN_CHANNEL")) {
const auto link = textcmdLink(
Messenger::Instance().createInternalLinkFull(qsl("spambot")),
lang(lng_cant_more_info));
Ui::show(Box<InformBox>(lng_error_public_groups_denied(
lt_more_info,
link)));
}
}).afterRequest(
history->sendRequestId
).send();
App::historyRegRandom(randomId, newId);
}
void ApiWrap::readServerHistory(not_null<History*> history) {
if (history->unreadCount()) {
readServerHistoryForce(history);
}
}
void ApiWrap::readServerHistoryForce(not_null<History*> history) {
const auto peer = history->peer;
const auto upTo = history->inboxRead(0);
if (const auto channel = peer->asChannel()) {
if (!channel->amIn()) {
return; // no read request for channels that I didn't join
} else if (const auto migrateFrom = channel->migrateFrom()) {
if (const auto migrated = App::historyLoaded(migrateFrom)) {
readServerHistory(migrated);
}
}
}
if (_readRequests.contains(peer)) {
const auto i = _readRequestsPending.find(peer);
if (i == _readRequestsPending.cend()) {
_readRequestsPending.emplace(peer, upTo);
} else if (i->second < upTo) {
i->second = upTo;
}
} else {
sendReadRequest(peer, upTo);
}
}
void ApiWrap::sendReadRequest(not_null<PeerData*> peer, MsgId upTo) {
const auto requestId = [&] {
const auto finished = [=] {
_readRequests.remove(peer);
if (const auto next = _readRequestsPending.take(peer)) {
sendReadRequest(peer, *next);
}
};
if (const auto channel = peer->asChannel()) {
return request(MTPchannels_ReadHistory(
channel->inputChannel,
MTP_int(upTo)
)).done([=](const MTPBool &result) {
finished();
}).fail([=](const RPCError &error) {
finished();
}).send();
}
return request(MTPmessages_ReadHistory(
peer->input,
MTP_int(upTo)
)).done([=](const MTPmessages_AffectedMessages &result) {
applyAffectedMessages(peer, result);
finished();
}).fail([=](const RPCError &error) {
finished();
}).send();
}();
_readRequests.emplace(peer, requestId, upTo);
}
ApiWrap::~ApiWrap() = default;