Skip to content

Commit 2f3f927

Browse files
committed
handle text and media server messages, update pushkeys
1 parent a4ccefe commit 2f3f927

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1171
-789
lines changed

ios/NotificationService/push_notification.pb.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ struct PushNotification: Sendable {
128128

129129
var kind: PushKind = .reaction
130130

131-
var messageID: Int64 {
132-
get {return _messageID ?? 0}
131+
var messageID: String {
132+
get {return _messageID ?? String()}
133133
set {_messageID = newValue}
134134
}
135135
/// Returns true if `messageID` has been explicitly set.
@@ -150,7 +150,7 @@ struct PushNotification: Sendable {
150150

151151
init() {}
152152

153-
fileprivate var _messageID: Int64? = nil
153+
fileprivate var _messageID: String? = nil
154154
fileprivate var _reactionContent: String? = nil
155155
}
156156

@@ -177,8 +177,8 @@ struct PushUser: Sendable {
177177

178178
var blocked: Bool = false
179179

180-
var lastMessageID: Int64 {
181-
get {return _lastMessageID ?? 0}
180+
var lastMessageID: String {
181+
get {return _lastMessageID ?? String()}
182182
set {_lastMessageID = newValue}
183183
}
184184
/// Returns true if `lastMessageID` has been explicitly set.
@@ -192,7 +192,7 @@ struct PushUser: Sendable {
192192

193193
init() {}
194194

195-
fileprivate var _lastMessageID: Int64? = nil
195+
fileprivate var _lastMessageID: String? = nil
196196
}
197197

198198
struct PushKey: Sendable {
@@ -273,7 +273,7 @@ extension PushNotification: SwiftProtobuf.Message, SwiftProtobuf._MessageImpleme
273273
// enabled. https://github.com/apple/swift-protobuf/issues/1034
274274
switch fieldNumber {
275275
case 1: try { try decoder.decodeSingularEnumField(value: &self.kind) }()
276-
case 2: try { try decoder.decodeSingularInt64Field(value: &self._messageID) }()
276+
case 2: try { try decoder.decodeSingularStringField(value: &self._messageID) }()
277277
case 3: try { try decoder.decodeSingularStringField(value: &self._reactionContent) }()
278278
default: break
279279
}
@@ -289,7 +289,7 @@ extension PushNotification: SwiftProtobuf.Message, SwiftProtobuf._MessageImpleme
289289
try visitor.visitSingularEnumField(value: self.kind, fieldNumber: 1)
290290
}
291291
try { if let v = self._messageID {
292-
try visitor.visitSingularInt64Field(value: v, fieldNumber: 2)
292+
try visitor.visitSingularStringField(value: v, fieldNumber: 2)
293293
} }()
294294
try { if let v = self._reactionContent {
295295
try visitor.visitSingularStringField(value: v, fieldNumber: 3)
@@ -349,7 +349,7 @@ extension PushUser: SwiftProtobuf.Message, SwiftProtobuf._MessageImplementationB
349349
case 1: try { try decoder.decodeSingularInt64Field(value: &self.userID) }()
350350
case 2: try { try decoder.decodeSingularStringField(value: &self.displayName) }()
351351
case 3: try { try decoder.decodeSingularBoolField(value: &self.blocked) }()
352-
case 4: try { try decoder.decodeSingularInt64Field(value: &self._lastMessageID) }()
352+
case 4: try { try decoder.decodeSingularStringField(value: &self._lastMessageID) }()
353353
case 5: try { try decoder.decodeRepeatedMessageField(value: &self.pushKeys) }()
354354
default: break
355355
}
@@ -371,7 +371,7 @@ extension PushUser: SwiftProtobuf.Message, SwiftProtobuf._MessageImplementationB
371371
try visitor.visitSingularBoolField(value: self.blocked, fieldNumber: 3)
372372
}
373373
try { if let v = self._lastMessageID {
374-
try visitor.visitSingularInt64Field(value: v, fieldNumber: 4)
374+
try visitor.visitSingularStringField(value: v, fieldNumber: 4)
375375
} }()
376376
if !self.pushKeys.isEmpty {
377377
try visitor.visitRepeatedMessageField(value: self.pushKeys, fieldNumber: 5)

lib/src/constants/secure_storage_keys.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ class SecureStorageKeys {
66
static const String userData = 'userData';
77
static const String twonlySafeLastBackupHash = 'twonly_safe_last_backup_hash';
88

9-
static const String receivingPushKeys = 'receiving_push_keys';
10-
static const String sendingPushKeys = 'sending_push_keys';
9+
static const String receivingPushKeys = 'push_keys_receiving';
10+
static const String sendingPushKeys = 'push_keys_sending';
1111
}

lib/src/database/daos/contacts.dao.dart

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,7 @@ class ContactsDao extends DatabaseAccessor<TwonlyDB> with _$ContactsDaoMixin {
120120
Stream<List<Contact>> watchNotAcceptedContacts() {
121121
return (select(contacts)
122122
..where(
123-
(t) =>
124-
t.accepted.equals(false) &
125-
t.archived.equals(false) &
126-
t.blocked.equals(false),
123+
(t) => t.accepted.equals(false) & t.blocked.equals(false),
127124
))
128125
.watch();
129126
// return (select(contacts)).watch();

lib/src/database/daos/groups.dao.dart

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,18 @@ class GroupsDao extends DatabaseAccessor<TwonlyDB> with _$GroupsDaoMixin {
1212
GroupsDao(super.db);
1313

1414
Future<bool> isContactInGroup(int contactId, String groupId) async {
15-
final entry = await (select(groupMembers)
16-
..where(
17-
(t) => t.contactId.equals(contactId) & t.groupId.equals(groupId)))
15+
final entry = await (select(groupMembers)..where(
16+
// ignore: require_trailing_commas
17+
(t) => t.contactId.equals(contactId) & t.groupId.equals(groupId)))
1818
.getSingleOrNull();
1919
return entry != null;
2020
}
21+
22+
Future<void> updateGroup(
23+
String groupId,
24+
GroupsCompanion updates,
25+
) async {
26+
await (update(groups)..where((c) => c.groupId.equals(groupId)))
27+
.write(updates);
28+
}
2129
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import 'package:drift/drift.dart';
2+
import 'package:twonly/src/database/tables/mediafiles.table.dart';
3+
import 'package:twonly/src/database/twonly.db.dart';
4+
import 'package:twonly/src/utils/log.dart';
5+
6+
part 'mediafiles.dao.g.dart';
7+
8+
@DriftAccessor(tables: [MediaFiles])
9+
class MediaFilesDao extends DatabaseAccessor<TwonlyDB>
10+
with _$MediaFilesDaoMixin {
11+
// this constructor is required so that the main database can create an instance
12+
// of this object.
13+
// ignore: matching_super_parameters
14+
MediaFilesDao(super.db);
15+
16+
Future<MediaFile?> insertMedia(MediaFilesCompanion mediaFile) async {
17+
try {
18+
final rowId = await into(mediaFiles).insert(mediaFile);
19+
20+
return await (select(mediaFiles)..where((t) => t.rowId.equals(rowId)))
21+
.getSingle();
22+
} catch (e) {
23+
Log.error('Could not insert media file: $e');
24+
return null;
25+
}
26+
}
27+
28+
Future<void> updateMedia(
29+
String mediaId,
30+
MediaFilesCompanion updates,
31+
) async {
32+
await (update(mediaFiles)..where((c) => c.mediaId.equals(mediaId)))
33+
.write(updates);
34+
}
35+
36+
Future<MediaFile?> getMediaFileById(String mediaId) async {
37+
return (select(mediaFiles)..where((t) => t.mediaId.equals(mediaId)))
38+
.getSingleOrNull();
39+
}
40+
41+
Future<void> resetPendingDownloadState() async {
42+
await (update(mediaFiles)
43+
..where(
44+
(c) => c.downloadState.equals(
45+
DownloadState.downloading.name,
46+
),
47+
))
48+
.write(
49+
const MediaFilesCompanion(
50+
downloadState: Value(DownloadState.pending),
51+
),
52+
);
53+
}
54+
}

lib/src/database/daos/mediafiles.dao.g.dart

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/database/daos/messages.dao.dart

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
import 'package:drift/drift.dart';
2+
import 'package:twonly/globals.dart';
23
import 'package:twonly/src/database/tables/contacts.table.dart';
4+
import 'package:twonly/src/database/tables/groups.table.dart';
35
import 'package:twonly/src/database/tables/mediafiles.table.dart';
46
import 'package:twonly/src/database/tables/messages.table.dart';
57
import 'package:twonly/src/database/twonly.db.dart';
68
import 'package:twonly/src/services/mediafile.service.dart';
9+
import 'package:twonly/src/utils/log.dart';
710

811
part 'messages.dao.g.dart';
912

10-
@DriftAccessor(tables: [Messages, Contacts, MediaFiles, MessageHistories])
13+
@DriftAccessor(
14+
tables: [
15+
Messages,
16+
Contacts,
17+
MediaFiles,
18+
MessageHistories,
19+
Groups,
20+
],
21+
)
1122
class MessagesDao extends DatabaseAccessor<TwonlyDB> with _$MessagesDaoMixin {
1223
// this constructor is required so that the main database can create an instance
1324
// of this object.
@@ -156,22 +167,6 @@ class MessagesDao extends DatabaseAccessor<TwonlyDB> with _$MessagesDaoMixin {
156167
// .write(updates);
157168
// }
158169

159-
// Future<void> resetPendingDownloadState() {
160-
// // All media files in the downloading state are reset to the pending state
161-
// // When the app is used in mobile network, they will not be downloaded at the start
162-
// // if they are not yet downloaded...
163-
// const updates =
164-
// MessagesCompanion(downloadState: Value(DownloadState.pending));
165-
// return (update(messages)
166-
// ..where(
167-
// (t) =>
168-
// t.messageOtherId.isNotNull() &
169-
// t.downloadState.equals(DownloadState.downloading.index) &
170-
// t.kind.equals(MessageKind.media.name),
171-
// ))
172-
// .write(updates);
173-
// }
174-
175170
Future<void> handleMessageDeletion(
176171
int contactId,
177172
String messageId,
@@ -278,28 +273,33 @@ class MessagesDao extends DatabaseAccessor<TwonlyDB> with _$MessagesDaoMixin {
278273
// .write(updatedValues);
279274
// }
280275

281-
// Future<void> updateMessageByMessageId(
282-
// int messageId,
283-
// MessagesCompanion updatedValues,
284-
// ) {
285-
// return (update(messages)..where((c) => c.messageId.equals(messageId)))
286-
// .write(updatedValues);
287-
// }
276+
Future<void> updateMessageId(
277+
String messageId,
278+
MessagesCompanion updatedValues,
279+
) {
280+
return (update(messages)..where((c) => c.messageId.equals(messageId)))
281+
.write(updatedValues);
282+
}
288283

289-
// Future<int?> insertMessage(MessagesCompanion message) async {
290-
// try {
291-
// await (update(contacts)
292-
// ..where(
293-
// (c) => c.userId.equals(message.contactId.value),
294-
// ))
295-
// .write(ContactsCompanion(lastMessageExchange: Value(DateTime.now())));
296-
297-
// return await into(messages).insert(message);
298-
// } catch (e) {
299-
// Log.error('Error while inserting message: $e');
300-
// return null;
301-
// }
302-
// }
284+
Future<Message?> insertMessage(MessagesCompanion message) async {
285+
try {
286+
final rowId = await into(messages).insert(message);
287+
288+
await twonlyDB.groupsDao.updateGroup(
289+
message.groupId.value,
290+
GroupsCompanion(
291+
lastMessageExchange: Value(DateTime.now()),
292+
archived: const Value(false),
293+
),
294+
);
295+
296+
return await (select(messages)..where((t) => t.rowId.equals(rowId)))
297+
.getSingle();
298+
} catch (e) {
299+
Log.error('Could not insert message: $e');
300+
return null;
301+
}
302+
}
303303

304304
// Future<void> deleteMessagesByContactId(int contactId) {
305305
// return (delete(messages)

lib/src/database/daos/messages.dao.g.dart

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/database/daos/reactions.dao.dart

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@ class ReactionsDao extends DatabaseAccessor<TwonlyDB> with _$ReactionsDaoMixin {
3131
))
3232
.go();
3333
if (emoji != null) {
34-
await into(reactions).insert(ReactionsCompanion(
35-
messageId: Value(messageId),
36-
emoji: Value(emoji),
37-
senderId: Value(contactId),
38-
));
34+
await into(reactions).insert(
35+
ReactionsCompanion(
36+
messageId: Value(messageId),
37+
emoji: Value(emoji),
38+
senderId: Value(contactId),
39+
),
40+
);
3941
}
4042
} catch (e) {
4143
Log.error(e);

lib/src/database/daos/receipts.dao.dart

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ class ReceiptsDao extends DatabaseAccessor<TwonlyDB> with _$ReceiptsDaoMixin {
1515

1616
Future<void> confirmReceipt(String receiptId, int fromUserId) async {
1717
final receipt = await (select(receipts)
18-
..where((t) =>
19-
t.receiptId.equals(receiptId) & t.contactId.equals(fromUserId)))
18+
..where(
19+
(t) =>
20+
t.receiptId.equals(receiptId) & t.contactId.equals(fromUserId),
21+
))
2022
.getSingleOrNull();
2123

2224
if (receipt == null) return;
@@ -26,7 +28,7 @@ class ReceiptsDao extends DatabaseAccessor<TwonlyDB> with _$ReceiptsDaoMixin {
2628
..where((t) => t.messageId.equals(receipt.messageId!)))
2729
.write(
2830
const MessagesCompanion(
29-
acknowledgeByUser: Value(true),
31+
ackByUser: Value(true),
3032
),
3133
);
3234
}
@@ -39,6 +41,14 @@ class ReceiptsDao extends DatabaseAccessor<TwonlyDB> with _$ReceiptsDaoMixin {
3941
.go();
4042
}
4143

44+
Future<void> deleteReceipt(String receiptId) async {
45+
await (delete(receipts)
46+
..where(
47+
(t) => t.receiptId.equals(receiptId),
48+
))
49+
.go();
50+
}
51+
4252
Future<Receipt?> insertReceipt(ReceiptsCompanion entry) async {
4353
try {
4454
final id = await into(receipts).insert(entry);
@@ -49,4 +59,33 @@ class ReceiptsDao extends DatabaseAccessor<TwonlyDB> with _$ReceiptsDaoMixin {
4959
return null;
5060
}
5161
}
62+
63+
Future<Receipt?> getReceiptById(String receiptId) async {
64+
try {
65+
return await (select(receipts)
66+
..where(
67+
(t) => t.receiptId.equals(receiptId),
68+
))
69+
.getSingleOrNull();
70+
} catch (e) {
71+
Log.error(e);
72+
return null;
73+
}
74+
}
75+
76+
Future<List<Receipt>> getReceiptsNotAckByServer() async {
77+
return (select(receipts)
78+
..where(
79+
(t) => t.ackByServerAt.isNull(),
80+
))
81+
.get();
82+
}
83+
84+
Future<void> updateReceipt(
85+
String receiptId,
86+
ReceiptsCompanion updates,
87+
) async {
88+
await (update(receipts)..where((c) => c.receiptId.equals(receiptId)))
89+
.write(updates);
90+
}
5291
}

0 commit comments

Comments
 (0)