File size: 8,138 Bytes
32f4c77
 
 
 
 
 
d2dda46
32f4c77
 
813ef48
32f4c77
 
9b2d0c7
813ef48
32f4c77
5eff0b7
32f4c77
f25edb3
32f4c77
 
 
 
 
 
 
 
 
 
 
bdc8b77
32f4c77
 
 
 
 
bdc8b77
32f4c77
 
 
 
 
 
 
 
bdc8b77
32f4c77
 
 
 
 
 
 
 
 
bdc8b77
32f4c77
 
 
 
 
 
 
 
 
 
 
 
 
bdc8b77
32f4c77
 
 
 
 
bdc8b77
32f4c77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bdc8b77
32f4c77
 
 
813ef48
 
 
 
 
 
 
 
 
 
bdc8b77
813ef48
 
 
 
 
bdc8b77
813ef48
 
bdc8b77
813ef48
 
 
 
 
 
 
 
bdc8b77
813ef48
 
 
d2dda46
32f4c77
 
 
 
 
 
 
 
 
bdc8b77
32f4c77
 
 
 
 
bdc8b77
32f4c77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bdc8b77
32f4c77
 
 
 
 
 
 
 
 
 
 
 
 
bdc8b77
32f4c77
 
 
 
 
bdc8b77
32f4c77
 
 
 
 
 
 
 
bdc8b77
32f4c77
 
 
 
 
 
 
 
 
 
 
 
bdc8b77
32f4c77
 
 
bdc8b77
32f4c77
bdc8b77
32f4c77
 
 
 
 
 
bdc8b77
32f4c77
 
 
813ef48
 
 
 
 
 
 
 
 
bdc8b77
813ef48
 
 
bdc8b77
813ef48
bdc8b77
813ef48
 
 
 
 
 
bdc8b77
813ef48
 
 
32f4c77
bdc8b77
32f4c77
 
 
9b2d0c7
 
 
 
 
32f4c77
bdc8b77
32f4c77
 
5eff0b7
813ef48
bdc8b77
813ef48
 
 
5eff0b7
bdc8b77
5eff0b7
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import { Request, Response } from "express";
import { auth } from "../../lib/auth";
import { toFetchHeaders } from "../../lib/utils/to-fetch-headers";
import {
  sendFriendRequest,
  acceptFriendRequest,
  declineFriendRequest,
  removeFriend,
  blockUser,
  unblockUser,
  getFriends,
  getFriendRequests,
  getFriendshipCounts,
  getBlockedUsers,
  getFriendship,
  searchGlobalUsers,
} from "../repository/friend-repository";
import { TargetUserIdSchema } from "../validation/friends-schema";

export async function requestFriend(req: Request, res: Response) {
  const result = TargetUserIdSchema.safeParse(req.body);
  if (!result.success) {
    return res
      .status(400)
      .json({ error: "userId is required and must be a string" });
  }

  const targetUserId = result.data.userId;

  if (req.user.id === targetUserId) {
    return res
      .status(400)
      .json({ error: "Cannot send a friend request to yourself" });
  }

  const existingFriendships = await getFriendship(req.user.id, targetUserId);
  for (const record of existingFriendships) {
    if (record.status === "BLOCKED") {
      return res.status(400).json({ error: "Action not allowed" });
    }
    if (record.status === "ACCEPTED") {
      return res.status(400).json({ error: "Already friends" });
    }
    if (record.status === "PENDING") {
      if (record.userId === req.user.id) {
        return res.status(400).json({ error: "Friend request already sent" });
      } else {
        return res
          .status(400)
          .json({ error: "User already sent you a friend request" });
      }
    }
  }

  await sendFriendRequest(req.user.id, targetUserId);
  return res.json({ success: true });
}

export async function acceptRequest(req: Request, res: Response) {
  const result = TargetUserIdSchema.safeParse(req.body);
  if (!result.success) {
    return res
      .status(400)
      .json({ error: "userId is required and must be a string" });
  }

  const targetUserId = result.data.userId;

  if (req.user.id === targetUserId) {
    return res
      .status(400)
      .json({ error: "Cannot accept a request from yourself" });
  }

  const existingFriendships = await getFriendship(req.user.id, targetUserId);

  for (const record of existingFriendships) {
    if (record.status === "BLOCKED") {
      return res.status(400).json({ error: "Action not allowed" });
    }
    if (record.status === "ACCEPTED") {
      return res.status(400).json({ error: "Already friends" });
    }
  }

  const incomingRequest = existingFriendships.find(
    (f) => f.userId === targetUserId && f.status === "PENDING",
  );

  if (!incomingRequest) {
    return res
      .status(400)
      .json({ error: "No pending friend request from this user" });
  }

  await acceptFriendRequest(req.user.id, targetUserId);
  return res.json({ success: true });
}

