2021-09-04 11:03:40 -07:00
|
|
|
/*
|
|
|
|
* For Client Socket
|
|
|
|
*/
|
|
|
|
const { TimeLogger } = require("../src/util");
|
|
|
|
const { R } = require("redbean-node");
|
2022-04-19 00:38:59 -07:00
|
|
|
const { UptimeKumaServer } = require("./uptime-kuma-server");
|
|
|
|
const io = UptimeKumaServer.getInstance().io;
|
2021-10-08 05:03:52 -07:00
|
|
|
const { setting } = require("./util-server");
|
|
|
|
const checkVersion = require("./check-version");
|
2021-09-04 11:03:40 -07:00
|
|
|
|
2021-11-09 21:24:31 -08:00
|
|
|
/**
|
|
|
|
* Send a list of notifications to the user.
|
|
|
|
* @param {Socket} socket The socket object that is connected to the client.
|
|
|
|
*
|
|
|
|
* Generated by Trelent
|
|
|
|
*/
|
2021-09-04 11:03:40 -07:00
|
|
|
async function sendNotificationList(socket) {
|
|
|
|
const timeLogger = new TimeLogger();
|
|
|
|
|
|
|
|
let result = [];
|
|
|
|
let list = await R.find("notification", " user_id = ? ", [
|
|
|
|
socket.userID,
|
|
|
|
]);
|
|
|
|
|
|
|
|
for (let bean of list) {
|
2021-10-08 05:03:52 -07:00
|
|
|
result.push(bean.export());
|
2021-09-04 11:03:40 -07:00
|
|
|
}
|
|
|
|
|
2021-10-08 05:03:52 -07:00
|
|
|
io.to(socket.userID).emit("notificationList", result);
|
2021-09-04 11:03:40 -07:00
|
|
|
|
|
|
|
timeLogger.print("Send Notification List");
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send Heartbeat History list to socket
|
|
|
|
* @param toUser True = send to all browsers with the same user id, False = send to the current browser only
|
|
|
|
* @param overwrite Overwrite client-side's heartbeat list
|
|
|
|
*/
|
|
|
|
async function sendHeartbeatList(socket, monitorID, toUser = false, overwrite = false) {
|
|
|
|
const timeLogger = new TimeLogger();
|
|
|
|
|
2021-09-08 20:57:23 -07:00
|
|
|
let list = await R.getAll(`
|
|
|
|
SELECT * FROM heartbeat
|
|
|
|
WHERE monitor_id = ?
|
2021-09-08 04:54:37 -07:00
|
|
|
ORDER BY time DESC
|
|
|
|
LIMIT 100
|
|
|
|
`, [
|
|
|
|
monitorID,
|
2021-10-08 05:03:52 -07:00
|
|
|
]);
|
2021-09-04 11:03:40 -07:00
|
|
|
|
2021-09-08 20:57:23 -07:00
|
|
|
let result = list.reverse();
|
2021-09-04 11:03:40 -07:00
|
|
|
|
|
|
|
if (toUser) {
|
|
|
|
io.to(socket.userID).emit("heartbeatList", monitorID, result, overwrite);
|
|
|
|
} else {
|
|
|
|
socket.emit("heartbeatList", monitorID, result, overwrite);
|
|
|
|
}
|
|
|
|
|
|
|
|
timeLogger.print(`[Monitor: ${monitorID}] sendHeartbeatList`);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Important Heart beat list (aka event list)
|
|
|
|
* @param socket
|
|
|
|
* @param monitorID
|
|
|
|
* @param toUser True = send to all browsers with the same user id, False = send to the current browser only
|
|
|
|
* @param overwrite Overwrite client-side's heartbeat list
|
|
|
|
*/
|
|
|
|
async function sendImportantHeartbeatList(socket, monitorID, toUser = false, overwrite = false) {
|
|
|
|
const timeLogger = new TimeLogger();
|
|
|
|
|
|
|
|
let list = await R.find("heartbeat", `
|
|
|
|
monitor_id = ?
|
|
|
|
AND important = 1
|
|
|
|
ORDER BY time DESC
|
|
|
|
LIMIT 500
|
|
|
|
`, [
|
|
|
|
monitorID,
|
2021-10-08 05:03:52 -07:00
|
|
|
]);
|
2021-09-04 11:03:40 -07:00
|
|
|
|
|
|
|
timeLogger.print(`[Monitor: ${monitorID}] sendImportantHeartbeatList`);
|
|
|
|
|
|
|
|
if (toUser) {
|
|
|
|
io.to(socket.userID).emit("importantHeartbeatList", monitorID, list, overwrite);
|
|
|
|
} else {
|
|
|
|
socket.emit("importantHeartbeatList", monitorID, list, overwrite);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-10-30 10:37:15 -07:00
|
|
|
/**
|
|
|
|
* Delivers proxy list
|
|
|
|
*
|
|
|
|
* @param socket
|
|
|
|
* @return {Promise<Bean[]>}
|
|
|
|
*/
|
|
|
|
async function sendProxyList(socket) {
|
|
|
|
const timeLogger = new TimeLogger();
|
|
|
|
|
2022-04-17 00:27:35 -07:00
|
|
|
const list = await R.find("proxy", " user_id = ? ", [ socket.userID ]);
|
2021-10-30 10:37:15 -07:00
|
|
|
io.to(socket.userID).emit("proxyList", list.map(bean => bean.export()));
|
|
|
|
|
|
|
|
timeLogger.print("Send Proxy List");
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2021-11-09 21:24:31 -08:00
|
|
|
/**
|
|
|
|
* Emits the version information to the client.
|
|
|
|
* @param {Socket} socket The socket object that is connected to the client.
|
|
|
|
*
|
|
|
|
* Generated by Trelent
|
|
|
|
*/
|
2021-10-08 05:03:52 -07:00
|
|
|
async function sendInfo(socket) {
|
|
|
|
socket.emit("info", {
|
|
|
|
version: checkVersion.version,
|
|
|
|
latestVersion: checkVersion.latestVersion,
|
|
|
|
primaryBaseURL: await setting("primaryBaseURL")
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-09-04 11:03:40 -07:00
|
|
|
module.exports = {
|
|
|
|
sendNotificationList,
|
|
|
|
sendImportantHeartbeatList,
|
|
|
|
sendHeartbeatList,
|
2021-10-30 10:37:15 -07:00
|
|
|
sendProxyList,
|
|
|
|
sendInfo,
|
2021-10-08 05:03:52 -07:00
|
|
|
};
|