Don't expect itemId from shareContact.

This commit is contained in:
John Preston 2023-09-05 19:28:52 +04:00
parent 191f832e52
commit 8adbbe6885
3 changed files with 46 additions and 57 deletions

View file

@ -3316,37 +3316,50 @@ void ApiWrap::forwardMessages(
_session->data().sendHistoryChangeNotifications(); _session->data().sendHistoryChangeNotifications();
} }
FullMsgId ApiWrap::shareContact( void ApiWrap::shareContact(
const QString &phone, const QString &phone,
const QString &firstName, const QString &firstName,
const QString &lastName, const QString &lastName,
const SendAction &action) { const SendAction &action,
Fn<void(bool)> done) {
const auto userId = UserId(0); const auto userId = UserId(0);
return sendSharedContact(phone, firstName, lastName, userId, action); sendSharedContact(
phone,
firstName,
lastName,
userId,
action,
std::move(done));
} }
FullMsgId ApiWrap::shareContact( void ApiWrap::shareContact(
not_null<UserData*> user, not_null<UserData*> user,
const SendAction &action) { const SendAction &action,
Fn<void(bool)> done) {
const auto userId = peerToUser(user->id); const auto userId = peerToUser(user->id);
const auto phone = _session->data().findContactPhone(user); const auto phone = _session->data().findContactPhone(user);
if (phone.isEmpty()) { if (phone.isEmpty()) {
return {}; if (done) {
done(false);
}
return;
} }
return sendSharedContact( return sendSharedContact(
phone, phone,
user->firstName, user->firstName,
user->lastName, user->lastName,
userId, userId,
action); action,
std::move(done));
} }
FullMsgId ApiWrap::sendSharedContact( void ApiWrap::sendSharedContact(
const QString &phone, const QString &phone,
const QString &firstName, const QString &firstName,
const QString &lastName, const QString &lastName,
UserId userId, UserId userId,
const SendAction &action) { const SendAction &action,
Fn<void(bool)> done) {
sendAction(action); sendAction(action);
const auto history = action.history; const auto history = action.history;
@ -3391,14 +3404,13 @@ FullMsgId ApiWrap::sendSharedContact(
MTP_string(), // vcard MTP_string(), // vcard
MTP_long(userId.bare)), MTP_long(userId.bare)),
HistoryMessageMarkupData()); HistoryMessageMarkupData());
const auto result = item->fullId();
const auto media = MTP_inputMediaContact( const auto media = MTP_inputMediaContact(
MTP_string(phone), MTP_string(phone),
MTP_string(firstName), MTP_string(firstName),
MTP_string(lastName), MTP_string(lastName),
MTP_string()); // vcard MTP_string()); // vcard
sendMedia(item, media, action.options); sendMedia(item, media, action.options, std::move(done));
_session->data().sendHistoryChangeNotifications(); _session->data().sendHistoryChangeNotifications();
_session->changes().historyUpdated( _session->changes().historyUpdated(
@ -3406,8 +3418,6 @@ FullMsgId ApiWrap::sendSharedContact(
(action.options.scheduled (action.options.scheduled
? Data::HistoryUpdate::Flag::ScheduledSent ? Data::HistoryUpdate::Flag::ScheduledSent
: Data::HistoryUpdate::Flag::MessageSent)); : Data::HistoryUpdate::Flag::MessageSent));
return result;
} }
void ApiWrap::sendVoiceMessage( void ApiWrap::sendVoiceMessage(
@ -3931,18 +3941,20 @@ void ApiWrap::uploadAlbumMedia(
void ApiWrap::sendMedia( void ApiWrap::sendMedia(
not_null<HistoryItem*> item, not_null<HistoryItem*> item,
const MTPInputMedia &media, const MTPInputMedia &media,
Api::SendOptions options) { Api::SendOptions options,
Fn<void(bool)> done) {
const auto randomId = base::RandomValue<uint64>(); const auto randomId = base::RandomValue<uint64>();
_session->data().registerMessageRandomId(randomId, item->fullId()); _session->data().registerMessageRandomId(randomId, item->fullId());
sendMediaWithRandomId(item, media, options, randomId); sendMediaWithRandomId(item, media, options, randomId, std::move(done));
} }
void ApiWrap::sendMediaWithRandomId( void ApiWrap::sendMediaWithRandomId(
not_null<HistoryItem*> item, not_null<HistoryItem*> item,
const MTPInputMedia &media, const MTPInputMedia &media,
Api::SendOptions options, Api::SendOptions options,
uint64 randomId) { uint64 randomId,
Fn<void(bool)> done) {
const auto history = item->history(); const auto history = item->history();
const auto replyTo = item->replyTo(); const auto replyTo = item->replyTo();
@ -3984,10 +3996,12 @@ void ApiWrap::sendMediaWithRandomId(
MTP_int(options.scheduled), MTP_int(options.scheduled),
(options.sendAs ? options.sendAs->input : MTP_inputPeerEmpty()) (options.sendAs ? options.sendAs->input : MTP_inputPeerEmpty())
), [=](const MTPUpdates &result, const MTP::Response &response) { ), [=](const MTPUpdates &result, const MTP::Response &response) {
if (done) done(true);
if (updateRecentStickers) { if (updateRecentStickers) {
requestRecentStickersForce(true); requestRecentStickersForce(true);
} }
}, [=](const MTP::Error &error, const MTP::Response &response) { }, [=](const MTP::Error &error, const MTP::Response &response) {
if (done) done(false);
sendMessageFail(error, peer, randomId, itemId); sendMessageFail(error, peer, randomId, itemId);
}); });
} }

