2025-03-10 16:40:40 -07:00
import { Guild , GuildMember } from "@/src/models/guildModel" ;
import { Account } from "@/src/models/loginModel" ;
2025-03-14 07:09:28 -07:00
import { fillInInventoryDataForGuildMember , hasGuildPermission } from "@/src/services/guildService" ;
2025-03-10 16:40:40 -07:00
import { createMessage } from "@/src/services/inboxService" ;
import { getInventory } from "@/src/services/inventoryService" ;
2025-04-01 02:28:24 -07:00
import { getAccountForRequest , getAccountIdForRequest , getSuffixedName } from "@/src/services/loginService" ;
2025-03-10 16:40:40 -07:00
import { IOid } from "@/src/types/commonTypes" ;
2025-03-14 07:09:28 -07:00
import { GuildPermission , IGuildMemberClient } from "@/src/types/guildTypes" ;
2025-04-01 02:28:24 -07:00
import { logger } from "@/src/utils/logger" ;
2025-03-10 16:40:40 -07:00
import { RequestHandler } from "express" ;
import { ExportFlavour } from "warframe-public-export-plus" ;
export const addToGuildController : RequestHandler = async ( req , res ) = > {
const payload = JSON . parse ( String ( req . body ) ) as IAddToGuildRequest ;
2025-04-01 02:28:24 -07:00
if ( "UserName" in payload ) {
// Clan recruiter sending an invite
2025-03-10 16:40:40 -07:00
2025-04-01 02:28:24 -07:00
const account = await Account . findOne ( { DisplayName : payload.UserName } ) ;
if ( ! account ) {
res . status ( 400 ) . json ( "Username does not exist" ) ;
return ;
}
2025-03-27 12:57:57 -07:00
2025-04-01 02:28:24 -07:00
const inventory = await getInventory ( account . _id . toString ( ) , "Settings" ) ;
// TODO: Also consider GIFT_MODE_FRIENDS once friends are implemented
if ( inventory . Settings ? . GuildInvRestriction == "GIFT_MODE_NONE" ) {
res . status ( 400 ) . json ( "Invite restricted" ) ;
return ;
}
const guild = ( await Guild . findById ( payload . GuildId . $oid , "Name Ranks" ) ) ! ;
const senderAccount = await getAccountForRequest ( req ) ;
if ( ! ( await hasGuildPermission ( guild , senderAccount . _id . toString ( ) , GuildPermission . Recruiter ) ) ) {
res . status ( 400 ) . json ( "Invalid permission" ) ;
}
2025-04-03 06:17:38 -07:00
try {
await GuildMember . insertOne ( {
2025-04-01 02:28:24 -07:00
accountId : account._id ,
2025-04-03 06:17:38 -07:00
guildId : payload.GuildId.$oid ,
status : 2 // outgoing invite
} ) ;
} catch ( e ) {
logger . debug ( ` guild invite failed due to ${ String ( e ) } ` ) ;
2025-04-01 02:28:24 -07:00
res . status ( 400 ) . json ( "User already invited to clan" ) ;
return ;
}
2025-03-10 16:40:40 -07:00
2025-04-01 02:28:24 -07:00
const senderInventory = await getInventory ( senderAccount . _id . toString ( ) , "ActiveAvatarImageType" ) ;
await createMessage ( account . _id , [
{
sndr : getSuffixedName ( senderAccount ) ,
msg : "/Lotus/Language/Menu/Mailbox_ClanInvite_Body" ,
arg : [
{
Key : "clan" ,
Tag : guild.Name
}
] ,
sub : "/Lotus/Language/Menu/Mailbox_ClanInvite_Title" ,
icon : ExportFlavour [ senderInventory . ActiveAvatarImageType ] . icon ,
contextInfo : payload.GuildId.$oid ,
highPriority : true ,
acceptAction : "GUILD_INVITE" ,
declineAction : "GUILD_INVITE" ,
hasAccountAction : true
}
] ) ;
2025-03-10 16:40:40 -07:00
2025-04-01 02:28:24 -07:00
const member : IGuildMemberClient = {
_id : { $oid : account._id.toString ( ) } ,
DisplayName : account.DisplayName ,
Rank : 7 ,
Status : 2
} ;
await fillInInventoryDataForGuildMember ( member ) ;
res . json ( { NewMember : member } ) ;
} else if ( "RequestMsg" in payload ) {
// Player applying to join a clan
const accountId = await getAccountIdForRequest ( req ) ;
try {
await GuildMember . insertOne ( {
accountId ,
guildId : payload.GuildId.$oid ,
status : 1 , // incoming invite
RequestMsg : payload.RequestMsg ,
RequestExpiry : new Date ( Date . now ( ) + 14 * 86400 * 1000 ) // TOVERIFY: I can't find any good information about this with regards to live, but 2 weeks seem reasonable.
} ) ;
} catch ( e ) {
2025-04-03 06:17:38 -07:00
logger . debug ( ` alliance invite failed due to ${ String ( e ) } ` ) ;
2025-04-01 02:28:24 -07:00
res . status ( 400 ) . send ( "Already requested" ) ;
2025-03-10 16:40:40 -07:00
}
2025-04-01 02:28:24 -07:00
res . end ( ) ;
} else {
logger . error ( ` data provided to ${ req . path } : ${ String ( req . body ) } ` ) ;
res . status ( 400 ) . end ( ) ;
}
2025-03-10 16:40:40 -07:00
} ;
interface IAddToGuildRequest {
2025-04-01 02:28:24 -07:00
UserName? : string ;
2025-03-10 16:40:40 -07:00
GuildId : IOid ;
2025-04-01 02:28:24 -07:00
RequestMsg? : string ;
2025-03-10 16:40:40 -07:00
}