Skip to content

Commit

Permalink
Set custom power_levels on room creation
Browse files Browse the repository at this point in the history
  • Loading branch information
erdnaxeli committed Sep 25, 2017
1 parent a03d15c commit b388b5f
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 5 deletions.
10 changes: 10 additions & 0 deletions config.sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ ircService:
modePowerMap:
o: 50

# A map of levels needed to do some action on the Matrix room. Those levels will be set
# on the room on creation. With corresponding modePowerMap, this allows Matrix users who
# are ops on the IRC side to edit the room settings.
powerLevels:
avatar: 50
name: 50
canonicalAlias: 50
historyVisibility: 50
redact: 50

botConfig:
# Enable the presence of the bot in IRC channels. The bot serves as the entity
# which maps from IRC -> Matrix. You can disable the bot entirely which
Expand Down
10 changes: 8 additions & 2 deletions lib/bridge/IrcHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,10 @@ IrcHandler.prototype.onInvite = Promise.coroutine(function*(req, server, fromUse

if (matrixRooms.length === 0) {
let ircRoom = yield this.ircBridge.trackChannel(server, channel, null);
let response = yield this.ircBridge.getAppServiceBridge().getIntent(
let userIntent = this.ircBridge.getAppServiceBridge().getIntent(
virtualMatrixUser.getId()
).createRoom({
);
let response = yield userIntent.createRoom({
options: {
room_alias_name: roomAlias.split(":")[0].substring(1), // localpart
name: channel,
Expand All @@ -303,6 +304,11 @@ IrcHandler.prototype.onInvite = Promise.coroutine(function*(req, server, fromUse
content: {
history_visibility: "joined"
}
},
{
type: "m.room.power_levels",
state_key: "",
content: ircServer.getPowerLevels(userIntent.getClient().credentials.userId)
}
]
}
Expand Down
19 changes: 16 additions & 3 deletions lib/bridge/MatrixHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,9 +392,8 @@ MatrixHandler.prototype._onAdminMessage = Promise.coroutine(function*(req, event
// track the channel then invite them.
// TODO: Dupes onAliasQuery a lot
let ircRoom = yield this.ircBridge.trackChannel(ircServer, ircChannel, key);
let response = yield this.ircBridge.getAppServiceBridge().getIntent(
event.user_id
).createRoom({
let userIntent = this.ircBridge.getAppServiceBridge().getIntent(event.user_id);
let response = yield userIntent.createRoom({
options: {
name: ircChannel,
visibility: "private",
Expand All @@ -416,6 +415,13 @@ MatrixHandler.prototype._onAdminMessage = Promise.coroutine(function*(req, event
content: {
history_visibility: "joined"
}
},
{
type: "m.room.power_levels",
state_key: "",
content: ircServer.getPowerLevels(
userIntent.getClient().credentials.userId
)
}
]
}
Expand Down Expand Up @@ -1353,6 +1359,13 @@ MatrixHandler.prototype._onAliasQuery = Promise.coroutine(function*(req, roomAli
content: {
history_visibility: "joined"
}
},
{
type: "m.room.power_levels",
state_key: "",
content: channelInfo.server.getPowerLevels(
botIntent.getClient().credentials.userIdl
)
}
]
}
Expand Down
13 changes: 13 additions & 0 deletions lib/config/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,19 @@ properties:
"^[a-zA-Z]$":
type: number
minimum: 0
powerLevels:
type: "object"
properties:
avatar:
type: "number"
name:
type: "number"
canonical_alias:
type: "number"
history_visibility:
type: "number"
redact:
type: "number"
botConfig:
type: "object"
properties:
Expand Down
24 changes: 24 additions & 0 deletions lib/irc/IrcServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,30 @@ IrcServer.prototype.getModePowerMap = function() {
return this.config.modePowerMap || {};
}

/**
* Get a map of power levels to set.
* @param {string} userId : The room creator user ID.
* @return {Object}
*/
IrcServer.prototype.getPowerLevels = function(userId) {
var powerLevels = this.config.powerLevels || {};
var content = {
redact: powerLevels.redact || 50,
events_default: 0,
state_default: 50,
events: {
"m.room.avatar": powerLevels.avatar || 50,
"m.room.name": powerLevels.name || 50,
"m.room.canonical_alias": powerLevels.canonicalAlias || 50,
"m.room.history_visibility": powerLevels.historyVisibility || 100,
"m.room.power_levels": 100
},
users: {}
}
content["users"][userId] = 100;
return content;
}

IrcServer.prototype.getHardCodedRoomIds = function() {
var roomIds = new Set();
var channels = Object.keys(this.config.mappings);
Expand Down

0 comments on commit b388b5f

Please sign in to comment.