mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-09 10:34:17 +00:00
use queued login notification
This commit is contained in:
parent
fa83c55264
commit
9d9155b0af
@ -1,6 +1,6 @@
|
||||
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 {
|
||||
@ -8,17 +8,34 @@ export default class API extends Server {
|
||||
static useEngine = "hyper-express"
|
||||
static routesPath = `${__dirname}/routes`
|
||||
static listen_port = process.env.HTTP_LISTEN_PORT ?? 3020
|
||||
static enableWebsockets = true
|
||||
|
||||
middlewares = {
|
||||
...SharedMiddlewares
|
||||
...SharedMiddlewares,
|
||||
}
|
||||
|
||||
contexts = {
|
||||
db: new DbManager(),
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,8 @@
|
||||
import { Session } from "@db_models"
|
||||
import Worker from "./worker.js"
|
||||
|
||||
export default {
|
||||
id: "notify-new-login",
|
||||
maxJobs: 400,
|
||||
process: Worker,
|
||||
}
|
@ -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)
|
||||
}
|
||||
}
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user