-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathAdminDeleteManager.swift
More file actions
171 lines (149 loc) · 5.15 KB
/
AdminDeleteManager.swift
File metadata and controls
171 lines (149 loc) · 5.15 KB
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
162
163
164
165
166
167
168
169
170
171
//
// Copyright 2026 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
//
public import LibSignalClient
import GRDB
public struct RemoteDeleteAuthor: Equatable {
public enum AuthorType: Equatable {
case admin(aci: Aci)
case regular
}
public let displayName: String
public let authorType: AuthorType
}
public class AdminDeleteManager {
let recipientDatabaseTable: RecipientDatabaseTable
let tsAccountManager: TSAccountManager
private let logger = PrefixedLogger(prefix: "AdminDelete")
init(
recipientDatabaseTable: RecipientDatabaseTable,
tsAccountManager: TSAccountManager,
) {
self.recipientDatabaseTable = recipientDatabaseTable
self.tsAccountManager = tsAccountManager
}
private func insertAdminDelete(
groupThread: TSGroupThread,
interactionId: Int64,
deleteAuthor: Aci,
tx: DBWriteTransaction,
) throws(TSMessage.RemoteDeleteError) {
guard
let groupModel = groupThread.groupModel as? TSGroupModelV2,
groupModel.membership.isFullMemberAndAdministrator(deleteAuthor)
else {
logger.error("Failed to process admin delete for non-admin")
throw .invalidDelete
}
guard
let deleteAuthorId = recipientDatabaseTable.fetchRecipient(
serviceId: deleteAuthor,
transaction: tx,
)?.id
else {
logger.error("Failed to process admin delete for missing signal recipient")
throw .invalidDelete
}
failIfThrows {
var adminDeleteRecord = AdminDeleteRecord(
interactionId: interactionId,
deleteAuthorId: deleteAuthorId,
)
try adminDeleteRecord.insert(tx.database)
}
}
public func tryToAdminDeleteMessage(
originalMessageAuthorAci: Aci,
deleteAuthorAci: Aci,
sentAtTimestamp: UInt64,
groupThread: TSGroupThread,
threadUniqueId: String?,
serverTimestamp: UInt64,
transaction: DBWriteTransaction,
) throws(TSMessage.RemoteDeleteError) {
guard SDS.fitsInInt64(sentAtTimestamp) else {
owsFailDebug("Unable to delete a message with invalid sentAtTimestamp: \(sentAtTimestamp)")
throw .invalidDelete
}
if
let threadUniqueId, let messageToDelete = InteractionFinder.findMessage(
withTimestamp: sentAtTimestamp,
threadId: threadUniqueId,
author: SignalServiceAddress(originalMessageAuthorAci),
transaction: transaction,
)
{
let allowDeleteTimeframe = RemoteConfig.current.adminDeleteMaxAgeInSeconds + .day
let latestMessage = try TSMessage.remotelyDeleteMessage(
messageToDelete,
deleteAuthorAci: deleteAuthorAci,
allowedDeleteTimeframeSeconds: allowDeleteTimeframe,
serverTimestamp: serverTimestamp,
transaction: transaction,
)
return try insertAdminDelete(
groupThread: groupThread,
interactionId: latestMessage.sqliteRowId!,
deleteAuthor: deleteAuthorAci,
tx: transaction,
)
} else {
throw .deletedMessageMissing
}
}
public func adminDeleteAuthor(interactionId: Int64, tx: DBReadTransaction) -> Aci? {
guard BuildFlags.AdminDelete.receive else {
return nil
}
return failIfThrows {
guard
let adminDeleteRecord = try AdminDeleteRecord
.filter(AdminDeleteRecord.Columns.interactionId == interactionId)
.fetchOne(tx.database)
else {
return nil
}
let signalRecipient = recipientDatabaseTable.fetchRecipient(
rowId: adminDeleteRecord.deleteAuthorId,
tx: tx,
)
return signalRecipient?.aci
}
}
public func canAdminDeleteMessage(
message: TSMessage,
thread: TSThread,
tx: DBReadTransaction,
) -> Bool {
guard BuildFlags.AdminDelete.send else {
return false
}
guard let groupThread = thread as? TSGroupThread else {
return false
}
guard let localAci = tsAccountManager.localIdentifiers(tx: tx)?.aci else {
return false
}
guard groupThread.groupModel.groupMembership.isFullMemberAndAdministrator(localAci) else {
return false
}
guard message.canBeRemotelyDeletedByAdmin else {
return false
}
return true
}
public func insertAdminDeleteForSignalRecipient(
_ recipientId: SignalRecipient.RowId,
interactionId: Int64,
tx: DBWriteTransaction,
) {
failIfThrows {
var adminDeleteRecord = AdminDeleteRecord(
interactionId: interactionId,
deleteAuthorId: recipientId,
)
try adminDeleteRecord.insert(tx.database)
}
}
}