From f3dca6efb74afd5928d69938fcc6fa5c178a3bd1 Mon Sep 17 00:00:00 2001 From: John Preston Date: Fri, 16 Aug 2024 10:54:02 +0200 Subject: [PATCH] Show correct signature in send-as channel posts. --- Telegram/SourceFiles/api/api_sending.cpp | 49 +++------------ Telegram/SourceFiles/apiwrap.cpp | 61 ++++--------------- Telegram/SourceFiles/history/history_item.cpp | 13 +--- Telegram/SourceFiles/history/history_item.h | 4 -- .../history/history_item_helpers.cpp | 28 ++++++++- .../history/history_item_helpers.h | 4 ++ .../main/session/send_as_peers.cpp | 10 ++- 7 files changed, 60 insertions(+), 109 deletions(-) diff --git a/Telegram/SourceFiles/api/api_sending.cpp b/Telegram/SourceFiles/api/api_sending.cpp index fb096e626..8655619c0 100644 --- a/Telegram/SourceFiles/api/api_sending.cpp +++ b/Telegram/SourceFiles/api/api_sending.cpp @@ -165,25 +165,15 @@ void SendExistingMedia( flags |= MessageFlag::HasReplyInfo; sendFlags |= MTPmessages_SendMedia::Flag::f_reply_to; } - const auto anonymousPost = peer->amAnonymous(); const auto silentPost = ShouldSendSilent(peer, action.options); InnerFillMessagePostFlags(action.options, peer, flags); if (silentPost) { sendFlags |= MTPmessages_SendMedia::Flag::f_silent; } const auto sendAs = action.options.sendAs; - const auto messageFromId = sendAs - ? sendAs->id - : anonymousPost - ? 0 - : session->userPeerId(); if (sendAs) { sendFlags |= MTPmessages_SendMedia::Flag::f_send_as; } - const auto messagePostAuthor = peer->isBroadcast() - ? session->user()->name() - : QString(); - auto caption = TextWithEntities{ message.textWithTags.text, TextUtilities::ConvertTextTagsToEntities(message.textWithTags.tags) @@ -219,11 +209,11 @@ void SendExistingMedia( history->addNewLocalMessage({ .id = newId.msg, .flags = flags, - .from = messageFromId, + .from = NewMessageFromId(action), .replyTo = action.replyTo, - .date = HistoryItem::NewMessageDate(action.options), + .date = NewMessageDate(action.options), .shortcutId = action.options.shortcutId, - .postAuthor = messagePostAuthor, + .postAuthor = NewMessagePostAuthor(action), .effectId = action.options.effectId, }, media, caption); @@ -361,25 +351,15 @@ bool SendDice(MessageToSend &message) { flags |= MessageFlag::HasReplyInfo; sendFlags |= MTPmessages_SendMedia::Flag::f_reply_to; } - const auto anonymousPost = peer->amAnonymous(); const auto silentPost = ShouldSendSilent(peer, action.options); InnerFillMessagePostFlags(action.options, peer, flags); if (silentPost) { sendFlags |= MTPmessages_SendMedia::Flag::f_silent; } const auto sendAs = action.options.sendAs; - const auto messageFromId = sendAs - ? sendAs->id - : anonymousPost - ? 0 - : session->userPeerId(); if (sendAs) { sendFlags |= MTPmessages_SendMedia::Flag::f_send_as; } - const auto messagePostAuthor = peer->isBroadcast() - ? session->user()->name() - : QString(); - if (action.options.scheduled) { flags |= MessageFlag::IsOrWasScheduled; sendFlags |= MTPmessages_SendMedia::Flag::f_schedule_date; @@ -401,11 +381,11 @@ bool SendDice(MessageToSend &message) { history->addNewLocalMessage({ .id = newId.msg, .flags = flags, - .from = messageFromId, + .from = NewMessageFromId(action), .replyTo = action.replyTo, - .date = HistoryItem::NewMessageDate(action.options), + .date = NewMessageDate(action.options), .shortcutId = action.options.shortcutId, - .postAuthor = messagePostAuthor, + .postAuthor = NewMessagePostAuthor(action), .effectId = action.options.effectId, }, TextWithEntities(), MTP_messageMediaDice( MTP_int(0), @@ -529,7 +509,6 @@ void SendConfirmedFile( if (file->to.replyTo) { flags |= MessageFlag::HasReplyInfo; } - const auto anonymousPost = peer->amAnonymous(); FillMessagePostFlags(action, peer, flags); if (file->to.options.scheduled) { flags |= MessageFlag::IsOrWasScheduled; @@ -551,16 +530,6 @@ void SendConfirmedFile( if (file->to.options.invertCaption) { flags |= MessageFlag::InvertMedia; } - - const auto messageFromId = file->to.options.sendAs - ? file->to.options.sendAs->id - : anonymousPost - ? PeerId() - : session->userPeerId(); - const auto messagePostAuthor = peer->isBroadcast() - ? session->user()->name() - : QString(); - const auto media = MTPMessageMedia([&] { if (file->type == SendMediaType::Photo) { using Flag = MTPDmessageMediaPhoto::Flag; @@ -626,11 +595,11 @@ void SendConfirmedFile( history->addNewLocalMessage({ .id = newId.msg, .flags = flags, - .from = messageFromId, + .from = NewMessageFromId(action), .replyTo = file->to.replyTo, - .date = HistoryItem::NewMessageDate(file->to.options), + .date = NewMessageDate(file->to.options), .shortcutId = file->to.options.shortcutId, - .postAuthor = messagePostAuthor, + .postAuthor = NewMessagePostAuthor(action), .groupedId = groupId, .effectId = file->to.options.effectId, }, caption, media); diff --git a/Telegram/SourceFiles/apiwrap.cpp b/Telegram/SourceFiles/apiwrap.cpp index 5220341a3..5023fdc5d 100644 --- a/Telegram/SourceFiles/apiwrap.cpp +++ b/Telegram/SourceFiles/apiwrap.cpp @@ -3283,7 +3283,6 @@ void ApiWrap::forwardMessages( histories.readInbox(history); } const auto sendAs = action.options.sendAs; - const auto anonymousPost = peer->amAnonymous(); const auto silentPost = ShouldSendSilent(peer, action.options); using SendFlag = MTPmessages_ForwardMessages::Flag; @@ -3376,22 +3375,14 @@ void ApiWrap::forwardMessages( peer->id, _session->data().nextLocalMessageId()); const auto self = _session->user(); - const auto messageFromId = sendAs - ? sendAs->id - : anonymousPost - ? PeerId(0) - : self->id; - const auto messagePostAuthor = peer->isBroadcast() - ? self->name() - : QString(); history->addNewLocalMessage({ .id = newId.msg, .flags = flags, - .from = messageFromId, + .from = NewMessageFromId(action), .replyTo = { .topicRootId = topMsgId }, - .date = HistoryItem::NewMessageDate(action.options), + .date = NewMessageDate(action.options), .shortcutId = action.options.shortcutId, - .postAuthor = messagePostAuthor, + .postAuthor = NewMessagePostAuthor(action), // forwarded messages don't have effects //.effectId = action.options.effectId, @@ -3466,8 +3457,6 @@ void ApiWrap::sendSharedContact( const auto newId = FullMsgId( peer->id, _session->data().nextLocalMessageId()); - const auto anonymousPost = peer->amAnonymous(); - auto flags = NewMessageFlags(peer); if (action.replyTo) { flags |= MessageFlag::HasReplyInfo; @@ -3479,22 +3468,14 @@ void ApiWrap::sendSharedContact( if (action.options.shortcutId) { flags |= MessageFlag::ShortcutMessage; } - const auto messageFromId = action.options.sendAs - ? action.options.sendAs->id - : anonymousPost - ? PeerId() - : _session->userPeerId(); - const auto messagePostAuthor = peer->isBroadcast() - ? _session->user()->name() - : QString(); const auto item = history->addNewLocalMessage({ .id = newId.msg, .flags = flags, - .from = messageFromId, + .from = NewMessageFromId(action), .replyTo = action.replyTo, - .date = HistoryItem::NewMessageDate(action.options), + .date = NewMessageDate(action.options), .shortcutId = action.options.shortcutId, - .postAuthor = messagePostAuthor, + .postAuthor = NewMessagePostAuthor(action), .effectId = action.options.effectId, }, TextWithEntities(), MTP_messageMediaContact( MTP_string(phone), @@ -3780,7 +3761,6 @@ void ApiWrap::sendMessage(MessageToSend &&message) { MTP_string(fields.url), MTP_int(page->pendingTill))); } - const auto anonymousPost = peer->amAnonymous(); const auto silentPost = ShouldSendSilent(peer, action.options); FillMessagePostFlags(action, peer, flags); if ((exactWebPage && !ignoreWebPage && message.webPage.invert) @@ -3808,18 +3788,10 @@ void ApiWrap::sendMessage(MessageToSend &&message) { history->startSavingCloudDraft(draftTopicRootId); } const auto sendAs = action.options.sendAs; - const auto messageFromId = sendAs - ? sendAs->id - : anonymousPost - ? PeerId() - : _session->userPeerId(); if (sendAs) { sendFlags |= MTPmessages_SendMessage::Flag::f_send_as; mediaFlags |= MTPmessages_SendMedia::Flag::f_send_as; } - const auto messagePostAuthor = peer->isBroadcast() - ? _session->user()->name() - : QString(); if (action.options.scheduled) { flags |= MessageFlag::IsOrWasScheduled; sendFlags |= MTPmessages_SendMessage::Flag::f_schedule_date; @@ -3837,11 +3809,11 @@ void ApiWrap::sendMessage(MessageToSend &&message) { lastMessage = history->addNewLocalMessage({ .id = newId.msg, .flags = flags, - .from = messageFromId, + .from = NewMessageFromId(action), .replyTo = action.replyTo, - .date = HistoryItem::NewMessageDate(action.options), + .date = NewMessageDate(action.options), .shortcutId = action.options.shortcutId, - .postAuthor = messagePostAuthor, + .postAuthor = NewMessagePostAuthor(action), .effectId = action.options.effectId, }, sending, media); const auto done = [=]( @@ -3987,7 +3959,6 @@ void ApiWrap::sendInlineResult( flags |= MessageFlag::HasReplyInfo; sendFlags |= SendFlag::f_reply_to; } - const auto anonymousPost = peer->amAnonymous(); const auto silentPost = ShouldSendSilent(peer, action.options); FillMessagePostFlags(action, peer, flags); if (silentPost) { @@ -4006,30 +3977,22 @@ void ApiWrap::sendInlineResult( } const auto sendAs = action.options.sendAs; - const auto messageFromId = sendAs - ? sendAs->id - : anonymousPost ? PeerId() - : _session->userPeerId(); if (sendAs) { sendFlags |= MTPmessages_SendInlineBotResult::Flag::f_send_as; } - const auto messagePostAuthor = peer->isBroadcast() - ? _session->user()->name() - : QString(); - _session->data().registerMessageRandomId(randomId, newId); data->addToHistory(history, { .id = newId.msg, .flags = flags, - .from = messageFromId, + .from = NewMessageFromId(action), .replyTo = action.replyTo, - .date = HistoryItem::NewMessageDate(action.options), + .date = NewMessageDate(action.options), .shortcutId = action.options.shortcutId, .viaBotId = ((bot && !action.options.hideViaBot) ? peerToUser(bot->id) : UserId()), - .postAuthor = messagePostAuthor, + .postAuthor = NewMessagePostAuthor(action), }); history->clearCloudDraft(topicRootId); diff --git a/Telegram/SourceFiles/history/history_item.cpp b/Telegram/SourceFiles/history/history_item.cpp index 70b38bcf2..6d89ca456 100644 --- a/Telegram/SourceFiles/history/history_item.cpp +++ b/Telegram/SourceFiles/history/history_item.cpp @@ -671,9 +671,7 @@ HistoryItem::HistoryItem( .flags = (MessageFlag::Local | MessageFlag::Sponsored | (history->peer->isChannel() ? MessageFlag::Post : MessageFlag(0))), - .date = HistoryItem::NewMessageDate(injectedAfter - ? injectedAfter->date() - : 0), + .date = NewMessageDate(injectedAfter ? injectedAfter->date() : 0), }) { const auto webpage = history->peer->owner().webpage( history->peer->owner().nextLocalMessageId().bare, @@ -761,15 +759,6 @@ TimeId HistoryItem::date() const { return _date; } -TimeId HistoryItem::NewMessageDate(TimeId scheduled) { - return scheduled ? scheduled : base::unixtime::now(); -} - -TimeId HistoryItem::NewMessageDate( - const Api::SendOptions &options) { - return options.shortcutId ? 1 : NewMessageDate(options.scheduled); -} - HistoryServiceDependentData *HistoryItem::GetServiceDependentData() { if (const auto pinned = Get()) { return pinned; diff --git a/Telegram/SourceFiles/history/history_item.h b/Telegram/SourceFiles/history/history_item.h index 682075e60..f3ad4b854 100644 --- a/Telegram/SourceFiles/history/history_item.h +++ b/Telegram/SourceFiles/history/history_item.h @@ -482,10 +482,6 @@ public: [[nodiscard]] Data::MessagePosition position() const; [[nodiscard]] TimeId date() const; - [[nodiscard]] static TimeId NewMessageDate(TimeId scheduled); - [[nodiscard]] static TimeId NewMessageDate( - const Api::SendOptions &options); - [[nodiscard]] Data::Media *media() const { return _media.get(); } diff --git a/Telegram/SourceFiles/history/history_item_helpers.cpp b/Telegram/SourceFiles/history/history_item_helpers.cpp index 463f70952..dabd0ee83 100644 --- a/Telegram/SourceFiles/history/history_item_helpers.cpp +++ b/Telegram/SourceFiles/history/history_item_helpers.cpp @@ -187,6 +187,32 @@ MessageFlags NewMessageFlags(not_null peer) { | (peer->isSelf() ? MessageFlag() : MessageFlag::Outgoing); } +TimeId NewMessageDate(TimeId scheduled) { + return scheduled ? scheduled : base::unixtime::now(); +} + +TimeId NewMessageDate(const Api::SendOptions &options) { + return options.shortcutId ? 1 : NewMessageDate(options.scheduled); +} + +PeerId NewMessageFromId(const Api::SendAction &action) { + return action.options.sendAs + ? action.options.sendAs->id + : action.history->peer->amAnonymous() + ? PeerId() + : action.history->session().userPeerId(); +} + +QString NewMessagePostAuthor(const Api::SendAction &action) { + return !action.history->peer->isBroadcast() + ? QString() + : (action.options.sendAs == action.history->peer) + ? QString() + : action.options.sendAs + ? action.options.sendAs->name() + : action.history->session().user()->name(); +} + bool ShouldSendSilent( not_null peer, const Api::SendOptions &options) { @@ -320,7 +346,7 @@ ClickHandlerPtr JumpToMessageClickHandler( const auto separate = Core::App().separateWindowFor(peer); const auto controller = separate ? separate->sessionController() - : peer->session().tryResolveWindow(); + : peer->session().tryResolveWindow(peer); if (controller) { auto params = Window::SectionShow{ Window::SectionShow::Way::Forward diff --git a/Telegram/SourceFiles/history/history_item_helpers.h b/Telegram/SourceFiles/history/history_item_helpers.h index 3d67e29ec..dd5121dbe 100644 --- a/Telegram/SourceFiles/history/history_item_helpers.h +++ b/Telegram/SourceFiles/history/history_item_helpers.h @@ -83,6 +83,10 @@ void RequestDependentMessageStory( PeerId peerId, StoryId storyId); [[nodiscard]] MessageFlags NewMessageFlags(not_null peer); +[[nodiscard]] TimeId NewMessageDate(TimeId scheduled); +[[nodiscard]] TimeId NewMessageDate(const Api::SendOptions &options); +[[nodiscard]] PeerId NewMessageFromId(const Api::SendAction &action); +[[nodiscard]] QString NewMessagePostAuthor(const Api::SendAction &action); [[nodiscard]] bool ShouldSendSilent( not_null peer, const Api::SendOptions &options); diff --git a/Telegram/SourceFiles/main/session/send_as_peers.cpp b/Telegram/SourceFiles/main/session/send_as_peers.cpp index 373a5ef40..050c87f6e 100644 --- a/Telegram/SourceFiles/main/session/send_as_peers.cpp +++ b/Telegram/SourceFiles/main/session/send_as_peers.cpp @@ -117,6 +117,12 @@ not_null SendAsPeers::ResolveChosen( not_null peer, const std::vector &list, PeerId chosen) { + const auto fallback = peer->amAnonymous() + ? peer + : peer->session().user(); + if (!chosen) { + chosen = fallback->id; + } const auto i = ranges::find(list, chosen, [](const SendAsPeer &as) { return as.peer->id; }); @@ -124,9 +130,7 @@ not_null SendAsPeers::ResolveChosen( ? i->peer : !list.empty() ? list.front().peer - : peer->amAnonymous() - ? peer - : peer->session().user(); + : fallback; } void SendAsPeers::request(not_null peer) {