Skip to content

Commit

Permalink
feat(matrix): add prefix to routes
Browse files Browse the repository at this point in the history
  • Loading branch information
Anillc committed Aug 12, 2023
1 parent fc1cb99 commit 455f13e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
2 changes: 2 additions & 0 deletions adapters/matrix/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ export namespace MatrixBot {
hsToken?: string
asToken?: string
host?: string
path?: string
}

export const Config: Schema<Config> = Schema.object({
Expand All @@ -193,6 +194,7 @@ export namespace MatrixBot {
hsToken: Schema.string().description('hs_token').role('secret').required(),
asToken: Schema.string().description('as_token').role('secret').required(),
endpoint: Schema.string().description('Matrix Homeserver 地址。默认为 `https://{host}`。'),
path: Schema.string().description('Matrix Application Service 的路径。默认为 `/matrix`。').default('/matrix'),
...omit(Quester.Config.dict, ['endpoint']),
})
}
46 changes: 35 additions & 11 deletions adapters/matrix/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ export class HttpAdapter extends Adapter.Server<MatrixBot> {

hook(callback: (ctx: KoaContext) => void) {
return (ctx: KoaContext) => {
const bots = this.bots.filter(bot => (bot instanceof MatrixBot) && (bot.config.hsToken === ctx.query.access_token))
const bots = this.bots.filter(bot =>
(bot instanceof MatrixBot) &&

Check failure on line 21 in adapters/matrix/src/http.ts

View workflow job for this annotation

GitHub Actions / lint

'&&' should be placed at the beginning of the line
(bot.config.hsToken === ctx.query.access_token) &&

Check failure on line 22 in adapters/matrix/src/http.ts

View workflow job for this annotation

GitHub Actions / lint

'&&' should be placed at the beginning of the line
(ctx.request.path.startsWith(bot.config.path)))
if (!bots.length) {
ctx.status = 403
ctx.body = { errcode: 'M_FORBIDDEN' }
Expand All @@ -30,17 +33,38 @@ export class HttpAdapter extends Adapter.Server<MatrixBot> {

public constructor(ctx: Context) {
super()
const put = (path: string, callback: (ctx: KoaContext) => void) => {
ctx.router.put(path, this.hook(callback).bind(this))
ctx.router.put('/_matrix/app/v1' + path, this.hook(callback).bind(this))
let fork: ReturnType<typeof ctx.plugin>
const registerRoutes = () => {
if (fork) fork.dispose()
fork = ctx.plugin(ctx => {
const put = (prefix: string, path: string, callback: (ctx: KoaContext) => void) => {
ctx.router.put(prefix + path, this.hook(callback).bind(this))
ctx.router.put(prefix + '/_matrix/app/v1' + path, this.hook(callback).bind(this))
}
const get = (prefix: string, path: string, callback: (ctx: KoaContext) => void) => {
ctx.router.get(prefix + path, this.hook(callback).bind(this))
ctx.router.get(prefix + '/_matrix/app/v1' + path, this.hook(callback).bind(this))
}
const prefixes = new Set<string>()
this.bots.forEach(bot => prefixes.add(bot.config.path))
for (const prefix of prefixes) {
put(prefix, '/transactions/:txnId', this.transactions)
get(prefix, '/users/:userId', this.users)
get(prefix, '/room/:roomAlias', this.rooms)
}
})
}
const get = (path: string, callback: (ctx: KoaContext) => void) => {
ctx.router.get(path, this.hook(callback).bind(this))
ctx.router.get('/_matrix/app/v1' + path, this.hook(callback).bind(this))
}
put('/transactions/:txnId', this.transactions)
get('/users/:userId', this.users)
get('/room/:roomAlias', this.rooms)
ctx.on('ready', () => {
registerRoutes()
ctx.on('bot-added', bot => {
if (!(bot instanceof MatrixBot)) return
registerRoutes()
})
ctx.on('bot-removed', bot => {
if (!(bot instanceof MatrixBot)) return
registerRoutes()
})
})
}

async start(bot: MatrixBot): Promise<void> {
Expand Down

0 comments on commit 455f13e

Please sign in to comment.