Merge pull request #432 from MidhunSureshR/sync-powerlevels

Member Panel - PR 3 - Update powerlevels from state event
This commit is contained in:
Bruno Windels 2021-08-06 15:02:21 +00:00 committed by GitHub
commit 7d9ccb9a3a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 4 deletions

View file

@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
import {PowerLevels} from "../../../../matrix/room/timeline/PowerLevels.js";
import {PowerLevels} from "../../../../matrix/room/PowerLevels.js";
export function createMemberComparator(powerLevels) {
const collator = new Intl.Collator();

View file

@ -190,7 +190,7 @@ import {HomeServer as MockHomeServer} from "../../../../mocks/HomeServer.js";
import {BaseMessageTile} from "./tiles/BaseMessageTile.js";
import {MappedList} from "../../../../observable/list/MappedList.js";
import {ObservableValue} from "../../../../observable/ObservableValue.js";
import {PowerLevels} from "../../../../matrix/room/timeline/PowerLevels.js";
import {PowerLevels} from "../../../../matrix/room/PowerLevels.js";
export function tests() {
const fragmentIdComparer = new FragmentIdComparer([]);

View file

@ -28,7 +28,7 @@ import {EventEntry} from "./timeline/entries/EventEntry.js";
import {ObservedEventMap} from "./ObservedEventMap.js";
import {DecryptionSource} from "../e2ee/common.js";
import {ensureLogItem} from "../../logging/utils.js";
import {PowerLevels} from "./timeline/PowerLevels.js";
import {PowerLevels} from "./PowerLevels.js";
import {RetainedObservableValue} from "../../observable/ObservableValue.js";
const EVENT_ENCRYPTED_TYPE = "m.room.encrypted";

View file

@ -14,6 +14,8 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
export const EVENT_TYPE = "m.room.power_levels";
export class PowerLevels {
constructor({powerLevelEvent, createEvent, ownUserId, membership}) {
this._plEvent = powerLevelEvent;

View file

@ -23,6 +23,7 @@ import {WrappedError} from "../error.js"
import {Heroes} from "./members/Heroes.js";
import {AttachmentUpload} from "./AttachmentUpload.js";
import {DecryptionSource} from "../e2ee/common.js";
import {PowerLevels, EVENT_TYPE as POWERLEVELS_EVENT_TYPE } from "./PowerLevels.js";
const EVENT_ENCRYPTED_TYPE = "m.room.encrypted";
@ -173,6 +174,7 @@ export class Room extends BaseRoom {
if (Array.isArray(roomResponse.timeline?.events)) {
removedPendingEvents = await this._sendQueue.removeRemoteEchos(roomResponse.timeline.events, txn, log);
}
const powerLevelsEvent = this._getPowerLevelsEvent(roomResponse);
return {
summaryChanges,
roomEncryption,
@ -182,6 +184,7 @@ export class Room extends BaseRoom {
removedPendingEvents,
memberChanges,
heroChanges,
powerLevelsEvent,
shouldFlushKeyShares,
};
}
@ -194,7 +197,7 @@ export class Room extends BaseRoom {
afterSync(changes, log) {
const {
summaryChanges, newEntries, updatedEntries, newLiveKey,
removedPendingEvents, memberChanges,
removedPendingEvents, memberChanges, powerLevelsEvent,
heroChanges, roomEncryption
} = changes;
log.set("id", this.id);
@ -236,6 +239,9 @@ export class Room extends BaseRoom {
emitChange = true;
}
}
if (powerLevelsEvent) {
this._updatePowerLevels(powerLevelsEvent);
}
if (emitChange) {
this._emitUpdate();
}
@ -262,6 +268,23 @@ export class Room extends BaseRoom {
}
}
_getPowerLevelsEvent(roomResponse) {
const isPowerlevelEvent = event => event.state_key === "" && event.type === POWERLEVELS_EVENT_TYPE;
const powerLevelEvent = roomResponse.timeline?.events.find(isPowerlevelEvent) ?? roomResponse.state?.events.find(isPowerlevelEvent);
return powerLevelEvent;
}
_updatePowerLevels(powerLevelEvent) {
if (this._powerLevels) {
const newPowerLevels = new PowerLevels({
powerLevelEvent,
ownUserId: this._user.id,
membership: this.membership,
});
this._powerLevels.set(newPowerLevels);
}
}
needsAfterSyncCompleted({shouldFlushKeyShares}) {
return shouldFlushKeyShares;
}