-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: hooks module with remove empty rooms hook
Closes #29
- Loading branch information
Showing
8 changed files
with
182 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
packages/tom-server/src/hooks/callback/on-room-membership-event.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { type TwakeLogger } from '@twake/logger' | ||
import type MatrixApplicationServer from '@twake/matrix-application-server' | ||
import { type ClientEvent } from '@twake/matrix-application-server' | ||
import fetch from 'node-fetch' | ||
import { type Config } from '../../types' | ||
import { type IMatrixDBRepository } from '../repositories/interfaces/matrix-db-repository.interface' | ||
|
||
export class OnRoomMembershipEvent { | ||
constructor( | ||
private readonly _applicationServer: MatrixApplicationServer, | ||
matrixDb: IMatrixDBRepository, | ||
conf: Config, | ||
logger: TwakeLogger | ||
) { | ||
this._applicationServer.on( | ||
'state event | type: m.room.member', | ||
(event: ClientEvent) => { | ||
if (event.content.membership === 'leave') { | ||
let statusCode: number | ||
const deleteRequestUrl = `https://${conf.matrix_server}/_synapse/admin/v1/rooms/${event.room_id}` | ||
matrixDb | ||
.hasRoomLocalServerUsers(event.room_id) | ||
// eslint-disable-next-line @typescript-eslint/promise-function-async | ||
.then((hasLocalServerUsers) => { | ||
if (!hasLocalServerUsers) { | ||
return fetch(encodeURI(deleteRequestUrl), { | ||
method: 'DELETE', | ||
headers: { | ||
Authorization: `Bearer ${this._applicationServer.appServiceRegistration.asToken}` | ||
}, | ||
body: JSON.stringify({}) | ||
}) | ||
} | ||
}) | ||
// eslint-disable-next-line @typescript-eslint/promise-function-async | ||
.then((response) => { | ||
if (response != null) { | ||
statusCode = response.status | ||
return response.json() | ||
} | ||
}) | ||
.then((body) => { | ||
if (body != null) { | ||
logger.info(JSON.stringify(body), { | ||
matrixUserId: `@${this._applicationServer.appServiceRegistration.senderLocalpart}:${conf.server_name}`, | ||
httpMethod: 'DELETE', | ||
requestUrl: deleteRequestUrl, | ||
status: statusCode | ||
}) | ||
} | ||
}) | ||
.catch((e) => { | ||
logger.error(e) | ||
}) | ||
} | ||
} | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { type TwakeLogger } from '@twake/logger' | ||
import type MatrixApplicationServer from '@twake/matrix-application-server' | ||
import { type MatrixDB } from '@twake/matrix-identity-server' | ||
import { type Config } from '../types' | ||
import { OnRoomMembershipEvent } from './callback/on-room-membership-event' | ||
import { MatrixDBRepository } from './repositories/matrix-db.repository' | ||
|
||
export type CallbackHooks = OnRoomMembershipEvent | ||
|
||
export class TwakeServerHooks { | ||
callbackHooks: CallbackHooks[] | ||
ready: Promise<void> | ||
constructor( | ||
applicationServer: MatrixApplicationServer, | ||
matrixDb: MatrixDB, | ||
conf: Config, | ||
logger: TwakeLogger | ||
) { | ||
this.callbackHooks = [] | ||
const matrixDbRepository = new MatrixDBRepository( | ||
matrixDb, | ||
conf.server_name | ||
) | ||
this.ready = new Promise<void>((resolve, reject) => { | ||
matrixDbRepository | ||
.addMatrixUserAdmin( | ||
`@${applicationServer.appServiceRegistration.senderLocalpart}:${conf.server_name}` | ||
) | ||
.then(() => { | ||
this.callbackHooks.push( | ||
new OnRoomMembershipEvent( | ||
applicationServer, | ||
matrixDbRepository, | ||
conf, | ||
logger | ||
) | ||
) | ||
resolve() | ||
}) | ||
.catch((e) => { | ||
reject(e) | ||
}) | ||
}) | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
packages/tom-server/src/hooks/repositories/interfaces/matrix-db-repository.interface.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { type DbGetResult } from '@twake/matrix-identity-server' | ||
|
||
export interface IMatrixDBRepository { | ||
addMatrixUserAdmin: (matrixAddress: string) => Promise<DbGetResult> | ||
hasRoomLocalServerUsers: (roomId: string) => Promise<boolean> | ||
} |
47 changes: 47 additions & 0 deletions
47
packages/tom-server/src/hooks/repositories/matrix-db.repository.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { type DbGetResult, type MatrixDB } from '@twake/matrix-identity-server' | ||
import lodash from 'lodash' | ||
import { type IMatrixDBRepository } from './interfaces/matrix-db-repository.interface' | ||
|
||
const { groupBy, mapValues, escapeRegExp } = lodash | ||
|
||
export class MatrixDBRepository implements IMatrixDBRepository { | ||
constructor( | ||
private readonly _matrixDb: MatrixDB, | ||
private readonly _matrixServer: string | ||
) {} | ||
|
||
async addMatrixUserAdmin(matrixAddress: string): Promise<DbGetResult> { | ||
const result = await this._matrixDb.insert('users', { | ||
name: matrixAddress, | ||
admin: 1 | ||
}) | ||
if (result.length === 0) { | ||
throw new Error(`Set ${matrixAddress} as Matrix homeserver admin failed`) | ||
} | ||
return result | ||
} | ||
|
||
async hasRoomLocalServerUsers(roomId: string): Promise<boolean> { | ||
const userIdLocalServerRegExp = new RegExp( | ||
`^@[^:]*:${escapeRegExp(this._matrixServer)}$` | ||
) | ||
const localServerUsers = ( | ||
(await this._matrixDb.get('room_memberships', ['user_id', 'membership'], { | ||
room_id: roomId | ||
})) as Array<{ user_id: string; membership: string }> | ||
).filter( | ||
(membershipDetail) => | ||
membershipDetail.user_id.match(userIdLocalServerRegExp) != null | ||
) | ||
|
||
const currentMembershipByUserId = mapValues( | ||
groupBy(localServerUsers, 'user_id'), | ||
(membershipsDetails) => | ||
membershipsDetails.map((detail) => detail.membership).pop() | ||
) | ||
|
||
return Object.keys(currentMembershipByUserId).some( | ||
(userId) => currentMembershipByUserId[userId] !== 'leave' | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters