qTox  Version: nightly | Commit: bc751c8e1cac455f9690654fcfe0f560d2d7dfdd
groupmessagedispatcher.cpp
Go to the documentation of this file.
1 /*
2  Copyright © 2019 by The qTox Project Contributors
3 
4  This file is part of qTox, a Qt-based graphical interface for Tox.
5 
6  qTox is libre software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  qTox is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with qTox. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "groupmessagedispatcher.h"
22 
23 #include <QtCore>
24 
26  ICoreIdHandler& idHandler_,
27  ICoreGroupMessageSender& messageSender_,
28  const IGroupSettings& groupSettings_)
29  : group(g_)
30  , processor(processor_)
31  , idHandler(idHandler_)
32  , messageSender(messageSender_)
33  , groupSettings(groupSettings_)
34 {
36 }
37 
38 std::pair<DispatchedMessageId, DispatchedMessageId>
39 GroupMessageDispatcher::sendMessage(bool isAction, QString const& content)
40 {
41  const auto firstMessageId = nextMessageId;
42  auto lastMessageId = firstMessageId;
43 
44  for (auto const& message : processor.processOutgoingMessage(isAction, content, ExtensionSet())) {
45  auto messageId = nextMessageId++;
46  lastMessageId = messageId;
47  if (group.getPeersCount() != 1) {
48  if (message.isAction) {
50  } else {
52  }
53  }
54 
55  // Emit both signals since we do not have receipts for groups
56  //
57  // NOTE: We could in theory keep track of our sent message and wait for
58  // toxcore to send it back to us to indicate a completed message, but
59  // this isn't necessarily the design of toxcore and associating the
60  // received message back would be difficult.
61  emit this->messageSent(messageId, message);
62  emit this->messageComplete(messageId);
63  }
64 
65  return std::make_pair(firstMessageId, lastMessageId);
66 }
67 
68 std::pair<DispatchedMessageId, DispatchedMessageId>
69 GroupMessageDispatcher::sendExtendedMessage(const QString& content, ExtensionSet extensions)
70 {
71  // Stub this api to immediately fail
72  auto messageId = nextMessageId++;
73  auto messages = processor.processOutgoingMessage(false, content, ExtensionSet());
74  emit this->messageSent(messageId, messages[0]);
76  return {messageId, messageId};
77 }
78 
85 void GroupMessageDispatcher::onMessageReceived(const ToxPk& sender, bool isAction, QString const& content)
86 {
87  bool isSelf = sender == idHandler.getSelfPublicKey();
88 
89  if (isSelf) {
90  return;
91  }
92 
93  if (groupSettings.getBlackList().contains(sender.toString())) {
94  qDebug() << "onGroupMessageReceived: Filtered:" << sender.toString();
95  return;
96  }
97 
98  emit messageReceived(sender, processor.processIncomingCoreMessage(isAction, content));
99 }
BrokenMessageReason::unsupportedExtensions
@ unsupportedExtensions
GroupMessageDispatcher::nextMessageId
DispatchedMessageId nextMessageId
Definition: groupmessagedispatcher.h:56
IGroupSettings
Definition: igroupsettings.h:26
GroupMessageDispatcher::groupSettings
const IGroupSettings & groupSettings
Definition: groupmessagedispatcher.h:55
Group::getPeersCount
int getPeersCount() const
Definition: group.cpp:150
GroupMessageDispatcher::onMessageReceived
void onMessageReceived(ToxPk const &sender, bool isAction, QString const &content)
Processes and dispatches received message from toxcore.
Definition: groupmessagedispatcher.cpp:85
GroupMessageDispatcher::GroupMessageDispatcher
GroupMessageDispatcher(Group &group, MessageProcessor processor, ICoreIdHandler &idHandler, ICoreGroupMessageSender &messageSender, const IGroupSettings &groupSettings)
Definition: groupmessagedispatcher.cpp:25
GroupMessageDispatcher::idHandler
ICoreIdHandler & idHandler
Definition: groupmessagedispatcher.h:53
IMessageDispatcher::messageSent
void messageSent(DispatchedMessageId id, const Message &message)
Emitted when a message is processed and sent.
Group::getId
uint32_t getId() const override
Definition: group.cpp:140
IGroupSettings::getBlackList
virtual QStringList getBlackList() const =0
GroupMessageDispatcher::sendExtendedMessage
std::pair< DispatchedMessageId, DispatchedMessageId > sendExtendedMessage(const QString &content, ExtensionSet extensions) override
Sends message to associated chat ensuring that extensions are available.
Definition: groupmessagedispatcher.cpp:69
GroupMessageDispatcher::sendMessage
std::pair< DispatchedMessageId, DispatchedMessageId > sendMessage(bool isAction, QString const &content) override
Sends message to associated chat.
Definition: groupmessagedispatcher.cpp:39
HistMessageContentType::message
@ message
IMessageDispatcher::messageComplete
void messageComplete(DispatchedMessageId id)
Emitted when a receiver report is received from the associated chat.
ICoreGroupMessageSender
Definition: icoregroupmessagesender.h:24
ToxPk
This class represents a Tox Public Key, which is a part of Tox ID.
Definition: toxpk.h:26
ICoreGroupMessageSender::sendGroupMessage
virtual void sendGroupMessage(int groupId, const QString &message)=0
ICoreIdHandler::getSelfPublicKey
virtual ToxPk getSelfPublicKey() const =0
GroupMessageDispatcher::processor
MessageProcessor processor
Definition: groupmessagedispatcher.h:52
MessageProcessor::processOutgoingMessage
std::vector< Message > processOutgoingMessage(bool isAction, const QString &content, ExtensionSet extensions)
Converts an outgoing message into one (or many) sanitized Message(s)
Definition: message.cpp:86
Group
Definition: group.h:34
ExtensionSet
std::bitset< ExtensionType::max > ExtensionSet
Definition: extension.h:32
MessageProcessor::processIncomingCoreMessage
Message processIncomingCoreMessage(bool isAction, const QString &content)
Converts an incoming message into a sanitized Message.
Definition: message.cpp:118
GroupMessageDispatcher::messageSender
ICoreGroupMessageSender & messageSender
Definition: groupmessagedispatcher.h:54
GroupMessageDispatcher::group
Group & group
Definition: groupmessagedispatcher.h:51
ICoreGroupMessageSender::sendGroupAction
virtual void sendGroupAction(int groupId, const QString &message)=0
ICoreIdHandler
Definition: icoreidhandler.h:25
IMessageDispatcher::messageReceived
void messageReceived(const ToxPk &sender, const Message &message)
Emitted when a message is received and processed.
ContactId::toString
QString toString() const
Converts the ContactId to a uppercase hex string.
Definition: contactid.cpp:78
MessageProcessor::enableMentions
void enableMentions()
Enables mention detection in the processor.
Definition: message.h:121
MessageProcessor
Definition: message.h:62
groupmessagedispatcher.h
IMessageDispatcher::messageBroken
void messageBroken(DispatchedMessageId id, BrokenMessageReason reason)
igroupsettings.h