Skip to content

Commit

Permalink
fix types and masquerading (mostly)
Browse files Browse the repository at this point in the history
  • Loading branch information
williamhorning committed Jun 20, 2024
1 parent da0c685 commit 39b1861
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 11 deletions.
1 change: 1 addition & 0 deletions packages/bolt-matrix/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export {
Intent,
Request,
type WeakEvent,
MatrixUser
} from 'npm:matrix-appservice-bridge@10.1.0';
export {
type bridge_channel,
Expand Down
28 changes: 28 additions & 0 deletions packages/bolt-matrix/matrix_user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { type Bridge, type Intent, MatrixUser, type message, Buffer } from './deps.ts';

export async function ensure_profile(bot: Bridge, mxintent: Intent, channel_id: string, msg: message) {
const store = bot.getUserStore()!;

let store_user = await store.getMatrixUser(mxintent.userId);

if (!store_user) {
store_user = new MatrixUser(mxintent.userId);
}

if ((store_user.get('avatar') !== msg.author.profile) &&
msg.author.profile) {
const mxc = await mxintent.uploadContent(
Buffer.from(
await (await fetch(msg.author.profile)).arrayBuffer()
)
);
store_user.set('avatar_mxc', mxc);
}

await store.setMatrixUser(store_user);

await mxintent.setRoomUserProfile(channel_id, {
avatar_url: store_user.get('avatar_mxc'),
displayname: msg.author.username,
});
}
15 changes: 8 additions & 7 deletions packages/bolt-matrix/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import { onEvent } from './events.ts';
import { to_matrix } from './to_matrix.ts';
import { setup_registration } from './setup_registration.ts';
import { ensure_profile } from './matrix_user.ts';

export type MatrixConfig = {
appserviceUrl: string;
Expand Down Expand Up @@ -49,21 +50,21 @@ export class matrix_plugin extends plugin<MatrixConfig, string[]> {
edit?: string[],
reply?: string,
) {
const mxid = `lightning-${msg.plugin}_${msg.author.id}`;
const mxintent = this.bot.getIntentFromLocalpart(mxid);
const local_part = `lightning-${msg.plugin}_${msg.author.id}`;
const mxintent = this.bot.getIntentFromLocalpart(local_part);

// TODO(jersey): fix the intent stuff
await ensure_profile(this.bot, mxintent, channel.id, msg);

const messages = await to_matrix(msg, mxintent, reply, edit);

const msg_ids = []
const msg_ids = [];

for (const message of messages) {
const result = await mxintent.sendMessage(
await new Promise((resolve) => setTimeout(resolve, 1000));
const { event_id } = await mxintent.sendMessage(
channel.id,
message,
);
msg_ids.push(result.event_id);
msg_ids.push(event_id);
}

return msg_ids;
Expand Down
2 changes: 1 addition & 1 deletion packages/bolt-matrix/to_matrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export async function to_matrix(
}

// TODO(jersey): eventually add nicer fallback
if (msg.embeds) {
if (msg.embeds && msg.embeds.length > 0) {
events[0].body = `${events[0].body}\n\n*this message includes embeds*`
events[0].formattedBody = render(events[0].body as string);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/lightning/src/lightning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class lightning extends EventEmitter<plugin_events> {
setup_bridges(this);

for (const p of this.config.plugins || []) {
if (!p.support.includes('0.7.0') || !p.support.includes('0.7.1')) continue;
if (!(p.support.includes('0.7.0') || p.support.includes('0.7.1'))) continue;
const plugin = new p.type(this, p.config);
this.plugins.set(plugin.name, plugin);
(async () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/lightning/src/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { deleted_message, message } from './messages.ts';

/** the way to make a plugin */
export interface create_plugin<
plugin_type extends plugin<plugin_type['config']>,
plugin_type extends plugin<plugin_type['config'], string | string[]>,
> {
/** the actual constructor of the plugin */
type: new (l: lightning, config: plugin_type['config']) => plugin_type;
Expand Down Expand Up @@ -40,7 +40,7 @@ export abstract class plugin<cfg, idtype extends string | string[] = string> ext
abstract name: string;

/** create a new plugin instance */
static new<T extends plugin<unknown>>(
static new<T extends plugin<T["config"], string | string[]>>(
this: new (l: lightning, config: T['config']) => T,
config: T['config'],
): create_plugin<T> {
Expand Down

0 comments on commit 39b1861

Please sign in to comment.