Update the profile for self-hosted go-neb instances

Fixes #166
This commit is contained in:
Travis Ralston 2018-03-31 22:01:23 -06:00
parent 766700ffd9
commit e490007cec
9 changed files with 86 additions and 0 deletions

View File

@ -40,6 +40,24 @@ widgetBlacklist:
database:
file: "dimension.db"
# Display settings that apply to self-hosted go-neb instances
goneb:
# The avatars to set for each bot. Usually these don't need to be changed, however if your homeserver
# is not able to reach t2bot.io then you should specify your own here. To not use an avatar for a bot,
# make the bot's avatar an empty string.
avatars:
giphy: "mxc://t2bot.io/c5eaab3ef0133c1a61d3c849026deb27"
imgur: "mxc://t2bot.io/6749eaf2b302bb2188ae931b2eeb1513"
github: "mxc://t2bot.io/905b64b3cd8e2347f91a60c5eb0832e1"
wikipedia: "mxc://t2bot.io/7edfb54e9ad9e13fec0df22636feedf1"
travisci: "mxc://t2bot.io/7f4703126906fab8bb27df34a17707a8"
rss: "mxc://t2bot.io/aace4fcbd045f30afc1b4e5f0928f2f3"
google: "mxc://t2bot.io/636ad10742b66c4729bf89881a505142"
guggy: "mxc://t2bot.io/e7ef0ed0ba651aaf907655704f9a7526"
echo: "mxc://t2bot.io/3407ff2db96b4e954fcbf2c6c0415a13"
circleci: "mxc://t2bot.io/cf7d875845a82a6b21f5f66de78f6bee"
jira: "mxc://t2bot.io/f4a38ebcc4280ba5b950163ca3e7c329"
# Settings for controlling how logging works
logging:
file: logs/dimension.log

View File

@ -17,6 +17,11 @@ export interface DimensionConfig {
file: string;
};
admins: string[];
goneb: {
avatars: {
[botType: string]: string; // mxc
};
};
logging: LogConfig;
}

View File

@ -294,6 +294,13 @@ export class NebStore {
const client = new NebClient(neb);
await client.updateUser(userId, integration.isEnabled, true); // creates the user in go-neb
client.updateUserProfile(userId, integration.name, config.goneb.avatars[integration.type]).then(() => {
LogService.info("NebStore", "Updated profile for " + userId);
}).catch(err => {
LogService.error("NebStore", "Error updating profile for " + userId);
LogService.error("NebStore", err);
});
const serviceId = appservice.id + "_integration_" + integrationType;
return NebBotUser.create({
serviceId: serviceId,
@ -320,6 +327,13 @@ export class NebStore {
const client = new NebClient(neb);
await client.updateUser(userId, integration.isEnabled, false); // creates the user in go-neb
client.updateUserProfile(userId, integration.name + " Notifications [" + forUserId + "]", config.goneb.avatars[integration.type]).then(() => {
LogService.info("NebStore", "Updated profile for " + userId);
}).catch(err => {
LogService.error("NebStore", "Error updating profile for " + userId);
LogService.error("NebStore", err);
});
const serviceId = appservice.id + "_integration_" + integrationType + "_notifications_" + safeUserId;
return NebNotificationUser.create({
serviceId: serviceId,

View File

@ -26,4 +26,40 @@ export class MatrixLiteClient {
);
return response['user_id'];
}
public async getDisplayName(): Promise<string> {
const response = await doClientApiCall(
"GET",
"/_matrix/client/r0/profile/" + (await this.whoAmI()) + "/displayname",
{access_token: this.accessToken},
);
return response['displayname'];
}
public async getAvatarUrl(): Promise<string> {
const response = await doClientApiCall(
"GET",
"/_matrix/client/r0/profile/" + (await this.whoAmI()) + "/avatar_url",
{access_token: this.accessToken},
);
return response['avatar_url'];
}
public async setDisplayName(newDisplayName: string): Promise<any> {
return doClientApiCall(
"PUT",
"/_matrix/client/r0/profile/" + (await this.whoAmI()) + "/displayname",
{access_token: this.accessToken},
{displayname: newDisplayName},
);
}
public async setAvatarUrl(newUrl: string): Promise<any> {
return doClientApiCall(
"PUT",
"/_matrix/client/r0/profile/" + (await this.whoAmI()) + "/avatar_url",
{access_token: this.accessToken},
{avatar_url: newUrl},
);
}
}

View File

@ -94,6 +94,7 @@ export async function doClientApiCall(method: string, endpoint: string, query?:
reject(err);
} else if (res.statusCode !== 200) {
LogService.error("matrix", "Got status code " + res.statusCode + " while calling client endpoint " + endpoint);
LogService.error("matrix", res.body);
reject(new Error("Error in request: invalid status code"));
} else {
if (typeof(res.body) === "string") res.body = JSON.parse(res.body);

View File

@ -5,6 +5,7 @@ import config from "../config";
import { LogService } from "matrix-js-snippets";
import { Service } from "./models/service";
import * as request from "request";
import { MatrixLiteClient } from "../matrix/MatrixLiteClient";
export class NebClient {
constructor(private neb: NebConfig) {
@ -57,6 +58,17 @@ export class NebClient {
}
}
public async updateUserProfile(userId: string, displayName: string, avatarUrl: string): Promise<any> {
const accessToken = await this.getAccessToken(userId);
const client = new MatrixLiteClient(accessToken);
const currentDisplayName = await client.getDisplayName();
const currentAvatarUrl = await client.getAvatarUrl();
if (currentDisplayName !== displayName) await client.setDisplayName(displayName);
if (currentAvatarUrl !== avatarUrl) await client.setAvatarUrl(avatarUrl);
}
private doRequest<T>(endpoint: string, body: any): Promise<T> {
const adminUrl = (this.neb.adminUrl.endsWith("/") ? this.neb.adminUrl.substring(0, this.neb.adminUrl.length - 1) : this.neb.adminUrl);
if (!endpoint.startsWith("/")) endpoint = "/" + endpoint;

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.6 KiB