Skip to content

Commit 0b970b0

Browse files
committed
Wire up helpers for checking if a user can invite to a room and getting its join rule
1 parent 371ca00 commit 0b970b0

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/models/room-state.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {EventEmitter} from "events";
2323
import {RoomMember} from "./room-member";
2424
import {logger} from '../logger';
2525
import * as utils from "../utils";
26+
import {EventType} from "../@types/event";
2627

2728
// possible statuses for out-of-band member loading
2829
const OOB_STATUS_NOTSTARTED = 1;
@@ -718,6 +719,16 @@ RoomState.prototype.mayTriggerNotifOfType = function(notifLevelKey, userId) {
718719
return member.powerLevel >= notifLevel;
719720
};
720721

722+
/**
723+
* Returns the join rule based on the m.room.join_rule state event, defaulting to `invite`.
724+
* @returns {string} the join_rule applied to this room
725+
*/
726+
RoomState.prototype.getJoinRule = function() {
727+
const joinRuleEvent = this.getStateEvents(EventType.RoomJoinRules, "");
728+
const joinRuleContent = joinRuleEvent ? joinRuleEvent.getContent() : {};
729+
return joinRuleContent["join_rule"] || "invite";
730+
};
731+
721732

722733
function _updateThirdPartyTokenCache(roomState, memberEvent) {
723734
if (!memberEvent.getContent().third_party_invite) {

src/models/room.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {RoomMember} from "./room-member";
3030
import {RoomSummary} from "./room-summary";
3131
import {logger} from '../logger';
3232
import {ReEmitter} from '../ReEmitter';
33+
import {EventType} from "../@types/event";
3334

3435
// These constants are used as sane defaults when the homeserver doesn't support
3536
// the m.room_versions capability. In practice, KNOWN_SAFE_ROOM_VERSION should be
@@ -1831,6 +1832,30 @@ Room.prototype.maySendMessage = function() {
18311832
this.currentState.maySendEvent('m.room.message', this.myUserId);
18321833
};
18331834

1835+
/**
1836+
* Returns whether the given user has permissions to issue an invite for this room.
1837+
* @param {string} userId the ID of the Matrix user to check permissions for
1838+
* @returns {boolean} true if the user should be permitted to issue invites for this room.
1839+
*/
1840+
Room.prototype.canInvite = function(userId) {
1841+
let canInvite = this.getMyMembership() === "join";
1842+
const powerLevelsEvent = this.currentState.getStateEvents(EventType.RoomPowerLevels, "");
1843+
const powerLevels = powerLevelsEvent && powerLevelsEvent.getContent();
1844+
const me = this.getMember(userId);
1845+
if (powerLevels && me && powerLevels.invite > me.powerLevel) {
1846+
canInvite = false;
1847+
}
1848+
return canInvite;
1849+
};
1850+
1851+
/**
1852+
* Returns the join rule based on the m.room.join_rule state event, defaulting to `invite`.
1853+
* @returns {string} the join_rule applied to this room
1854+
*/
1855+
Room.prototype.getJoinRule = function() {
1856+
return this.currentState.getJoinRule();
1857+
};
1858+
18341859
/**
18351860
* This is an internal method. Calculates the name of the room from the current
18361861
* room state.

0 commit comments

Comments
 (0)