@vex-chat/libvex 6.7.0 → 7.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,7 +5,7 @@
5
5
  */
6
6
 
7
7
  import type { Message } from "../index.js";
8
- import type { Storage } from "../Storage.js";
8
+ import type { MessageUpdatePatch, Storage } from "../Storage.js";
9
9
  import type {
10
10
  PreKeysCrypto,
11
11
  SessionCrypto,
@@ -597,8 +597,6 @@ export class SqliteStorage extends EventEmitter implements Storage {
597
597
  }
598
598
  }
599
599
 
600
- // ── Devices ──────────────────────────────────────────────────────────────
601
-
602
600
  async savePreKeys(
603
601
  preKeys: UnsavedPreKey[],
604
602
  oneTime: boolean,
@@ -637,6 +635,8 @@ export class SqliteStorage extends EventEmitter implements Storage {
637
635
  return saved;
638
636
  }
639
637
 
638
+ // ── Devices ──────────────────────────────────────────────────────────────
639
+
640
640
  async saveSession(session: SessionSQL): Promise<void> {
641
641
  if (this.closing) {
642
642
  return;
@@ -703,6 +703,68 @@ export class SqliteStorage extends EventEmitter implements Storage {
703
703
  }
704
704
  }
705
705
 
706
+ async updateMessage(
707
+ mailID: string,
708
+ patch: MessageUpdatePatch,
709
+ ): Promise<boolean> {
710
+ if (this.isClosingNow()) {
711
+ return false;
712
+ }
713
+ await this.untilReady();
714
+ if (
715
+ patch.message === undefined &&
716
+ !Object.prototype.hasOwnProperty.call(patch, "extra")
717
+ ) {
718
+ return false;
719
+ }
720
+
721
+ const row = await this.db
722
+ .selectFrom("messages")
723
+ .selectAll()
724
+ .where("mailID", "=", mailID)
725
+ .executeTakeFirst();
726
+ if (!row) {
727
+ return false;
728
+ }
729
+
730
+ const current = (await this.decryptMessagesAsync([row]))[0];
731
+ if (!current) {
732
+ return false;
733
+ }
734
+ const next: Message = {
735
+ ...current,
736
+ ...(patch.message !== undefined ? { message: patch.message } : {}),
737
+ ...(Object.prototype.hasOwnProperty.call(patch, "extra")
738
+ ? { extra: patch.extra }
739
+ : {}),
740
+ };
741
+ const storedPlaintext = encodeStoredMessagePlaintext(next);
742
+ const fips = getCryptoProfile() === "fips";
743
+ const ct = fips
744
+ ? await xSecretboxAsync(
745
+ XUtils.decodeUTF8(storedPlaintext),
746
+ XUtils.decodeHex(row.nonce),
747
+ this.atRestAesKey,
748
+ )
749
+ : xSecretbox(
750
+ XUtils.decodeUTF8(storedPlaintext),
751
+ XUtils.decodeHex(row.nonce),
752
+ this.atRestAesKey,
753
+ );
754
+ if (this.isClosingNow()) {
755
+ return false;
756
+ }
757
+ const result = await this.db
758
+ .updateTable("messages")
759
+ .set({
760
+ extra: null,
761
+ message: XUtils.encodeHex(ct),
762
+ })
763
+ .where("mailID", "=", mailID)
764
+ .executeTakeFirst();
765
+ return Number(result.numUpdatedRows) > 0;
766
+ }
767
+
706
768
  // ── Purge ────────────────────────────────────────────────────────────────
707
769
 
708
770
  private async decryptMessagesAsync(