vector-im-hydrogen-web/src/matrix/room/members/RoomMember.js

138 lines
3.7 KiB
JavaScript
Raw Normal View History

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;
}
const content = memberEvent.content;
2020-08-31 14:21:18 +02:00
const prevContent = getPrevContentFromStateEvent(memberEvent);
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);
}
/**
* 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`.
*/
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);
return this._validateAndCreateMember(roomId, userId,
content?.membership,
content?.displayname,
content?.avatar_url
);
}
static _validateAndCreateMember(roomId, userId, membership, displayName, avatarUrl) {
if (typeof membership !== "string") {
return;
}
return new RoomMember({
roomId,
userId,
membership,
avatarUrl,
displayName,
2020-06-26 23:26:24 +02:00
});
}
2020-08-31 16:07:29 +02:00
get membership() {
return this._data.membership;
}
/**
* @return {String?} the display name, if any
*/
2020-08-21 18:13:53 +02:00
get displayName() {
return this._data.displayName;
}
/**
* @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() {
return this._data;
2020-06-26 23:26:24 +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;
}
get roomId() {
return this._roomId;
}
get userId() {
return this._memberEvent.state_key;
}
2020-08-31 16:07:18 +02:00
get previousMembership() {
2020-08-31 14:21:18 +02:00
return getPrevContentFromStateEvent(this._memberEvent)?.membership;
}
2020-08-31 16:07:18 +02:00
get membership() {
return this._memberEvent.content?.membership;
}
}