BlueGit

timfa questward

Files

questward / server / src / friends / service.js

service.js

service.js 7.1 KiB Download Raw
1import { layersFromBag } from "../../../shared/appearance/layers.js";
2import { hairFromCharacterRow } from "../../../shared/appearance/hair.js";
3
4/**
5 * Account-wide friends: requests and friendships key on account_id.
6 * Same-account characters are always treated as friends.
7 */
8export class FriendService
9{
10 constructor (stateDb)
11 {
12 this.stateDb = stateDb;
13 }
14
15 snapshot (characterId)
16 {
17 const accountId = this.#accountId(characterId);
18 return {
19 friends: this.listFriendAccounts(accountId),
20 incoming: this.#namedAccountRequests(
21 this.stateDb.listIncomingAccountRequests(accountId),
22 "from_account_id"
23 ),
24 outgoing: this.#namedAccountRequests(
25 this.stateDb.listOutgoingAccountRequests(accountId),
26 "to_account_id"
27 )
28 };
29 }
30
31 /** Flat character list for pigeon address book (all chars on friend accounts). */
32 listFriends (characterId)
33 {
34 const accounts = this.listFriendAccounts(this.#accountId(characterId));
35 const out = [];
36 for (const account of accounts)
37 {
38 for (const character of account.characters ?? [])
39 { out.push(character); }
40 }
41 return out;
42 }
43
44 listFriendAccounts (accountId)
45 {
46 const id = accountId | 0;
47 if (!id)
48 { return []; }
49 return this.stateDb.listAccountFriendRecords(id)
50 .map((row) => this.#publicAccount(row.account_id))
51 .filter(Boolean);
52 }
53
54 areFriends (characterId, otherCharacterId)
55 {
56 return this.stateDb.areFriends(characterId, otherCharacterId);
57 }
58
59 request (fromCharacterId, toCharacterId)
60 {
61 const fromId = Number(fromCharacterId);
62 const toId = Number(toCharacterId);
63 if (fromId === toId)
64 { throw userError("You cannot friend yourself."); }
65 const from = this.#requireCharacter(fromId);
66 const to = this.#requireCharacter(toId);
67 const fromAccount = from.account_id | 0;
68 const toAccount = to.account_id | 0;
69 if (fromAccount === toAccount)
70 { throw userError("You are already friends with your own characters."); }
71 if (this.stateDb.areAccountFriends(fromAccount, toAccount))
72 { throw userError("You are already friends."); }
73 if (this.stateDb.getAccountFriendRequest(fromAccount, toAccount))
74 { throw userError("Friend request already sent."); }
75 if (this.stateDb.getAccountFriendRequest(toAccount, fromAccount))
76 {
77 this.stateDb.deleteAccountFriendRequest(toAccount, fromAccount);
78 this.stateDb.insertAccountFriendship(fromAccount, toAccount);
79 return this.snapshot(fromId);
80 }
81 this.stateDb.insertAccountFriendRequest(fromAccount, toAccount);
82 return this.snapshot(fromId);
83 }
84
85 accept (characterId, fromCharacterId)
86 {
87 const toChar = this.#requireCharacter(characterId);
88 const fromChar = this.#requireCharacter(fromCharacterId);
89 const toAccount = toChar.account_id | 0;
90 const fromAccount = fromChar.account_id | 0;
91 if (!this.stateDb.getAccountFriendRequest(fromAccount, toAccount))
92 { throw userError("There is no friend request to accept."); }
93 this.stateDb.deleteAccountFriendRequest(fromAccount, toAccount);
94 this.stateDb.deleteAccountFriendRequest(toAccount, fromAccount);
95 if (!this.stateDb.areAccountFriends(fromAccount, toAccount))
96 { this.stateDb.insertAccountFriendship(fromAccount, toAccount); }
97 return this.snapshot(toChar.character_id);
98 }
99
100 /** Accept / decline / remove by target account id (friends panel). */
101 acceptAccount (characterId, fromAccountId)
102 {
103 const toChar = this.#requireCharacter(characterId);
104 const toAccount = toChar.account_id | 0;
105 const fromAccount = fromAccountId | 0;
106 if (!this.stateDb.getAccountFriendRequest(fromAccount, toAccount))
107 { throw userError("There is no friend request to accept."); }
108 this.stateDb.deleteAccountFriendRequest(fromAccount, toAccount);
109 this.stateDb.deleteAccountFriendRequest(toAccount, fromAccount);
110 if (!this.stateDb.areAccountFriends(fromAccount, toAccount))
111 { this.stateDb.insertAccountFriendship(fromAccount, toAccount); }
112 return this.snapshot(toChar.character_id);
113 }
114
115 decline (characterId, otherCharacterId)
116 {
117 const actor = this.#requireCharacter(characterId);
118 const other = this.stateDb.getCharacter(otherCharacterId);
119 if (!other)
120 { return this.snapshot(actor.character_id); }
121 this.stateDb.deleteAccountFriendRequest(actor.account_id, other.account_id);
122 this.stateDb.deleteAccountFriendRequest(other.account_id, actor.account_id);
123 return this.snapshot(actor.character_id);
124 }
125
126 declineAccount (characterId, otherAccountId)
127 {
128 const actor = this.#requireCharacter(characterId);
129 const otherAccount = otherAccountId | 0;
130 this.stateDb.deleteAccountFriendRequest(actor.account_id, otherAccount);
131 this.stateDb.deleteAccountFriendRequest(otherAccount, actor.account_id);
132 return this.snapshot(actor.character_id);
133 }
134
135 remove (characterId, otherCharacterId)
136 {
137 const actor = this.#requireCharacter(characterId);
138 const other = this.#requireCharacter(otherCharacterId);
139 if (!this.stateDb.areAccountFriends(actor.account_id, other.account_id))
140 { throw userError("You are not friends with that player."); }
141 if ((actor.account_id | 0) === (other.account_id | 0))
142 { throw userError("You cannot unfriend your own characters."); }
143 this.stateDb.deleteAccountFriendship(actor.account_id, other.account_id);
144 return this.snapshot(actor.character_id);
145 }
146
147 removeAccount (characterId, otherAccountId)
148 {
149 const actor = this.#requireCharacter(characterId);
150 const otherAccount = otherAccountId | 0;
151 if ((actor.account_id | 0) === otherAccount)
152 { throw userError("You cannot unfriend your own characters."); }
153 if (!this.stateDb.areAccountFriends(actor.account_id, otherAccount))
154 { throw userError("You are not friends with that player."); }
155 this.stateDb.deleteAccountFriendship(actor.account_id, otherAccount);
156 return this.snapshot(actor.character_id);
157 }
158
159 findByFullName (firstName, lastName)
160 {
161 const row = this.stateDb.getCharacterByName(firstName, lastName);
162 return row ? this.#publicCharacter(row.character_id) : null;
163 }
164
165 #namedAccountRequests (rows, idField)
166 {
167 return rows.map((row) => this.#publicAccount(row[idField])).filter(Boolean);
168 }
169
170 #publicAccount (accountId)
171 {
172 const account = this.stateDb.getAccount(accountId);
173 if (!account)
174 { return null; }
175 const characters = this.stateDb.listCharacters(account.account_id)
176 .filter((row) => !row.deletion_scheduled_at)
177 .map((row) => this.#publicCharacter(row.character_id))
178 .filter(Boolean);
179 return {
180 accountId: account.account_id,
181 username: this.stateDb.accountDisplayName(account.account_id),
182 characters
183 };
184 }
185
186 #publicCharacter (characterId)
187 {
188 const row = this.stateDb.getCharacter(characterId);
189 if (!row)
190 { return null; }
191 const bag = this.stateDb.getBag(characterId);
192 return {
193 characterId: row.character_id,
194 accountId: row.account_id,
195 firstName: row.first_name,
196 lastName: row.last_name,
197 sex: row.sex,
198 layers: layersFromBag(row.sex, bag, hairFromCharacterRow(row))
199 };
200 }
201
202 #accountId (characterId)
203 {
204 return this.#requireCharacter(characterId).account_id | 0;
205 }
206
207 #requireCharacter (characterId)
208 {
209 const row = this.stateDb.getCharacter(characterId);
210 if (!row)
211 { throw userError("That player does not exist."); }
212 return row;
213 }
214}
215
216function userError (message)
217{
218 const error = new Error(message);
219 error.userFacing = true;
220 return error;
221}