summaryrefslogtreecommitdiff
path: root/includes/external/matrix/index.js
blob: 5149005283d8c894ed8adc34e0ce7faf9ec186f4 (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
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 });
})();