diff --git a/.gitignore b/.gitignore index 4cf3f3f..c1ce237 100644 --- a/.gitignore +++ b/.gitignore @@ -66,3 +66,4 @@ t/ .DS_Store .idea +config.yaml diff --git a/bin/matrix-appservice-wechaty.ts b/bin/matrix-appservice-wechaty.ts index d56ae30..00402ea 100644 --- a/bin/matrix-appservice-wechaty.ts +++ b/bin/matrix-appservice-wechaty.ts @@ -6,7 +6,9 @@ import { log, VERSION, } from '../src/' +import dotenv from 'dotenv' +dotenv.config() async function main () { if (process.env.LOG_LEVEL) { log.level(process.env.LOG_LEVEL as any) diff --git a/package.json b/package.json index e2c624c..dcd8bac 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "matrix-appservice-wechaty", - "version": "0.8.28", + "version": "0.8.30", "description": "Matrix Application Services Bridge for Wechat", "main": "dist/src/index.js", "typings": "dist/src/index.d.ts", @@ -13,6 +13,8 @@ "lint:es": "eslint --ignore-pattern tests/fixtures/ '{bin,src,tests}/**/*.ts' ", "lint:ts": "tsc --noEmit", "dev": "LOG_LEVEL=silly ts-node bin/matrix-appservice-wechaty.ts", + "dev:genarate-registration": "ts-node bin/matrix-appservice-wechaty.ts --config config.yaml --url http://localhost:8788 --generate-registration", + "dev:service": "ts-node bin/matrix-appservice-wechaty.ts --config config.yaml --file wechaty-registration.yaml", "dev:watch": "LOG_LEVEL=silly npx nodemon --watch '{bin,src,tests}/**/*.ts' --exec './node_modules/.bin/ts-node' bin/matrix-appservice-wechaty.ts", "pack": "npm pack", "sloc": "sloc bin scripts src tests --details --format cli-table --keys total,source,comment && sloc bin scripts src tests", @@ -40,10 +42,11 @@ "homepage": "https://github.com/huan/matrix-appservice-wechaty#readme", "dependencies": { "cuid": "^2.1.8", - "matrix-appservice-bridge": "^2.4.1", + "dotenv": "^10.0.0", + "matrix-appservice-bridge": "^2.7.0", "read-pkg-up": "^7.0.1", "update-notifier": "^5.1.0", - "wechaty": "^0.62.2" + "wechaty": "^0.62.3" }, "devDependencies": { "@chatie/eslint-config": "^0.12.3", diff --git a/src/appservice-manager.ts b/src/appservice-manager.ts index 3c65a9f..6530292 100644 --- a/src/appservice-manager.ts +++ b/src/appservice-manager.ts @@ -1,4 +1,5 @@ import cuid from 'cuid' +import { ReadStream } from 'fs' import { Bridge, @@ -7,6 +8,7 @@ import { UserBridgeStore, MatrixRoom, AppServiceRegistration, + FileUploadOpts, } from 'matrix-appservice-bridge' import { @@ -210,6 +212,9 @@ export class AppserviceManager extends Manager { }) const matrixRoom = new MatrixRoom(roomInfo.room_id) + for await (const userId of userIdList.slice(1)) { + await this.bridge.getIntent(userId).join(matrixRoom.getId()) + } return matrixRoom } @@ -232,4 +237,18 @@ export class AppserviceManager extends Manager { return Object.keys(result.joined) } + public async setProfile (userId: string, avataUrl: string, displayName: string): Promise { + const intent = this.bridge.getIntent(userId) + await intent.setAvatarUrl(avataUrl) + await intent.setDisplayName(displayName) + } + + public async uploadContent ( + content: string | Buffer | ReadStream, + userId?: string, + opts?: FileUploadOpts | undefined + ): Promise { + return this.bridge.getIntent(userId).uploadContent(content, opts) + } + } diff --git a/src/middle-manager.ts b/src/middle-manager.ts index 9e869c2..9fb8f34 100644 --- a/src/middle-manager.ts +++ b/src/middle-manager.ts @@ -264,12 +264,29 @@ export class MiddleManager extends Manager { ) const matrixUserId = this.appserviceManager.generateVirtualUserId() - const matrixUser = new MatrixUser(matrixUserId) + + const avatarFile = await wechatyUser.avatar() + const avatarBuffer = await avatarFile.toBuffer() + const avatarUrl = await this.appserviceManager.uploadContent( + avatarBuffer, + matrixUserId, + { type: avatarFile.mimeType }, + ) + + const matrixUser = new MatrixUser(matrixUserId, { + avatarUrl, + displayName: wechatyUser.name(), + }) // userData.name = wechatyUser.name() + APPSERVICE_NAME_POSTFIX matrixUser.set(WECHATY_USER_DATA_KEY, userData) await this.appserviceManager.userStore.setMatrixUser(matrixUser) + void this.appserviceManager.setProfile( + matrixUserId, + avatarUrl, + wechatyUser.name() + ) return matrixUser } @@ -300,7 +317,8 @@ export class MiddleManager extends Manager { roomName = await wechatyRoomOrUser.topic() for await (const member of wechatyRoomOrUser) { const matrixUser = await this.matrixUser(member) - inviteeIdList.push(matrixUser.getId()) + this.wechatyManager.ifSelfWechaty(member.id) + || inviteeIdList.push(matrixUser.getId()) } } else if (wechatyRoomOrUser instanceof WechatyUser) { // User: direct message diff --git a/src/wechaty-manager.ts b/src/wechaty-manager.ts index 3d0e75f..1ea97ec 100644 --- a/src/wechaty-manager.ts +++ b/src/wechaty-manager.ts @@ -19,6 +19,7 @@ export class WechatyManager extends Manager { protected matrixWechatyDict: Map protected wechatyMatrixDict: WeakMap + protected selfWechaty: Wechaty | undefined public appserviceManager!: AppserviceManager public middleManager!: MiddleManager @@ -237,6 +238,7 @@ export class WechatyManager extends Manager { wechatyContact: Contact, ): Promise { log.verbose('WechatyManager', 'onLogin(%s)', wechatyContact) + this.selfWechaty = wechatyContact.wechaty const text = 'You are now logged in to Wechat. Your user name is: ' + wechatyContact.name() @@ -270,7 +272,7 @@ export class WechatyManager extends Manager { log.verbose('WechatyManager', 'onMessage("%s") from "%s" to "%s" with age "%s" (timestamp: "%s")', message, message.from()!.id, - message.to()!.id, + (message.to() || message.room())!.id, message.age(), (message as any).payload.timestamp, ) @@ -350,4 +352,8 @@ export class WechatyManager extends Manager { // ) } + public ifSelfWechaty (wechatyId:string):boolean { + return this.selfWechaty!.puppet.selfId() === wechatyId + } + }