2020-08-19 16:30:05 +02:00
|
|
|
/*
|
|
|
|
Copyright 2020 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.
|
|
|
|
*/
|
|
|
|
|
2020-08-31 14:21:18 +02:00
|
|
|
import {getPrevContentFromStateEvent} from "../common.js";
|
|
|
|
|
2020-06-26 23:26:24 +02:00
|
|
|
export const EVENT_TYPE = "m.room.member";
|
|
|
|
|
|
|
|
export class RoomMember {
|
|
|
|
constructor(data) {
|
|
|
|
this._data = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static fromMemberEvent(roomId, memberEvent) {
|
|
|
|
const userId = memberEvent && memberEvent.state_key;
|
2020-08-19 16:28:09 +02:00
|
|
|
if (typeof userId !== "string") {
|
2020-06-26 23:26:24 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-08-27 09:51:44 +02:00
|
|
|
const content = memberEvent.content;
|
2020-08-31 14:21:18 +02:00
|
|
|
const prevContent = getPrevContentFromStateEvent(memberEvent);
|
2020-08-27 09:51:44 +02:00
|
|
|
const membership = content?.membership;
|
|
|
|
// fall back to prev_content for these as synapse doesn't (always?)
|
|
|
|
// put them on content for "leave" memberships
|
|
|
|
const displayName = content?.displayname || prevContent?.displayname;
|
|
|
|
const avatarUrl = content?.avatar_url || prevContent?.avatar_url;
|
|
|
|
return this._validateAndCreateMember(roomId, userId, membership, displayName, avatarUrl);
|
2020-08-20 15:23:56 +02:00
|
|
|
}
|
2020-08-27 09:51:44 +02:00
|
|
|
/**
|
|
|
|
* Creates a (historical) member from a member event that is the next member event
|
|
|
|
* after the point in time where we need a member for. This will use `prev_content`.
|
|
|
|
*/
|
2020-08-20 15:23:56 +02:00
|
|
|
static fromReplacingMemberEvent(roomId, memberEvent) {
|
|
|
|
const userId = memberEvent && memberEvent.state_key;
|
|
|
|
if (typeof userId !== "string") {
|
|
|
|
return;
|
|
|
|
}
|
2020-08-31 14:21:18 +02:00
|
|
|
const content = getPrevContentFromStateEvent(memberEvent);
|
2020-08-27 09:51:44 +02:00
|
|
|
return this._validateAndCreateMember(roomId, userId,
|
|
|
|
content?.membership,
|
|
|
|
content?.displayname,
|
|
|
|
content?.avatar_url
|
|
|
|
);
|
2020-08-20 15:23:56 +02:00
|
|
|
}
|
|
|
|
|
2020-08-27 09:51:44 +02:00
|
|
|
static _validateAndCreateMember(roomId, userId, membership, displayName, avatarUrl) {
|
2020-08-19 16:10:07 +02:00
|
|
|
if (typeof membership !== "string") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return new RoomMember({
|
|
|
|
roomId,
|
|
|
|
userId,
|
|
|
|
membership,
|
|
|
|
avatarUrl,
|
|
|
|
displayName,
|
2020-06-26 23:26:24 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-27 09:51:00 +02:00
|
|
|
/**
|
|
|
|
* @return {String?} the display name, if any
|
|
|
|
*/
|
2020-08-21 18:13:53 +02:00
|
|
|
get displayName() {
|
|
|
|
return this._data.displayName;
|
|
|
|
}
|
|
|
|
|
2020-08-27 09:51:00 +02:00
|
|
|
/**
|
|
|
|
* @return {String} the display name or userId
|
|
|
|
*/
|
|
|
|
get name() {
|
|
|
|
return this._data.displayName || this._data.userId;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {String?} the avatar mxc url, if any
|
|
|
|
*/
|
2020-08-21 18:13:53 +02:00
|
|
|
get avatarUrl() {
|
|
|
|
return this._data.avatarUrl;
|
|
|
|
}
|
|
|
|
|
2020-06-26 23:26:24 +02:00
|
|
|
get roomId() {
|
|
|
|
return this._data.roomId;
|
|
|
|
}
|
|
|
|
|
|
|
|
get userId() {
|
|
|
|
return this._data.userId;
|
|
|
|
}
|
|
|
|
|
|
|
|
serialize() {
|
2020-08-19 16:10:07 +02:00
|
|
|
return this._data;
|
2020-06-26 23:26:24 +02:00
|
|
|
}
|
|
|
|
}
|
2020-08-31 09:50:57 +02:00
|
|
|
|
|
|
|
export class MemberChange {
|
|
|
|
constructor(roomId, memberEvent) {
|
|
|
|
this._roomId = roomId;
|
|
|
|
this._memberEvent = memberEvent;
|
|
|
|
this._member = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
get member() {
|
|
|
|
if (!this._member) {
|
|
|
|
this._member = RoomMember.fromMemberEvent(this._roomId, this._memberEvent);
|
|
|
|
}
|
|
|
|
return this._member;
|
|
|
|
}
|
|
|
|
|
2020-08-31 14:11:08 +02:00
|
|
|
get roomId() {
|
|
|
|
return this._roomId;
|
|
|
|
}
|
|
|
|
|
|
|
|
get userId() {
|
2020-08-31 09:50:57 +02:00
|
|
|
return this._memberEvent.state_key;
|
|
|
|
}
|
|
|
|
|
|
|
|
previousMembership() {
|
2020-08-31 14:21:18 +02:00
|
|
|
return getPrevContentFromStateEvent(this._memberEvent)?.membership;
|
2020-08-31 09:50:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
membership() {
|
|
|
|
return this._memberEvent.content?.membership;
|
|
|
|
}
|
|
|
|
}
|