Skip to content

Commit 9337c70

Browse files
Merge pull request #81 from d3m3vilurr/rename-channel
Rename slack channel
2 parents 21b2fe0 + 7e71ae3 commit 9337c70

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

app.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class App extends MatrixPuppetBridgeBase {
1818
this.slackPrefix = 'slack';
1919
this.servicePrefix = `${this.slackPrefix}_${this.teamName}`;
2020
this.notifyToSlack = notify;
21+
this.matrixRoomStatus = {};
2122
}
2223
getServiceName() {
2324
return "Slack";
@@ -136,6 +137,13 @@ class App extends MatrixPuppetBridgeBase {
136137
user: data.user,
137138
});
138139
});
140+
this.client.on('rename', (data)=>{
141+
console.log(data);
142+
// rename channel
143+
this.renameChannelEvent({
144+
channel: data.channel,
145+
});
146+
});
139147
debug('registered message listener');
140148
}
141149
getPayload(data) {
@@ -374,6 +382,30 @@ class App extends MatrixPuppetBridgeBase {
374382
});
375383
});
376384
}
385+
386+
async _renameChannelEvent(matrixRoomId, name) {
387+
const botIntent = this.getIntentFromApplicationServerBot();
388+
const ret = await botIntent.setRoomName(matrixRoomId, name);
389+
this.updateRoomStatesCache(matrixRoomId, 'name', name);
390+
return ret;
391+
}
392+
393+
async renameChannelEvent(data) {
394+
const payload = this.getPayload(data);
395+
const roomAlias = this.getRoomAliasFromThirdPartyRoomId(payload.roomId);
396+
try {
397+
const room = await this.puppet.getClient().getRoomIdForAlias(roomAlias);
398+
return this._renameChannelEvent(room.room_id, data.name);
399+
} catch (err) {
400+
console.error(err);
401+
this.sendStatusMsg({
402+
fixedWidthOutput: true,
403+
roomAliasLocalPart: `${this.slackPrefix}_${this.getStatusRoomPostfix()}`
404+
}, err.stack).catch((err)=>{
405+
console.error(err);
406+
});
407+
}
408+
}
377409
getThirdPartyRoomDataById(id) {
378410
const directName = (user) => this.client.getUserById(user).name;
379411
const directTopic = () => `Slack Direct Message (Team: ${this.teamName})`
@@ -424,6 +456,44 @@ class App extends MatrixPuppetBridgeBase {
424456
const filename = this.tagMatrixMessage(data.filename);
425457
return this.client.sendFileMessage(data.url, data.text, filename, id);
426458
}
459+
460+
async getRoomState(matrixRoomId, type) {
461+
// prevent refetch from matrix server after first fetching
462+
const cache = this.matrixRoomStatus[matrixRoomId] || {};
463+
if (cache[type]) {
464+
return cache[type];
465+
}
466+
const puppetClient = this.puppet.getClient();
467+
switch(type) {
468+
case 'name':
469+
const roomName = await puppetClient.getStateEvent(matrixRoomId, 'm.room.name');
470+
if (roomName && roomName.name) {
471+
this.updateRoomStatesCache(matrixRoomId, 'name', roomName.name);
472+
}
473+
return roomName.name;
474+
// TODO
475+
}
476+
}
477+
478+
updateRoomStatesCache(matrixRoomId, type, data) {
479+
const roomStatus = this.matrixRoomStatus[matrixRoomId] = this.matrixRoomStatus[matrixRoomId] || {};
480+
roomStatus[type] = data;
481+
}
482+
483+
// HACK: recheck the old name when after getOrCreateMatrixRoomFromThirdPartyRoomId
484+
// if room has old name, forcefully update new name after bind
485+
async getOrCreateMatrixRoomFromThirdPartyRoomId(thirdPartyRoomId) {
486+
const matrixRoomId = await super.getOrCreateMatrixRoomFromThirdPartyRoomId(thirdPartyRoomId);
487+
const name = await this.getRoomState(matrixRoomId, 'name');
488+
const chan = this.client.getChannelById(thirdPartyRoomId) || {};
489+
if (!chan.name) {
490+
return matrixRoomId;
491+
}
492+
if (name !== chan.name) {
493+
await this._renameChannelEvent(matrixRoomId, chan.name);
494+
}
495+
return matrixRoomId;
496+
}
427497
}
428498

429499
module.exports = App;

client.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ class Client extends EventEmitter {
8585
}
8686
if (chan.name !== data.channel.name) {
8787
chan.name = data.channel.name;
88+
this.emit('rename', {
89+
channel: chan.id,
90+
name: chan.name,
91+
});
8892
}
8993
}
9094
break;

0 commit comments

Comments
 (0)