export async function cancelRequest(req: Request, res: Response) {
  const result = TargetUserIdSchema.safeParse(req.body);
  if (!result.success) {
    return res
      .status(400)
      .json({ error: "userId is required and must be a string" });
  }

  const targetUserId = result.data.userId;

  if (req.user.id === targetUserId) {
    return res
      .status(400)
      .json({ error: "Cannot cancel a request to yourself" });
  }

  const existingFriendships = await getFriendship(req.user.id, targetUserId);

  const outgoingRequest = existingFriendships.find(
    (f) => f.userId === req.user.id && f.status === "PENDING",
  );

  if (!outgoingRequest) {
    return res
      .status(400)
      .json({ error: "No pending friend request to cancel" });
  }

  await removeFriend(req.user.id, targetUserId);
  return res.json({ success: true });
}

export async function declineRequest(req: Request, res: Response) {
  const result = TargetUserIdSchema.safeParse(req.body);
  if (!result.success) {
    return res
      .status(400)
      .json({ error: "userId is required and must be a string" });
  }

  const targetUserId = result.data.userId;

  if (req.user.id === targetUserId) {
    return res
      .status(400)
      .json({ error: "Cannot reject a request from yourself" });
  }

  const existingFriendships = await getFriendship(req.user.id, targetUserId);

  for (const record of existingFriendships) {
    if (record.status === "BLOCKED") {
      return res.status(400).json({ error: "Action not allowed" });
    }
    if (record.status === "ACCEPTED") {
      return res.status(400).json({ error: "Already friends" });
    }
  }

  const incomingRequest = existingFriendships.find(
    (f) => f.userId === targetUserId && f.status === "PENDING",
  );

  if (!incomingRequest) {
    return res
      .status(400)
      .json({ error: "No pending friend request from this user" });
  }

  await declineFriendRequest(req.user.id, targetUserId);
  return res.json({ success: true });
}

export async function remove(req: Request, res: Response) {
  const result = TargetUserIdSchema.safeParse(req.body);
  if (!result.success) {
    return res
      .status(400)
      .json({ error: "userId is required and must be a string" });
  }

  const targetUserId = result.data.userId;

  if (req.user.id === targetUserId) {
    return res
      .status(400)
      .json({ error: "Cannot remove yourself as a friend" });
  }

  const existingFriendships = await getFriendship(req.user.id, targetUserId);
  const isFriend = existingFriendships.some((f) => f.status === "ACCEPTED");

  if (!isFriend) {
    return res
      .status(400)
      .json({ error: "You are not friends with this user" });
  }

  await removeFriend(req.user.id, targetUserId);
  return res.json({ success: true });
}

export async function block(req: Request, res: Response) {
  const result = TargetUserIdSchema.safeParse(req.body);
  if (!result.success) {
    return res
      .status(400)
      .json({ error: "userId is required and must be a string" });
  }

  const targetUserId = result.data.userId;
  if (req.user.id === targetUserId) {
    return res.status(400).json({ error: "Cannot block yourself" });
  }

  const existingFriendships = await getFriendship(req.user.id, targetUserId);
  const alreadyBlocked = existingFriendships.some(
    (f) => f.userId === req.user.id && f.status === "BLOCKED",
  );

  if (alreadyBlocked) {
    return res.status(400).json({ error: "User is already blocked" });
  }

  await blockUser(req.user.id, targetUserId);
  return res.json({ success: true });
}

export async function unblock(req: Request, res: Response) {
  const result = TargetUserIdSchema.safeParse(req.body);
  if (!result.success) {
    return res
      .status(400)
      .json({ error: "userId is required and must be a string" });
  }

  const targetUserId = result.data.userId;
  if (req.user.id === targetUserId) {
    return res.status(400).json({ error: "Cannot unblock yourself" });
  }

  const existingFriendships = await getFriendship(req.user.id, targetUserId);
  const currentlyBlocked = existingFriendships.some(
    (f) => f.userId === req.user.id && f.status === "BLOCKED",
  );

  if (!currentlyBlocked) {
    return res.status(400).json({ error: "User is not blocked" });
  }

  await unblockUser(req.user.id, targetUserId);
  return res.json({ success: true });
}

export async function listFriends(req: Request, res: Response) {
  const friendsList = await getFriends(req.user.id);
  return res.json({ success: true, data: friendsList });
}

export async function getCounts(req: Request, res: Response) {
  const counts = await getFriendshipCounts(req.user.id);
  return res.json({ success: true, data: counts });
}

export async function listRequests(req: Request, res: Response) {
  const requests = await getFriendRequests(req.user.id);
  return res.json({ success: true, data: requests });
}

export async function listBlocked(req: Request, res: Response) {
  const blockedUsers = await getBlockedUsers(req.user.id);
  return res.json({ success: true, data: blockedUsers });
}

export async function searchUsers(req: Request, res: Response) {
  const result = await searchGlobalUsers(req.query.q as string, req.user.id);

  return res.json({ success: true, data: result });
}