diff --git a/ppfun-bridge/README.md b/ppfun-bridge/README.md index 7dd17d3..6a2f024 100644 --- a/ppfun-bridge/README.md +++ b/ppfun-bridge/README.md @@ -47,6 +47,12 @@ and watch its logs with pm2 log ppfun-bridge ``` +## Room Creation + +The bridge automatically creates matrix rooms for all public pixelplanet channels available and makes the '@pp_admin:pixelplanet.fun' user admin. +You want to create this admin user first to be able to login as it. +Or change the room-creation code in `src/ppfunMatrixBridge.js` in the `connectRooms` method. + ## References diff --git a/ppfun-bridge/deploy.sh b/ppfun-bridge/deploy.sh index 8f87ff5..d4bd36f 100755 --- a/ppfun-bridge/deploy.sh +++ b/ppfun-bridge/deploy.sh @@ -1,3 +1,4 @@ #!/bin/bash scp ./src/*.js pixelplanet:~/ppfun-bridge/src/ scp ./*.js pixelplanet:~/ppfun-bridge/ +ssh pixelplanet 'pm2 stop ppfun-bridge; cd ~/ppfun-bridge; pm2 start ecosystem.yml' diff --git a/ppfun-bridge/ecosystem.yml b/ppfun-bridge/ecosystem.yml index 1b8e05a..5d84329 100644 --- a/ppfun-bridge/ecosystem.yml +++ b/ppfun-bridge/ecosystem.yml @@ -4,7 +4,7 @@ apps: node_args: --nouse-idle-notification --expose-gc env: APISOCKET_KEY: "YOUR_APISOCKET_KEY_FROM_PIXELPLANET_CONFIG" - APISOCKET_URL: "https://pixelplanet.fun/mcws" + APISOCKET_URL: "wss://pixelplanet.fun/mcws" PPFUN_UID: 1 REGISTRATION_YAML: "/etc/matrix-synapse/ppfun-registration.yaml" HOMESERVER_URL: "http://localhost:8008" diff --git a/ppfun-bridge/ppfun-registration.yaml b/ppfun-bridge/ppfun-registration.yaml index 6355f3d..94fa6e7 100644 --- a/ppfun-bridge/ppfun-registration.yaml +++ b/ppfun-bridge/ppfun-registration.yaml @@ -3,6 +3,7 @@ hs_token: CHANGE_THIS_TO_SOME_LONG_RANDOM_STRING as_token: CHANGE_THIS_TO_SOME_DIFFERENT_LONG_RANDOM_STRING # the url of the application service, its set to 8009 per default url: http://localhost:8009 +# the pp prefix is hardcoded sender_localpart: pp namespaces: users: diff --git a/ppfun-bridge/src/ppfunMatrixBridge.js b/ppfun-bridge/src/ppfunMatrixBridge.js index e0c3aa3..714c6d8 100644 --- a/ppfun-bridge/src/ppfunMatrixBridge.js +++ b/ppfun-bridge/src/ppfunMatrixBridge.js @@ -24,8 +24,6 @@ class PPfunMatrixBridge { // because ppfun apisocket doesn't return your own sent messages // to you this.echoSuppression = new Map(); - this.pToMroomMap.set(1, "!scTbMproDsaihhGesQ:pixelplanet.fun"); - this.mToProomMap.set("!scTbMproDsaihhGesQ:pixelplanet.fun", 1); this.ppfunSocket = new PPfunSocket( apiSocketUrl, apiSocketKey, @@ -85,7 +83,6 @@ class PPfunMatrixBridge { async recMatrix(request, context) { const event = request.getData(); - console.log(event); if (event.type === "m.room.message" && event.content && event.content.msgtype === "m.text" @@ -168,11 +165,59 @@ class PPfunMatrixBridge { intent.sendText(roomId, msg); } - connectRooms(roomList) { - console.log(roomList); - this.syncChannels = false; - for (let i = 0; i < roomList.length; i += 1) { - const [ppfun_id, ppfun_name] = roomList[i]; + /* + * creates matrix rooms for the pixelplaent channels if neccessary and + * opulates the mToProomMap and pToMroomMap. + * @param roomList list of rooms and their names like: + * [[roomId, roomName], [roomId2, roomName2], ...] + * @return Promise number of how many rooms got created + */ + async connectRooms(roomList) { + try { + const intent = this.matrixBridge.getIntent(); + this.syncChannels = false; + for (let i = 0; i < roomList.length; i += 1) { + const [ppfun_id, ppfun_name] = roomList[i]; + const localpart = `${this.prefix}_${ppfun_name}` + const alias = `#${localpart}:${this.domain}`; + let room_id = null; + try { + console.log('resolving', ppfun_id, ppfun_name, alias); + room_id = await intent.resolveRoom(alias); + } catch { + console.log(`Matrix room ${alias} does not exist. Try to create it`); + if (ppfun_name === 'es') { + /* + * see https://spec.matrix.org/v1.1/client-server-api/ + * look for requestBody of createRoom + */ + room_id = await intent.createRoom({ + options: { + name: `[${ppfun_name}] pixelplanet.fun`, + topic: `This is the official room for the ${ppfun_name} channel on pixelplanet.fun. A pixel drawing game.`, + room_alias_name: localpart, + visibility: 'public', + preset: 'public_chat', + }, + }); + room_id = room_id.room_id; + await intent.setRoomAvatar(room_id, 'mxc://pixelplanet.fun/cXJRbbaaqkzpOydGVxASpizK'); + } + } + if (room_id) { + try { + await intent.setPowerLevel(room_id, `@${this.prefix}_admin:pixelplanet.fun`, 100); + await intent.setPowerLevel(room_id, '@hf:pixelplanet.fun', 100); + } catch { + console.warn(`Couldn't give @pp_admin or @hf admin rights for ${alias}`); + } + console.log(`Mapped ${ppfun_name} | ${ppfun_id} to ${alias} | ${room_id}`); + this.mToProomMap.set(room_id, ppfun_id); + this.pToMroomMap.set(ppfun_id, room_id); + } + } + } catch (e) { + console.error(`Could not sync matrix rooms to ppfun channels: ${e}`); } } } diff --git a/ppfun-bridge/src/ppfunsocket.js b/ppfun-bridge/src/ppfunsocket.js index 0d97788..8f5eafb 100644 --- a/ppfun-bridge/src/ppfunsocket.js +++ b/ppfun-bridge/src/ppfunsocket.js @@ -29,7 +29,7 @@ class PPfunSocket extends EventEmitter { getFlag(uid) { let flag = this.flagMap.get(uid); if (!flag) { - if (!this.flagMap.has(uid)) { + if (flag !== null) { this.flagMap.set(uid, null); this.ws.send(`["getflag", ${uid}]`); } diff --git a/synapse-auth-module/deploy.sh b/synapse-auth-module/deploy.sh index 4011f0e..8b3ffd6 100755 --- a/synapse-auth-module/deploy.sh +++ b/synapse-auth-module/deploy.sh @@ -1,4 +1,4 @@ #!/bin/bash -scp ./ppfun-auth.py root@pixelplanet:/etc/matrix-synapse/testmodule.py +scp ./ppfun_auth.py root@pixelplanet:/etc/matrix-synapse/ppfun_auth.py ssh root@pixelplanet 'systemctl restart matrix-synapse' diff --git a/synapse-auth-module/ppfun-auth.py b/synapse-auth-module/ppfun_auth.py similarity index 100% rename from synapse-auth-module/ppfun-auth.py rename to synapse-auth-module/ppfun_auth.py