summaryrefslogtreecommitdiffstats
path: root/server/notification-providers/lunasea.js
blob: 787a704a96802807ef7e8defd4ef4fd170459612 (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
const NotificationProvider = require("./notification-provider");
const axios = require("axios");
const { DOWN, UP } = require("../../src/util");

class LunaSea extends NotificationProvider {
    name = "lunasea";

    /**
     * @inheritdoc
     */
    async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
        const okMsg = "Sent Successfully.";
        const url = "https://notify.lunasea.app/v1";

        try {
            const target = this.getTarget(notification);
            if (heartbeatJSON == null) {
                let testdata = {
                    "title": "Uptime Kuma Alert",
                    "body": msg,
                };
                await axios.post(`${url}/custom/${target}`, testdata);
                return okMsg;
            }

            if (heartbeatJSON["status"] === DOWN) {
                let downdata = {
                    "title": "UptimeKuma Alert: " + monitorJSON["name"],
                    "body": "[🔴 Down] " +
                        heartbeatJSON["msg"] +
                        `\nTime (${heartbeatJSON["timezone"]}): ${heartbeatJSON["localDateTime"]}`
                };
                await axios.post(`${url}/custom/${target}`, downdata);
                return okMsg;
            }

            if (heartbeatJSON["status"] === UP) {
                let updata = {
                    "title": "UptimeKuma Alert: " + monitorJSON["name"],
                    "body": "[✅ Up] " +
                        heartbeatJSON["msg"] +
                        `\nTime (${heartbeatJSON["timezone"]}): ${heartbeatJSON["localDateTime"]}`
                };
                await axios.post(`${url}/custom/${target}`, updata);
                return okMsg;
            }

        } catch (error) {
            this.throwGeneralAxiosError(error);
        }
    }

    /**
     * Generates the lunasea target to send the notification to
     * @param {BeanModel} notification Notification details
     * @returns {string} The target to send the notification to
     */
    getTarget(notification) {
        if (notification.lunaseaTarget === "user") {
            return "user/" + notification.lunaseaUserID;
        }
        return "device/" + notification.lunaseaDevice;

    }
}

module.exports = LunaSea;