import { layersFromBag } from "../../../shared/appearance/layers.js"; import { hairFromCharacterRow } from "../../../shared/appearance/hair.js"; /** * Account-wide friends: requests and friendships key on account_id. * Same-account characters are always treated as friends. */ export class FriendService { constructor (stateDb) { this.stateDb = stateDb; } snapshot (characterId) { const accountId = this.#accountId(characterId); return { friends: this.listFriendAccounts(accountId), incoming: this.#namedAccountRequests( this.stateDb.listIncomingAccountRequests(accountId), "from_account_id" ), outgoing: this.#namedAccountRequests( this.stateDb.listOutgoingAccountRequests(accountId), "to_account_id" ) }; } /** Flat character list for pigeon address book (all chars on friend accounts). */ listFriends (characterId) { const accounts = this.listFriendAccounts(this.#accountId(characterId)); const out = []; for (const account of accounts) { for (const character of account.characters ?? []) { out.push(character); } } return out; } listFriendAccounts (accountId) { const id = accountId | 0; if (!id) { return []; } return this.stateDb.listAccountFriendRecords(id) .map((row) => this.#publicAccount(row.account_id)) .filter(Boolean); } areFriends (characterId, otherCharacterId) { return this.stateDb.areFriends(characterId, otherCharacterId); } request (fromCharacterId, toCharacterId) { const fromId = Number(fromCharacterId); const toId = Number(toCharacterId); if (fromId === toId) { throw userError("You cannot friend yourself."); } const from = this.#requireCharacter(fromId); const to = this.#requireCharacter(toId); const fromAccount = from.account_id | 0; const toAccount = to.account_id | 0; if (fromAccount === toAccount) { throw userError("You are already friends with your own characters."); } if (this.stateDb.areAccountFriends(fromAccount, toAccount)) { throw userError("You are already friends."); } if (this.stateDb.getAccountFriendRequest(fromAccount, toAccount)) { throw userError("Friend request already sent."); } if (this.stateDb.getAccountFriendRequest(toAccount, fromAccount)) { this.stateDb.deleteAccountFriendRequest(toAccount, fromAccount); this.stateDb.insertAccountFriendship(fromAccount, toAccount); return this.snapshot(fromId); } this.stateDb.insertAccountFriendRequest(fromAccount, toAccount); return this.snapshot(fromId); } accept (characterId, fromCharacterId) { const toChar = this.#requireCharacter(characterId); const fromChar = this.#requireCharacter(fromCharacterId); const toAccount = toChar.account_id | 0; const fromAccount = fromChar.account_id | 0; if (!this.stateDb.getAccountFriendRequest(fromAccount, toAccount)) { throw userError("There is no friend request to accept."); } this.stateDb.deleteAccountFriendRequest(fromAccount, toAccount); this.stateDb.deleteAccountFriendRequest(toAccount, fromAccount); if (!this.stateDb.areAccountFriends(fromAccount, toAccount)) { this.stateDb.insertAccountFriendship(fromAccount, toAccount); } return this.snapshot(toChar.character_id); } /** Accept / decline / remove by target account id (friends panel). */ acceptAccount (characterId, fromAccountId) { const toChar = this.#requireCharacter(characterId); const toAccount = toChar.account_id | 0; const fromAccount = fromAccountId | 0; if (!this.stateDb.getAccountFriendRequest(fromAccount, toAccount)) { throw userError("There is no friend request to accept."); } this.stateDb.deleteAccountFriendRequest(fromAccount, toAccount); this.stateDb.deleteAccountFriendRequest(toAccount, fromAccount); if (!this.stateDb.areAccountFriends(fromAccount, toAccount)) { this.stateDb.insertAccountFriendship(fromAccount, toAccount); } return this.snapshot(toChar.character_id); } decline (characterId, otherCharacterId) { const actor = this.#requireCharacter(characterId); const other = this.stateDb.getCharacter(otherCharacterId); if (!other) { return this.snapshot(actor.character_id); } this.stateDb.deleteAccountFriendRequest(actor.account_id, other.account_id); this.stateDb.deleteAccountFriendRequest(other.account_id, actor.account_id); return this.snapshot(actor.character_id); } declineAccount (characterId, otherAccountId) { const actor = this.#requireCharacter(characterId); const otherAccount = otherAccountId | 0; this.stateDb.deleteAccountFriendRequest(actor.account_id, otherAccount); this.stateDb.deleteAccountFriendRequest(otherAccount, actor.account_id); return this.snapshot(actor.character_id); } remove (characterId, otherCharacterId) { const actor = this.#requireCharacter(characterId); const other = this.#requireCharacter(otherCharacterId); if (!this.stateDb.areAccountFriends(actor.account_id, other.account_id)) { throw userError("You are not friends with that player."); } if ((actor.account_id | 0) === (other.account_id | 0)) { throw userError("You cannot unfriend your own characters."); } this.stateDb.deleteAccountFriendship(actor.account_id, other.account_id); return this.snapshot(actor.character_id); } removeAccount (characterId, otherAccountId) { const actor = this.#requireCharacter(characterId); const otherAccount = otherAccountId | 0; if ((actor.account_id | 0) === otherAccount) { throw userError("You cannot unfriend your own characters."); } if (!this.stateDb.areAccountFriends(actor.account_id, otherAccount)) { throw userError("You are not friends with that player."); } this.stateDb.deleteAccountFriendship(actor.account_id, otherAccount); return this.snapshot(actor.character_id); } findByFullName (firstName, lastName) { const row = this.stateDb.getCharacterByName(firstName, lastName); return row ? this.#publicCharacter(row.character_id) : null; } #namedAccountRequests (rows, idField) { return rows.map((row) => this.#publicAccount(row[idField])).filter(Boolean); } #publicAccount (accountId) { const account = this.stateDb.getAccount(accountId); if (!account) { return null; } const characters = this.stateDb.listCharacters(account.account_id) .filter((row) => !row.deletion_scheduled_at) .map((row) => this.#publicCharacter(row.character_id)) .filter(Boolean); return { accountId: account.account_id, username: this.stateDb.accountDisplayName(account.account_id), characters }; } #publicCharacter (characterId) { const row = this.stateDb.getCharacter(characterId); if (!row) { return null; } const bag = this.stateDb.getBag(characterId); return { characterId: row.character_id, accountId: row.account_id, firstName: row.first_name, lastName: row.last_name, sex: row.sex, layers: layersFromBag(row.sex, bag, hairFromCharacterRow(row)) }; } #accountId (characterId) { return this.#requireCharacter(characterId).account_id | 0; } #requireCharacter (characterId) { const row = this.stateDb.getCharacter(characterId); if (!row) { throw userError("That player does not exist."); } return row; } } function userError (message) { const error = new Error(message); error.userFacing = true; return error; }