Skip to content

Commit f547fa7

Browse files
authored
Merge pull request #1609 from matrix-org/t3chguy/spaces1
Room helpers for invite permissions and join rules
2 parents e24b151 + 3028fe9 commit f547fa7

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

src/models/room-state.js

+11
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

+35-1
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
@@ -1822,7 +1823,7 @@ Room.prototype.getAccountData = function(type) {
18221823

18231824

18241825
/**
1825-
* Returns wheter the syncing user has permission to send a message in the room
1826+
* Returns whether the syncing user has permission to send a message in the room
18261827
* @return {boolean} true if the user should be permitted to send
18271828
* message events into the room.
18281829
*/
@@ -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.
@@ -2048,3 +2073,12 @@ function memberNamesToRoomName(names, count = (names.length + 1)) {
20482073
*
20492074
* @param {EventStatus} oldStatus The previous event status.
20502075
*/
2076+
2077+
/**
2078+
* Fires when the logged in user's membership in the room is updated.
2079+
*
2080+
* @event module:models/room~Room#"Room.myMembership"
2081+
* @param {Room} room The room in which the membership has been updated
2082+
* @param {string} membership The new membership value
2083+
* @param {string} prevMembership The previous membership value
2084+
*/

0 commit comments

Comments
 (0)