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

Matrix bridge #46

Merged
merged 19 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions packages/bolt-matrix/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ export async function onEvent(this: MatrixPlugin, request: Request<WeakEvent>) {
}
}
if (event.type === 'm.room.message' && !event['m.new_content']) {
this.emit('messageCreate', await messageToCore(event, intent));
this.emit('messageCreate', await messageToCore(event, intent, this.config.homeserverUrl));
austinhuang0131 marked this conversation as resolved.
Show resolved Hide resolved
}
if (event.type === 'm.room.message' && event['m.new_content']) {
this.emit('messageUpdate', await messageToCore(event, intent));
this.emit('messageUpdate', await messageToCore(event, intent, this.config.homeserverUrl));
austinhuang0131 marked this conversation as resolved.
Show resolved Hide resolved
}
if (event.type === 'm.room.redaction') {
this.emit('messageDelete', {
Expand All @@ -34,15 +34,16 @@ export async function onEvent(this: MatrixPlugin, request: Request<WeakEvent>) {

export async function messageToCore(
event: WeakEvent,
intent: Intent
intent: Intent,
homeserverUrl: String
austinhuang0131 marked this conversation as resolved.
Show resolved Hide resolved
): Promise<BoltMessage<WeakEvent>> {
const sender = await intent.getProfileInfo(event.sender);
return {
author: {
username: sender.displayname || event.sender,
rawname: event.sender,
id: event.sender,
profile: sender.avatar_url
profile: `${sender.avatar_url.replace("mxc://", `${homeserverUrl}/_matrix/media/v3/thumbnail/`)}?width=96&height=96&method=scale`
austinhuang0131 marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
profile: `${sender.avatar_url.replace("mxc://", `${homeserverUrl}/_matrix/media/v3/thumbnail/`)}?width=96&height=96&method=scale`
profile: `${sender.avatar_url.replace("mxc://", `${homeserverUrl}/_matrix/media/v3/thumbnail/`)}?width=96&height=96&method=scale`,
color: '#0DBD8B'

},
channel: event.room_id,
id: event.event_id,
Expand All @@ -57,11 +58,9 @@ export async function messageToCore(

export function coreToMessage(msg: BoltMessage<unknown>) {
return {
content: {
body: msg.content
? msg.content
: "*this bridge doesn't support anything except text at the moment*",
msgtype: 'm.text'
}
body: msg.content
? msg.content
: "*this bridge doesn't support anything except text at the moment*",
msgtype: 'm.text'
};
}
35 changes: 8 additions & 27 deletions packages/bolt-matrix/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import { coreToMessage, onEvent } from './events.ts';

type MatrixConfig = {
accessToken: string;
appserviceUrl: string;
williamhorning marked this conversation as resolved.
Show resolved Hide resolved
homeserverUrl: string;
domain: string;
port?: number;
Expand All @@ -21,7 +21,7 @@ type MatrixConfig = {
export default class MatrixPlugin extends BoltPlugin {
bot: Bridge;
config: MatrixConfig;
name = 'bolt-revolt';
name = 'bolt-matrix';
version = '0.5.4';
bolt?: Bolt;
constructor(config: MatrixConfig) {
Expand All @@ -31,23 +31,6 @@ export default class MatrixPlugin extends BoltPlugin {
homeserverUrl: this.config.homeserverUrl,
domain: this.config.domain,
registration: this.config.reg_path,
bridgeEncryption: {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason to remove all the encryption code? I'd prefer it if bolt-matrix supported encrypted rooms but if there's a reason I guess that's fine

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because it doesn't launch on my end with it, i might revisit it in the future, but almost all matrix-appservice-${platform} don't have encryption

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah okay, i guess we could go back to that in the future then

homeserverUrl: config.homeserverUrl,
store: {
getStoredSession: async (userId: string) => {
return JSON.parse(
(await this.bolt?.redis?.get(`mtx-session-${userId}`)) || 'null'
);
},
setStoredSession: async (session: ClientEncryptionSession) => {
await this.bolt?.redis?.set(
`mtx-session-${session.userId}`,
JSON.stringify(session)
);
},
async updateSyncToken() {}
}
},
controller: {
onEvent: onEvent.bind(this)
},
Expand All @@ -59,16 +42,14 @@ export default class MatrixPlugin extends BoltPlugin {
async start(bolt: Bolt) {
this.bolt = bolt;
if (!existsSync(this.config.reg_path)) {
const reg = new AppServiceRegistration(this.config.homeserverUrl);
const reg = new AppServiceRegistration(this.config.appserviceUrl);
reg.setAppServiceToken(AppServiceRegistration.generateToken());
reg.setHomeserverToken(AppServiceRegistration.generateToken());
reg.setId(
'b4d15f02f7e406db25563c1a74ac78863dc4fbcc5595db8d835f6ee6ffef1448'
);
reg.setId(AppServiceRegistration.generateToken());
austinhuang0131 marked this conversation as resolved.
Show resolved Hide resolved
reg.setProtocols(['bolt']);
reg.setRateLimited(false);
reg.setSenderLocalpart('boltbot');
reg.addRegexPattern('users', '@bolt_*', true);
reg.setSenderLocalpart('bot.bolt');
reg.addRegexPattern('users', `@bolt-(discord|revolt)_.+:${this.config.domain}`, true);
austinhuang0131 marked this conversation as resolved.
Show resolved Hide resolved
reg.outputAsYaml(this.config.reg_path);
}
await this.bot.run(this.config.port || 8081);
Expand All @@ -80,9 +61,9 @@ export default class MatrixPlugin extends BoltPlugin {
}
async bridgeMessage(data: BoltBridgeMessageArgs) {
const intent = this.bot.getIntent(
`${data.data.platform.name}_${
`@${data.data.platform.name}_${
'author' in data.data ? data.data.author.id : 'deletion'
}`
}:${this.config.domain}`
);
const room = data.data.bridgePlatform.senddata as string;
switch (data.type) {
Expand Down