Show reactions in messages.

This commit is contained in:
John Preston 2019-09-11 13:55:39 +03:00
parent f5c7b206bb
commit 3a43217301
8 changed files with 68 additions and 2 deletions

View file

@ -10,6 +10,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "history/history.h" #include "history/history.h"
#include "history/history_item.h" #include "history/history_item.h"
#include "main/main_session.h" #include "main/main_session.h"
#include "data/data_session.h"
#include "apiwrap.h" #include "apiwrap.h"
namespace Data { namespace Data {
@ -48,6 +49,7 @@ void MessageReactions::add(const QString &reaction) {
++_list[reaction]; ++_list[reaction];
} }
sendRequest(); sendRequest();
_item->history()->owner().requestItemResize(_item);
} }
void MessageReactions::set( void MessageReactions::set(
@ -80,6 +82,7 @@ void MessageReactions::set(
_chosen = QString(); _chosen = QString();
} }
} }
_item->history()->owner().requestItemResize(_item);
} }
const base::flat_map<QString, int> &MessageReactions::list() const { const base::flat_map<QString, int> &MessageReactions::list() const {

View file

@ -513,6 +513,9 @@ HistoryMessage::HistoryMessage(
setGroupId( setGroupId(
MessageGroupId::FromRaw(history->peer->id, groupedId->v)); MessageGroupId::FromRaw(history->peer->id, groupedId->v));
} }
if (const auto reactions = data.vreactions()) {
updateReactions(*reactions);
}
applyTTL(data); applyTTL(data);
} }

View file

