borogove
created pr with
borogove/preserve-status.1
commented
lgtm
cmds
checkout latest patchset:
ssh pr.pico.sh pull borogove:preserve-status | git am -3checkout specific patchset revision:
ssh pr.pico.sh pull borogove:preserve-status [rev] | git am -3add changes to patch request:
git format-patch main --stdout | ssh pr.pico.sh borogove:preserve-statusadd comment to patch request:
ssh pr.pico.sh comment borogove:preserve-status "lgtm!"
Patchset
borogove/preserve-status.1
Preserve status when syncing over top a sent message
Stephen Paul Weber
2026-11-24T17:06:56ZDiff stat
4 files changed, 516 insertions(+), 3 deletions(-)
Semantic diff summary
0 added,
1 modified,
0 signature changed,
0 removed
across 2 analyzed files
(2 files skipped: unsupported file type)
+9
-0
borogove/persistence/IDB.js
#
| ... | ... | @@ -1407,6 +1407,15 @@ export default async (dbname, media, tokenize, stemmer) => { | |
| 1407 | 1407 | message.versions.length < 1 | |
| 1408 | 1408 | ) { | |
| 1409 | 1409 | // Duplicate, we trust our own sent ids | |
| 1410 | + | if ( | |
| 1411 | + | [ | |
| 1412 | + | enums.borogove_MessageStatus.MessageDeliveredToDevice, | |
| 1413 | + | enums.borogove_MessageStatus.MessageFailedToSend, | |
| 1414 | + | ].includes(result.value.status) | |
| 1415 | + | ) { | |
| 1416 | + | message.status = result.value.status; | |
| 1417 | + | message.statusText = result.value.statusText; | |
| 1418 | + | } | |
| 1410 | 1419 | await promisifyRequest(result.delete()); | |
| 1411 | 1420 | } else if ( | |
| 1412 | 1421 | result?.value && |
+22
-2
borogove/persistence/Sqlite.hx
#
| ... | ... | @@ -1947,7 +1947,7 @@ class Sqlite implements Persistence implements KeyValueStore { | |
| 1947 | 1947 | insertCol("type", (m) -> originalMessage(m).type), | |
| 1948 | 1948 | insertCol("stanza", (m) -> originalMessage(m).asStanza().toString()), | |
| 1949 | 1949 | insertCol("status_text", (m) -> originalMessage(m).statusText), | |
| 1950 | - | insertCol("sort_id", (m) -> originalMessage(m).sortId), | |
| 1950 | + | insertCol("sort_id", (m) -> originalMessage(m).sortId ?? m.sortId), | |
| 1951 | 1951 | insertCol("debug", (m) -> originalMessage(m).debug == null ? null : Json.stringify(originalMessage(m).debug), "jsonb(?)"), | |
| 1952 | 1952 | insertCol("encryption", (m) -> { | |
| 1953 | 1953 | final message = originalMessage(m); |
| ... | ... | @@ -1958,7 +1958,27 @@ class Sqlite implements Persistence implements KeyValueStore { | |
| 1958 | 1958 | ||
| 1959 | 1959 | return db.exec( | |
| 1960 | 1960 | // Hmm, if there is an existing one this loses the original timestamp though | |
| 1961 | - | 'INSERT OR REPLACE INTO messages (${columns.map(c -> c.name).join(", ")}) VALUES $values', | |
| 1961 | + | 'INSERT INTO messages (${columns.map(c -> c.name).join(", ")}) VALUES $values | |
| 1962 | + | ON CONFLICT DO UPDATE SET | |
| 1963 | + | mam_id=excluded.mam_id, | |
| 1964 | + | mam_by=excluded.mam_by, | |
| 1965 | + | stanza_id=excluded.stanza_id, | |
| 1966 | + | correction_id=excluded.correction_id, | |
| 1967 | + | sync_point=excluded.sync_point, | |
| 1968 | + | chat_id=excluded.chat_id, | |
| 1969 | + | sender_id=excluded.sender_id, | |
| 1970 | + | time_sent=excluded.time_sent, | |
| 1971 | + | time_received=excluded.time_received, | |
| 1972 | + | direction=excluded.direction, | |
| 1973 | + | type=excluded.type, | |
| 1974 | + | stanza=excluded.stanza, | |
| 1975 | + | sort_id=excluded.sort_id, | |
| 1976 | + | debug=excluded.debug, | |
| 1977 | + | encryption=excluded.encryption, | |
| 1978 | + | status=CASE WHEN messages.status IN (${Std.string(MessageDeliveredToDevice)}, ${Std.string(MessageFailedToSend)}) | |
| 1979 | + | THEN messages.status ELSE excluded.status END, | |
| 1980 | + | status_text=CASE WHEN messages.status IN (${Std.string(MessageDeliveredToDevice)}, ${Std.string(MessageFailedToSend)}) | |
| 1981 | + | THEN messages.status_text ELSE excluded.status_text END', | |
| 1962 | 1982 | messages.flatMap(m -> columns.map(c -> c.value(m))), | |
| 1963 | 1983 | ); | |
| 1964 | 1984 | } |
+230
-1
test/TestSqlite.hx
#
| ... | ... | @@ -800,7 +800,8 @@ class TestSqlite extends utest.Test { | |
| 800 | 800 | ?source: String, | |
| 801 | 801 | ?body: String, | |
| 802 | 802 | ?received: Bool, | |
| 803 | - | ?status: MessageStatus | |
| 803 | + | ?status: MessageStatus, | |
| 804 | + | ?statusText: Null<String> | |
| 804 | 805 | }):ChatMessage { | |
| 805 | 806 | final senderId = params.senderId ?? "version@example.com"; | |
| 806 | 807 | final chatId = params.chatId ?? "chat@example.com"; |
| ... | ... | @@ -814,6 +815,7 @@ class TestSqlite extends utest.Test { | |
| 814 | 815 | builder.timeReceived = params.timestamp; | |
| 815 | 816 | builder.syncPoint = params.syncPoint ?? false; | |
| 816 | 817 | builder.status = params.status ?? MessagePending; | |
| 818 | + | builder.statusText = params.statusText; | |
| 817 | 819 | builder.versions = params.versions ?? []; | |
| 818 | 820 | builder.encryption = params.encryption; | |
| 819 | 821 | if (params.source != null) builder.debug = { source: params.source }; |
| ... | ... | @@ -1036,6 +1038,233 @@ class TestSqlite extends utest.Test { | |
| 1036 | 1038 | }); | |
| 1037 | 1039 | } | |
| 1038 | 1040 | ||
| 1041 | + | private function makeSentMessage(params: { | |
| 1042 | + | account: String, | |
| 1043 | + | chatId: String, | |
| 1044 | + | localId: String, | |
| 1045 | + | ?serverId: String, | |
| 1046 | + | sortId: String, | |
| 1047 | + | status: MessageStatus, | |
| 1048 | + | ?statusText: Null<String>, | |
| 1049 | + | ?syncPoint: Bool | |
| 1050 | + | }):ChatMessage { | |
| 1051 | + | final builder = new ChatMessageBuilder(); | |
| 1052 | + | builder.localId = params.localId; | |
| 1053 | + | builder.serverId = params.serverId; | |
| 1054 | + | builder.serverIdBy = params.serverId == null ? null : params.account; | |
| 1055 | + | builder.senderId = params.account; | |
| 1056 | + | builder.direction = MessageSent; | |
| 1057 | + | builder.sortId = params.sortId; | |
| 1058 | + | builder.syncPoint = params.syncPoint ?? false; | |
| 1059 | + | builder.status = params.status; | |
| 1060 | + | builder.statusText = params.statusText; | |
| 1061 | + | builder.setBody(Html.text("Hello there")); | |
| 1062 | + | builder.to = JID.parse(params.chatId); | |
| 1063 | + | builder.from = JID.parse(params.account); | |
| 1064 | + | builder.recipients = [builder.to]; | |
| 1065 | + | builder.replyTo = [builder.from]; | |
| 1066 | + | return builder.build(); | |
| 1067 | + | } | |
| 1068 | + | ||
| 1069 | + | public function testMamRestorePreservesDeliveredToDeviceStatus(async: Async) { | |
| 1070 | + | final account = "alice@example.com"; | |
| 1071 | + | final chatId = "hatter@example.com"; | |
| 1072 | + | final localId = "mam-status-device"; | |
| 1073 | + | ||
| 1074 | + | persistence.storeMessages(account, [makeSentMessage({ | |
| 1075 | + | account: account, | |
| 1076 | + | chatId: chatId, | |
| 1077 | + | localId: localId, | |
| 1078 | + | sortId: "mam-device-a0", | |
| 1079 | + | status: MessageDeliveredToDevice, | |
| 1080 | + | statusText: "Delivered to device" | |
| 1081 | + | })]).then(_ -> { | |
| 1082 | + | return persistence.storeMessages(account, [makeSentMessage({ | |
| 1083 | + | account: account, | |
| 1084 | + | chatId: chatId, | |
| 1085 | + | localId: localId, | |
| 1086 | + | serverId: "mam-device-srv", | |
| 1087 | + | sortId: "mam-device-b0", | |
| 1088 | + | status: MessageDeliveredToServer, | |
| 1089 | + | syncPoint: true | |
| 1090 | + | })]); | |
| 1091 | + | }).then(stored -> { | |
| 1092 | + | Assert.equals(MessageDeliveredToDevice, stored[0].status); | |
| 1093 | + | return persistence.getMessage(account, chatId, "mam-device-srv", null); | |
| 1094 | + | }).then(fetched -> { | |
| 1095 | + | Assert.notNull(fetched); | |
| 1096 | + | Assert.equals(MessageDeliveredToDevice, fetched.status); | |
| 1097 | + | Assert.equals("Delivered to device", fetched.statusText); | |
| 1098 | + | // The rest of the MAM copy is still allowed to enrich the local message | |
| 1099 | + | Assert.equals("mam-device-srv", fetched.serverId); | |
| 1100 | + | Assert.equals(account, fetched.serverIdBy); | |
| 1101 | + | Assert.isTrue(fetched.syncPoint); | |
| 1102 | + | return persistence.getMessagesBefore(account, chatId, null); | |
| 1103 | + | }).then(messages -> { | |
| 1104 | + | Assert.equals(1, messages.length); | |
| 1105 | + | async.done(); | |
| 1106 | + | }).catchError(e -> { | |
| 1107 | + | Assert.fail(Std.string(e)); | |
| 1108 | + | async.done(); | |
| 1109 | + | }); | |
| 1110 | + | } | |
| 1111 | + | ||
| 1112 | + | public function testMamRestorePreservesFailedToSendStatus(async: Async) { | |
| 1113 | + | final account = "alice@example.com"; | |
| 1114 | + | final chatId = "hatter@example.com"; | |
| 1115 | + | final localId = "mam-status-failed"; | |
| 1116 | + | ||
| 1117 | + | persistence.storeMessages(account, [makeSentMessage({ | |
| 1118 | + | account: account, | |
| 1119 | + | chatId: chatId, | |
| 1120 | + | localId: localId, | |
| 1121 | + | sortId: "mam-failed-a0", | |
| 1122 | + | status: MessageFailedToSend, | |
| 1123 | + | statusText: "Message too old to auto-resend" | |
| 1124 | + | })]).then(_ -> { | |
| 1125 | + | return persistence.storeMessages(account, [makeSentMessage({ | |
| 1126 | + | account: account, | |
| 1127 | + | chatId: chatId, | |
| 1128 | + | localId: localId, | |
| 1129 | + | serverId: "mam-failed-srv", | |
| 1130 | + | sortId: "mam-failed-b0", | |
| 1131 | + | status: MessageDeliveredToServer, | |
| 1132 | + | syncPoint: true | |
| 1133 | + | })]); | |
| 1134 | + | }).then(_ -> { | |
| 1135 | + | return persistence.getMessage(account, chatId, "mam-failed-srv", null); | |
| 1136 | + | }).then(fetched -> { | |
| 1137 | + | Assert.notNull(fetched); | |
| 1138 | + | Assert.equals(MessageFailedToSend, fetched.status); | |
| 1139 | + | Assert.equals("Message too old to auto-resend", fetched.statusText); | |
| 1140 | + | Assert.equals("mam-failed-srv", fetched.serverId); | |
| 1141 | + | async.done(); | |
| 1142 | + | }).catchError(e -> { | |
| 1143 | + | Assert.fail(Std.string(e)); | |
| 1144 | + | async.done(); | |
| 1145 | + | }); | |
| 1146 | + | } | |
| 1147 | + | ||
| 1148 | + | public function testMamRestoreAdvancesPendingStatus(async: Async) { | |
| 1149 | + | final account = "alice@example.com"; | |
| 1150 | + | final chatId = "hatter@example.com"; | |
| 1151 | + | final localId = "mam-status-pending"; | |
| 1152 | + | ||
| 1153 | + | persistence.storeMessages(account, [makeSentMessage({ | |
| 1154 | + | account: account, | |
| 1155 | + | chatId: chatId, | |
| 1156 | + | localId: localId, | |
| 1157 | + | sortId: "mam-pending-a0", | |
| 1158 | + | status: MessagePending | |
| 1159 | + | })]).then(_ -> { | |
| 1160 | + | return persistence.storeMessages(account, [makeSentMessage({ | |
| 1161 | + | account: account, | |
| 1162 | + | chatId: chatId, | |
| 1163 | + | localId: localId, | |
| 1164 | + | serverId: "mam-pending-srv", | |
| 1165 | + | sortId: "mam-pending-b0", | |
| 1166 | + | status: MessageDeliveredToServer, | |
| 1167 | + | syncPoint: true | |
| 1168 | + | })]); | |
| 1169 | + | }).then(_ -> { | |
| 1170 | + | return persistence.getMessage(account, chatId, "mam-pending-srv", null); | |
| 1171 | + | }).then(fetched -> { | |
| 1172 | + | Assert.notNull(fetched); | |
| 1173 | + | Assert.equals(MessageDeliveredToServer, fetched.status); | |
| 1174 | + | return persistence.getMessagesByStatus(account, MessagePending); | |
| 1175 | + | }).then(pending -> { | |
| 1176 | + | Assert.equals(0, pending.length); | |
| 1177 | + | async.done(); | |
| 1178 | + | }).catchError(e -> { | |
| 1179 | + | Assert.fail(Std.string(e)); | |
| 1180 | + | async.done(); | |
| 1181 | + | }); | |
| 1182 | + | } | |
| 1183 | + | ||
| 1184 | + | public function testRepeatedMamRestoreKeepsStatusAndSingleRow(async: Async) { | |
| 1185 | + | final account = "alice@example.com"; | |
| 1186 | + | final chatId = "hatter@example.com"; | |
| 1187 | + | final localId = "mam-status-repeat"; | |
| 1188 | + | ||
| 1189 | + | persistence.storeMessages(account, [makeSentMessage({ | |
| 1190 | + | account: account, | |
| 1191 | + | chatId: chatId, | |
| 1192 | + | localId: localId, | |
| 1193 | + | sortId: "mam-repeat-a0", | |
| 1194 | + | status: MessageDeliveredToDevice, | |
| 1195 | + | statusText: "Delivered to device" | |
| 1196 | + | })]).then(_ -> { | |
| 1197 | + | return persistence.storeMessages(account, [makeSentMessage({ | |
| 1198 | + | account: account, | |
| 1199 | + | chatId: chatId, | |
| 1200 | + | localId: localId, | |
| 1201 | + | serverId: "mam-repeat-srv", | |
| 1202 | + | sortId: "mam-repeat-b0", | |
| 1203 | + | status: MessageDeliveredToServer, | |
| 1204 | + | syncPoint: true | |
| 1205 | + | })]); | |
| 1206 | + | }).then(_ -> { | |
| 1207 | + | return persistence.storeMessages(account, [makeSentMessage({ | |
| 1208 | + | account: account, | |
| 1209 | + | chatId: chatId, | |
| 1210 | + | localId: localId, | |
| 1211 | + | serverId: "mam-repeat-srv", | |
| 1212 | + | sortId: "mam-repeat-c0", | |
| 1213 | + | status: MessageDeliveredToServer, | |
| 1214 | + | syncPoint: true | |
| 1215 | + | })]); | |
| 1216 | + | }).then(_ -> { | |
| 1217 | + | return persistence.getMessagesBefore(account, chatId, null); | |
| 1218 | + | }).then(messages -> { | |
| 1219 | + | Assert.equals(1, messages.length); | |
| 1220 | + | Assert.equals(MessageDeliveredToDevice, messages[0].status); | |
| 1221 | + | Assert.equals("Delivered to device", messages[0].statusText); | |
| 1222 | + | Assert.equals("mam-repeat-srv", messages[0].serverId); | |
| 1223 | + | async.done(); | |
| 1224 | + | }).catchError(e -> { | |
| 1225 | + | Assert.fail(Std.string(e)); | |
| 1226 | + | async.done(); | |
| 1227 | + | }); | |
| 1228 | + | } | |
| 1229 | + | ||
| 1230 | + | public function testStoresMessageSortIdWhenVersionHasNone(async: Async) { | |
| 1231 | + | final account = "alice@example.com"; | |
| 1232 | + | final chatId = "hatter@example.com"; | |
| 1233 | + | final sid = "call-sort-sid"; | |
| 1234 | + | ||
| 1235 | + | final builder = new ChatMessageBuilder(); | |
| 1236 | + | builder.localId = sid; | |
| 1237 | + | builder.senderId = account; | |
| 1238 | + | builder.direction = MessageSent; | |
| 1239 | + | builder.type = MessageCall; | |
| 1240 | + | builder.to = JID.parse(chatId); | |
| 1241 | + | builder.from = JID.parse(account); | |
| 1242 | + | builder.recipients = [builder.to]; | |
| 1243 | + | builder.replyTo = [builder.from]; | |
| 1244 | + | builder.setBody(Html.text("call ringing")); | |
| 1245 | + | builder.payloads = [new Stanza("ringing", { xmlns: "urn:xmpp:jingle-message:0", id: sid })]; | |
| 1246 | + | // Bundled before the message has a sortId, exactly like mkCallMessage | |
| 1247 | + | builder.versions = [builder.build()]; | |
| 1248 | + | builder.sortId = "call-sort-b0"; | |
| 1249 | + | ||
| 1250 | + | persistence.storeMessages(account, [builder.build()]).then(stored -> { | |
| 1251 | + | Assert.equals(1, stored.length); | |
| 1252 | + | Assert.equals("call-sort-b0", stored[0].sortId); | |
| 1253 | + | return persistence.getMessage(account, chatId, null, sid); | |
| 1254 | + | }).then(fetched -> { | |
| 1255 | + | Assert.notNull(fetched); | |
| 1256 | + | Assert.equals("call-sort-b0", fetched.sortId); | |
| 1257 | + | return persistence.getMessagesBefore(account, chatId, null); | |
| 1258 | + | }).then(messages -> { | |
| 1259 | + | Assert.equals(1, messages.length); | |
| 1260 | + | Assert.equals("call-sort-b0", messages[0].sortId); | |
| 1261 | + | async.done(); | |
| 1262 | + | }).catchError(e -> { | |
| 1263 | + | Assert.fail(Std.string(e)); | |
| 1264 | + | async.done(); | |
| 1265 | + | }); | |
| 1266 | + | } | |
| 1267 | + | ||
| 1039 | 1268 | private function assertEncryption(message: Null<ChatMessage>, expected: EncryptionInfo) { | |
| 1040 | 1269 | Assert.notNull(message); | |
| 1041 | 1270 | Assert.notNull(message.encryption); |
+255
-0
test/persistence-tests.ts
#
| ... | ... | @@ -1283,6 +1283,261 @@ export function sharedPersistenceTests(test) { | |
| 1283 | 1283 | expect(updated.statusText).toBe("Delivered"); | |
| 1284 | 1284 | }); | |
| 1285 | 1285 | ||
| 1286 | + | const sentMessage = ( | |
| 1287 | + | borogove: any, | |
| 1288 | + | opts: { | |
| 1289 | + | account: string; | |
| 1290 | + | chatId: string; | |
| 1291 | + | localId: string; | |
| 1292 | + | serverId?: string; | |
| 1293 | + | sortId: string; | |
| 1294 | + | status: number; | |
| 1295 | + | statusText?: string | null; | |
| 1296 | + | syncPoint?: boolean; | |
| 1297 | + | }, | |
| 1298 | + | ) => { | |
| 1299 | + | const builder = new borogove.ChatMessageBuilder({ | |
| 1300 | + | localId: opts.localId, | |
| 1301 | + | serverId: opts.serverId ?? null, | |
| 1302 | + | serverIdBy: opts.serverId == null ? null : opts.account, | |
| 1303 | + | senderId: opts.account, | |
| 1304 | + | direction: borogove.MessageDirection.MessageSent, | |
| 1305 | + | }); | |
| 1306 | + | builder.sortId = opts.sortId; | |
| 1307 | + | builder.syncPoint = opts.syncPoint ?? false; | |
| 1308 | + | builder.setBody(borogove.Html.text("Hello there")); | |
| 1309 | + | builder.to = borogove.JID.parse(opts.chatId); | |
| 1310 | + | builder.from = borogove.JID.parse(opts.account); | |
| 1311 | + | builder.recipients = [builder.to]; | |
| 1312 | + | builder.replyTo = [builder.from]; | |
| 1313 | + | builder.status = opts.status; | |
| 1314 | + | builder.statusText = opts.statusText ?? null; | |
| 1315 | + | return builder.build(); | |
| 1316 | + | }; | |
| 1317 | + | ||
| 1318 | + | test("MAM re-store preserves delivered-to-device status on sent messages", async ({ | |
| 1319 | + | borogove, | |
| 1320 | + | persistence, | |
| 1321 | + | }) => { | |
| 1322 | + | const account = "mam-status-alice@example.com"; | |
| 1323 | + | const chatId = "hatter@example.com"; | |
| 1324 | + | const localId = "mam-status-device"; | |
| 1325 | + | ||
| 1326 | + | await persistence.storeMessages(account, [ | |
| 1327 | + | sentMessage(borogove, { | |
| 1328 | + | account, | |
| 1329 | + | chatId, | |
| 1330 | + | localId, | |
| 1331 | + | sortId: "mam-device-a0", | |
| 1332 | + | status: borogove.MessageStatus.MessageDeliveredToDevice, | |
| 1333 | + | statusText: "Delivered to device", | |
| 1334 | + | }), | |
| 1335 | + | ]); | |
| 1336 | + | ||
| 1337 | + | const [stored] = await persistence.storeMessages(account, [ | |
| 1338 | + | sentMessage(borogove, { | |
| 1339 | + | account, | |
| 1340 | + | chatId, | |
| 1341 | + | localId, | |
| 1342 | + | serverId: "mam-device-srv", | |
| 1343 | + | sortId: "mam-device-b0", | |
| 1344 | + | status: borogove.MessageStatus.MessageDeliveredToServer, | |
| 1345 | + | syncPoint: true, | |
| 1346 | + | }), | |
| 1347 | + | ]); | |
| 1348 | + | ||
| 1349 | + | expect(stored.status).toBe(borogove.MessageStatus.MessageDeliveredToDevice); | |
| 1350 | + | ||
| 1351 | + | const fetched = await persistence.getMessage( | |
| 1352 | + | account, | |
| 1353 | + | chatId, | |
| 1354 | + | "mam-device-srv", | |
| 1355 | + | null, | |
| 1356 | + | ); | |
| 1357 | + | expect(fetched?.status).toBe( | |
| 1358 | + | borogove.MessageStatus.MessageDeliveredToDevice, | |
| 1359 | + | ); | |
| 1360 | + | expect(fetched?.statusText).toBe("Delivered to device"); | |
| 1361 | + | // The rest of the MAM copy is still allowed to enrich the local message | |
| 1362 | + | expect(fetched?.serverId).toBe("mam-device-srv"); | |
| 1363 | + | expect(fetched?.serverIdBy).toBe(account); | |
| 1364 | + | expect(fetched?.syncPoint).toBe(true); | |
| 1365 | + | ||
| 1366 | + | const messages = await persistence.getMessagesBefore(account, chatId); | |
| 1367 | + | expect(messages.length).toBe(1); | |
| 1368 | + | }); | |
| 1369 | + | ||
| 1370 | + | test("MAM re-store preserves failed-to-send status on sent messages", async ({ | |
| 1371 | + | borogove, | |
| 1372 | + | persistence, | |
| 1373 | + | }) => { | |
| 1374 | + | const account = "mam-status-alice@example.com"; | |
| 1375 | + | const chatId = "hatter@example.com"; | |
| 1376 | + | const localId = "mam-status-failed"; | |
| 1377 | + | ||
| 1378 | + | await persistence.storeMessages(account, [ | |
| 1379 | + | sentMessage(borogove, { | |
| 1380 | + | account, | |
| 1381 | + | chatId, | |
| 1382 | + | localId, | |
| 1383 | + | sortId: "mam-failed-a0", | |
| 1384 | + | status: borogove.MessageStatus.MessageFailedToSend, | |
| 1385 | + | statusText: "Message too old to auto-resend", | |
| 1386 | + | }), | |
| 1387 | + | ]); | |
| 1388 | + | ||
| 1389 | + | await persistence.storeMessages(account, [ | |
| 1390 | + | sentMessage(borogove, { | |
| 1391 | + | account, | |
| 1392 | + | chatId, | |
| 1393 | + | localId, | |
| 1394 | + | serverId: "mam-failed-srv", | |
| 1395 | + | sortId: "mam-failed-b0", | |
| 1396 | + | status: borogove.MessageStatus.MessageDeliveredToServer, | |
| 1397 | + | syncPoint: true, | |
| 1398 | + | }), | |
| 1399 | + | ]); | |
| 1400 | + | ||
| 1401 | + | const fetched = await persistence.getMessage( | |
| 1402 | + | account, | |
| 1403 | + | chatId, | |
| 1404 | + | "mam-failed-srv", | |
| 1405 | + | null, | |
| 1406 | + | ); | |
| 1407 | + | expect(fetched?.status).toBe(borogove.MessageStatus.MessageFailedToSend); | |
| 1408 | + | expect(fetched?.statusText).toBe("Message too old to auto-resend"); | |
| 1409 | + | expect(fetched?.serverId).toBe("mam-failed-srv"); | |
| 1410 | + | }); | |
| 1411 | + | ||
| 1412 | + | test("MAM re-store advances a pending sent message to delivered-to-server", async ({ | |
| 1413 | + | borogove, | |
| 1414 | + | persistence, | |
| 1415 | + | }) => { | |
| 1416 | + | const account = "mam-status-alice@example.com"; | |
| 1417 | + | const chatId = "hatter@example.com"; | |
| 1418 | + | const localId = "mam-status-pending"; | |
| 1419 | + | ||
| 1420 | + | await persistence.storeMessages(account, [ | |
| 1421 | + | sentMessage(borogove, { | |
| 1422 | + | account, | |
| 1423 | + | chatId, | |
| 1424 | + | localId, | |
| 1425 | + | sortId: "mam-pending-a0", | |
| 1426 | + | status: borogove.MessageStatus.MessagePending, | |
| 1427 | + | }), | |
| 1428 | + | ]); | |
| 1429 | + | ||
| 1430 | + | await persistence.storeMessages(account, [ | |
| 1431 | + | sentMessage(borogove, { | |
| 1432 | + | account, | |
| 1433 | + | chatId, | |
| 1434 | + | localId, | |
| 1435 | + | serverId: "mam-pending-srv", | |
| 1436 | + | sortId: "mam-pending-b0", | |
| 1437 | + | status: borogove.MessageStatus.MessageDeliveredToServer, | |
| 1438 | + | syncPoint: true, | |
| 1439 | + | }), | |
| 1440 | + | ]); | |
| 1441 | + | ||
| 1442 | + | const fetched = await persistence.getMessage( | |
| 1443 | + | account, | |
| 1444 | + | chatId, | |
| 1445 | + | "mam-pending-srv", | |
| 1446 | + | null, | |
| 1447 | + | ); | |
| 1448 | + | expect(fetched?.status).toBe( | |
| 1449 | + | borogove.MessageStatus.MessageDeliveredToServer, | |
| 1450 | + | ); | |
| 1451 | + | ||
| 1452 | + | const pending = await persistence.getMessagesByStatus( | |
| 1453 | + | account, | |
| 1454 | + | borogove.MessageStatus.MessagePending, | |
| 1455 | + | ); | |
| 1456 | + | expect(pending.length).toBe(0); | |
| 1457 | + | }); | |
| 1458 | + | ||
| 1459 | + | test("repeated MAM re-store keeps the preserved status and a single row", async ({ | |
| 1460 | + | borogove, | |
| 1461 | + | persistence, | |
| 1462 | + | }) => { | |
| 1463 | + | const account = "mam-status-alice@example.com"; | |
| 1464 | + | const chatId = "hatter@example.com"; | |
| 1465 | + | const localId = "mam-status-repeat"; | |
| 1466 | + | ||
| 1467 | + | await persistence.storeMessages(account, [ | |
| 1468 | + | sentMessage(borogove, { | |
| 1469 | + | account, | |
| 1470 | + | chatId, | |
| 1471 | + | localId, | |
| 1472 | + | sortId: "mam-repeat-a0", | |
| 1473 | + | status: borogove.MessageStatus.MessageDeliveredToDevice, | |
| 1474 | + | statusText: "Delivered to device", | |
| 1475 | + | }), | |
| 1476 | + | ]); | |
| 1477 | + | ||
| 1478 | + | for (const sortId of ["mam-repeat-b0", "mam-repeat-c0"]) { | |
| 1479 | + | await persistence.storeMessages(account, [ | |
| 1480 | + | sentMessage(borogove, { | |
| 1481 | + | account, | |
| 1482 | + | chatId, | |
| 1483 | + | localId, | |
| 1484 | + | serverId: "mam-repeat-srv", | |
| 1485 | + | sortId, | |
| 1486 | + | status: borogove.MessageStatus.MessageDeliveredToServer, | |
| 1487 | + | syncPoint: true, | |
| 1488 | + | }), | |
| 1489 | + | ]); | |
| 1490 | + | } | |
| 1491 | + | ||
| 1492 | + | const messages = await persistence.getMessagesBefore(account, chatId); | |
| 1493 | + | expect(messages.length).toBe(1); | |
| 1494 | + | expect(messages[0].status).toBe( | |
| 1495 | + | borogove.MessageStatus.MessageDeliveredToDevice, | |
| 1496 | + | ); | |
| 1497 | + | expect(messages[0].statusText).toBe("Delivered to device"); | |
| 1498 | + | expect(messages[0].serverId).toBe("mam-repeat-srv"); | |
| 1499 | + | }); | |
| 1500 | + | ||
| 1501 | + | test("stores the message sortId when its bundled version has none", async ({ | |
| 1502 | + | borogove, | |
| 1503 | + | persistence, | |
| 1504 | + | }) => { | |
| 1505 | + | const account = "call-sort-alice@example.com"; | |
| 1506 | + | const chatId = "call-sort-hatter@example.com"; | |
| 1507 | + | const sid = "call-sort-sid"; | |
| 1508 | + | ||
| 1509 | + | const builder = new borogove.ChatMessageBuilder({ | |
| 1510 | + | localId: sid, | |
| 1511 | + | senderId: account, | |
| 1512 | + | direction: borogove.MessageDirection.MessageSent, | |
| 1513 | + | type: borogove.MessageType.MessageCall, | |
| 1514 | + | }); | |
| 1515 | + | builder.to = borogove.JID.parse(chatId); | |
| 1516 | + | builder.from = borogove.JID.parse(account); | |
| 1517 | + | builder.recipients = [builder.to]; | |
| 1518 | + | builder.replyTo = [builder.from]; | |
| 1519 | + | builder.setBody(borogove.Html.text("call ringing")); | |
| 1520 | + | builder.payloads = [ | |
| 1521 | + | borogove.Stanza.parse( | |
| 1522 | + | `<ringing xmlns="urn:xmpp:jingle-message:0" id="${sid}"/>`, | |
| 1523 | + | ), | |
| 1524 | + | ]; | |
| 1525 | + | // Bundled before the message has a sortId, exactly like mkCallMessage | |
| 1526 | + | builder.versions = [builder.build()]; | |
| 1527 | + | builder.sortId = "call-sort-b0"; | |
| 1528 | + | ||
| 1529 | + | const [stored] = await persistence.storeMessages(account, [ | |
| 1530 | + | builder.build(), | |
| 1531 | + | ]); | |
| 1532 | + | expect(stored.sortId).toBe("call-sort-b0"); | |
| 1533 | + | ||
| 1534 | + | const fetched = await persistence.getMessage(account, chatId, null, sid); | |
| 1535 | + | expect(fetched?.sortId).toBe("call-sort-b0"); | |
| 1536 | + | ||
| 1537 | + | const messages = await persistence.getMessagesBefore(account, chatId); | |
| 1538 | + | expect(messages.map((message) => message.sortId)).toEqual(["call-sort-b0"]); | |
| 1539 | + | }); | |
| 1540 | + | ||
| 1286 | 1541 | test("removeAccount and listAccounts", async ({ persistence }) => { | |
| 1287 | 1542 | await persistence.storeLogin("alice@example.com", "client1", "Alice", null); | |
| 1288 | 1543 | await persistence.storeLogin("bob@example.com", "client2", "Bob", null); |