Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rooms to directory #179

Merged
merged 4 commits into from Jul 18, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -11,7 +11,8 @@
"start": "npm run-script build && node ./build/src/discordas.js -p 9005 -c config.yaml",
"getbotlink": "node ./build/tools/addbot.js",
"adminme": "node ./build/tools/adminme.js",
"usertool": "node ./build/tools/userClientTools.js"
"usertool": "node ./build/tools/userClientTools.js",
"directoryfix": "node ./build/tools/addRoomsToDirectory.js"
},
"repository": {
"type": "git",
Expand Down
22 changes: 13 additions & 9 deletions src/matrixroomhandler.ts
Expand Up @@ -61,25 +61,29 @@ export class MatrixRoomHandler {
this.bridge = bridge;
}

public OnAliasQueried (alias: string, roomId: string) {
// Join a whole bunch of users.
public async OnAliasQueried (alias: string, roomId: string) {
const channel = await this.discord.GetChannelFromRoomId(roomId) as Discord.GuildChannel;
// Fire and forget RoomDirectory mapping
this.bridge.getIntent().getClient().setRoomDirectoryVisibilityAppService(
channel.guild.id,
roomId,
"public",
);
let promiseChain: any = Bluebird.resolve();

// Join a whole bunch of users.
/* We delay the joins to give some implementations a chance to breathe */
let delay = this.config.limits.roomGhostJoinDelay;
return this.discord.GetChannelFromRoomId(roomId).then((channel: Discord.Channel) => {
for (const member of (<Discord.TextChannel> channel).members.array()) {
for (const member of (<Discord.TextChannel> channel).members.array()) {
if (member.id === this.discord.GetBotId()) {
continue;
}
promiseChain = promiseChain.return(Bluebird.delay(delay).then(() => {
return this.discord.InitJoinUser(member, [roomId]);
}));
delay += this.config.limits.roomGhostJoinDelay;
}
}).catch((err) => {
log.verbose("OnAliasQueried => %s", err);
throw err;
});
}
await promiseChain;
}

public OnEvent (request, context): Promise<any> {
Expand Down
4 changes: 4 additions & 0 deletions test/test_matrixroomhandler.ts
Expand Up @@ -38,6 +38,7 @@ function createRH(opts: any = {}) {
GetChannelFromRoomId: (roomid: string) => {
if (roomid === "!accept:localhost") {
const chan = new MockChannel();
chan.guild = new MockGuild("asb");
if (opts.createMembers) {
chan.members.set("12345", new MockMember("12345", "testuser1"));
chan.members.set("54321", new MockMember("54321", "testuser2"));
Expand Down Expand Up @@ -84,6 +85,9 @@ function createRH(opts: any = {}) {
getStateEvent: () => {
return Promise.resolve(opts.powerLevels || {});
},
setRoomDirectoryVisibilityAppService: () => {
return Promise.resolve();
},
};
const provisioner = {
AskBridgePermission: () => {
Expand Down
102 changes: 102 additions & 0 deletions tools/addRoomsToDirectory.ts
@@ -0,0 +1,102 @@
/* tslint:disable:no-console */
/**
* Allows you to become an admin for a room the bot is in control of.
*/

import { AppServiceRegistration, ClientFactory, Bridge } from "matrix-appservice-bridge";
import * as yaml from "js-yaml";
import * as fs from "fs";
import * as args from "command-line-args";
import * as usage from "command-line-usage";
import * as log from "npmlog";
import { DiscordBridgeConfig } from "../src/config";

const optionDefinitions = [
{
name: "help",
alias: "h",
type: Boolean,
description: "Display this usage guide.",
},
{
name: "config",
alias: "c",
type: String,
defaultValue: "config.yaml",
description: "The AS config file.",
typeLabel: "<config.yaml>",
},
{
name: "store",
alias: "s",
type: String,
defaultValue: "room-store.db",
description: "The location of the room store.",
},
];

const options = args(optionDefinitions);

if (options.help) {
/* tslint:disable:no-console */
console.log(usage([
{
header: "Add rooms to directory",
content: "A tool to set all the bridged rooms to visible in the directory."},
{
header: "Options",
optionList: optionDefinitions,
},
]));
process.exit(0);
}
const yamlConfig = yaml.safeLoad(fs.readFileSync("./discord-registration.yaml", "utf8"));
const registration = AppServiceRegistration.fromObject(yamlConfig);
const config: DiscordBridgeConfig = yaml.safeLoad(fs.readFileSync(options.config, "utf8")) as DiscordBridgeConfig;

if (registration === null) {
throw new Error("Failed to parse registration file");
}

const clientFactory = new ClientFactory({
appServiceUserId: "@" + registration.sender_localpart + ":" + config.bridge.domain,
token: registration.as_token,
url: config.bridge.homeserverUrl,
});

const bridge = new Bridge({
homeserverUrl: true,
registration: true,
domain: "rubbish",
controller: {
onEvent: () => { },
},
roomStore: options.store,
});

bridge.loadDatabases().catch((e) => {
log.error("AddRoom", `Failed to load database`, e);
}).then(() => {
return bridge.getRoomStore().getEntriesByRemoteRoomData({
discord_type: "text",
});
}).then((rooms) => {
rooms = rooms.filter((r) => r.remote.get("plumbed") !== true );
const client = clientFactory.getClientAs();
log.info("AddRoom", `Got ${rooms.length} rooms to set`);
rooms.forEach((room) => {
const guild = room.remote.get("discord_guild");
const roomId = room.matrix.getId();
client.setRoomDirectoryVisibilityAppService(
guild,
roomId,
"public",
).then(() => {
log.info("AddRoom", `Set ${roomId} to visible in ${guild}'s directory`);
}).catch((e) => {
log.error("AddRoom", `Failed to set ${roomId} to visible in ${guild}'s directory`, e);
});
});
}).catch((e) => {
log.error("AddRoom", `Failed to run script`, e);
});