summaryrefslogtreecommitdiffstats
path: root/src/components/NotificationDialog.vue
blob: f6d728029986e2a4172f9a8499344214a99e91fa (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<template>
    <form @submit.prevent="submit">
        <div ref="modal" class="modal fade" tabindex="-1" data-bs-backdrop="static">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 id="exampleModalLabel" class="modal-title">
                            {{ $t("Setup Notification") }}
                        </h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close" />
                    </div>
                    <div class="modal-body">
                        <div class="mb-3">
                            <label for="notification-type" class="form-label">{{ $t("Notification Type") }}</label>
                            <select id="notification-type" v-model="notification.type" class="form-select">
                                <option v-for="(name, type) in notificationNameList.regularList" :key="type" :value="type">{{ name }}</option>
                                <optgroup :label="$t('notificationRegional')">
                                    <option v-for="(name, type) in notificationNameList.regionalList" :key="type" :value="type">{{ name }}</option>
                                </optgroup>
                            </select>
                        </div>

                        <div class="mb-3">
                            <label for="notification-name" class="form-label">{{ $t("Friendly Name") }}</label>
                            <input id="notification-name" v-model="notification.name" type="text" class="form-control" required>
                        </div>

                        <!-- form body -->
                        <component :is="currentForm" />

                        <div class="mb-3 mt-4">
                            <hr class="dropdown-divider mb-4">

                            <div class="form-check form-switch">
                                <input v-model="notification.isDefault" class="form-check-input" type="checkbox">
                                <label class="form-check-label">{{ $t("Default enabled") }}</label>
                            </div>
                            <div class="form-text">
                                {{ $t("enableDefaultNotificationDescription") }}
                            </div>

                            <br>

                            <div class="form-check form-switch">
                                <input v-model="notification.applyExisting" class="form-check-input" type="checkbox">
                                <label class="form-check-label">{{ $t("Apply on all existing monitors") }}</label>
                            </div>
                        </div>
                    </div>

                    <div class="modal-footer">
                        <button v-if="id" type="button" class="btn btn-danger" :disabled="processing" @click="deleteConfirm">
                            {{ $t("Delete") }}
                        </button>
                        <button type="button" class="btn btn-warning" :disabled="processing" @click="test">
                            {{ $t("Test") }}
                        </button>
                        <button type="submit" class="btn btn-primary" :disabled="processing">
                            <div v-if="processing" class="spinner-border spinner-border-sm me-1"></div>
                            {{ $t("Save") }}
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </form>

    <Confirm ref="confirmDelete" btn-style="btn-danger" :yes-text="$t('Yes')" :no-text="$t('No')" @yes="deleteNotification">
        {{ $t("deleteNotificationMsg") }}
    </Confirm>
</template>

<script>
import { Modal } from "bootstrap";

import Confirm from "./Confirm.vue";
import NotificationFormList from "./notifications";

export default {
    components: {
        Confirm,
    },
    props: {},
    emits: [ "added" ],
    data() {
        return {
            model: null,
            processing: false,
            id: null,
            notificationTypes: Object.keys(NotificationFormList).sort((a, b) => {
                return a.toLowerCase().localeCompare(b.toLowerCase());
            }),
            notification: {
                name: "",
                /** @type { null | keyof NotificationFormList } */
                type: null,
                isDefault: false,
                // Do not set default value here, please scroll to show()
            }
        };
    },

    computed: {
        currentForm() {
            if (!this.notification.type) {
                return null;
            }
            return NotificationFormList[this.notification.type];
        },

        notificationNameList() {
            let regularList = {
                "alerta": "Alerta",
                "AlertNow": "AlertNow",
                "apprise": this.$t("apprise"),
                "Bark": "Bark",
                "Bitrix24": "Bitrix24",
                "clicksendsms": "ClickSend SMS",
                "CallMeBot": "CallMeBot (WhatsApp, Telegram Call, Facebook Messanger)",
                "discord": "Discord",
                "Elks": "46elks",
                "GoogleChat": "Google Chat (Google Workspace)",
                "gorush": "Gorush",
                "gotify": "Gotify",
                "GrafanaOncall": "Grafana Oncall",
                "HeiiOnCall": "Heii On-Call",
                "HomeAssistant": "Home Assistant",
                "Keep": "Keep",
                "Kook": "Kook",
                "line": "LINE Messenger",
                "LineNotify": "LINE Notify",
                "lunasea": "LunaSea",
                "matrix": "Matrix",
                "mattermost": "Mattermost",
                "nostr": "Nostr",
                "ntfy": "Ntfy",
                "octopush": "Octopush",
                "OneBot": "OneBot",
                "Onesender": "Onesender",
                "Opsgenie": "Opsgenie",
                "PagerDuty": "PagerDuty",
                "PagerTree": "PagerTree",
                "pushbullet": "Pushbullet",
                "PushByTechulus": "Push by Techulus",
                "pushover": "Pushover",
                "pushy": "Pushy",
                "rocket.chat": "Rocket.Chat",
                "signal": "Signal",
                "SIGNL4": "SIGNL4",
                "slack": "Slack",
                "squadcast": "SquadCast",
                "SMSEagle": "SMSEagle",
                "SMSPartner": "SMS Partner",
                "smtp": this.$t("smtp"),
                "stackfield": "Stackfield",
                "teams": "Microsoft Teams",
                "telegram": "Telegram",
                "threema": "Threema",
                "twilio": "Twilio",
                "Splunk": "Splunk",
                "webhook": "Webhook",
                "GoAlert": "GoAlert",
                "ZohoCliq": "ZohoCliq",
                "SevenIO": "SevenIO",
                "whapi": "WhatsApp (Whapi)",
                "gtxmessaging": "GtxMessaging",
                "Cellsynt": "Cellsynt",
                "SendGrid": "SendGrid"
            };

            // Put notifications here if it's not supported in most regions or its documentation is not in English
            let regionalList = {
                "AliyunSMS": "AliyunSMS (阿里云短信服务)",
                "DingDing": "DingDing (钉钉自定义机器人)",
                "Feishu": "Feishu (飞书)",
                "FlashDuty": "FlashDuty (快猫星云)",
                "FreeMobile": "FreeMobile (mobile.free.fr)",
                "PushDeer": "PushDeer",
                "promosms": "PromoSMS",
                "serwersms": "SerwerSMS.pl",
                "SMSManager": "SmsManager (smsmanager.cz)",
                "WeCom": "WeCom (企业微信群机器人)",
                "ServerChan": "ServerChan (Server酱)",
                "smsc": "SMSC",
                "WPush": "WPush(wpush.cn)",
            };

            // Sort by notification name
            // No idea how, but it works
            // https://stackoverflow.com/questions/1069666/sorting-object-property-by-values
            let sort = (list2) => {
                return Object.entries(list2)
                    .sort(([ , a ], [ , b ]) => a.localeCompare(b))
                    .reduce((r, [ k, v ]) => ({
                        ...r,
                        [k]: v
                    }), {});
            };

            return {
                regularList: sort(regularList),
                regionalList: sort(regionalList),
            };
        },

        notificationFullNameList() {
            let list = {};
            for (let [ key, value ] of Object.entries(this.notificationNameList.regularList)) {
                list[key] = value;
            }
            for (let [ key, value ] of Object.entries(this.notificationNameList.regionalList)) {
                list[key] = value;
            }
            return list;
        },
    },

    watch: {
        "notification.type"(to, from) {
            let oldName;
            if (from) {
                oldName = this.getUniqueDefaultName(from);
            } else {
                oldName = "";
            }

            if (! this.notification.name || this.notification.name === oldName) {
                this.notification.name = this.getUniqueDefaultName(to);
            }
        },
    },
    mounted() {
        this.modal = new Modal(this.$refs.modal);
    },
    methods: {

        /**
         * Show dialog to confirm deletion
         * @returns {void}
         */
        deleteConfirm() {
            this.modal.hide();
            this.$refs.confirmDelete.show();
        },

        /**
         * Show settings for specified notification
         * @param {number} notificationID ID of notification to show
         * @returns {void}
         */
        show(notificationID) {
            if (notificationID) {
                this.id = notificationID;

                for (let n of this.$root.notificationList) {
                    if (n.id === notificationID) {
                        this.notification = JSON.parse(n.config);
                        break;
                    }
                }
            } else {
                this.id = null;
                this.notification = {
                    name: "",
                    type: "telegram",
                    isDefault: false,
                };
            }

            this.modal.show();
        },

        /**
         * Submit the form to the server
         * @returns {void}
         */
        submit() {
            this.processing = true;
            this.$root.getSocket().emit("addNotification", this.notification, this.id, (res) => {
                this.$root.toastRes(res);
                this.processing = false;

                if (res.ok) {
                    this.modal.hide();

                    // Emit added event, doesn't emit edit.
                    if (! this.id) {
                        this.$emit("added", res.id);
                    }

                }
            });
        },

        /**
         * Test the notification endpoint
         * @returns {void}
         */
        test() {
            this.processing = true;
            this.$root.getSocket().emit("testNotification", this.notification, (res) => {
                this.$root.toastRes(res);
                this.processing = false;
            });
        },

        /**
         * Delete the notification endpoint
         * @returns {void}
         */
        deleteNotification() {
            this.processing = true;
            this.$root.getSocket().emit("deleteNotification", this.id, (res) => {
                this.$root.toastRes(res);
                this.processing = false;

                if (res.ok) {
                    this.modal.hide();
                }
            });
        },
        /**
         * Get a unique default name for the notification
         * @param {keyof NotificationFormList} notificationKey
         * Notification to retrieve
         * @returns {string} Default name
         */
        getUniqueDefaultName(notificationKey) {

            let index = 1;
            let name = "";
            do {
                name = this.$t("defaultNotificationName", {
                    notification: this.notificationFullNameList[notificationKey].replace(/\(.+\)/, "").trim(),
                    number: index++
                });
            } while (this.$root.notificationList.find(it => it.name === name));
            return name;
        }
    },
};
</script>

<style lang="scss" scoped>
@import "../assets/vars.scss";

.dark {
    .modal-dialog .form-text, .modal-dialog p {
        color: $dark-font-color;
    }
}
</style>