Skip to content

Commit

Permalink
Deal with new UNIQUEness of thread.pinned_order when importing from d…
Browse files Browse the repository at this point in the history
…esktop (pinned status is only imported when desktop database is newer)
  • Loading branch information
bepaald committed Mar 5, 2025
1 parent 535085b commit 19d044c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
2 changes: 1 addition & 1 deletion autoversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
#ifndef VERSION_H_
#define VERSION_H_

#define VERSIONDATE "20250305.100036"
#define VERSIONDATE "20250305.155908"

#endif
25 changes: 19 additions & 6 deletions signalbackup/importfromdesktop.cc
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,9 @@ bool SignalBackup::importFromDesktop(std::unique_ptr<DesktopDatabase> const &dtd
bool warned_createcontacts = createmissingcontacts_valid; // no warning if explicitly requesting this...

// find out which database is newer
long long int maxdate_desktop_db = dtdb->d_database.getSingleResultAs<long long int>("SELECT MAX(MAX(json_extract(json, '$.received_at_ms')),MAX(received_at)) FROM messages", 0);
long long int maxdate_desktop_db = dtdb->d_database.getSingleResultAs<long long int>("SELECT MAX(MAX(COALESCE(received_at_ms, json_extract(json, '$.received_at_ms'))),MAX(received_at)) FROM messages", 0);
long long int maxdate_android_db = d_database.getSingleResultAs<long long int>("SELECT MAX(date_received) FROM " + d_mms_table, 0);

if (d_database.containsTable("sms"))
maxdate_android_db = d_database.getSingleResultAs<long long int>("SELECT MAX((SELECT MAX(date_received) FROM " + d_mms_table + "),(SELECT MAX(" + d_sms_date_received + ") FROM sms))", 0);
bool desktop_is_newer = maxdate_desktop_db > maxdate_android_db;
Expand Down Expand Up @@ -449,8 +450,7 @@ bool SignalBackup::importFromDesktop(std::unique_ptr<DesktopDatabase> const &dtd
if (!insertRow("thread",
{{d_thread_recipient_id, recipientid_for_thread},
{"active", 1},
{"archived", results_all_conversations.getValueAs<long long int>(i, "is_archived")},
{"pinned", results_all_conversations.getValueAs<long long int>(i, "is_pinned")}},
{"archived", results_all_conversations.getValueAs<long long int>(i, "is_archived")}},
"_id", &new_thread_id))
{
Logger::message_end();
Expand Down Expand Up @@ -479,10 +479,23 @@ bool SignalBackup::importFromDesktop(std::unique_ptr<DesktopDatabase> const &dtd
// we have the Android thread id (ttid) and the desktop data (results_all_conversations.value(i, "xxx")), update
// Androids pinned and archived status if desktop is newer:
if (desktop_is_newer)
if (!d_database.exec("UPDATE thread SET archived = ?, " + d_thread_pinned + " = ? WHERE _id = ?", {results_all_conversations.getValueAs<long long int>(i, "is_archived"),
results_all_conversations.getValueAs<long long int>(i, "is_pinned"),
ttid}))
{
bool res = d_database.exec("UPDATE thread SET archived = ? WHERE _id = ?", {results_all_conversations.getValueAs<long long int>(i, "is_archived"), ttid});
if (res)
{
if (results_all_conversations.valueAsInt(i, "is_pinned", 0) > 0) // pin in Android database
res &= d_database.exec("UPDATE thread SET " + d_thread_pinned + " = IFNULL((SELECT MAX(" + d_thread_pinned + ") FROM thread), 0) + 1 "
"WHERE _id = ? AND (" + d_thread_pinned + " IS NULL OR " + d_thread_pinned + " = 0)", ttid);
else // unpin in Android database
{
// to unpin something, set it to the default value. This was '0' before dbv266, 'NULL' after...
std::string pinned_default = d_database.getSingleResultAs<std::string>("SELECT dflt_value FROM pragma_table_info('thread') WHERE name = '" + d_thread_pinned + "'", std::string());
res &= d_database.exec("UPDATE thread SET " + d_thread_pinned + " = " + pinned_default + " WHERE _id = ?", ttid);
}
}
if (!res)
Logger::warning("Failed to update thread properties (id: ", ttid, ")");
}

// now lets get all messages for this conversation
SqliteDB::QueryResults results_all_messages_from_conversation;
Expand Down

0 comments on commit 19d044c

Please sign in to comment.