2021-04-20 16:11:21 +02:00
|
|
|
/*
|
|
|
|
Copyright 2021 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-09-16 10:23:03 +02:00
|
|
|
import {EventEmitter} from "../../utils/EventEmitter";
|
2021-04-20 16:11:21 +02:00
|
|
|
import {SummaryData, processStateEvent} from "./RoomSummary.js";
|
|
|
|
import {Heroes} from "./members/Heroes.js";
|
|
|
|
import {MemberChange, RoomMember, EVENT_TYPE as MEMBER_EVENT_TYPE} from "./members/RoomMember.js";
|
|
|
|
|
|
|
|
export class Invite extends EventEmitter {
|
2021-04-27 11:01:33 +02:00
|
|
|
constructor({roomId, user, hsApi, mediaRepository, emitCollectionRemove, emitCollectionUpdate, platform}) {
|
2021-04-20 16:11:21 +02:00
|
|
|
super();
|
|
|
|
this._roomId = roomId;
|
|
|
|
this._user = user;
|
|
|
|
this._hsApi = hsApi;
|
|
|
|
this._emitCollectionRemove = emitCollectionRemove;
|
2021-04-20 19:01:40 +02:00
|
|
|
this._emitCollectionUpdate = emitCollectionUpdate;
|
2021-04-27 11:01:33 +02:00
|
|
|
this._mediaRepository = mediaRepository;
|
2021-04-21 15:31:55 +02:00
|
|
|
this._platform = platform;
|
2021-04-20 16:11:21 +02:00
|
|
|
this._inviteData = null;
|
2021-04-20 19:01:40 +02:00
|
|
|
this._accepting = false;
|
|
|
|
this._rejecting = false;
|
|
|
|
this._accepted = false;
|
|
|
|
this._rejected = false;
|
2021-04-20 16:11:21 +02:00
|
|
|
}
|
|
|
|
|
2021-04-22 17:22:47 +02:00
|
|
|
get isInvite() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-04-20 16:11:21 +02:00
|
|
|
get id() {
|
|
|
|
return this._roomId;
|
|
|
|
}
|
|
|
|
|
|
|
|
get name() {
|
|
|
|
return this._inviteData.name || this._inviteData.canonicalAlias;
|
|
|
|
}
|
|
|
|
|
|
|
|
get isDirectMessage() {
|
|
|
|
return this._inviteData.isDirectMessage;
|
|
|
|
}
|
|
|
|
|
|
|
|
get avatarUrl() {
|
|
|
|
return this._inviteData.avatarUrl;
|
|
|
|
}
|
|
|
|
|
2021-07-01 00:21:11 +02:00
|
|
|
/** @see BaseRoom.avatarColorId */
|
2021-06-30 01:50:42 +02:00
|
|
|
get avatarColorId() {
|
2021-07-17 22:29:20 +02:00
|
|
|
return this._inviteData.avatarColorId || this.id;
|
2021-06-30 01:50:42 +02:00
|
|
|
}
|
|
|
|
|
2021-04-20 16:11:21 +02:00
|
|
|
get timestamp() {
|
|
|
|
return this._inviteData.timestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
get isEncrypted() {
|
|
|
|
return this._inviteData.isEncrypted;
|
|
|
|
}
|
|
|
|
|
|
|
|
get inviter() {
|
|
|
|
return this._inviter;
|
|
|
|
}
|
|
|
|
|
2021-04-27 14:26:40 +02:00
|
|
|
get isPublic() {
|
|
|
|
return this._inviteData.joinRule === "public";
|
|
|
|
}
|
|
|
|
|
|
|
|
get canonicalAlias() {
|
|
|
|
return this._inviteData.canonicalAlias;
|
2021-04-20 16:11:21 +02:00
|
|
|
}
|
|
|
|
|
2021-04-21 15:31:55 +02:00
|
|
|
async accept(log = null) {
|
|
|
|
await this._platform.logger.wrapOrRun(log, "acceptInvite", async log => {
|
|
|
|
this._accepting = true;
|
|
|
|
this._emitChange("accepting");
|
|
|
|
await this._hsApi.join(this._roomId, {log}).response();
|
|
|
|
});
|
2021-04-20 16:11:21 +02:00
|
|
|
}
|
|
|
|
|
2021-04-21 15:31:55 +02:00
|
|
|
async reject(log = null) {
|
|
|
|
await this._platform.logger.wrapOrRun(log, "rejectInvite", async log => {
|
|
|
|
this._rejecting = true;
|
|
|
|
this._emitChange("rejecting");
|
|
|
|
await this._hsApi.leave(this._roomId, {log}).response();
|
|
|
|
});
|
2021-04-20 19:01:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get accepting() {
|
|
|
|
return this._accepting;
|
|
|
|
}
|
|
|
|
|
|
|
|
get accepted() {
|
|
|
|
return this._accepted;
|
|
|
|
}
|
2021-04-20 16:11:21 +02:00
|
|
|
|
2021-04-20 19:01:40 +02:00
|
|
|
get rejecting() {
|
|
|
|
return this._rejecting;
|
|
|
|
}
|
|
|
|
|
|
|
|
get rejected() {
|
|
|
|
return this._rejected;
|
|
|
|
}
|
|
|
|
|
2021-04-27 11:01:33 +02:00
|
|
|
get mediaRepository() {
|
|
|
|
return this._mediaRepository;
|
|
|
|
}
|
|
|
|
|
2021-04-20 19:01:40 +02:00
|
|
|
_emitChange(params) {
|
|
|
|
this.emit("change");
|
2021-04-26 10:45:27 +02:00
|
|
|
this._emitCollectionUpdate(this, params);
|
2021-04-20 16:11:21 +02:00
|
|
|
}
|
|
|
|
|
2021-04-20 18:09:48 +02:00
|
|
|
load(inviteData, log) {
|
|
|
|
log.set("id", this.id);
|
2021-04-20 16:11:21 +02:00
|
|
|
this._inviteData = inviteData;
|
|
|
|
this._inviter = inviteData.inviter ? new RoomMember(inviteData.inviter) : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
async writeSync(membership, roomResponse, txn, log) {
|
|
|
|
if (membership === "invite") {
|
2021-04-20 17:56:53 +02:00
|
|
|
log.set("id", this.id);
|
|
|
|
log.set("add", true);
|
|
|
|
const inviteState = roomResponse["invite_state"]?.events;
|
|
|
|
if (!Array.isArray(inviteState)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const summaryData = this._createSummaryData(inviteState);
|
|
|
|
let heroes;
|
|
|
|
if (!summaryData.name && !summaryData.canonicalAlias) {
|
|
|
|
heroes = await this._createHeroes(inviteState);
|
|
|
|
}
|
|
|
|
const myInvite = this._getMyInvite(inviteState);
|
|
|
|
if (!myInvite) {
|
2021-04-20 16:11:21 +02:00
|
|
|
return null;
|
2021-04-20 17:56:53 +02:00
|
|
|
}
|
|
|
|
const inviter = this._getInviter(myInvite, inviteState);
|
|
|
|
const inviteData = this._createData(inviteState, myInvite, inviter, summaryData, heroes);
|
|
|
|
txn.invites.set(inviteData);
|
|
|
|
return {inviteData, inviter};
|
|
|
|
} else {
|
|
|
|
log.set("id", this.id);
|
|
|
|
log.set("membership", membership);
|
|
|
|
txn.invites.remove(this.id);
|
2021-04-20 19:01:40 +02:00
|
|
|
return {removed: true, membership};
|
2021-04-20 16:11:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-11 13:05:02 +02:00
|
|
|
afterSync(changes, log) {
|
|
|
|
log.set("id", this.id);
|
2021-04-20 16:11:21 +02:00
|
|
|
if (changes) {
|
2021-04-20 19:01:40 +02:00
|
|
|
if (changes.removed) {
|
|
|
|
this._accepting = false;
|
|
|
|
this._rejecting = false;
|
|
|
|
if (changes.membership === "join") {
|
|
|
|
this._accepted = true;
|
|
|
|
} else {
|
|
|
|
this._rejected = true;
|
|
|
|
}
|
|
|
|
this.emit("change");
|
|
|
|
} else {
|
2021-05-11 13:07:57 +02:00
|
|
|
// no emit change, adding to the collection is done by sync
|
2021-04-20 19:01:40 +02:00
|
|
|
this._inviteData = changes.inviteData;
|
|
|
|
this._inviter = changes.inviter;
|
|
|
|
}
|
2021-04-20 16:11:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_createData(inviteState, myInvite, inviter, summaryData, heroes) {
|
|
|
|
const name = heroes ? heroes.roomName : summaryData.name;
|
|
|
|
const avatarUrl = heroes ? heroes.roomAvatarUrl : summaryData.avatarUrl;
|
2021-07-17 22:23:13 +02:00
|
|
|
const avatarColorId = heroes?.roomAvatarColorId || this.id;
|
2021-04-20 16:11:21 +02:00
|
|
|
return {
|
|
|
|
roomId: this.id,
|
|
|
|
isEncrypted: !!summaryData.encryption,
|
|
|
|
isDirectMessage: this._isDirectMessage(myInvite),
|
|
|
|
// type:
|
|
|
|
name,
|
|
|
|
avatarUrl,
|
2021-07-01 00:07:27 +02:00
|
|
|
avatarColorId,
|
2021-04-20 16:11:21 +02:00
|
|
|
canonicalAlias: summaryData.canonicalAlias,
|
2021-04-21 15:31:55 +02:00
|
|
|
timestamp: this._platform.clock.now(),
|
2021-04-20 16:11:21 +02:00
|
|
|
joinRule: this._getJoinRule(inviteState),
|
|
|
|
inviter: inviter?.serialize(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
_isDirectMessage(myInvite) {
|
|
|
|
return !!(myInvite?.content?.is_direct);
|
|
|
|
}
|
|
|
|
|
|
|
|
_createSummaryData(inviteState) {
|
|
|
|
return inviteState.reduce(processStateEvent, new SummaryData(null, this.id));
|
|
|
|
}
|
|
|
|
|
|
|
|
async _createHeroes(inviteState) {
|
|
|
|
const members = inviteState.filter(e => e.type === MEMBER_EVENT_TYPE);
|
|
|
|
const otherMembers = members.filter(e => e.state_key !== this._user.id);
|
|
|
|
const memberChanges = otherMembers.reduce((map, e) => {
|
|
|
|
const member = RoomMember.fromMemberEvent(this.id, e);
|
|
|
|
map.set(member.userId, new MemberChange(member, null));
|
|
|
|
return map;
|
|
|
|
}, new Map());
|
|
|
|
const otherUserIds = otherMembers.map(e => e.state_key);
|
|
|
|
const heroes = new Heroes(this.id);
|
|
|
|
const changes = await heroes.calculateChanges(otherUserIds, memberChanges, null);
|
|
|
|
// we don't get an actual lazy-loading m.heroes summary on invites,
|
|
|
|
// so just count the members by hand
|
|
|
|
const countSummary = new SummaryData(null, this.id);
|
|
|
|
countSummary.joinCount = members.reduce((sum, e) => sum + (e.content?.membership === "join" ? 1 : 0), 0);
|
|
|
|
countSummary.inviteCount = members.reduce((sum, e) => sum + (e.content?.membership === "invite" ? 1 : 0), 0);
|
|
|
|
heroes.applyChanges(changes, countSummary);
|
|
|
|
return heroes;
|
|
|
|
}
|
|
|
|
|
|
|
|
_getMyInvite(inviteState) {
|
|
|
|
return inviteState.find(e => e.type === MEMBER_EVENT_TYPE && e.state_key === this._user.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
_getInviter(myInvite, inviteState) {
|
|
|
|
const inviterMemberEvent = inviteState.find(e => e.type === MEMBER_EVENT_TYPE && e.state_key === myInvite.sender);
|
|
|
|
if (inviterMemberEvent) {
|
|
|
|
return RoomMember.fromMemberEvent(this.id, inviterMemberEvent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_getJoinRule(inviteState) {
|
|
|
|
const event = inviteState.find(e => e.type === "m.room.join_rules");
|
|
|
|
if (event) {
|
|
|
|
return event.content?.join_rule;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
import {NullLogItem} from "../../logging/NullLogger.js";
|
|
|
|
import {Clock as MockClock} from "../../mocks/Clock.js";
|
|
|
|
import {default as roomInviteFixture} from "../../fixtures/matrix/invites/room.js";
|
|
|
|
import {default as dmInviteFixture} from "../../fixtures/matrix/invites/dm.js";
|
|
|
|
|
|
|
|
export function tests() {
|
|
|
|
|
|
|
|
function createStorage() {
|
|
|
|
const invitesMap = new Map();
|
|
|
|
return {
|
|
|
|
invitesMap,
|
|
|
|
invites: {
|
|
|
|
set(invite) {
|
|
|
|
invitesMap.set(invite.roomId, invite);
|
2021-04-20 17:57:03 +02:00
|
|
|
},
|
|
|
|
remove(roomId) {
|
|
|
|
invitesMap.delete(roomId);
|
2021-04-20 16:11:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const roomId = "!123:hs.tld";
|
|
|
|
const aliceAvatarUrl = "mxc://hs.tld/def456";
|
|
|
|
const roomAvatarUrl = "mxc://hs.tld/roomavatar";
|
|
|
|
|
|
|
|
return {
|
|
|
|
"invite for room has correct fields": async assert => {
|
|
|
|
const invite = new Invite({
|
|
|
|
roomId,
|
2021-04-21 15:31:55 +02:00
|
|
|
platform: {clock: new MockClock(1001)},
|
2021-04-20 16:11:21 +02:00
|
|
|
user: {id: "@bob:hs.tld"}
|
|
|
|
});
|
|
|
|
const txn = createStorage();
|
|
|
|
const changes = await invite.writeSync("invite", roomInviteFixture, txn, new NullLogItem());
|
|
|
|
assert.equal(txn.invitesMap.get(roomId).roomId, roomId);
|
2021-05-11 17:04:36 +02:00
|
|
|
invite.afterSync(changes, new NullLogItem());
|
2021-04-20 16:11:21 +02:00
|
|
|
assert.equal(invite.name, "Invite example");
|
|
|
|
assert.equal(invite.avatarUrl, roomAvatarUrl);
|
2021-04-27 14:26:40 +02:00
|
|
|
assert.equal(invite.isPublic, false);
|
2021-04-20 16:11:21 +02:00
|
|
|
assert.equal(invite.timestamp, 1001);
|
|
|
|
assert.equal(invite.isEncrypted, false);
|
|
|
|
assert.equal(invite.isDirectMessage, false);
|
|
|
|
assert(invite.inviter);
|
|
|
|
assert.equal(invite.inviter.userId, "@alice:hs.tld");
|
|
|
|
assert.equal(invite.inviter.displayName, "Alice");
|
|
|
|
assert.equal(invite.inviter.avatarUrl, aliceAvatarUrl);
|
|
|
|
},
|
|
|
|
"invite for encrypted DM has correct fields": async assert => {
|
|
|
|
const invite = new Invite({
|
|
|
|
roomId,
|
2021-04-21 15:31:55 +02:00
|
|
|
platform: {clock: new MockClock(1003)},
|
2021-04-20 16:11:21 +02:00
|
|
|
user: {id: "@bob:hs.tld"}
|
|
|
|
});
|
|
|
|
const txn = createStorage();
|
|
|
|
const changes = await invite.writeSync("invite", dmInviteFixture, txn, new NullLogItem());
|
|
|
|
assert.equal(txn.invitesMap.get(roomId).roomId, roomId);
|
2021-05-11 17:04:36 +02:00
|
|
|
invite.afterSync(changes, new NullLogItem());
|
2021-04-20 16:11:21 +02:00
|
|
|
assert.equal(invite.name, "Alice");
|
|
|
|
assert.equal(invite.avatarUrl, aliceAvatarUrl);
|
|
|
|
assert.equal(invite.timestamp, 1003);
|
|
|
|
assert.equal(invite.isEncrypted, true);
|
|
|
|
assert.equal(invite.isDirectMessage, true);
|
|
|
|
assert(invite.inviter);
|
|
|
|
assert.equal(invite.inviter.userId, "@alice:hs.tld");
|
|
|
|
assert.equal(invite.inviter.displayName, "Alice");
|
|
|
|
assert.equal(invite.inviter.avatarUrl, aliceAvatarUrl);
|
|
|
|
},
|
2021-04-20 17:34:45 +02:00
|
|
|
"load persisted invite has correct fields": async assert => {
|
|
|
|
const writeInvite = new Invite({
|
|
|
|
roomId,
|
2021-04-21 15:31:55 +02:00
|
|
|
platform: {clock: new MockClock(1003)},
|
2021-04-20 17:34:45 +02:00
|
|
|
user: {id: "@bob:hs.tld"}
|
|
|
|
});
|
|
|
|
const txn = createStorage();
|
|
|
|
await writeInvite.writeSync("invite", dmInviteFixture, txn, new NullLogItem());
|
|
|
|
const invite = new Invite({roomId});
|
2021-04-20 18:09:48 +02:00
|
|
|
invite.load(txn.invitesMap.get(roomId), new NullLogItem());
|
2021-04-20 17:34:45 +02:00
|
|
|
assert.equal(invite.name, "Alice");
|
|
|
|
assert.equal(invite.avatarUrl, aliceAvatarUrl);
|
|
|
|
assert.equal(invite.timestamp, 1003);
|
|
|
|
assert.equal(invite.isEncrypted, true);
|
|
|
|
assert.equal(invite.isDirectMessage, true);
|
|
|
|
assert(invite.inviter);
|
|
|
|
assert.equal(invite.inviter.userId, "@alice:hs.tld");
|
|
|
|
assert.equal(invite.inviter.displayName, "Alice");
|
|
|
|
assert.equal(invite.inviter.avatarUrl, aliceAvatarUrl);
|
|
|
|
},
|
2021-05-05 17:47:46 +02:00
|
|
|
"syncing join sets accepted": async assert => {
|
|
|
|
let changeEmitCount = 0;
|
2021-04-20 17:57:03 +02:00
|
|
|
const invite = new Invite({
|
|
|
|
roomId,
|
2021-04-21 15:31:55 +02:00
|
|
|
platform: {clock: new MockClock(1003)},
|
2021-04-20 17:57:03 +02:00
|
|
|
user: {id: "@bob:hs.tld"},
|
|
|
|
});
|
2021-05-05 17:47:46 +02:00
|
|
|
invite.on("change", () => { changeEmitCount += 1; });
|
2021-04-20 17:57:03 +02:00
|
|
|
const txn = createStorage();
|
|
|
|
const changes = await invite.writeSync("invite", dmInviteFixture, txn, new NullLogItem());
|
|
|
|
assert.equal(txn.invitesMap.get(roomId).roomId, roomId);
|
2021-05-11 17:04:36 +02:00
|
|
|
invite.afterSync(changes, new NullLogItem());
|
2021-04-20 17:57:03 +02:00
|
|
|
const joinChanges = await invite.writeSync("join", null, txn, new NullLogItem());
|
2021-05-05 17:47:46 +02:00
|
|
|
assert.strictEqual(changeEmitCount, 0);
|
2021-05-11 17:04:36 +02:00
|
|
|
invite.afterSync(joinChanges, new NullLogItem());
|
2021-05-05 17:47:46 +02:00
|
|
|
assert.strictEqual(changeEmitCount, 1);
|
2021-04-20 17:57:03 +02:00
|
|
|
assert.equal(txn.invitesMap.get(roomId), undefined);
|
2021-04-21 15:32:13 +02:00
|
|
|
assert.equal(invite.rejected, false);
|
|
|
|
assert.equal(invite.accepted, true);
|
2021-04-20 17:57:03 +02:00
|
|
|
}
|
2021-04-20 16:11:21 +02:00
|
|
|
}
|
|
|
|
}
|