const user = process.argv[2]; const info = require('../../app.json')['matrix'][user]; const fs = require('fs'); 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 child_process = require("child_process"); const client = sdk.createClient({ baseUrl: "https://chat.equestria.dev", accessToken: token, userId: "@" + user + ":equestria.dev" }); function sleep(ms) { return new Promise((res, rej) => { setTimeout(res, ms); }); } 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 avatar1 = system.avatar_url; let avatar2 = null; if (fronters.members.length === 1 || (fronters.members.length === 2 && (fronters.members[0].name === "scootaloo" && fronters.members[1].name === "rainbowdash"))) { name = fronters.members[0].display_name ?? fronters.members[0].name; avatar1 = fronters.members[0].avatar_url ?? avatar1; } else if (fronters.members.length >= 2 && !(fronters.members[0].name === "scootaloo" && fronters.members[1].name === "rainbowdash")) { name = (fronters.members[0].display_name ?? fronters.members[0].name) + " and " + (fronters.members[1].display_name ?? fronters.members[1].name); avatar1 = fronters.members[0].avatar_url ?? avatar1; avatar2 = fronters.members[1].avatar_url ?? avatar2; } 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 } }); await sleep(2000); console.log("Uploading avatar"); let pfp; if (avatar2) { let img1 = (await axios.get(avatar1, {responseType: "arraybuffer"})).data; let img2 = (await axios.get(avatar2, {responseType: "arraybuffer"})).data; fs.writeFileSync("/tmp/chm-" + user + "-1", img1); fs.writeFileSync("/tmp/chm-" + user + "-2", img2); child_process.execSync("convert /tmp/chm-" + user + "-1 -gravity center -resize 512x512 -crop 50%x100% /tmp/chm-" + user + "-1.webp"); child_process.execSync("convert /tmp/chm-" + user + "-2 -gravity center -resize 512x512 -crop 50%x100% /tmp/chm-" + user + "-2.webp"); child_process.execSync("montage -mode concatenate -tile x1 /tmp/chm-" + user + "-1.webp /tmp/chm-" + user + "-2.webp /tmp/chm-" + user + ".webp"); pfp = fs.readFileSync("/tmp/chm-" + user + ".webp"); fs.unlinkSync("/tmp/chm-" + user + "-1"); fs.unlinkSync("/tmp/chm-" + user + "-2"); fs.unlinkSync("/tmp/chm-" + user + "-1.webp"); fs.unlinkSync("/tmp/chm-" + user + "-2.webp"); fs.unlinkSync("/tmp/chm-" + user + ".webp"); } else { pfp = (await axios.get(avatar1, {responseType: "arraybuffer"})).data; } let res = await client.uploadContent(pfp); 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 }); })();