Skip to content

Commit

Permalink
move to patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
Sorunome committed Apr 28, 2019
1 parent 20fe6b4 commit 73b1aab
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 23 deletions.
6 changes: 4 additions & 2 deletions config/config.sample.yaml
Expand Up @@ -90,5 +90,7 @@ limits:
# fininished handling it, causing us to echo it back to the room)
discordSendDelay: 750
ghosts:
# the tag to append to ghost nicknames
tag: ""
# Pattern for the ghosts nick, available is :nick, :username, :tag and :id
nickPattern: ":nick"
# Pattern for the ghosts username, available is :username, :tag and :id
usernamePattern: ":username#:tag"
4 changes: 3 additions & 1 deletion config/config.schema.yaml
Expand Up @@ -112,5 +112,7 @@ properties:
ghosts:
type: "object"
properties:
tag:
nickPattern:
type: "string"
usernamePattern:
type: "string"
8 changes: 2 additions & 6 deletions src/channelsyncroniser.ts
Expand Up @@ -163,14 +163,10 @@ export class ChannelSyncroniser {
return channelState;
}

const patternMap = {
const name: string = Util.ApplyPatternString(this.config.channel.namePattern, {
guild: channel.guild.name,
name: "#" + channel.name,
};
let name: string = this.config.channel.namePattern;
for (const p of Object.keys(patternMap)) {
name = name.replace(new RegExp(":" + p, "g"), patternMap[p]);
}
});
const topic = channel.topic;
const icon = channel.guild.icon;
let iconUrl: string | null = null;
Expand Down
3 changes: 2 additions & 1 deletion src/config.ts
Expand Up @@ -112,5 +112,6 @@ export class LoggingFile {
}

