This commit is contained in:
HF 2022-01-14 01:18:47 +01:00
parent e92764c0d7
commit 884828e61f
8 changed files with 64 additions and 11 deletions

View File

@ -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

View File

@ -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'

View File

@ -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"

View File

@ -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:

View File

@ -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<int> 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}`);
}
}
}

View File

@ -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}]`);
}

View File

@ -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'