2025-01-31 14:15:36 +01:00
|
|
|
import { IMessageDatabase, Inbox } from "@/src/models/inboxModel";
|
|
|
|
import { getAccountForRequest } from "@/src/services/loginService";
|
2025-03-31 09:18:00 -07:00
|
|
|
import { HydratedDocument, Types } from "mongoose";
|
2025-01-31 14:15:36 +01:00
|
|
|
import { Request } from "express";
|
2025-06-24 11:19:32 -07:00
|
|
|
import { unixTimesInMs } from "../constants/timeConstants";
|
|
|
|
import { config } from "./configService";
|
2025-01-31 14:15:36 +01:00
|
|
|
|
|
|
|
export const getAllMessagesSorted = async (accountId: string): Promise<HydratedDocument<IMessageDatabase>[]> => {
|
|
|
|
const inbox = await Inbox.find({ ownerId: accountId }).sort({ date: -1 });
|
|
|
|
return inbox;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getMessage = async (messageId: string): Promise<HydratedDocument<IMessageDatabase>> => {
|
2025-03-24 11:32:08 -07:00
|
|
|
const message = await Inbox.findById(messageId);
|
2025-01-31 14:15:36 +01:00
|
|
|
|
|
|
|
if (!message) {
|
|
|
|
throw new Error(`Message not found ${messageId}`);
|
|
|
|
}
|
|
|
|
return message;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const deleteMessageRead = async (messageId: string): Promise<void> => {
|
|
|
|
await Inbox.findOneAndDelete({ _id: messageId, r: true });
|
|
|
|
};
|
|
|
|
|
|
|
|
export const deleteAllMessagesRead = async (accountId: string): Promise<void> => {
|
|
|
|
await Inbox.deleteMany({ ownerId: accountId, r: true });
|
|
|
|
};
|
|
|
|
|
2025-02-24 20:56:34 -08:00
|
|
|
export const createNewEventMessages = async (req: Request): Promise<void> => {
|
2025-01-31 14:15:36 +01:00
|
|
|
const account = await getAccountForRequest(req);
|
2025-06-24 11:19:32 -07:00
|
|
|
const newEventMessages: IMessageCreationTemplate[] = [];
|
2025-01-31 14:15:36 +01:00
|
|
|
|
2025-06-24 11:19:32 -07:00
|
|
|
// Baro
|
|
|
|
const baroIndex = Math.trunc((Date.now() - 910800000) / (unixTimesInMs.day * 14));
|
|
|
|
const baroStart = baroIndex * (unixTimesInMs.day * 14) + 910800000;
|
|
|
|
const baroActualStart = baroStart + unixTimesInMs.day * (config.baroAlwaysAvailable ? 0 : 12);
|
|
|
|
if (account.LatestEventMessageDate.getTime() < baroActualStart) {
|
|
|
|
newEventMessages.push({
|
|
|
|
sndr: "/Lotus/Language/G1Quests/VoidTraderName",
|
|
|
|
sub: "/Lotus/Language/CommunityMessages/VoidTraderAppearanceTitle",
|
|
|
|
msg: "/Lotus/Language/CommunityMessages/VoidTraderAppearanceMessage",
|
|
|
|
icon: "/Lotus/Interface/Icons/Npcs/BaroKiTeerPortrait.png",
|
|
|
|
startDate: new Date(baroActualStart),
|
|
|
|
endDate: new Date(baroStart + unixTimesInMs.day * 14),
|
|
|
|
CrossPlatform: true,
|
|
|
|
arg: [
|
|
|
|
{
|
|
|
|
Key: "NODE_NAME",
|
|
|
|
Tag: ["EarthHUB", "MercuryHUB", "SaturnHUB", "PlutoHUB"][baroIndex % 4]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
date: new Date(baroActualStart)
|
|
|
|
});
|
|
|
|
}
|
2025-01-31 14:15:36 +01:00
|
|
|
|
|
|
|
if (newEventMessages.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2025-06-24 11:19:32 -07:00
|
|
|
await createMessage(account._id, newEventMessages);
|
2025-01-31 14:15:36 +01:00
|
|
|
|
|
|
|
const latestEventMessage = newEventMessages.reduce((prev, current) =>
|
2025-06-24 11:19:32 -07:00
|
|
|
prev.startDate! > current.startDate! ? prev : current
|
2025-01-31 14:15:36 +01:00
|
|
|
);
|
2025-06-24 11:19:32 -07:00
|
|
|
account.LatestEventMessageDate = new Date(latestEventMessage.startDate!);
|
2025-01-31 14:15:36 +01:00
|
|
|
await account.save();
|
|
|
|
};
|
|
|
|
|
2025-06-18 05:50:43 -07:00
|
|
|
export const createMessage = async (
|
|
|
|
accountId: string | Types.ObjectId,
|
|
|
|
messages: IMessageCreationTemplate[]
|
2025-06-24 11:19:32 -07:00
|
|
|
): Promise<void> => {
|
2025-01-31 14:15:36 +01:00
|
|
|
const ownerIdMessages = messages.map(m => ({
|
|
|
|
...m,
|
2025-06-24 11:19:32 -07:00
|
|
|
date: m.date ?? new Date(),
|
2025-01-31 14:15:36 +01:00
|
|
|
ownerId: accountId
|
|
|
|
}));
|
2025-06-24 11:19:32 -07:00
|
|
|
await Inbox.insertMany(ownerIdMessages);
|
2025-01-31 14:15:36 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
export interface IMessageCreationTemplate extends Omit<IMessageDatabase, "_id" | "date" | "ownerId"> {
|
2025-06-24 11:19:32 -07:00
|
|
|
date?: Date;
|
2025-01-31 14:15:36 +01:00
|
|
|
}
|