@ -1064,6 +1064,10 @@ void HistoryService::createFromMtp(const MTPDmessage &message) {
default: Unexpected("Media type in HistoryService::createFromMtp()"); default: Unexpected("Media type in HistoryService::createFromMtp()");
} }
if (const auto reactions = message.vreactions()) {
updateReactions(*reactions);
}
} }
void HistoryService::createFromMtp(const MTPDmessageService &message) { void HistoryService::createFromMtp(const MTPDmessageService &message) {

View file

@ -30,6 +30,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "lang/lang_keys.h" #include "lang/lang_keys.h"
#include "mainwidget.h" #include "mainwidget.h"
#include "main/main_session.h" #include "main/main_session.h"
#include "ui/text/text_options.h"
#include "window/window_session_controller.h" #include "window/window_session_controller.h"
#include "apiwrap.h" #include "apiwrap.h"
@ -236,6 +237,25 @@ LogEntryOriginal &LogEntryOriginal::operator=(LogEntryOriginal &&other) {
LogEntryOriginal::~LogEntryOriginal() = default; LogEntryOriginal::~LogEntryOriginal() = default;
void Reactions::update(const base::flat_map<QString, int> &list) {
auto sorted = ranges::view::all(
list
) | ranges::view::transform([](const auto &pair) {
return std::make_pair(pair.first, pair.second);
}) | ranges::to_vector;
ranges::sort(sorted, std::greater<>(), &std::pair<QString, int>::second);
auto fullCount = 0;
auto composed = QString();
for (const auto &[string, count] : sorted) {
composed.append(string);
fullCount += count;
}
composed += QString::number(fullCount);
text.setText(st::msgDateTextStyle, composed, Ui::NameTextOptions());
}
Message::Message( Message::Message(
not_null<ElementDelegate*> delegate, not_null<ElementDelegate*> delegate,
not_null<HistoryMessage*> data, not_null<HistoryMessage*> data,
@ -317,6 +337,14 @@ QSize Message::performCountOptimalSize() {
refreshEditedBadge(); refreshEditedBadge();
refreshRightBadge(); refreshRightBadge();
if (const auto list = item->reactions(); !list.empty()) {
AddComponents(Reactions::Bit());
const auto reactions = Get<Reactions>();
reactions->update(list);
} else {
RemoveComponents(Reactions::Bit());
}
if (drawBubble()) { if (drawBubble()) {
const auto forwarded = item->Get<HistoryMessageForwarded>(); const auto forwarded = item->Get<HistoryMessageForwarded>();
const auto reply = displayedReply(); const auto reply = displayedReply();
@ -1778,7 +1806,11 @@ void Message::drawInfo(
const auto viewIconTop = infoBottom + st::historyViewsTop; const auto viewIconTop = infoBottom + st::historyViewsTop;
const auto pinIconTop = infoBottom + st::historyPinTop; const auto pinIconTop = infoBottom + st::historyPinTop;
auto left = infoRight - infoW; auto left = infoRight - infoW;
if (auto views = item->Get<HistoryMessageViews>()) { if (const auto reactions = Get<Reactions>()) {
reactions->text.draw(p, left, dateY, reactions->text.maxWidth());
left += reactions->text.maxWidth() + st::historyReactionsSkip;
}
if (const auto views = item->Get<HistoryMessageViews>()) {
const auto textTop = infoBottom - st::msgDateFont->descent; const auto textTop = infoBottom - st::msgDateFont->descent;
if (views->replies.count > 0 if (views->replies.count > 0
&& !views->commentsMegagroupId && !views->commentsMegagroupId
@ -1885,7 +1917,7 @@ bool Message::pointInTime(
int Message::infoWidth() const { int Message::infoWidth() const {
const auto item = message(); const auto item = message();
auto result = item->_timeWidth; auto result = item->_timeWidth;
if (auto views = item->Get<HistoryMessageViews>()) { if (const auto views = item->Get<HistoryMessageViews>()) {
if (views->views.count >= 0) { if (views->views.count >= 0) {
result += st::historyViewsSpace result += st::historyViewsSpace
+ views->views.textWidth + views->views.textWidth
@ -1915,6 +1947,9 @@ int Message::infoWidth() const {
} else if (hasOutLayout()) { } else if (hasOutLayout()) {
result += st::historySendStateSpace; result += st::historySendStateSpace;
} }
if (const auto reactions = Get<Reactions>()) {
result += st::historyReactionsSkip + reactions->text.maxWidth();
}
return result; return result;
} }
@ -1960,6 +1995,10 @@ int Message::timeLeft() const {
if (displayPinIcon()) { if (displayPinIcon()) {
result += st::historyPinWidth; result += st::historyPinWidth;
} }
if (const auto reactions = Get<Reactions>()) {
result += st::historyReactionsSkip + reactions->text.maxWidth();
}
return result; return result;
} }

View file

@ -39,6 +39,12 @@ struct PsaTooltipState : public RuntimeComponent<PsaTooltipState, Element> {
mutable bool buttonVisible = true; mutable bool buttonVisible = true;
}; };
struct Reactions : public RuntimeComponent<Reactions, Element> {
void update(const base::flat_map<QString, int> &list);
Ui::Text::String text;
};
class Message : public Element, public base::has_weak_ptr { class Message : public Element, public base::has_weak_ptr {
public: public:
Message( Message(

View file

@ -148,6 +148,14 @@ QSize Contact::countOptimalSize() {
accumulate_max(maxWidth, tleft + _name.maxWidth() + tright); accumulate_max(maxWidth, tleft + _name.maxWidth() + tright);
accumulate_min(maxWidth, st::msgMaxWidth); accumulate_min(maxWidth, st::msgMaxWidth);
auto minHeight = st.padding.top() + st.thumbSize + st.padding.bottom(); auto minHeight = st.padding.top() + st.thumbSize + st.padding.bottom();
const auto msgsigned = item->Get<HistoryMessageSigned>();
const auto views = item->Get<HistoryMessageViews>();
if ((msgsigned && !msgsigned->isAnonymousRank)
|| (views
&& (views->views.count >= 0 || views->replies.count > 0))
|| !item->reactions().empty()) {
minHeight += st::msgDateFont->height - st::msgDateDelta.y();
}
if (!isBubbleTop()) { if (!isBubbleTop()) {
minHeight -= st::msgFileTopMinus; minHeight -= st::msgFileTopMinus;
} }

View file

@ -273,6 +273,7 @@ QSize Document::countOptimalSize() {
if (!captioned && ((msgsigned && !msgsigned->isAnonymousRank) if (!captioned && ((msgsigned && !msgsigned->isAnonymousRank)
|| (views || (views
&& (views->views.count >= 0 || views->replies.count > 0)) && (views->views.count >= 0 || views->replies.count > 0))
|| !item->reactions().empty()
|| _parent->displayEditedBadge())) { || _parent->displayEditedBadge())) {
minHeight += st::msgDateFont->height - st::msgDateDelta.y(); minHeight += st::msgDateFont->height - st::msgDateDelta.y();
} }

View file

@ -209,6 +209,8 @@ historyPinOutIcon: icon {{ "history_pin", historyOutIconFg }};
historyPinOutSelectedIcon: icon {{ "history_pin", historyOutIconFgSelected }}; historyPinOutSelectedIcon: icon {{ "history_pin", historyOutIconFgSelected }};
historyPinInvertedIcon: icon {{ "history_pin", historySendingInvertedIconFg }}; historyPinInvertedIcon: icon {{ "history_pin", historySendingInvertedIconFg }};
historyReactionsSkip: 8px;
historyComposeField: InputField(defaultInputField) { historyComposeField: InputField(defaultInputField) {
font: msgFont; font: msgFont;
textMargins: margins(0px, 0px, 0px, 0px); textMargins: margins(0px, 0px, 0px, 0px);