1 package org
.asamk
.signal
.manager
.storage
.sendLog
;
3 import org
.asamk
.signal
.manager
.groups
.GroupId
;
4 import org
.asamk
.signal
.manager
.groups
.GroupUtils
;
5 import org
.asamk
.signal
.manager
.storage
.Database
;
6 import org
.asamk
.signal
.manager
.storage
.Utils
;
7 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientId
;
8 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientResolver
;
9 import org
.signal
.libsignal
.zkgroup
.InvalidInputException
;
10 import org
.signal
.libsignal
.zkgroup
.groups
.GroupMasterKey
;
11 import org
.slf4j
.Logger
;
12 import org
.slf4j
.LoggerFactory
;
13 import org
.whispersystems
.signalservice
.api
.crypto
.ContentHint
;
14 import org
.whispersystems
.signalservice
.api
.messages
.SendMessageResult
;
15 import org
.whispersystems
.signalservice
.internal
.push
.SignalServiceProtos
;
17 import java
.io
.IOException
;
18 import java
.sql
.Connection
;
19 import java
.sql
.SQLException
;
20 import java
.time
.Duration
;
21 import java
.util
.List
;
22 import java
.util
.Objects
;
23 import java
.util
.Optional
;
25 public class MessageSendLogStore
implements AutoCloseable
{
27 private static final Logger logger
= LoggerFactory
.getLogger(MessageSendLogStore
.class);
29 private static final String TABLE_MESSAGE_SEND_LOG
= "message_send_log";
30 private static final String TABLE_MESSAGE_SEND_LOG_CONTENT
= "message_send_log_content";
32 private static final Duration LOG_DURATION
= Duration
.ofDays(1);
34 private final RecipientResolver recipientResolver
;
35 private final Database database
;
36 private final Thread cleanupThread
;
38 public MessageSendLogStore(
39 final RecipientResolver recipientResolver
, final Database database
41 this.recipientResolver
= recipientResolver
;
42 this.database
= database
;
43 this.cleanupThread
= new Thread(() -> {
45 final var interval
= Duration
.ofHours(1).toMillis();
46 while (!Thread
.interrupted()) {
47 try (final var connection
= database
.getConnection()) {
48 deleteOutdatedEntries(connection
);
49 } catch (SQLException e
) {
50 logger
.warn("Deleting outdated entries failed");
53 Thread
.sleep(interval
);
55 } catch (InterruptedException e
) {
56 logger
.debug("Stopping msl cleanup thread");
59 cleanupThread
.setName("msl-cleanup");
60 cleanupThread
.setDaemon(true);
61 cleanupThread
.start();
64 public static void createSql(Connection connection
) throws SQLException
{
65 // When modifying the CREATE statement here, also add a migration in AccountDatabase.java
66 try (final var statement
= connection
.createStatement()) {
67 statement
.executeUpdate("""
68 CREATE TABLE message_send_log (
69 _id INTEGER PRIMARY KEY,
70 content_id INTEGER NOT NULL REFERENCES message_send_log_content (_id) ON DELETE CASCADE,
71 recipient_id INTEGER NOT NULL REFERENCES recipient (_id) ON DELETE CASCADE,
72 device_id INTEGER NOT NULL
74 CREATE TABLE message_send_log_content (
75 _id INTEGER PRIMARY KEY,
77 timestamp INTEGER NOT NULL,
78 content BLOB NOT NULL,
79 content_hint INTEGER NOT NULL,
80 urgent BOOLEAN NOT NULL
82 CREATE INDEX mslc_timestamp_index ON message_send_log_content (timestamp);
83 CREATE INDEX msl_recipient_index ON message_send_log (recipient_id, device_id, content_id);
84 CREATE INDEX msl_content_index ON message_send_log (content_id);
89 public List
<MessageSendLogEntry
> findMessages(
90 final RecipientId recipientId
, final int deviceId
, final long timestamp
, final boolean isSenderKey
93 SELECT group_id, content, content_hint
95 INNER JOIN %s lc ON l.content_id = lc._id
96 WHERE l.recipient_id = ? AND l.device_id = ? AND lc.timestamp = ?
97 """.formatted(TABLE_MESSAGE_SEND_LOG
, TABLE_MESSAGE_SEND_LOG_CONTENT
);
98 try (final var connection
= database
.getConnection()) {
99 deleteOutdatedEntries(connection
);
101 try (final var statement
= connection
.prepareStatement(sql
)) {
102 statement
.setLong(1, recipientId
.id());
103 statement
.setInt(2, deviceId
);
104 statement
.setLong(3, timestamp
);
105 try (var result
= Utils
.executeQueryForStream(statement
, resultSet
-> {
106 final var groupId
= Optional
.ofNullable(resultSet
.getBytes("group_id"))
107 .map(GroupId
::unknownVersion
);
108 final SignalServiceProtos
.Content content
;
110 content
= SignalServiceProtos
.Content
.parseFrom(resultSet
.getBinaryStream("content"));
111 } catch (IOException e
) {
112 logger
.warn("Failed to parse content from message send log", e
);
115 final var contentHint
= ContentHint
.fromType(resultSet
.getInt("content_hint"));
116 final var urgent
= resultSet
.getBoolean("urgent");
117 return new MessageSendLogEntry(groupId
, content
, contentHint
, urgent
);
119 return result
.filter(Objects
::nonNull
)
120 .filter(e
-> !isSenderKey
|| e
.groupId().isPresent())
124 } catch (SQLException e
) {
125 logger
.warn("Failed read from message send log", e
);
130 public long insertIfPossible(
131 long sentTimestamp
, SendMessageResult sendMessageResult
, ContentHint contentHint
, boolean urgent
133 final RecipientDevices recipientDevice
= getRecipientDevices(sendMessageResult
);
134 if (recipientDevice
== null) {
138 return insert(List
.of(recipientDevice
),
140 sendMessageResult
.getSuccess().getContent().get(),
145 public long insertIfPossible(
146 long sentTimestamp
, List
<SendMessageResult
> sendMessageResults
, ContentHint contentHint
, boolean urgent
148 final var recipientDevices
= sendMessageResults
.stream()
149 .map(this::getRecipientDevices
)
150 .filter(Objects
::nonNull
)
152 if (recipientDevices
.isEmpty()) {
156 final var content
= sendMessageResults
.stream()
157 .filter(r
-> r
.isSuccess() && r
.getSuccess().getContent().isPresent())
158 .map(r
-> r
.getSuccess().getContent().get())
162 return insert(recipientDevices
, sentTimestamp
, content
, contentHint
, urgent
);
165 public void addRecipientToExistingEntryIfPossible(final long contentId
, final SendMessageResult sendMessageResult
) {
166 final RecipientDevices recipientDevice
= getRecipientDevices(sendMessageResult
);
167 if (recipientDevice
== null) {
171 insertRecipientsForExistingContent(contentId
, List
.of(recipientDevice
));
174 public void addRecipientToExistingEntryIfPossible(
175 final long contentId
, final List
<SendMessageResult
> sendMessageResults
177 final var recipientDevices
= sendMessageResults
.stream()
178 .map(this::getRecipientDevices
)
179 .filter(Objects
::nonNull
)
181 if (recipientDevices
.isEmpty()) {
185 insertRecipientsForExistingContent(contentId
, recipientDevices
);
188 public void deleteEntryForGroup(long sentTimestamp
, GroupId groupId
) {
191 WHERE lc.timestamp = ? AND lc.group_id = ?
192 """.formatted(TABLE_MESSAGE_SEND_LOG_CONTENT
);
193 try (final var connection
= database
.getConnection()) {
194 try (final var statement
= connection
.prepareStatement(sql
)) {
195 statement
.setLong(1, sentTimestamp
);
196 statement
.setBytes(2, groupId
.serialize());
197 statement
.executeUpdate();
199 } catch (SQLException e
) {
200 logger
.warn("Failed delete from message send log", e
);
204 public void deleteEntryForRecipientNonGroup(long sentTimestamp
, RecipientId recipientId
) {
207 WHERE lc.timestamp = ? AND lc.group_id IS NULL AND lc._id IN (SELECT content_id FROM %s l WHERE l.recipient_id = ?)
208 """.formatted(TABLE_MESSAGE_SEND_LOG_CONTENT
, TABLE_MESSAGE_SEND_LOG
);
209 try (final var connection
= database
.getConnection()) {
210 connection
.setAutoCommit(false);
211 try (final var statement
= connection
.prepareStatement(sql
)) {
212 statement
.setLong(1, sentTimestamp
);
213 statement
.setLong(2, recipientId
.id());
214 statement
.executeUpdate();
217 deleteOrphanedLogContents(connection
);
219 } catch (SQLException e
) {
220 logger
.warn("Failed delete from message send log", e
);
224 public void deleteEntryForRecipient(long sentTimestamp
, RecipientId recipientId
, int deviceId
) {
225 deleteEntriesForRecipient(List
.of(sentTimestamp
), recipientId
, deviceId
);
228 public void deleteEntriesForRecipient(List
<Long
> sentTimestamps
, RecipientId recipientId
, int deviceId
) {
231 WHERE l.content_id IN (SELECT _id FROM %s lc WHERE lc.timestamp = ?) AND l.recipient_id = ? AND l.device_id = ?
232 """.formatted(TABLE_MESSAGE_SEND_LOG
, TABLE_MESSAGE_SEND_LOG_CONTENT
);
233 try (final var connection
= database
.getConnection()) {
234 connection
.setAutoCommit(false);
235 try (final var statement
= connection
.prepareStatement(sql
)) {
236 for (final var sentTimestamp
: sentTimestamps
) {
237 statement
.setLong(1, sentTimestamp
);
238 statement
.setLong(2, recipientId
.id());
239 statement
.setInt(3, deviceId
);
240 statement
.executeUpdate();
244 deleteOrphanedLogContents(connection
);
246 } catch (SQLException e
) {
247 logger
.warn("Failed delete from message send log", e
);
252 public void close() {
253 cleanupThread
.interrupt();
255 cleanupThread
.join();
256 } catch (InterruptedException ignored
) {
260 private RecipientDevices
getRecipientDevices(final SendMessageResult sendMessageResult
) {
261 if (sendMessageResult
.isSuccess() && sendMessageResult
.getSuccess().getContent().isPresent()) {
262 final var recipientId
= recipientResolver
.resolveRecipient(sendMessageResult
.getAddress());
263 return new RecipientDevices(recipientId
, sendMessageResult
.getSuccess().getDevices());
270 final List
<RecipientDevices
> recipientDevices
,
271 final long sentTimestamp
,
272 final SignalServiceProtos
.Content content
,
273 final ContentHint contentHint
,
276 byte[] groupId
= getGroupId(content
);
279 INSERT INTO %s (timestamp, group_id, content, content_hint, urgent)
281 """.formatted(TABLE_MESSAGE_SEND_LOG_CONTENT
);
282 try (final var connection
= database
.getConnection()) {
283 connection
.setAutoCommit(false);
284 final long contentId
;
285 try (final var statement
= connection
.prepareStatement(sql
)) {
286 statement
.setLong(1, sentTimestamp
);
287 statement
.setBytes(2, groupId
);
288 statement
.setBytes(3, content
.toByteArray());
289 statement
.setInt(4, contentHint
.getType());
290 statement
.setBoolean(5, urgent
);
291 statement
.executeUpdate();
292 final var generatedKeys
= statement
.getGeneratedKeys();
293 if (generatedKeys
.next()) {
294 contentId
= generatedKeys
.getLong(1);
299 if (contentId
== -1) {
300 logger
.warn("Failed to insert message send log content");
303 insertRecipientsForExistingContent(contentId
, recipientDevices
, connection
);
307 } catch (SQLException e
) {
308 logger
.warn("Failed to insert into message send log", e
);
313 private byte[] getGroupId(final SignalServiceProtos
.Content content
) {
315 return !content
.hasDataMessage()
317 : content
.getDataMessage().hasGroup()
318 ? content
.getDataMessage().getGroup().getId().toByteArray()
319 : content
.getDataMessage().hasGroupV2()
320 ? GroupUtils
.getGroupIdV2(new GroupMasterKey(content
.getDataMessage()
323 .toByteArray())).serialize()
325 } catch (InvalidInputException e
) {
326 logger
.warn("Failed to parse groupId id from content");
331 private void insertRecipientsForExistingContent(
332 final long contentId
, final List
<RecipientDevices
> recipientDevices
334 try (final var connection
= database
.getConnection()) {
335 connection
.setAutoCommit(false);
336 insertRecipientsForExistingContent(contentId
, recipientDevices
, connection
);
338 } catch (SQLException e
) {
339 logger
.warn("Failed to append recipients to message send log", e
);
343 private void insertRecipientsForExistingContent(
344 final long contentId
, final List
<RecipientDevices
> recipientDevices
, final Connection connection
345 ) throws SQLException
{
347 INSERT INTO %s (recipient_id, device_id, content_id)
349 """.formatted(TABLE_MESSAGE_SEND_LOG
);
350 try (final var statement
= connection
.prepareStatement(sql
)) {
351 for (final var recipientDevice
: recipientDevices
) {
352 for (final var deviceId
: recipientDevice
.deviceIds()) {
353 statement
.setLong(1, recipientDevice
.recipientId().id());
354 statement
.setInt(2, deviceId
);
355 statement
.setLong(3, contentId
);
356 statement
.executeUpdate();
362 private void deleteOutdatedEntries(final Connection connection
) throws SQLException
{
366 """.formatted(TABLE_MESSAGE_SEND_LOG_CONTENT
);
367 try (final var statement
= connection
.prepareStatement(sql
)) {
368 statement
.setLong(1, System
.currentTimeMillis() - LOG_DURATION
.toMillis());
369 final var rowCount
= statement
.executeUpdate();
371 logger
.debug("Removed {} outdated entries from the message send log", rowCount
);
373 logger
.trace("No outdated entries to be removed from message send log.");
378 private void deleteOrphanedLogContents(final Connection connection
) throws SQLException
{
381 WHERE _id NOT IN (SELECT content_id FROM %s)
382 """.formatted(TABLE_MESSAGE_SEND_LOG_CONTENT
, TABLE_MESSAGE_SEND_LOG
);
383 try (final var statement
= connection
.prepareStatement(sql
)) {
384 statement
.executeUpdate();
388 private record RecipientDevices(RecipientId recipientId
, List
<Integer
> deviceIds
) {}