From 633c92eae865e957121e08de634aeee11a8b3992 Mon Sep 17 00:00:00 2001 From: RaindropsSys Date: Mon, 24 Apr 2023 14:03:36 +0200 Subject: Updated 18 files, added 1692 files and deleted includes/system/compare.inc (automated) --- includes/external/matrix/index.js | 78 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 includes/external/matrix/index.js (limited to 'includes/external/matrix/index.js') diff --git a/includes/external/matrix/index.js b/includes/external/matrix/index.js new file mode 100644 index 0000000..5149005 --- /dev/null +++ b/includes/external/matrix/index.js @@ -0,0 +1,78 @@ +const user = process.argv[2]; +const info = require('../../app.json')['matrix'][user]; + +if (typeof info !== "object") throw new Error("Unable to find info"); + +const token = info.token; + +const sdk = require("matrix-js-sdk"); +const axios = require("axios"); +const client = sdk.createClient({ + baseUrl: "https://chat.equestria.dev", + accessToken: token, + userId: "@" + user + ":equestria.dev" +}); + +client.once("sync", async function (state, prevState, res) { + if (state === "PREPARED") { + console.log("Ready"); + + let system = require('../../data/' + (user === "raindrops" ? "gdapd" : (user === "cloudburst" ? "ynmuc" : "other")) + "/general.json"); + let fronters = require('../../data/' + (user === "raindrops" ? "gdapd" : (user === "cloudburst" ? "ynmuc" : "other")) + "/fronters.json"); + + console.log(system.name); + console.log(fronters.members.length + " member(s) at front"); + + let name = system.name; + let avatar = system.avatar_url; + + if (fronters.members.length === 1) { + name = fronters.members[0].display_name ?? fronters.members[0].name; + avatar = fronters.members[0].avatar_url ?? avatar; + } else if (fronters.members.length >= 2) { + name = (fronters.members[0].display_name ?? fronters.members[0].name) + " and " + (fronters.members[1].display_name ?? fronters.members[1].name); + } + + await client.setDeviceDetails(info.device, { + display_name: "Cold Haze PluralKit integration" + }); + + console.log("Setting display name " + name); + await axios.put("https://chat.equestria.dev/_matrix/client/r0/profile/%40" + user + "%3Aequestria.dev/displayname", { + displayname: name + }, { + headers: { + Authorization: "Bearer " + token + } + }); + await axios.get("https://chat.equestria.dev/_matrix/client/r0/profile/%40" + user + "%3Aequestria.dev", { + headers: { + Authorization: "Bearer " + token + } + }); + + console.log("Uploading avatar"); + let res = await client.uploadContent((await axios.get(avatar, {responseType: "arraybuffer"})).data); + await axios.put("https://chat.equestria.dev/_matrix/client/r0/profile/%40" + user + "%3Aequestria.dev/avatar_url", { + avatar_url: res.content_uri + }, { + headers: { + Authorization: "Bearer " + token + } + }); + await axios.get("https://chat.equestria.dev/_matrix/client/r0/profile/%40" + user + "%3Aequestria.dev", { + headers: { + Authorization: "Bearer " + token + } + }); + + process.exit(0); + } else { + console.log(state); + process.exit(1); + } +}); + +(async () => { + await client.startClient({ initialSyncLimit: 0 }); +})(); \ No newline at end of file -- cgit