@@ -26,6 +26,7 @@ import { getAvatarElement } from "../utils/discord-avatar";
2626import { formatTypingStatsRatio } from "../utils/misc" ;
2727import { getLanguageDisplayString } from "../utils/strings" ;
2828import * as DB from "../db" ;
29+ import { addFriend , getReceiverUid } from "../db" ;
2930import { getAuthenticatedUser } from "../firebase" ;
3031import * as ServerConfiguration from "../ape/server-configuration" ;
3132import { authEvent } from "../events/auth" ;
@@ -42,35 +43,6 @@ let friendsTable: SortedTable<Friend> | undefined = undefined;
4243let pendingRequests : Connection [ ] | undefined ;
4344let friendsList : Friend [ ] | undefined ;
4445
45- export function getReceiverUid (
46- connection : Pick < Connection , "initiatorUid" | "receiverUid" > ,
47- ) : string {
48- const me = getAuthenticatedUser ( ) ;
49- if ( me === null ) {
50- throw new Error ( "expected to be authenticated in getReceiverUid" ) ;
51- }
52-
53- if ( me . uid === connection . initiatorUid ) return connection . receiverUid ;
54- return connection . initiatorUid ;
55- }
56-
57- export async function addFriend ( receiverName : string ) : Promise < true | string > {
58- const result = await Ape . connections . create ( { body : { receiverName } } ) ;
59-
60- if ( result . status !== 200 ) {
61- return `Friend request failed: ${ result . body . message } ` ;
62- } else {
63- const snapshot = DB . getSnapshot ( ) ;
64- if ( snapshot !== undefined ) {
65- const receiverUid = getReceiverUid ( result . body . data ) ;
66- // oxlint-disable-next-line no-unsafe-member-access
67- snapshot . connections [ receiverUid ] = result . body . data . status ;
68- updatePendingConnections ( ) ;
69- }
70- return true ;
71- }
72- }
73-
7446const addFriendModal = new SimpleModal ( {
7547 id : "addFriend" ,
7648 title : "Add a friend" ,
@@ -94,6 +66,7 @@ const addFriendModal = new SimpleModal({
9466 const result = await addFriend ( receiverName ) ;
9567
9668 if ( result === true ) {
69+ updatePendingConnections ( ) ;
9770 return { status : "success" , message : `Request sent to ${ receiverName } ` } ;
9871 }
9972
0 commit comments