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"
21 #include "src/persistence/igroupsettings.h"
22 
23 #include <QtCore>
24 
GroupMessageDispatcher(Group & g_,MessageProcessor processor_,ICoreIdHandler & idHandler_,ICoreGroupMessageSender & messageSender_,const IGroupSettings & groupSettings_)25 GroupMessageDispatcher::GroupMessageDispatcher(Group& g_, MessageProcessor processor_,
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 {
35     processor.enableMentions();
36 }
37 
38 std::pair<DispatchedMessageId, DispatchedMessageId>
sendMessage(bool isAction,QString const & content)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)) {
45         auto messageId = nextMessageId++;
46         lastMessageId = messageId;
47         if (group.getPeersCount() != 1) {
48             if (message.isAction) {
49                 messageSender.sendGroupAction(group.getId(), message.content);
50             } else {
51                 messageSender.sendGroupMessage(group.getId(), message.content);
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 /**
69  * @brief Processes and dispatches received message from toxcore
70  * @param[in] sender
71  * @param[in] isAction True if is action
72  * @param[in] content Message content
73  */
onMessageReceived(const ToxPk & sender,bool isAction,QString const & content)74 void GroupMessageDispatcher::onMessageReceived(const ToxPk& sender, bool isAction, QString const& content)
75 {
76     bool isSelf = sender == idHandler.getSelfPublicKey();
77 
78     if (isSelf) {
79         return;
80     }
81 
82     if (groupSettings.getBlackList().contains(sender.toString())) {
83         qDebug() << "onGroupMessageReceived: Filtered:" << sender.toString();
84         return;
85     }
86 
87     emit messageReceived(sender, processor.processIncomingMessage(isAction, content));
88 }
89