Merge lp:~tiagosh/telephony-service/fix-1482401 into lp:telephony-service

Proposed by Tiago Salem Herrmann
Status: Merged
Approved by: Gustavo Pichorim Boiko
Approved revision: 1113
Merged at revision: 1116
Proposed branch: lp:~tiagosh/telephony-service/fix-1482401
Merge into: lp:telephony-service
Diff against target: 265 lines (+91/-37)
3 files modified
handler/texthandler.cpp (+54/-36)
handler/texthandler.h (+1/-1)
tests/handler/HandlerTest.cpp (+36/-0)
To merge this branch: bzr merge lp:~tiagosh/telephony-service/fix-1482401
Reviewer Review Type Date Requested Status
Gustavo Pichorim Boiko (community) Approve
PS Jenkins bot continuous-integration Approve
Review via email: mp+268328@code.launchpad.net

Commit message

Change existingChat() to existingChannels().

Description of the change

Change existingChat() to existingChannels().
There might be more than on channel for the same recipient, as phone numbers can be slightly different.

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Gustavo Pichorim Boiko (boiko) wrote :

Looks good!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'handler/texthandler.cpp'
2--- handler/texthandler.cpp 2015-06-05 20:36:01 +0000
3+++ handler/texthandler.cpp 2015-08-18 13:23:27 +0000
4@@ -93,7 +93,6 @@
5 startChat(recipients, accountId);
6 }
7 }
8-
9 }
10 }
11
12@@ -216,8 +215,8 @@
13 return;
14 }
15
16- Tp::TextChannelPtr channel = existingChat(recipients, accountId);
17- if (channel.isNull()) {
18+ QList<Tp::TextChannelPtr> channels = existingChannels(recipients, accountId);
19+ if (channels.isEmpty()) {
20 mPendingMMSs[accountId][recipients].append(attachments);
21 startChat(recipients, accountId);
22 return;
23@@ -225,7 +224,7 @@
24
25 Tp::MessagePartList message = buildMMS(attachments);
26
27- connect(channel->send(message),
28+ connect(channels.last()->send(message),
29 SIGNAL(finished(Tp::PendingOperation*)),
30 SLOT(onMessageSent(Tp::PendingOperation*)));
31 }
32@@ -241,8 +240,8 @@
33 mPendingSilentMessages[accountId][recipients].append(message);
34 return;
35 }
36- Tp::TextChannelPtr channel = existingChat(recipients, accountId);
37- if (channel.isNull()) {
38+ QList<Tp::TextChannelPtr> channels = existingChannels(recipients, accountId);
39+ if (channels.isEmpty()) {
40 mPendingSilentMessages[accountId][recipients].append(message);
41 startChat(recipients, accountId);
42 return;
43@@ -258,7 +257,7 @@
44 body["content-type"] = QDBusVariant("text/plain");
45 fullMessage << header << body;
46
47- connect(channel->send(fullMessage),
48+ connect(channels.last()->send(fullMessage),
49 SIGNAL(finished(Tp::PendingOperation*)),
50 SLOT(onMessageSent(Tp::PendingOperation*)));
51 }
52@@ -294,33 +293,34 @@
53 return;
54 }
55
56- Tp::TextChannelPtr channel = existingChat(recipients, account->accountId());
57- if (channel.isNull()) {
58+ QList<Tp::TextChannelPtr> channels = existingChannels(recipients, account->accountId());
59+ if (channels.isEmpty()) {
60 mPendingMessages[account->accountId()][recipients].append(message);
61 startChat(recipients, account->accountId());
62 return;
63 }
64
65- connect(channel->send(message),
66+ connect(channels.last()->send(message),
67 SIGNAL(finished(Tp::PendingOperation*)),
68 SLOT(onMessageSent(Tp::PendingOperation*)));
69 }
70
71 void TextHandler::acknowledgeMessages(const QStringList &recipients, const QStringList &messageIds, const QString &accountId)
72 {
73- Tp::TextChannelPtr channel = existingChat(recipients, accountId);
74- if (channel.isNull()) {
75+ QList<Tp::TextChannelPtr> channels = existingChannels(recipients, accountId);
76+ if (channels.isEmpty()) {
77 return;
78 }
79
80 QList<Tp::ReceivedMessage> messagesToAck;
81- Q_FOREACH(const Tp::ReceivedMessage &message, channel->messageQueue()) {
82- if (messageIds.contains(message.messageToken())) {
83- messagesToAck.append(message);
84+ Q_FOREACH(const Tp::TextChannelPtr &channel, channels) {
85+ Q_FOREACH(const Tp::ReceivedMessage &message, channel->messageQueue()) {
86+ if (messageIds.contains(message.messageToken())) {
87+ messagesToAck.append(message);
88+ }
89 }
90+ channel->acknowledge(messagesToAck);
91 }
92-
93- channel->acknowledge(messagesToAck);
94 }
95
96 void TextHandler::onTextChannelAvailable(Tp::TextChannelPtr channel)
97@@ -347,36 +347,54 @@
98 QMap<QStringList, QStringList> &pendingMessages = mPendingMessages[accountId];
99 QMap<QStringList, QStringList>::iterator it = pendingMessages.begin();
100 while (it != pendingMessages.end()) {
101- if (existingChat(it.key(), accountId) == channel) {
102- Q_FOREACH(const QString &message, it.value()) {
103- sendMessage(recipients, message, accountId);
104+ bool found = false;
105+ Q_FOREACH(const Tp::TextChannelPtr &existingChannel, existingChannels(it.key(), accountId)) {
106+ if (existingChannel == channel) {
107+ Q_FOREACH(const QString &message, it.value()) {
108+ sendMessage(recipients, message, accountId);
109+ }
110+ it = pendingMessages.erase(it);
111+ found = true;
112+ break;
113 }
114- it = pendingMessages.erase(it);
115- } else {
116+ }
117+ if (!found) {
118 ++it;
119 }
120 }
121 QMap<QStringList, QList<AttachmentList>> &pendingMMSs = mPendingMMSs[accountId];
122 QMap<QStringList, QList<AttachmentList>>::iterator it2 = pendingMMSs.begin();
123 while (it2 != pendingMMSs.end()) {
124- if (existingChat(it2.key(), accountId) == channel) {
125- Q_FOREACH(const AttachmentList &attachments, it2.value()) {
126- sendMMS(recipients, attachments, accountId);
127+ bool found = false;
128+ Q_FOREACH(const Tp::TextChannelPtr &existingChannel, existingChannels(it2.key(), accountId)) {
129+ if (existingChannel == channel) {
130+ Q_FOREACH(const AttachmentList &attachments, it2.value()) {
131+ sendMMS(recipients, attachments, accountId);
132+ }
133+ it2 = pendingMMSs.erase(it2);
134+ found = true;
135+ break;
136 }
137- it2 = pendingMMSs.erase(it2);
138- } else {
139+ }
140+ if (!found) {
141 ++it2;
142 }
143 }
144 QMap<QStringList, QStringList> &pendingSilentMessages = mPendingSilentMessages[accountId];
145 QMap<QStringList, QStringList>::iterator it3 = pendingSilentMessages.begin();
146 while (it3 != pendingSilentMessages.end()) {
147- if (existingChat(it3.key(), accountId) == channel) {
148- Q_FOREACH(const QString &message, it3.value()) {
149- sendSilentMessage(recipients, message, accountId);
150+ bool found = false;
151+ Q_FOREACH(const Tp::TextChannelPtr &existingChannel, existingChannels(it3.key(), accountId)) {
152+ if (existingChannel == channel) {
153+ Q_FOREACH(const QString &message, it3.value()) {
154+ sendSilentMessage(recipients, message, accountId);
155+ }
156+ it3 = pendingSilentMessages.erase(it3);
157+ found = true;
158+ break;
159 }
160- it3 = pendingSilentMessages.erase(it3);
161- } else {
162+ }
163+ if (!found) {
164 ++it3;
165 }
166 }
167@@ -396,9 +414,9 @@
168 }
169 }
170
171-Tp::TextChannelPtr TextHandler::existingChat(const QStringList &targetIds, const QString &accountId)
172+QList<Tp::TextChannelPtr> TextHandler::existingChannels(const QStringList &targetIds, const QString &accountId)
173 {
174- Tp::TextChannelPtr channel;
175+ QList<Tp::TextChannelPtr> channels;
176
177 Q_FOREACH(const Tp::TextChannelPtr &channel, mChannels) {
178 int count = 0;
179@@ -415,10 +433,10 @@
180 }
181 }
182 if (count == targetIds.size()) {
183- return channel;
184+ channels.append(channel);
185 }
186 }
187- return channel;
188+ return channels;
189 }
190
191 void TextHandler::onContactsAvailable(Tp::PendingOperation *op)
192
193=== modified file 'handler/texthandler.h'
194--- handler/texthandler.h 2015-03-04 18:02:13 +0000
195+++ handler/texthandler.h 2015-08-18 13:23:27 +0000
196@@ -49,7 +49,7 @@
197 void onConnectedChanged();
198
199 protected:
200- Tp::TextChannelPtr existingChat(const QStringList &targetIds, const QString &accountId);
201+ QList<Tp::TextChannelPtr> existingChannels(const QStringList &targetIds, const QString &accountId);
202
203 private:
204 explicit TextHandler(QObject *parent = 0);
205
206=== modified file 'tests/handler/HandlerTest.cpp'
207--- tests/handler/HandlerTest.cpp 2015-07-23 21:08:40 +0000
208+++ tests/handler/HandlerTest.cpp 2015-08-18 13:23:27 +0000
209@@ -19,6 +19,7 @@
210 #include <QtCore/QObject>
211 #include <QtTest/QtTest>
212 #include "telepathytest.h"
213+#include "chatmanager.h"
214 #include "handlercontroller.h"
215 #include "mockcontroller.h"
216 #include "approver.h"
217@@ -40,6 +41,7 @@
218 void testCallProperties();
219 void testConferenceCall();
220 void testSendMessage();
221+ void testAcknowledgeMessage();
222 void testActiveCallIndicator();
223 void testNotApprovedChannels();
224
225@@ -283,6 +285,40 @@
226 QCOMPARE(messageProperties["Recipients"].value<QStringList>().first(), recipient);
227 }
228
229+void HandlerTest::testAcknowledgeMessage()
230+{
231+ // if we register the observer before this test, other tests fail
232+ TelepathyHelper::instance()->registerChannelObserver();
233+ QString recipient("84376666");
234+ QString recipient2("+554184376666");
235+ QString message("Hello, world!");
236+ QSignalSpy messageSentSpy(mMockController, SIGNAL(MessageSent(QString,QVariantMap)));
237+
238+ // first send a message to a certain number so the handler request one channel
239+ HandlerController::instance()->sendMessage(recipient, message, mTpAccount->uniqueIdentifier());
240+ TRY_COMPARE(messageSentSpy.count(), 1);
241+
242+ QSignalSpy messageReceivedSpy(ChatManager::instance(), SIGNAL(messageReceived(QString,QString,QDateTime,QString,bool)));
243+
244+ // now receive a message from a very similar number so CM creates another
245+ // channel and the handler needs to deal with both
246+ QVariantMap properties;
247+ properties["Sender"] = recipient2;
248+ properties["Recipients"] = (QStringList() << recipient2);
249+ mMockController->PlaceIncomingMessage(message, properties);
250+
251+ TRY_COMPARE(messageReceivedSpy.count(), 1);
252+ QString receivedMessageId = messageReceivedSpy.first()[3].toString();
253+
254+ // then acknoledge the message that arrived in the second channel and make sure handler
255+ // does the right thing
256+ QSignalSpy messageReadSpy(mMockController, SIGNAL(MessageRead(QString)));
257+ ChatManager::instance()->acknowledgeMessage(properties["Recipients"].toStringList(), receivedMessageId, mTpAccount->uniqueIdentifier());
258+
259+ TRY_COMPARE(messageReadSpy.count(), 1);
260+ QCOMPARE(messageReadSpy.first()[0].toString(), receivedMessageId);
261+}
262+
263 void HandlerTest::testActiveCallIndicator()
264 {
265 // start by making sure the property is false by default

Subscribers

People subscribed via source and target branches