File: tgsetquote.cc

package info (click to toggle)
signalbackup-tools 20250313.1-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 3,752 kB
  • sloc: cpp: 47,042; sh: 477; ansic: 399; ruby: 19; makefile: 3
file content (134 lines) | stat: -rw-r--r-- 6,512 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
/*
  Copyright (C) 2023-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"

bool SignalBackup::tgSetQuote(long long int quoted_message_id, long long int new_msg_id)
{
  SqliteDB::QueryResults quote_res;
  if (!d_database.exec("SELECT body, " + d_mms_recipient_id + ", " + d_mms_date_sent + " FROM " + d_mms_table + " "
                       "WHERE _id = ?", quoted_message_id, &quote_res) ||
      quote_res.rows() != 1)
  {
    Logger::warning("Failed to get quote data.");
    return false;
  }

  long long int quote_id = quote_res.valueAsInt(0, d_mms_date_sent, -1);
  long long int quote_author = quote_res.valueAsInt(0, d_mms_recipient_id, -1);
  std::string quote_body = quote_res.valueAsString(0, "body");
  if (quote_id == -1 || quote_author == -1)
  {
    Logger::warning("Failed to get quote data.");
    return false;
  }

  if (!d_database.exec("UPDATE " + d_mms_table + " SET "
                       "quote_id = ?, "
                       "quote_author = ?, "
                       "quote_body = ?, "
                       "quote_type = 0, "
                       "quote_missing = 0 "
                       "WHERE _id = ?",
                       {quote_id,
                        quote_author,
                        (quote_res.isNull(0, "body") ? std::any(nullptr) : std::any(quote_body)),
                        new_msg_id}))
  {
    Logger::warning("Failed to set quote data.");
    return false;
  }

  // set quoted-attachment
  SqliteDB::QueryResults quote_att_res;
  std::string query = "SELECT _id, " + d_part_mid + ", " + d_part_ct + ", " + d_part_pending + ", data_size, " +
    (d_database.tableContainsColumn(d_part_table, "unique_id") ? "unique_id" : "-1 AS unique_id") +
    ", voice_note, width, height, quote";
  if (d_database.tableContainsColumn(d_part_table, "data_hash"))
    query += ", data_hash";
  else if (d_database.tableContainsColumn(d_part_table, "data_hash_start") &&
           d_database.tableContainsColumn(d_part_table, "data_hash_end"))
    query += ", data_hash_start, data_hash_end";
  query += " FROM " + d_part_table + " WHERE " + d_part_mid + " = ?";
  if (d_database.exec(query, quoted_message_id, &quote_att_res) &&
      quote_att_res.rows() >= 1)
  {
    //quote_att_res.prettyPrint();

    // get unique id for new attachments
    long long int unique_id = d_database.getSingleResultAs<long long int>("SELECT " + d_mms_date_sent + " FROM " + d_mms_table +
                                                                          " WHERE _id = ?", new_msg_id, -1);
    if (d_database.tableContainsColumn(d_part_table, "unique_id") && unique_id == -1)
    {
      Logger::warning("Failed to get unique_id for attachment in quote. Skipping.");
      return false;
    }

    // get unique id for existing attachments (to retrieve attachment from d_attachments)...
    long long int quoted_unique_id = quote_att_res.valueAsInt(0, "unique_id", -1);
    if (d_database.tableContainsColumn(d_part_table, "unique_id") && unique_id == -1)
    {
      Logger::warning("Failed to get unique_id for quoted attachment. Skipping.");
      return false;
    }

    for (unsigned int i = 0; i < quote_att_res.rows(); ++i)
    {
      // set sql data
      std::any retval;
      if (!insertRow(d_part_table,
                     {{d_part_mid, new_msg_id},
                      {d_part_ct, quote_att_res.value(i, d_part_ct)},
                      {d_part_pending, quote_att_res.value(i, d_part_pending)},
                      {"data_size", quote_att_res.value(i, "data_size")},
                      {(d_database.tableContainsColumn(d_part_table, "unique_id") ? "unique_id" : ""), unique_id},
                      {"voice_note", quote_att_res.value(i, "voice_note")},
                      {"width", quote_att_res.value(i, "width")},
                      {"height", quote_att_res.value(i, "height")},
                      {"quote", 1},
                      {(d_database.tableContainsColumn(d_part_table, "data_hash") ? "data_hash" : ""),
                       (d_database.tableContainsColumn(d_part_table, "data_hash") ? quote_att_res.value(i, "data_hash") : std::any())},
                      {(d_database.tableContainsColumn(d_part_table, "data_hash_start") ? "data_hash_start" : ""),
                       (d_database.tableContainsColumn(d_part_table, "data_hash_start") ? quote_att_res.value(i, "data_hash_start") : std::any())},
                      {(d_database.tableContainsColumn(d_part_table, "data_hash_end") ? "data_hash_end" : ""),
                       (d_database.tableContainsColumn(d_part_table, "data_hash_end") ? quote_att_res.value(i, "data_hash_end") : std::any())}},
                     "_id", &retval))
      {
        Logger::error("Inserting part-data");
        continue;
      }
      long long int new_part_id = std::any_cast<long long int>(retval);

      // add attachment
      if (!bepaald::contains(d_attachments, std::pair{quote_att_res.valueAsInt(i, "_id", -1), quoted_unique_id}))
      {
        Logger::warning("Failed to find original of quoted attachment. Skipping.");
        d_database.exec("DELETE FROM " + d_part_table + " WHERE _id = ?", new_part_id);
        continue;
      }
      std::unique_ptr<AttachmentFrame> new_attachment_frame(new AttachmentFrame(*d_attachments.at({quote_att_res.valueAsInt(i, "_id", -1), quoted_unique_id}).get()));
      //std::cout << "Creating new attachment: " << quote_att_res.valueAsInt(i, "_id", -1) << ", "  << quoted_unique_id << " -> " << new_part_id << ", " << unique_id << std::endl;
      new_attachment_frame->setRowId(new_part_id);
      new_attachment_frame->setAttachmentId(unique_id);
      //new_attachment_frame->printInfo();
      d_attachments.emplace(std::make_pair(new_part_id, unique_id), new_attachment_frame.release());
    }
  }
  return true;
}