Skip to content

Commit f95954c

Browse files
authored
Add support for stable name for MSC4115 (#4232)
* add support for stable name for MSC4115 * fix types issues * prettier * actually, it still returns `undefined`
1 parent fa5f2d3 commit f95954c

File tree

3 files changed

+72
-9
lines changed

3 files changed

+72
-9
lines changed

spec/integ/crypto/crypto.spec.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,27 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("crypto (%s)", (backend: string,
630630
expect(ev.decryptionFailureReason).toEqual(DecryptionFailureCode.HISTORICAL_MESSAGE_USER_NOT_JOINED);
631631
});
632632

633+
newBackendOnly(
634+
"fails with NOT_JOINED if user is not member of room (MSC4115 unstable prefix)",
635+
async () => {
636+
fetchMock.get("path:/_matrix/client/v3/room_keys/version", {
637+
status: 404,
638+
body: { errcode: "M_NOT_FOUND", error: "No current backup version." },
639+
});
640+
expectAliceKeyQuery({ device_keys: { "@alice:localhost": {} }, failures: {} });
641+
await startClientAndAwaitFirstSync();
642+
643+
const ev = await sendEventAndAwaitDecryption({
644+
unsigned: {
645+
[UNSIGNED_MEMBERSHIP_FIELD.altName!]: "leave",
646+
},
647+
});
648+
expect(ev.decryptionFailureReason).toEqual(
649+
DecryptionFailureCode.HISTORICAL_MESSAGE_USER_NOT_JOINED,
650+
);
651+
},
652+
);
653+
633654
newBackendOnly(
634655
"fails with another error when the server reports user was a member of the room",
635656
async () => {
@@ -654,6 +675,30 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("crypto (%s)", (backend: string,
654675
},
655676
);
656677

678+
newBackendOnly(
679+
"fails with another error when the server reports user was a member of the room (MSC4115 unstable prefix)",
680+
async () => {
681+
// This tests that when the server reports that the user
682+
// was invited at the time the event was sent, then we
683+
// don't get a HISTORICAL_MESSAGE_USER_NOT_JOINED error,
684+
// and instead get some other error, since the user should
685+
// have gotten the key for the event.
686+
fetchMock.get("path:/_matrix/client/v3/room_keys/version", {
687+
status: 404,
688+
body: { errcode: "M_NOT_FOUND", error: "No current backup version." },
689+
});
690+
expectAliceKeyQuery({ device_keys: { "@alice:localhost": {} }, failures: {} });
691+
await startClientAndAwaitFirstSync();
692+
693+
const ev = await sendEventAndAwaitDecryption({
694+
unsigned: {
695+
[UNSIGNED_MEMBERSHIP_FIELD.altName!]: "invite",
696+
},
697+
});
698+
expect(ev.decryptionFailureReason).toEqual(DecryptionFailureCode.HISTORICAL_MESSAGE_NO_KEY_BACKUP);
699+
},
700+
);
701+
657702
newBackendOnly(
658703
"fails with another error when the server reports user was a member of the room",
659704
async () => {
@@ -676,6 +721,29 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("crypto (%s)", (backend: string,
676721
expect(ev.decryptionFailureReason).toEqual(DecryptionFailureCode.HISTORICAL_MESSAGE_NO_KEY_BACKUP);
677722
},
678723
);
724+
725+
newBackendOnly(
726+
"fails with another error when the server reports user was a member of the room (MSC4115 unstable prefix)",
727+
async () => {
728+
// This tests that when the server reports the user's
729+
// membership, and reports that the user was joined, then we
730+
// don't get a HISTORICAL_MESSAGE_USER_NOT_JOINED error, and
731+
// instead get some other error.
732+
fetchMock.get("path:/_matrix/client/v3/room_keys/version", {
733+
status: 404,
734+
body: { errcode: "M_NOT_FOUND", error: "No current backup version." },
735+
});
736+
expectAliceKeyQuery({ device_keys: { "@alice:localhost": {} }, failures: {} });
737+
await startClientAndAwaitFirstSync();
738+
739+
const ev = await sendEventAndAwaitDecryption({
740+
unsigned: {
741+
[UNSIGNED_MEMBERSHIP_FIELD.altName!]: "join",
742+
},
743+
});
744+
expect(ev.decryptionFailureReason).toEqual(DecryptionFailureCode.HISTORICAL_MESSAGE_NO_KEY_BACKUP);
745+
},
746+
);
679747
});
680748

681749
it("Decryption fails with Unable to decrypt for other errors", async () => {

src/@types/event.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
import { UnstableValue } from "../NamespacedValue";
17+
import { NamespacedValue, UnstableValue } from "../NamespacedValue";
1818
import {
1919
PolicyRuleEventContent,
2020
RoomAvatarEventContent,
@@ -302,7 +302,7 @@ export const UNSIGNED_THREAD_ID_FIELD = new UnstableValue("thread_id", "org.matr
302302
*
303303
* @experimental
304304
*/
305-
export const UNSIGNED_MEMBERSHIP_FIELD = new UnstableValue("membership", "io.element.msc4115.membership");
305+
export const UNSIGNED_MEMBERSHIP_FIELD = new NamespacedValue("membership", "io.element.msc4115.membership");
306306

307307
/**
308308
* Mapped type from event type to content type for all specified non-state room events.

src/models/event.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ export interface IUnsigned {
7777
"invite_room_state"?: StrippedState[];
7878
"m.relations"?: Record<RelationType | string, any>; // No common pattern for aggregated relations
7979
[UNSIGNED_THREAD_ID_FIELD.name]?: string;
80-
[UNSIGNED_MEMBERSHIP_FIELD.name]?: Membership | string;
8180
}
8281

8382
export interface IThreadBundledRelationship {
@@ -717,13 +716,9 @@ export class MatrixEvent extends TypedEventEmitter<MatrixEventEmittedEvents, Mat
717716
* @returns The user's room membership, or `undefined` if the server does
718717
* not report it.
719718
*/
720-
public getMembershipAtEvent(): Membership | string | undefined {
719+
public getMembershipAtEvent(): Optional<Membership | string> {
721720
const unsigned = this.getUnsigned();
722-
if (typeof unsigned[UNSIGNED_MEMBERSHIP_FIELD.name] === "string") {
723-
return unsigned[UNSIGNED_MEMBERSHIP_FIELD.name];
724-
} else {
725-
return undefined;
726-
}
721+
return UNSIGNED_MEMBERSHIP_FIELD.findIn<Membership | string>(unsigned);
727722
}
728723

729724
/**

0 commit comments

Comments
 (0)