View file

@ -290,14 +290,16 @@ public:
Data::ResolvedForwardDraft &&draft, Data::ResolvedForwardDraft &&draft,
const SendAction &action, const SendAction &action,
FnMut<void()> &&successCallback = nullptr); FnMut<void()> &&successCallback = nullptr);
FullMsgId shareContact( void shareContact(
const QString &phone, const QString &phone,
const QString &firstName, const QString &firstName,
const QString &lastName, const QString &lastName,
const SendAction &action); const SendAction &action,
FullMsgId shareContact( Fn<void(bool)> done = nullptr);
void shareContact(
not_null<UserData*> user, not_null<UserData*> user,
const SendAction &action); const SendAction &action,
Fn<void(bool)> done = nullptr);
void applyAffectedMessages( void applyAffectedMessages(
not_null<PeerData*> peer, not_null<PeerData*> peer,
const MTPmessages_AffectedMessages &result); const MTPmessages_AffectedMessages &result);
@ -486,12 +488,13 @@ private:
SharedMediaType type, SharedMediaType type,
Api::SearchResult &&parsed); Api::SearchResult &&parsed);
FullMsgId sendSharedContact( void sendSharedContact(
const QString &phone, const QString &phone,
const QString &firstName, const QString &firstName,
const QString &lastName, const QString &lastName,
UserId userId, UserId userId,
const SendAction &action); const SendAction &action,
Fn<void(bool)> done);
void deleteHistory( void deleteHistory(
not_null<PeerData*> peer, not_null<PeerData*> peer,
@ -518,12 +521,14 @@ private:
void sendMedia( void sendMedia(
not_null<HistoryItem*> item, not_null<HistoryItem*> item,
const MTPInputMedia &media, const MTPInputMedia &media,
Api::SendOptions options); Api::SendOptions options,
Fn<void(bool)> done = nullptr);
void sendMediaWithRandomId( void sendMediaWithRandomId(
not_null<HistoryItem*> item, not_null<HistoryItem*> item,
const MTPInputMedia &media, const MTPInputMedia &media,
Api::SendOptions options, Api::SendOptions options,
uint64 randomId); uint64 randomId,
Fn<void(bool)> done = nullptr);
FileLoadTo fileLoadTaskOptions(const SendAction &action) const; FileLoadTo fileLoadTaskOptions(const SendAction &action) const;
void getTopPromotionDelayed(TimeId now, TimeId next); void getTopPromotionDelayed(TimeId now, TimeId next);

View file

@ -645,40 +645,10 @@ void AttachWebView::botSharePhone(Fn<void(bool shared)> callback) {
} }
auto action = Api::SendAction(history); auto action = Api::SendAction(history);
action.clearDraft = false; action.clearDraft = false;
const auto id = history->session().api().shareContact( history->session().api().shareContact(
_bot->session().user(), _bot->session().user(),
action); action,
const auto owner = &_bot->owner(); std::move(callback));
const auto lifetime = std::make_shared<rpl::lifetime>();
const auto check = [=] {
const auto item = id ? owner->message(id) : nullptr;
if (!item || item->hasFailed()) {
lifetime->destroy();
callback(false);
}
};
_bot->session().changes().historyUpdates(
history,
Data::HistoryUpdate::Flag::ClientSideMessages
) | rpl::start_with_next(check, *lifetime);
owner->itemRemoved(
) | rpl::start_with_next([=](not_null<const HistoryItem*> item) {
if (item->fullId() == id) {
check();
}
}, *lifetime);
owner->itemIdChanged(
) | rpl::start_with_next([=](const Data::Session::IdChange &change) {
if (FullMsgId(change.newId.peer, change.oldId) == id) {
lifetime->destroy();
callback(true);
}
}, *lifetime);
check();
} }
void AttachWebView::botInvokeCustomMethod( void AttachWebView::botInvokeCustomMethod(