Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/models/room-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {EventEmitter} from "events";
import {RoomMember} from "./room-member";
import {logger} from '../logger';
import * as utils from "../utils";
import {EventType} from "../@types/event";

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

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


function _updateThirdPartyTokenCache(roomState, memberEvent) {
if (!memberEvent.getContent().third_party_invite) {
Expand Down
36 changes: 35 additions & 1 deletion src/models/room.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {RoomMember} from "./room-member";
import {RoomSummary} from "./room-summary";
import {logger} from '../logger';
import {ReEmitter} from '../ReEmitter';
import {EventType} from "../@types/event";

// These constants are used as sane defaults when the homeserver doesn't support
// the m.room_versions capability. In practice, KNOWN_SAFE_ROOM_VERSION should be
Expand Down Expand Up @@ -1822,7 +1823,7 @@ Room.prototype.getAccountData = function(type) {


/**
* Returns wheter the syncing user has permission to send a message in the room
* Returns whether the syncing user has permission to send a message in the room
* @return {boolean} true if the user should be permitted to send
* message events into the room.
*/
Expand All @@ -1831,6 +1832,30 @@ Room.prototype.maySendMessage = function() {
this.currentState.maySendEvent('m.room.message', this.myUserId);
};

/**
* Returns whether the given user has permissions to issue an invite for this room.
* @param {string} userId the ID of the Matrix user to check permissions for
* @returns {boolean} true if the user should be permitted to issue invites for this room.
*/
Room.prototype.canInvite = function(userId) {
let canInvite = this.getMyMembership() === "join";
const powerLevelsEvent = this.currentState.getStateEvents(EventType.RoomPowerLevels, "");
const powerLevels = powerLevelsEvent && powerLevelsEvent.getContent();
const me = this.getMember(userId);
if (powerLevels && me && powerLevels.invite > me.powerLevel) {
canInvite = false;
}
return canInvite;
};

/**
* Returns the join rule based on the m.room.join_rule state event, defaulting to `invite`.
* @returns {string} the join_rule applied to this room
*/
Room.prototype.getJoinRule = function() {
return this.currentState.getJoinRule();
};

/**
* This is an internal method. Calculates the name of the room from the current
* room state.
Expand Down Expand Up @@ -2048,3 +2073,12 @@ function memberNamesToRoomName(names, count = (names.length + 1)) {
*
* @param {EventStatus} oldStatus The previous event status.
*/

/**
* Fires when the logged in user's membership in the room is updated.
*
* @event module:models/room~Room#"Room.myMembership"
* @param {Room} room The room in which the membership has been updated
* @param {string} membership The new membership value
* @param {string} prevMembership The previous membership value
*/