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,25 +1,42 @@
import { Server } from "linebridge"
import DbManager from "@shared-classes/DbManager"
import TaskQueueManager from "@shared-classes/TaskQueueManager"
import SharedMiddlewares from "@shared-middlewares"
export default class API extends Server {
static refName = "auth"
static useEngine = "hyper-express"
static routesPath = `${__dirname}/routes`
static listen_port = process.env.HTTP_LISTEN_PORT ?? 3020
static refName = "auth"
static useEngine = "hyper-express"
static routesPath = `${__dirname}/routes`
static listen_port = process.env.HTTP_LISTEN_PORT ?? 3020
static enableWebsockets = true
middlewares = {
...SharedMiddlewares
}
middlewares = {
...SharedMiddlewares,
}
contexts = {
db: new DbManager(),
}
contexts = {
db: new DbManager(),
}
async onInitialize() {
await this.contexts.db.initialize()
}
queuesManager = new TaskQueueManager(
{
workersPath: `${__dirname}/queues`,
},
this,
)
async onInitialize() {
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")
}
}
Boot(API)
Boot(API)

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
try {
global.ipc.call("ems", "new:login", authData).catch((error) => {
// whoipsi dupsi
console.error(error)
global.queues.createJob("notify-new-login", {
authData,
minDate: new Date().getTime() - 3 * 30 * 24 * 60 * 60 * 1000,
currentToken: token,
})
} catch (error) {
// whoipsi dupsi