class DiscordBridgeConfigGhosts {
public tag: string = "";
public nickPattern: string = ":nick";
public usernamePattern: string = ":username#:tag";
}
19 changes: 12 additions & 7 deletions src/usersyncroniser.ts
Expand Up @@ -234,8 +234,11 @@ export class UserSyncroniser {
id: discordUser.id,
mxUserId: `@_discord_${discordUser.id}${mxidExtra}:${this.config.bridge.domain}`,
});
const displayName = this.displayNameForUser(discordUser)
+ (this.config.ghosts.tag ? " " + this.config.ghosts.tag : "");
const displayName = Util.ApplyPatternString(this.config.ghosts.usernamePattern, {
id: discordUser.id,
tag: discordUser.discriminator,
username: discordUser.username,
});
// Determine if the user exists.
const remoteId = discordUser.id + mxidExtra;
const remoteUser = await this.userStore.getRemoteUser(remoteId);
Expand Down Expand Up @@ -271,10 +274,16 @@ export class UserSyncroniser {
public async GetUserStateForGuildMember(
newMember: GuildMember,
): Promise<IGuildMemberState> {
const name = Util.ApplyPatternString(this.config.ghosts.nickPattern, {
id: newMember.user.id,
nick: newMember.displayName,
tag: newMember.user.discriminator,
username: newMember.user.username,
});
const guildState: IGuildMemberState = Object.assign({}, DEFAULT_GUILD_STATE, {
bot: newMember.user.bot,
displayColor: newMember.displayColor,
displayName: newMember.displayName + (this.config.ghosts.tag ? " " + this.config.ghosts.tag : ""),
displayName: name,
id: newMember.id,
mxUserId: `@_discord_${newMember.id}:${this.config.bridge.domain}`,
roles: newMember.roles.map((role) => { return {
Expand Down Expand Up @@ -395,10 +404,6 @@ export class UserSyncroniser {
});
}

private displayNameForUser(discordUser): string {
return `${discordUser.username}#${discordUser.discriminator}`;
}

private async leave(intent: Intent, roomId: string, checkCache: boolean = true) {
const userId = intent.getClient().getUserId();
if (checkCache && ![null, "join", "invite"]
Expand Down
11 changes: 11 additions & 0 deletions src/util.ts
Expand Up @@ -47,6 +47,10 @@ export interface ICommandParameters {
[index: string]: ICommandParameter;
}

export interface IPatternMap {
[index: string]: string;
}

export class Util {
/**
* downloadFile - This function will take a URL and store the resulting data into
Expand Down Expand Up @@ -274,6 +278,13 @@ export class Util {
const htmlColor = pad.substring(0, pad.length - colorHex.length) + colorHex;
return htmlColor;
}

public static ApplyPatternString(str: string, patternMap: IPatternMap): string {
for (const p of Object.keys(patternMap)) {
str = str.replace(new RegExp(":" + p, "g"), patternMap[p]);
}
return str;
}
}

interface IUploadResult {
Expand Down
2 changes: 2 additions & 0 deletions test/test_channelsyncroniser.ts
Expand Up @@ -24,6 +24,7 @@ import { MockGuild } from "./mocks/guild";
import { MockMember } from "./mocks/member";
import { MatrixEventProcessor, MatrixEventProcessorOpts } from "../src/matrixeventprocessor";
import { DiscordBridgeConfig } from "../src/config";
import { Util } from "../src/util";
import { MockChannel } from "./mocks/channel";
import { Bridge, MatrixRoom, RemoteRoom } from "matrix-appservice-bridge";
// we are a test file and thus need those
Expand All @@ -44,6 +45,7 @@ let ROOM_DIRECTORY_VISIBILITY: any = null;
const ChannelSync = (Proxyquire("../src/channelsyncroniser", {
"./util": {
Util: {
ApplyPatternString: Util.ApplyPatternString,
UploadContentFromUrl: async () => {
UTIL_UPLOADED_AVATAR = true;
return {mxcUrl: "avatarset"};
Expand Down
13 changes: 7 additions & 6 deletions test/test_usersyncroniser.ts
Expand Up @@ -55,6 +55,7 @@ const GUILD_ROOM_IDS_WITH_ROLE = ["!abc:localhost", "!def:localhost"];
const UserSync = (Proxyquire("../src/usersyncroniser", {
"./util": {
Util: {
ApplyPatternString: Util.ApplyPatternString,
AsyncForEach: Util.AsyncForEach,
UploadContentFromUrl: async () => {
UTIL_UPLOADED_AVATAR = true;
Expand All @@ -64,7 +65,7 @@ const UserSync = (Proxyquire("../src/usersyncroniser", {
},
})).UserSyncroniser;

function CreateUserSync(remoteUsers: RemoteUser[] = [], nickTag: string = ""): UserSyncroniser {
function CreateUserSync(remoteUsers: RemoteUser[] = [], ghostConfig: any = {}): UserSyncroniser {
UTIL_UPLOADED_AVATAR = false;
SEV_ROOM_ID = null;
SEV_CONTENT = null;
Expand Down Expand Up @@ -153,7 +154,7 @@ function CreateUserSync(remoteUsers: RemoteUser[] = [], nickTag: string = ""): U
};
const config = new DiscordBridgeConfig();
config.bridge.domain = "localhost";
config.ghosts.tag = nickTag;
config.ghosts = Object.assign({}, config.ghosts, ghostConfig);
return new UserSync(bridge as Bridge, config, discordbot, userStore as any);
}

Expand Down Expand Up @@ -197,12 +198,12 @@ describe("UserSyncroniser", () => {
expect(state.avatarId, "AvatarID").is.empty;
expect(state.avatarUrl, "AvatarUrl").is.null;
});
it("Will obay name tags", async () => {
it("Will obay name patterns", async () => {
const remoteUser = new RemoteUser("123456");
remoteUser.avatarurl = "test.jpg";
remoteUser.displayname = "TestUsername";

const userSync = CreateUserSync([remoteUser], "(Discord)");
const userSync = CreateUserSync([remoteUser], {usernamePattern: ":username#:tag (Discord)"});
const user = new MockUser(
"123456",
"TestUsername",
Expand Down Expand Up @@ -489,8 +490,8 @@ describe("UserSyncroniser", () => {
const state = await userSync.GetUserStateForGuildMember(member as any);
expect(state.displayName).to.be.equal("BestDog");
});
it("Will will obay name tags", async () => {
const userSync = CreateUserSync([new RemoteUser("123456")], "(Discord)");
it("Will will obay nick pattern", async () => {
const userSync = CreateUserSync([new RemoteUser("123456")], { nickPattern: ":nick (Discord)" });
const guild = new MockGuild(
"654321");
const member = new MockMember(
Expand Down

0 comments on commit 73b1aab

Please sign in to comment.