| 1 | export class FriendService |
| 2 | { |
| 3 | constructor (stateDb) |
| 4 | { |
| 5 | this.stateDb = stateDb; |
| 6 | } |
| 7 | |
| 8 | snapshot (characterId) |
| 9 | { |
| 10 | return { |
| 11 | friends: this.listFriends(characterId), |
| 12 | incoming: this.#namedRequests(this.stateDb.listIncomingRequests(characterId), "from_character_id"), |
| 13 | outgoing: this.#namedRequests(this.stateDb.listOutgoingRequests(characterId), "to_character_id") |
| 14 | }; |
| 15 | } |
| 16 | |
| 17 | listFriends (characterId) |
| 18 | { |
| 19 | return this.stateDb.listFriendRecords(characterId).map((row) => this.#publicCharacter(row.character_id)).filter(Boolean); |
| 20 | } |
| 21 | |
| 22 | request (fromCharacterId, toCharacterId) |
| 23 | { |
| 24 | const fromId = Number(fromCharacterId); |
| 25 | const toId = Number(toCharacterId); |
| 26 | if (fromId === toId) |
| 27 | { throw userError("You cannot friend yourself."); } |
| 28 | this.#requireCharacter(fromId); |
| 29 | this.#requireCharacter(toId); |
| 30 | if (this.stateDb.areFriends(fromId, toId)) |
| 31 | { throw userError("You are already friends."); } |
| 32 | if (this.stateDb.getFriendRequest(fromId, toId)) |
| 33 | { throw userError("Friend request already sent."); } |
| 34 | if (this.stateDb.getFriendRequest(toId, fromId)) |
| 35 | { |
| 36 | this.stateDb.deleteFriendRequest(toId, fromId); |
| 37 | this.stateDb.insertFriendship(fromId, toId); |
| 38 | return this.snapshot(fromId); |
| 39 | } |
| 40 | this.stateDb.insertFriendRequest(fromId, toId); |
| 41 | return this.snapshot(fromId); |
| 42 | } |
| 43 | |
| 44 | accept (characterId, fromCharacterId) |
| 45 | { |
| 46 | const toId = Number(characterId); |
| 47 | const fromId = Number(fromCharacterId); |
| 48 | this.#requireCharacter(toId); |
| 49 | this.#requireCharacter(fromId); |
| 50 | if (!this.stateDb.getFriendRequest(fromId, toId)) |
| 51 | { throw userError("There is no friend request to accept."); } |
| 52 | this.stateDb.deleteFriendRequest(fromId, toId); |
| 53 | this.stateDb.deleteFriendRequest(toId, fromId); |
| 54 | if (!this.stateDb.areFriends(fromId, toId)) |
| 55 | { this.stateDb.insertFriendship(fromId, toId); } |
| 56 | return this.snapshot(toId); |
| 57 | } |
| 58 | |
| 59 | decline (characterId, otherCharacterId) |
| 60 | { |
| 61 | const actorId = Number(characterId); |
| 62 | const otherId = Number(otherCharacterId); |
| 63 | this.stateDb.deleteFriendRequest(actorId, otherId); |
| 64 | this.stateDb.deleteFriendRequest(otherId, actorId); |
| 65 | return this.snapshot(actorId); |
| 66 | } |
| 67 | |
| 68 | remove (characterId, otherCharacterId) |
| 69 | { |
| 70 | const actorId = Number(characterId); |
| 71 | const otherId = Number(otherCharacterId); |
| 72 | if (!this.stateDb.areFriends(actorId, otherId)) |
| 73 | { throw userError("You are not friends with that player."); } |
| 74 | this.stateDb.deleteFriendship(actorId, otherId); |
| 75 | return this.snapshot(actorId); |
| 76 | } |
| 77 | |
| 78 | findByFullName (firstName, lastName) |
| 79 | { |
| 80 | const row = this.stateDb.getCharacterByName(firstName, lastName); |
| 81 | return row ? this.#publicCharacter(row.character_id) : null; |
| 82 | } |
| 83 | |
| 84 | #namedRequests (rows, idField) |
| 85 | { |
| 86 | return rows.map((row) => this.#publicCharacter(row[idField])).filter(Boolean); |
| 87 | } |
| 88 | |
| 89 | #publicCharacter (characterId) |
| 90 | { |
| 91 | const row = this.stateDb.getCharacter(characterId); |
| 92 | if (!row) |
| 93 | { return null; } |
| 94 | return { |
| 95 | characterId: row.character_id, |
| 96 | firstName: row.first_name, |
| 97 | lastName: row.last_name |
| 98 | }; |
| 99 | } |
| 100 | |
| 101 | #requireCharacter (characterId) |
| 102 | { |
| 103 | if (!this.stateDb.getCharacter(characterId)) |
| 104 | { throw userError("That player does not exist."); } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | function userError (message) |
| 109 | { |
| 110 | const error = new Error(message); |
| 111 | error.userFacing = true; |
| 112 | return error; |
| 113 | } |