File: dtsetmessagedeliveryreceipts.cc

package info (click to toggle)
signalbackup-tools 20250313.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,752 kB
  • sloc: cpp: 47,042; sh: 477; ansic: 399; ruby: 19; makefile: 3
file content (161 lines) | stat: -rw-r--r-- 7,428 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
  Copyright (C) 2022-2024  Selwin van Dijk

  This file is part of signalbackup-tools.

  signalbackup-tools is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  signalbackup-tools is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with signalbackup-tools.  If not, see <https://www.gnu.org/licenses/>.
*/

#include "signalbackup.ih"

void SignalBackup::dtSetMessageDeliveryReceipts(SqliteDB const &ddb, long long int rowid, std::map<std::string, long long int> *savedmap,
                                                std::string const &databasedir, bool createcontacts, long long int msg_id, bool is_mms,
                                                bool isgroup, bool create_valid_contacts, bool *warn)
{
  //  public static final int STATUS_UNKNOWN     = -1;
  //  public static final int STATUS_UNDELIVERED = 0;
  //  public static final int STATUS_DELIVERED   = 1;
  //  public static final int STATUS_READ        = 2;
  //  public static final int STATUS_VIEWED      = 3;
  //  public static final int STATUS_SKIPPED     = 4;
  long long int constexpr STATUS_DELIVERED = 1;
  long long int constexpr STATUS_READ = 2;

  SqliteDB::QueryResults status_results;
  if (!ddb.exec("SELECT "
                "delivery_details.key AS conv_id,"
                "conversations." + d_dt_c_uuid + " AS uuid,"
                "conversations.e164 AS e164,"
                "json_extract(delivery_details.value, '$.status') AS status,"
                "COALESCE(json_extract(delivery_details.value, '$.updatedAt'), delivery_details.sent_at) AS updated_timestamp"
                " FROM "
                "(SELECT sent_at,key,value FROM messages,json_each(messages.json, '$.sendStateByConversationId') WHERE rowid IS ?) AS delivery_details"
                " LEFT JOIN conversations ON conversations.id IS conv_id", rowid, &status_results))
  {
    Logger::error("Getting message delivery status");
    return;
  }

  // results:
  //              key = 37fb0475-13e7-43e6-965a-4b11d9370488
  //           status = Sent
  //updated_timestamp = 1668710610793
  //
  //              key = d1c1693d-91e1-486f-8634-29c22afb881b
  //           status = Delivered
  //updated_timestamp = 1668710612716

  //status_results.prettyPrint();

  long long int deliveryreceiptcount = 0;
  long long int readreceiptcount = 0;
  long long int updatedtimestamp = -1;
  for (unsigned int i = 0; i < status_results.rows(); ++i)
  {
    if (status_results.valueAsString(i, "status") == "Delivered")
    {
      ++deliveryreceiptcount;
      if (updatedtimestamp == -1)
        updatedtimestamp = status_results.valueAsInt(i, "updated_timestamp", -1);

      if (isgroup && !status_results.isNull(i, "updated_timestamp")) // add per-group-member details to cdelivery_receipts table
      {
        long long int member_id = getRecipientIdFromUuidMapped(status_results.valueAsString(i, "uuid"), savedmap, createcontacts);
        if (member_id == -1) // try phone
          member_id = getRecipientIdFromPhoneMapped(status_results.valueAsString(i, "e164"), savedmap, createcontacts);
        if (member_id == -1)
        {
          if (createcontacts)
          {
            if ((member_id = dtCreateRecipient(ddb, status_results.valueAsString(i, "uuid"), std::string(), std::string(), databasedir, savedmap, create_valid_contacts, warn)) == -1)
            {
              Logger::error("Failed to create delivery_receipt member. Skipping");
              continue;
            }
          }
          else
          {
            Logger::error("Failed to get id of delivery_receipt member. Skipping");
            continue;
          }
        }
        if (!insertRow("group_receipts", {{"mms_id", msg_id},
                                          {"address", member_id},
                                          {"status", STATUS_DELIVERED},
                                          {"timestamp", status_results.getValueAs<long long int>(i, "updated_timestamp")}}))
          Logger::error("Inserting group_receipt");
      }
    }
    else if (status_results.valueAsString(i, "status") == "Read")
    {
      ++readreceiptcount;
      if (updatedtimestamp == -1)
        updatedtimestamp = status_results.valueAsInt(i, "updated_timestamp", -1);

      if (isgroup && !status_results.isNull(i, "updated_timestamp")) // add per-group-member details to cdelivery_receipts table
      {
        long long int member_id = getRecipientIdFromUuidMapped(status_results.valueAsString(i, "uuid"), savedmap, createcontacts);
        if (member_id == -1) // try phone
          member_id = getRecipientIdFromPhoneMapped(status_results.valueAsString(i, "e164"), savedmap, createcontacts);
        if (member_id == -1)
        {
          if (createcontacts)
          {
            if ((member_id = dtCreateRecipient(ddb, status_results.valueAsString(i, "uuid"), std::string(), std::string(), databasedir, savedmap, create_valid_contacts, warn)) == -1)
            {
              Logger::error("Failed to create delivery_receipt member. Skipping");
              continue;
            }
          }
          else
          {
            Logger::error("Failed to get id of delivery_receipt member. Skipping");
            continue;
          }
        }
        if (!insertRow("group_receipts", {{"mms_id", msg_id},
                                          {"address", member_id},
                                          {"status", STATUS_READ},
                                          {"timestamp", status_results.getValueAs<long long int>(i, "updated_timestamp")}}))
          Logger::error("Inserting group_receipt");
      }
    }
  }

  // update the message in its table (mms/sms)
  if (deliveryreceiptcount < readreceiptcount)
    deliveryreceiptcount = readreceiptcount; // lets just say read messages are also delivered...
  if (deliveryreceiptcount)
    if (!d_database.exec("UPDATE " + (is_mms ? d_mms_table : "sms"s) + " SET " + d_mms_delivery_receipts + " = ? WHERE _id = ?", {deliveryreceiptcount, msg_id}))
      Logger::error("Updating ", (is_mms ? d_mms_table : "sms"), " ", d_mms_delivery_receipts, ".");
  if (readreceiptcount)
    if (!d_database.exec("UPDATE " + (is_mms ? d_mms_table : "sms"s) + " SET " + d_mms_read_receipts + " = ? WHERE _id = ?", {readreceiptcount, msg_id}))
      Logger::error("Updating ", (is_mms ? d_mms_table : "sms"), " ", d_mms_read_receipts, ".");

  // update receipt timestamp (if available)
  if (d_database.tableContainsColumn((is_mms ? d_mms_table : "sms"s), "receipt_timestamp"))
    if (!d_database.exec("UPDATE " + (is_mms ? d_mms_table : "sms"s) + " SET receipt_timestamp = ? WHERE _id = ?", {updatedtimestamp, msg_id}))
      Logger::error("Updating ", (is_mms ? d_mms_table : "sms"), " receipt_timestamp.");



  //insert into group_receipts
  //          sqlite> SELECT * from group_receipts  where _id = 8;
  //         _id = 8
  //      mms_id = 27
  //     address = 4
  //      status = 1
  //   timestamp = 1669579600157
  //unidentified = 1
}