-
Notifications
You must be signed in to change notification settings - Fork 0
/
profiles.go
396 lines (351 loc) · 12.6 KB
/
profiles.go
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package routes
import (
"fmt"
"regexp"
"github.com/gofiber/fiber/v2"
"github.com/kayprogrammer/socialnet-v6/managers"
"github.com/kayprogrammer/socialnet-v6/models"
"github.com/kayprogrammer/socialnet-v6/models/choices"
"github.com/kayprogrammer/socialnet-v6/schemas"
"github.com/kayprogrammer/socialnet-v6/utils"
"gorm.io/gorm/clause"
)
// @Summary Retrieve cities based on query params
// @Description This endpoint retrieves the first 10 cities that matches the query params
// @Tags Profiles
// @Param name query string false "City name"
// @Success 200 {object} schemas.CitiesResponseSchema
// @Router /profiles/cities [get]
func (endpoint Endpoint) RetrieveCities(c *fiber.Ctx) error {
db := endpoint.DB
message := "Cities Fetched"
name := c.Query("name")
// Define a regular expression to match non-word characters (excluding spaces).
re := regexp.MustCompile(`[^\w\s]`)
// Use the regular expression to replace matching substrings with an empty string.
name = re.ReplaceAllString(name, "")
cities := []models.City{}
db.Preload(clause.Associations).Where("name ILIKE ?", "%"+name+"%").Find(&cities)
if len(cities) == 0 {
message = "No match found"
}
response := schemas.CitiesResponseSchema{
ResponseSchema: SuccessResponse(message),
Data: cities,
}.Init()
return c.Status(200).JSON(response)
}
// @Summary Retrieve Users
// @Description This endpoint retrieves a paginated list of users
// @Tags Profiles
// @Param page query int false "Current Page" default(1)
// @Success 200 {object} schemas.ProfilesResponseSchema
// @Router /profiles [get]
// @Security BearerAuth
func (endpoint Endpoint) RetrieveUsers(c *fiber.Ctx) error {
db := endpoint.DB
user := RequestUser(c)
users := []models.User{}
query := db.Preload(clause.Associations)
if user != nil {
query.Not(models.User{BaseModel: models.BaseModel{ID: user.ID}})
}
query.Find(&users)
// Paginate, Convert type and return Users
paginatedData, paginatedUsers, err := PaginateQueryset(users, c)
if err != nil {
return c.Status(400).JSON(err)
}
users = paginatedUsers.([]models.User)
response := schemas.ProfilesResponseSchema{
ResponseSchema: SuccessResponse("Users fetched"),
Data: schemas.ProfilesResponseDataSchema{
PaginatedResponseDataSchema: *paginatedData,
Items: users,
}.Init(),
}
return c.Status(200).JSON(response)
}
// @Summary Retrieve User Profile
// @Description This endpoint retrieves a user profile
// @Tags Profiles
// @Param username path string true "Username of user"
// @Success 200 {object} schemas.ProfileResponseSchema
// @Router /profiles/profile/{username} [get]
func (endpoint Endpoint) RetrieveUserProfile(c *fiber.Ctx) error {
db := endpoint.DB
username := c.Params("username")
user := models.User{}
db.Preload("CityObj").Preload("AvatarObj").Take(&user, models.User{Username: username})
if user.ID == nil {
return c.Status(404).JSON(utils.RequestErr(utils.ERR_NON_EXISTENT, "No user with that username"))
}
// Return User
response := schemas.ProfileResponseSchema{
ResponseSchema: SuccessResponse("User details fetched"),
Data: user.Init(),
}
return c.Status(200).JSON(response)
}
// @Summary Update User Profile
// @Description This endpoint updates a user profile
// @Tags Profiles
// @Param profile body schemas.ProfileUpdateSchema true "Profile object"
// @Success 200 {object} schemas.ProfileResponseSchema
// @Router /profiles/profile [patch]
// @Security BearerAuth
func (endpoint Endpoint) UpdateProfile(c *fiber.Ctx) error {
db := endpoint.DB
user := RequestUser(c)
data := schemas.ProfileUpdateSchema{}
// Validate request
if errCode, errData := ValidateRequest(c, &data); errData != nil {
return c.Status(*errCode).JSON(errData)
}
// Validate City Value
cityID := data.CityID
if cityID != nil {
city := models.City{}
db.Take(&city, models.City{BaseModel: models.BaseModel{ID: *cityID}})
if city.ID == nil {
data := map[string]string{
"city_id": "No city with that ID",
}
return c.Status(422).JSON(utils.RequestErr(utils.ERR_INVALID_ENTRY, "Invalid Entry", data))
}
user.CityObj = &city
}
// Create OR Update File
fileType := data.FileType
if fileType != nil {
file := models.File{ResourceType: *fileType}.UpdateOrCreate(db, user.AvatarId)
user.AvatarObj = &file
}
// Set values & save
user = data.SetValues(user)
db.Save(&user)
// Return repsonse
updatedData := schemas.ProfileUpdateResponseDataSchema{
User: *user,
}.Init(fileType)
response := schemas.ProfileUpdateResponseSchema{
ResponseSchema: SuccessResponse("User updated"),
Data: updatedData,
}
return c.Status(200).JSON(response)
}
// @Summary Delete User's Account
// @Description This endpoint deletes a particular user's account (irreversible)
// @Tags Profiles
// @Param password body schemas.DeleteUserSchema true "Password"
// @Success 200 {object} schemas.ResponseSchema
// @Router /profiles/profile [post]
// @Security BearerAuth
func (endpoint Endpoint) DeleteUser(c *fiber.Ctx) error {
db := endpoint.DB
user := RequestUser(c)
data := schemas.DeleteUserSchema{}
// Validate request
if errCode, errData := ValidateRequest(c, &data); errData != nil {
return c.Status(*errCode).JSON(errData)
}
// Check if password is valid
if !utils.CheckPasswordHash(data.Password, user.Password) {
data := map[string]string{
"password": "Incorrect password",
}
return c.Status(422).JSON(utils.RequestErr(utils.ERR_INVALID_ENTRY, "Invalid Entry", data))
}
// Delete User
db.Delete(&user)
return c.Status(200).JSON(SuccessResponse("User deleted"))
}
var friendManager = managers.FriendManager{}
// @Summary Retrieve Friends
// @Description This endpoint retrieves friends of a user
// @Tags Profiles
// @Param page query int false "Current Page" default(1)
// @Success 200 {object} schemas.ProfilesResponseSchema
// @Router /profiles/friends [get]
// @Security BearerAuth
func (endpoint Endpoint) RetrieveFriends(c *fiber.Ctx) error {
db := endpoint.DB
user := RequestUser(c)
friends := friendManager.GetFriends(db, *user)
// Paginate and return Friends
paginatedData, paginatedFriends, err := PaginateQueryset(friends, c, 20)
if err != nil {
return c.Status(400).JSON(err)
}
friends = paginatedFriends.([]models.User)
response := schemas.ProfilesResponseSchema{
ResponseSchema: SuccessResponse("Friends fetched"),
Data: schemas.ProfilesResponseDataSchema{
PaginatedResponseDataSchema: *paginatedData,
Items: friends,
}.Init(),
}
return c.Status(200).JSON(response)
}
// @Summary Retrieve Friend Requests
// @Description This endpoint retrieves friend requests of a user
// @Tags Profiles
// @Param page query int false "Current Page" default(1)
// @Success 200 {object} schemas.ProfilesResponseSchema
// @Router /profiles/friends/requests [get]
// @Security BearerAuth
func (endpoint Endpoint) RetrieveFriendRequests(c *fiber.Ctx) error {
db := endpoint.DB
user := RequestUser(c)
friendsRequests := friendManager.GetFriendRequests(db, user)
// Paginate, Convert type and return Friends Requests
paginatedData, paginatedFriendRequests, err := PaginateQueryset(friendsRequests, c, 20)
if err != nil {
return c.Status(400).JSON(err)
}
friendsRequests = paginatedFriendRequests.([]models.User)
response := schemas.ProfilesResponseSchema{
ResponseSchema: SuccessResponse("Friend Requests fetched"),
Data: schemas.ProfilesResponseDataSchema{
PaginatedResponseDataSchema: *paginatedData,
Items: friendsRequests,
}.Init(),
}
return c.Status(200).JSON(response)
}
// @Summary Send Or Delete Friend Request
// @Description This endpoint sends or delete friend requests
// @Tags Profiles
// @Param friend_request body schemas.SendFriendRequestSchema true "Friend Request object"
// @Success 200 {object} schemas.ResponseSchema
// @Router /profiles/friends/requests [post]
// @Security BearerAuth
func (endpoint Endpoint) SendOrDeleteFriendRequest(c *fiber.Ctx) error {
db := endpoint.DB
user := RequestUser(c)
data := schemas.SendFriendRequestSchema{}
// Validate request
if errCode, errData := ValidateRequest(c, &data); errData != nil {
return c.Status(*errCode).JSON(errData)
}
requestee, friend, errData := friendManager.GetRequesteeAndFriendObj(db, user, data.Username)
if errData != nil {
return c.Status(404).JSON(errData)
}
message := "Friend Request sent"
statusCode := 201
if friend.ID != nil {
statusCode = 200
message = "Friend Request removed"
if friend.Status == "ACCEPTED" {
message = "This user is already your friend"
} else if user.ID.String() != friend.RequesterID.String() {
return c.Status(403).JSON(utils.RequestErr(utils.ERR_NOT_ALLOWED, "The user already sent you a friend request!"))
} else {
// Delete friend successfully
db.Delete(&friend)
}
} else {
// Create Friend Object
db.Create(&models.Friend{RequesterID: user.ID, RequesteeID: requestee.ID, Status: choices.FPENDING})
}
response := SuccessResponse(message)
return c.Status(statusCode).JSON(response)
}
// @Summary Accept Or Reject a Friend Request
// @Description This endpoint accepts or reject a friend request
// @Tags Profiles
// @Param friend_request body schemas.AcceptFriendRequestSchema true "Friend Request object"
// @Success 200 {object} schemas.ResponseSchema
// @Router /profiles/friends/requests [put]
// @Security BearerAuth
func (endpoint Endpoint) AcceptOrRejectFriendRequest(c *fiber.Ctx) error {
db := endpoint.DB
user := RequestUser(c)
data := schemas.AcceptFriendRequestSchema{}
// Validate request
if errCode, errData := ValidateRequest(c, &data); errData != nil {
return c.Status(*errCode).JSON(errData)
}
_, friend, errData := friendManager.GetRequesteeAndFriendObj(db, user, data.Username, choices.FPENDING)
if errData != nil {
return c.Status(404).JSON(errData)
}
if friend.ID == nil {
return c.Status(404).JSON(utils.RequestErr(utils.ERR_NON_EXISTENT, "No friend request exist between you and that user"))
}
if friend.RequesterID.String() == user.ID.String() {
return c.Status(403).JSON(utils.RequestErr(utils.ERR_NOT_ALLOWED, "You cannot accept or reject a friend request you sent"))
}
// Update or delete friend request based on status
message := "Accepted"
if data.Accepted {
// Update Friend Request
friend.Status = choices.FACCEPTED
db.Save(&friend)
} else {
// Delete Friend Request
message = "Rejected"
db.Delete(&friend)
}
return c.Status(200).JSON(SuccessResponse(fmt.Sprintf("Friend Request %s", message)))
}
var notificationManager = managers.NotificationManager{}
// @Summary Retrieve User Notifications
// @Description This endpoint retrieves a paginated list of auth user's notifications. Use post, comment, reply slug to navigate to the post, comment or reply.
// @Tags Profiles
// @Param page query int false "Current Page" default(1)
// @Success 200 {object} schemas.NotificationsResponseSchema
// @Router /profiles/notifications [get]
// @Security BearerAuth
func (endpoint Endpoint) RetrieveUserNotifications(c *fiber.Ctx) error {
db := endpoint.DB
user := RequestUser(c)
notifications := notificationManager.GetQueryset(db, user.ID)
// Paginate, Convert type and return notifications
paginatedData, paginatedNotifications, err := PaginateQueryset(notifications, c)
if err != nil {
return c.Status(400).JSON(err)
}
notifications = paginatedNotifications.([]models.Notification)
response := schemas.NotificationsResponseSchema{
ResponseSchema: SuccessResponse("Notifications fetched"),
Data: schemas.NotificationsResponseDataSchema{
PaginatedResponseDataSchema: *paginatedData,
Items: notifications,
}.Init(user.ID),
}
return c.Status(200).JSON(response)
}
// @Summary Read Notifications
// @Description This endpoint reads a notification
// @Tags Profiles
// @Param read_data body schemas.ReadNotificationSchema true "Read Notification Data"
// @Success 200 {object} schemas.ResponseSchema
// @Router /profiles/notifications [post]
// @Security BearerAuth
func (endpoint Endpoint) ReadNotification(c *fiber.Ctx) error {
db := endpoint.DB
user := RequestUser(c)
data := schemas.ReadNotificationSchema{}
// Validate request
if errCode, errData := ValidateRequest(c, &data); errData != nil {
return c.Status(*errCode).JSON(errData)
}
notificationID := data.ID
markAllAsRead := data.MarkAllAsRead
respMessage := "Notifications read"
if markAllAsRead {
// Mark all notifications as read
notificationManager.MarkAsRead(db, user)
} else if notificationID != nil {
// Mark single notification as read
err := notificationManager.ReadOne(db, user, *notificationID)
if err != nil {
return c.Status(404).JSON(err)
}
respMessage = "Notification read"
}
response := SuccessResponse(respMessage)
return c.Status(200).JSON(response)
}