use queued login notification

This commit is contained in:
SrGooglo 2025-02-25 23:10:36 +00:00
parent fa83c55264
commit 9d9155b0af
4 changed files with 74 additions and 18 deletions

View File

@ -1,6 +1,6 @@
import { Server } from "linebridge" import { Server } from "linebridge"
import DbManager from "@shared-classes/DbManager" import DbManager from "@shared-classes/DbManager"
import TaskQueueManager from "@shared-classes/TaskQueueManager"
import SharedMiddlewares from "@shared-middlewares" import SharedMiddlewares from "@shared-middlewares"
export default class API extends Server { export default class API extends Server {
@ -8,17 +8,34 @@ export default class API extends Server {
static useEngine = "hyper-express" static useEngine = "hyper-express"
static routesPath = `${__dirname}/routes` static routesPath = `${__dirname}/routes`
static listen_port = process.env.HTTP_LISTEN_PORT ?? 3020 static listen_port = process.env.HTTP_LISTEN_PORT ?? 3020
static enableWebsockets = true
middlewares = { middlewares = {
...SharedMiddlewares ...SharedMiddlewares,
} }
contexts = { contexts = {
db: new DbManager(), db: new DbManager(),
} }
queuesManager = new TaskQueueManager(
{
workersPath: `${__dirname}/queues`,
},
this,
)
async onInitialize() { async onInitialize() {
await this.contexts.db.initialize() await this.contexts.db.initialize()
await this.queuesManager.initialize({
redisOptions: this.engine.ws.redis.options,
})
global.queues = this.queuesManager
}
onExit() {
this.queuesManager.cleanUp()
console.log("Jijija")
} }
} }

View File

@ -0,0 +1,8 @@
import { Session } from "@db_models"
import Worker from "./worker.js"
export default {
id: "notify-new-login",
maxJobs: 400,
process: Worker,
}

View File

@ -0,0 +1,30 @@
import { Session } from "@db_models"
module.exports = async (job) => {
const { authData, minDate, ignoreToken } = job.data
try {
console.log(
`Checking last login for user ${authData.user_id} from IP ${authData.ip_address}`,
)
const session = await Session.findOne({
user_id: authData.user_id,
ip_address: authData.ip_address,
created_at: { $gte: minDate },
token: { $ne: authData.token },
})
if (!session) {
console.log(
`No session found for user ${authData.user_id} from IP ${authData.ip_address}`,
)
console.log("Sending notification")
global.ipc.call("ems", "new:login", authData).catch((error) => {
// whoipsi dupsi
console.error(error)
})
}
} catch (error) {
console.error(error)
}
}

View File

@ -133,9 +133,10 @@ export default async (req, res) => {
// emit to ems to notify user for the new login, in the background // emit to ems to notify user for the new login, in the background
try { try {
global.ipc.call("ems", "new:login", authData).catch((error) => { global.queues.createJob("notify-new-login", {
// whoipsi dupsi authData,
console.error(error) minDate: new Date().getTime() - 3 * 30 * 24 * 60 * 60 * 1000,
currentToken: token,
}) })
} catch (error) { } catch (error) {
// whoipsi dupsi // whoipsi dupsi