2023-05-19 15:22:48 -03:00
|
|
|
import { RequestHandler } from "express";
|
2025-01-31 14:15:36 +01:00
|
|
|
import { Inbox } from "@/src/models/inboxModel";
|
|
|
|
import {
|
2025-03-27 12:57:44 -07:00
|
|
|
createMessage,
|
2025-01-31 14:15:36 +01:00
|
|
|
createNewEventMessages,
|
|
|
|
deleteAllMessagesRead,
|
|
|
|
deleteMessageRead,
|
|
|
|
getAllMessagesSorted,
|
|
|
|
getMessage
|
|
|
|
} from "@/src/services/inboxService";
|
2025-03-27 12:57:44 -07:00
|
|
|
import { getAccountForRequest, getAccountFromSuffixedName, getSuffixedName } from "@/src/services/loginService";
|
|
|
|
import { addItems, combineInventoryChanges, getInventory } from "@/src/services/inventoryService";
|
2025-01-31 14:15:36 +01:00
|
|
|
import { logger } from "@/src/utils/logger";
|
2025-03-27 12:57:44 -07:00
|
|
|
import { ExportFlavour, ExportGear } from "warframe-public-export-plus";
|
|
|
|
import { handleStoreItemAcquisition } from "@/src/services/purchaseService";
|
2023-05-19 15:22:48 -03:00
|
|
|
|
2025-01-31 14:15:36 +01:00
|
|
|
export const inboxController: RequestHandler = async (req, res) => {
|
|
|
|
const { deleteId, lastMessage: latestClientMessageId, messageId } = req.query;
|
|
|
|
|
2025-03-27 12:57:44 -07:00
|
|
|
const account = await getAccountForRequest(req);
|
|
|
|
const accountId = account._id.toString();
|
2025-01-31 14:15:36 +01:00
|
|
|
|
|
|
|
if (deleteId) {
|
|
|
|
if (deleteId === "DeleteAllRead") {
|
|
|
|
await deleteAllMessagesRead(accountId);
|
|
|
|
res.status(200).end();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await deleteMessageRead(deleteId as string);
|
|
|
|
res.status(200).end();
|
|
|
|
} else if (messageId) {
|
|
|
|
const message = await getMessage(messageId as string);
|
|
|
|
message.r = true;
|
2025-03-27 12:57:44 -07:00
|
|
|
await message.save();
|
|
|
|
|
2025-01-31 14:15:36 +01:00
|
|
|
const attachmentItems = message.att;
|
|
|
|
const attachmentCountedItems = message.countedAtt;
|
|
|
|
|
2025-03-27 12:57:44 -07:00
|
|
|
if (!attachmentItems && !attachmentCountedItems && !message.gifts) {
|
2025-01-31 14:15:36 +01:00
|
|
|
res.status(200).end();
|
|
|
|
return;
|
|
|
|
}
|
2023-05-19 15:22:48 -03:00
|
|
|
|
2025-01-31 14:15:36 +01:00
|
|
|
const inventory = await getInventory(accountId);
|
|
|
|
const inventoryChanges = {};
|
|
|
|
if (attachmentItems) {
|
|
|
|
await addItems(
|
|
|
|
inventory,
|
2025-02-18 05:39:45 -08:00
|
|
|
attachmentItems.map(attItem => ({
|
|
|
|
ItemType: attItem,
|
|
|
|
ItemCount: attItem in ExportGear ? (ExportGear[attItem].purchaseQuantity ?? 1) : 1
|
|
|
|
})),
|
2025-01-31 14:15:36 +01:00
|
|
|
inventoryChanges
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (attachmentCountedItems) {
|
|
|
|
await addItems(inventory, attachmentCountedItems, inventoryChanges);
|
|
|
|
}
|
2025-03-27 12:57:44 -07:00
|
|
|
if (message.gifts) {
|
|
|
|
const sender = await getAccountFromSuffixedName(message.sndr);
|
|
|
|
const recipientName = getSuffixedName(account);
|
|
|
|
const giftQuantity = message.arg!.find(x => x.Key == "GIFT_QUANTITY")!.Tag as number;
|
|
|
|
for (const gift of message.gifts) {
|
|
|
|
combineInventoryChanges(
|
|
|
|
inventoryChanges,
|
|
|
|
(await handleStoreItemAcquisition(gift.GiftType, inventory, giftQuantity)).InventoryChanges
|
|
|
|
);
|
|
|
|
if (sender) {
|
|
|
|
await createMessage(sender._id.toString(), [
|
|
|
|
{
|
|
|
|
sndr: recipientName,
|
|
|
|
msg: "/Lotus/Language/Menu/GiftReceivedConfirmationBody",
|
|
|
|
arg: [
|
|
|
|
{
|
|
|
|
Key: "RECIPIENT_NAME",
|
|
|
|
Tag: recipientName
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "GIFT_TYPE",
|
|
|
|
Tag: gift.GiftType
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "GIFT_QUANTITY",
|
|
|
|
Tag: giftQuantity
|
|
|
|
}
|
|
|
|
],
|
|
|
|
sub: "/Lotus/Language/Menu/GiftReceivedConfirmationSubject",
|
|
|
|
icon: ExportFlavour[inventory.ActiveAvatarImageType].icon,
|
|
|
|
highPriority: true
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2025-01-31 14:15:36 +01:00
|
|
|
await inventory.save();
|
|
|
|
res.json({ InventoryChanges: inventoryChanges });
|
|
|
|
} else if (latestClientMessageId) {
|
|
|
|
await createNewEventMessages(req);
|
|
|
|
const messages = await Inbox.find({ ownerId: accountId }).sort({ date: 1 });
|
|
|
|
|
|
|
|
const latestClientMessage = messages.find(m => m._id.toString() === latestClientMessageId);
|
|
|
|
|
|
|
|
if (!latestClientMessage) {
|
|
|
|
logger.debug(`this should only happen after DeleteAllRead `);
|
|
|
|
res.json({ Inbox: messages });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const newMessages = messages.filter(m => m.date > latestClientMessage.date);
|
|
|
|
|
|
|
|
if (newMessages.length === 0) {
|
|
|
|
res.send("no-new");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
res.json({ Inbox: newMessages });
|
|
|
|
} else {
|
|
|
|
//newly created event messages must be newer than account.LatestEventMessageDate
|
|
|
|
await createNewEventMessages(req);
|
|
|
|
const messages = await getAllMessagesSorted(accountId);
|
|
|
|
const inbox = messages.map(m => m.toJSON());
|
|
|
|
res.json({ Inbox: inbox });
|
|
|
|
}
|
|
|
|
};
|