You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-11-28 05:03:59 +03:00
introduce Room.myMembership event
As you don't always have your own member with lazy loading of members enabled, looking at the sync response section where a room appears is the most reliable way of determining the syncing user's membership in a room. Before we already used this to read the current room membership with `room.getMyMembership()`, but we were still using the `RoomMember.membership` event to detect when the syncing user's membership changed. This event will help make those checks work well with LL enabled.
This commit is contained in:
@@ -178,7 +178,7 @@ function Room(roomId, client, myUserId, opts) {
|
|||||||
|
|
||||||
// read by megolm; boolean value - null indicates "use global value"
|
// read by megolm; boolean value - null indicates "use global value"
|
||||||
this._blacklistUnverifiedDevices = null;
|
this._blacklistUnverifiedDevices = null;
|
||||||
this._syncedMembership = null;
|
this._selfMembership = null;
|
||||||
this._summaryHeroes = null;
|
this._summaryHeroes = null;
|
||||||
// awaited by getEncryptionTargetMembers while room members are loading
|
// awaited by getEncryptionTargetMembers while room members are loading
|
||||||
|
|
||||||
@@ -263,7 +263,7 @@ Room.prototype.getMyMembership = function() {
|
|||||||
return me.membership;
|
return me.membership;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return this._syncedMembership;
|
return this._selfMembership;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -278,7 +278,7 @@ Room.prototype.getDMInviter = function() {
|
|||||||
return me.getDMInviter();
|
return me.getDMInviter();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (this._syncedMembership === "invite") {
|
if (this._selfMembership === "invite") {
|
||||||
// fall back to summary information
|
// fall back to summary information
|
||||||
const memberCount = this.getInvitedAndJoinedMemberCount();
|
const memberCount = this.getInvitedAndJoinedMemberCount();
|
||||||
if (memberCount == 2 && this._summaryHeroes.length) {
|
if (memberCount == 2 && this._summaryHeroes.length) {
|
||||||
@@ -362,8 +362,15 @@ Room.prototype.getAvatarFallbackMember = function() {
|
|||||||
* Sets the membership this room was received as during sync
|
* Sets the membership this room was received as during sync
|
||||||
* @param {string} membership join | leave | invite
|
* @param {string} membership join | leave | invite
|
||||||
*/
|
*/
|
||||||
Room.prototype.setSyncedMembership = function(membership) {
|
Room.prototype.updateMyMembership = function(membership) {
|
||||||
this._syncedMembership = membership;
|
const prevMembership = this._selfMembership;
|
||||||
|
this._selfMembership = membership;
|
||||||
|
if (prevMembership !== membership) {
|
||||||
|
if (membership === "leave") {
|
||||||
|
this._cleanupAfterLeaving();
|
||||||
|
}
|
||||||
|
this.emit("Room.myMembership", this, membership, prevMembership);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Room.prototype._loadMembersFromServer = async function() {
|
Room.prototype._loadMembersFromServer = async function() {
|
||||||
@@ -470,7 +477,7 @@ Room.prototype.clearLoadedMembersIfNeeded = async function() {
|
|||||||
* called when sync receives this room in the leave section
|
* called when sync receives this room in the leave section
|
||||||
* to do cleanup after leaving a room. Possibly called multiple times.
|
* to do cleanup after leaving a room. Possibly called multiple times.
|
||||||
*/
|
*/
|
||||||
Room.prototype.onLeft = function() {
|
Room.prototype._cleanupAfterLeaving = function() {
|
||||||
this.clearLoadedMembersIfNeeded().catch((err) => {
|
this.clearLoadedMembersIfNeeded().catch((err) => {
|
||||||
console.error(`error after clearing loaded members from ` +
|
console.error(`error after clearing loaded members from ` +
|
||||||
`room ${this.roomId} after leaving`);
|
`room ${this.roomId} after leaving`);
|
||||||
|
|||||||
13
src/sync.js
13
src/sync.js
@@ -123,6 +123,7 @@ SyncApi.prototype.createRoom = function(roomId) {
|
|||||||
"Room.timelineReset",
|
"Room.timelineReset",
|
||||||
"Room.localEchoUpdated",
|
"Room.localEchoUpdated",
|
||||||
"Room.accountData",
|
"Room.accountData",
|
||||||
|
"Room.myMembership",
|
||||||
]);
|
]);
|
||||||
this._registerStateListeners(room);
|
this._registerStateListeners(room);
|
||||||
return room;
|
return room;
|
||||||
@@ -976,9 +977,10 @@ SyncApi.prototype._processSyncResponse = async function(
|
|||||||
// Handle invites
|
// Handle invites
|
||||||
inviteRooms.forEach(function(inviteObj) {
|
inviteRooms.forEach(function(inviteObj) {
|
||||||
const room = inviteObj.room;
|
const room = inviteObj.room;
|
||||||
room.setSyncedMembership("invite");
|
|
||||||
const stateEvents =
|
const stateEvents =
|
||||||
self._mapSyncEventsFormat(inviteObj.invite_state, room);
|
self._mapSyncEventsFormat(inviteObj.invite_state, room);
|
||||||
|
|
||||||
|
room.updateMyMembership("invite");
|
||||||
self._processRoomEvents(room, stateEvents);
|
self._processRoomEvents(room, stateEvents);
|
||||||
if (inviteObj.isBrandNewRoom) {
|
if (inviteObj.isBrandNewRoom) {
|
||||||
room.recalculate();
|
room.recalculate();
|
||||||
@@ -993,7 +995,6 @@ SyncApi.prototype._processSyncResponse = async function(
|
|||||||
// Handle joins
|
// Handle joins
|
||||||
await Promise.mapSeries(joinRooms, async function(joinObj) {
|
await Promise.mapSeries(joinRooms, async function(joinObj) {
|
||||||
const room = joinObj.room;
|
const room = joinObj.room;
|
||||||
room.setSyncedMembership("join");
|
|
||||||
const stateEvents = self._mapSyncEventsFormat(joinObj.state, room);
|
const stateEvents = self._mapSyncEventsFormat(joinObj.state, room);
|
||||||
const timelineEvents = self._mapSyncEventsFormat(joinObj.timeline, room);
|
const timelineEvents = self._mapSyncEventsFormat(joinObj.timeline, room);
|
||||||
const ephemeralEvents = self._mapSyncEventsFormat(joinObj.ephemeral);
|
const ephemeralEvents = self._mapSyncEventsFormat(joinObj.ephemeral);
|
||||||
@@ -1009,6 +1010,8 @@ SyncApi.prototype._processSyncResponse = async function(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
room.updateMyMembership("join");
|
||||||
|
|
||||||
joinObj.timeline = joinObj.timeline || {};
|
joinObj.timeline = joinObj.timeline || {};
|
||||||
|
|
||||||
if (joinObj.isBrandNewRoom) {
|
if (joinObj.isBrandNewRoom) {
|
||||||
@@ -1116,8 +1119,6 @@ SyncApi.prototype._processSyncResponse = async function(
|
|||||||
// Handle leaves (e.g. kicked rooms)
|
// Handle leaves (e.g. kicked rooms)
|
||||||
leaveRooms.forEach(function(leaveObj) {
|
leaveRooms.forEach(function(leaveObj) {
|
||||||
const room = leaveObj.room;
|
const room = leaveObj.room;
|
||||||
room.setSyncedMembership("leave");
|
|
||||||
|
|
||||||
const stateEvents =
|
const stateEvents =
|
||||||
self._mapSyncEventsFormat(leaveObj.state, room);
|
self._mapSyncEventsFormat(leaveObj.state, room);
|
||||||
const timelineEvents =
|
const timelineEvents =
|
||||||
@@ -1125,6 +1126,8 @@ SyncApi.prototype._processSyncResponse = async function(
|
|||||||
const accountDataEvents =
|
const accountDataEvents =
|
||||||
self._mapSyncEventsFormat(leaveObj.account_data);
|
self._mapSyncEventsFormat(leaveObj.account_data);
|
||||||
|
|
||||||
|
room.updateMyMembership("leave");
|
||||||
|
|
||||||
self._processRoomEvents(room, stateEvents, timelineEvents);
|
self._processRoomEvents(room, stateEvents, timelineEvents);
|
||||||
room.addAccountData(accountDataEvents);
|
room.addAccountData(accountDataEvents);
|
||||||
|
|
||||||
@@ -1145,8 +1148,6 @@ SyncApi.prototype._processSyncResponse = async function(
|
|||||||
accountDataEvents.forEach(function(e) {
|
accountDataEvents.forEach(function(e) {
|
||||||
client.emit("event", e);
|
client.emit("event", e);
|
||||||
});
|
});
|
||||||
|
|
||||||
room.onLeft();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// update the notification timeline, if appropriate.
|
// update the notification timeline, if appropriate.
|
||||||
|
|||||||
Reference in New Issue
Block a user