Merge lp:~libqtelegram-team/telegram-app/dialog-bad-dates into lp:telegram-app/dev

Proposed by Roberto Mier Escandon
Status: Merged
Approved by: Giulio Collura
Approved revision: 139
Merged at revision: 138
Proposed branch: lp:~libqtelegram-team/telegram-app/dialog-bad-dates
Merge into: lp:telegram-app/dev
Diff against target: 110 lines (+55/-14)
1 file modified
qmlplugin/data.cpp (+55/-14)
To merge this branch: bzr merge lp:~libqtelegram-team/telegram-app/dialog-bad-dates
Reviewer Review Type Date Requested Status
Giulio Collura Approve
Review via email: mp+245111@code.launchpad.net

Description of the change

Changed condition to update dialogs date when inserted a messages. Not in all cases that is inserted a message the dialog date must be updated (for instance, when db is empty, on first app start, a lot of messages are inserted when history is requested)

Steps to reproduce the bug were:
- Start app up
- Enter into a dialog
- Restart app

Expected result:
- Dialogs are shown in same order and with same dates as first start

Result:
- Dialog where entered (and requested its history) has a bad date and is in bad position in list

To post a comment you must log in.
Revision history for this message
Giulio Collura (gcollura) wrote :

The code is fine and the issue is gone too. Thanks Roberto!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'qmlplugin/data.cpp'
2--- qmlplugin/data.cpp 2014-12-18 07:49:09 +0000
3+++ qmlplugin/data.cpp 2014-12-18 13:11:41 +0000
4@@ -3091,7 +3091,7 @@
5
6 QSqlQuery query(mDbManager.database());
7
8- query.prepare("SELECT id FROM dialogs WHERE id=:id");
9+ query.prepare("SELECT id, date FROM dialogs WHERE id=:id");
10 query.bindValue(":id", dialogId);
11
12 if (!query.exec()) {
13@@ -3114,20 +3114,27 @@
14 return false;
15 }
16 } else {
17- // dialog already exists, update its date and increase unread count if needed.
18- QString arg1 = mustIncreaseUnreadCount ? "unreadCount=unreadCount+1," : "";
19- QString sql = QString("UPDATE dialogs SET %1 date=:date WHERE id=:id").arg(arg1);
20-
21- query.prepare(sql);
22- query.bindValue(":id", dialogId);
23- query.bindValue(":date", message.date());
24-
25- if (!query.exec()) {
26- qCCritical(TG_PLUGIN_LOGIC) << "Error:" << query.lastError() << query.lastQuery();
27- mDbManager.rollbackTransaction();
28- return false;
29+ qint32 dialogDate = query.value("date").toInt();
30+ bool mustUpdateDate = message.date() > dialogDate;
31+
32+ if (mustIncreaseUnreadCount || mustUpdateDate) {
33+ // dialog already exists, update its date and increase unread count if needed.
34+ QString arg1 = mustIncreaseUnreadCount ? "unreadCount=unreadCount+1," : "";
35+ QString arg2 = mustUpdateDate ? "date=:date" : "";
36+ QString sql = QString("UPDATE dialogs SET %1 %2 WHERE id=:id").arg(arg1, arg2);
37+
38+ query.prepare(sql);
39+ query.bindValue(":id", dialogId);
40+ if (mustUpdateDate) {
41+ query.bindValue(":date", message.date());
42+ }
43+
44+ if (!query.exec()) {
45+ qCCritical(TG_PLUGIN_LOGIC) << "Error:" << query.lastError() << query.lastQuery();
46+ mDbManager.rollbackTransaction();
47+ return false;
48+ }
49 }
50-
51 }
52
53 qint64 mediaId = 0;
54@@ -3335,8 +3342,38 @@
55
56 bool Data::updateMessage(qint32 oldMessageId, qint32 newMessageId, qint32 date) {
57
58+ mDbManager.beginTransaction();
59+
60 QSqlQuery query(mDbManager.database());
61
62+ qint32 dialogId = getDialogIdFromMessageId(oldMessageId);
63+ query.prepare("SELECT date FROM dialogs WHERE id=:dialogId");
64+ query.bindValue(":dialogId", dialogId);
65+ if (!query.exec()) {
66+ qCCritical(TG_PLUGIN_LOGIC) << "Error getting date from dialog" << dialogId << query.lastError() << query.lastQuery();
67+ mDbManager.rollbackTransaction();
68+ return false;
69+ }
70+
71+ if (!query.next()) {
72+ qCCritical(TG_PLUGIN_LOGIC) << "Could not find any related register to dialog" << dialogId;
73+ mDbManager.rollbackTransaction();
74+ return false;
75+ }
76+
77+ qint32 dialogDate = query.value("date").toInt();
78+
79+ if (date > dialogDate) {
80+ query.prepare("UPDATE dialogs SET date=:date WHERE id=:dialogId");
81+ query.bindValue(":date", date);
82+ query.bindValue(":dialogId", dialogId);
83+ if (!query.exec()) {
84+ qCCritical(TG_PLUGIN_LOGIC) << "Error updating date for dialog" << dialogId << query.lastError() << query.lastQuery();
85+ mDbManager.rollbackTransaction();
86+ return false;
87+ }
88+ }
89+
90 //when updating message date is because and update date is taken from server. Update sent to true also
91 query.prepare("UPDATE messages SET date=:date, id=:newId, sent=1 WHERE id=:oldId");
92 query.bindValue(":date", date);
93@@ -3344,14 +3381,18 @@
94 query.bindValue(":newId", newMessageId);
95 if (!query.exec()) {
96 qCCritical(TG_PLUGIN_LOGIC) << "Error:" << query.lastError() << query.lastQuery();
97+ mDbManager.rollbackTransaction();
98 return false;
99 }
100
101 if (query.numRowsAffected() == 0) {
102 qCCritical(TG_PLUGIN_LOGIC) << "No row was updated during this execution:" << query.lastQuery();
103+ mDbManager.rollbackTransaction();
104 return false;
105 }
106
107+ mDbManager.finishTransaction();
108+
109 return true;
110 }
111

Subscribers

People subscribed via source and target branches

to all changes: