summaryrefslogtreecommitdiff
path: root/includes/external/matrix/index.js
blob: e699d0dcba37aad71c499de894d76cc564553ee1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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");
        let members = require('../../data/' + (user === "raindrops" ? "gdapd" : (user === "cloudburst" ? "ynmuc" : "other")) + "/members.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;

        let fronter1 = null;
        let fronter2 = 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 });
})();