-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
145 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
const { TimeLogger } = require("../src/util"); | ||
const { R } = require("redbean-node"); | ||
const { UptimeKumaServer } = require("./uptime-kuma-server"); | ||
const server = UptimeKumaServer.getInstance(); | ||
const io = server.io; | ||
|
||
/** | ||
* Send list of users to client | ||
* @param {Socket} socket Socket.io socket instance | ||
* @returns {Promise<Bean[]>} list of users | ||
*/ | ||
async function sendUserList(socket) { | ||
const timeLogger = new TimeLogger(); | ||
const userList = await R.getAll("SELECT id, username, active FROM user"); | ||
|
||
io.to(socket.userID).emit("userList", userList); | ||
timeLogger.print("Send User List"); | ||
|
||
return userList; | ||
} | ||
|
||
/** | ||
* Fetch specified user | ||
* @param {number} userID ID of user to retrieve | ||
* @returns {Promise<Bean[]>} User | ||
*/ | ||
async function getUser(userID) { | ||
const timeLogger = new TimeLogger(); | ||
|
||
const user = await R.getRow( | ||
"SELECT id, username, active FROM user WHERE id = ? ", | ||
[ userID ] | ||
); | ||
|
||
if (!user) { | ||
throw new Error("User not found"); | ||
} | ||
|
||
timeLogger.print(`Get user ${userID}`); | ||
|
||
return user; | ||
} | ||
|
||
/** | ||
* Saves and updates given user entity | ||
* @param {Socket} socket Socket.io socket instance | ||
* @param {object} user user to update | ||
* @returns {Promise<void>} | ||
*/ | ||
async function saveUser(socket, user) { | ||
const timeLogger = new TimeLogger(); | ||
const { id, username, active } = user; | ||
|
||
const bean = await R.findOne("user", " id = ? ", [ id ]); | ||
|
||
if (!bean) { | ||
throw new Error("User not found"); | ||
} | ||
|
||
if (username) { | ||
bean.username = username; | ||
} | ||
if (active !== undefined) { | ||
bean.active = active; | ||
} | ||
|
||
await R.store(bean); | ||
|
||
io.to(socket.userID).emit("saveUser", bean); | ||
|
||
timeLogger.print(`Save user ${user.id}`); | ||
} | ||
|
||
module.exports = { | ||
sendUserList, | ||
getUser, | ||
saveUser | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters