Repository: louislam/uptime-kuma
File: server/notification-providers/dingding.js (JavaScript)

Code before:
const NotificationProvider = require("./notification-provider");
const { DOWN, UP } = require("../../src/util");
const { default: axios } = require("axios");
const Crypto = require("crypto");

class DingDing extends NotificationProvider {
    name = "DingDing";

    /**
     * @inheritdoc
     */
    async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
        const okMsg = "Sent Successfully.";
        const mentionAll = notification.mentioning === "everyone";
        const mobileList = notification.mentioning === "specify-mobiles" ? notification.mobileList.split(",") : [];
        const userList = notification.mentioning === "specify-users" ? notification.userList.split(",") : [];
        const mentionStr = [ ...mobileList || [], ...userList || [] ].map(item => `@${item}`).join(" ");
        try {
            if (heartbeatJSON != null) {
                let params = {
                    msgtype: "markdown",
                    markdown: {
                        title: `[${this.statusToString(heartbeatJSON["status"])}] ${monitorJSON["name"]}`,
                        text: `## [${this.statusToString(heartbeatJSON["status"])}] ${monitorJSON["name"]} \n> ${heartbeatJSON["msg"]}\n> Time (${heartbeatJSON["timezone"]}): ${heartbeatJSON["localDateTime"]}${"\n\n" + mentionStr}`,
                    },
                    at: {
                        isAtAll: mentionAll,
                        atUserIds: userList,
                        atMobiles: mobileList
                    }
                };
                if (await this.sendToDingDing(notification, params)) {
                    return okMsg;
                }
            } else {
                let params = {
                    msgtype: "text",
                    text: {
                        content: `${msg}${"\n" + mentionStr}`
                    },
                    at: {
                        isAtAll: mentionAll,

Review comment (refactor):
please handle users
- using spaces => trimming the input
- using `@` => trimming the input unless this is allowed
- (optional) drop duplicates from the list

Code after:
const NotificationProvider = require("./notification-provider");
const { DOWN, UP } = require("../../src/util");
const { default: axios } = require("axios");
const Crypto = require("crypto");

class DingDing extends NotificationProvider {
    name = "DingDing";

    /**
     * @inheritdoc
     */
    async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
        const okMsg = "Sent Successfully.";
        const mentionAll = notification.mentioning === "everyone";
        const mobileList = notification.mentioning === "specify-mobiles" ? notification.mobileList : [];
        const userList = notification.mentioning === "specify-users" ? notification.userList : [];
        const finalList = [ ...mobileList || [], ...userList || [] ];
        const mentionStr = finalList.length > 0 ? "\n" : "" + finalList.map(item => `@${item}`).join(" ");
        try {
            if (heartbeatJSON != null) {
                let params = {
                    msgtype: "markdown",
                    markdown: {
                        title: `[${this.statusToString(heartbeatJSON["status"])}] ${monitorJSON["name"]}`,
                        text: `## [${this.statusToString(heartbeatJSON["status"])}] ${monitorJSON["name"]} \n> ${heartbeatJSON["msg"]}\n> Time (${heartbeatJSON["timezone"]}): ${heartbeatJSON["localDateTime"]}${mentionStr}`,
                    },
                    at: {
                        isAtAll: mentionAll,
                        atUserIds: userList,
                        atMobiles: mobileList
                    }
                };
                if (await this.sendToDingDing(notification, params)) {
                    return okMsg;
                }
            } else {
                let params = {
                    msgtype: "text",
                    text: {
                        content: `${msg}${mentionStr}`
                    },
                    at: {