Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Member Panel - PR 3 - Update powerlevels from state event #432

Merged
merged 8 commits into from
Aug 6, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/matrix/room/Room.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 "./timeline/PowerLevels.js";
bwindels marked this conversation as resolved.
Show resolved Hide resolved

const EVENT_ENCRYPTED_TYPE = "m.room.encrypted";

Expand Down Expand Up @@ -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,
Expand All @@ -182,6 +184,7 @@ export class Room extends BaseRoom {
removedPendingEvents,
memberChanges,
heroChanges,
powerLevelsEvent,
shouldFlushKeyShares,
};
}
Expand All @@ -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);
Expand Down Expand Up @@ -236,6 +239,9 @@ export class Room extends BaseRoom {
emitChange = true;
}
}
if (powerLevelsEvent) {
this._updatePowerLevels(powerLevelsEvent);
}
if (emitChange) {
this._emitUpdate();
}
Expand All @@ -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;
}
Expand Down
2 changes: 2 additions & 0 deletions src/matrix/room/timeline/PowerLevels.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down