diff --git a/vitty-backend-api/api/v2/friendHandler.go b/vitty-backend-api/api/v2/friendHandler.go index e4e3ace..fe412dd 100644 --- a/vitty-backend-api/api/v2/friendHandler.go +++ b/vitty-backend-api/api/v2/friendHandler.go @@ -1,6 +1,8 @@ package v2 import ( + "log" + "github.com/GDGVIT/vitty-backend/vitty-backend-api/api/middleware" "github.com/GDGVIT/vitty-backend/vitty-backend-api/api/serializers" "github.com/GDGVIT/vitty-backend/vitty-backend-api/internal/database" @@ -22,8 +24,9 @@ func friendHandler(api fiber.Router) { friendGroup.Use(middleware.JWTAuthMiddleware) friendGroup.Get("/:username", getFriends) friendGroup.Delete("/:username", removeFriend) + friendGroup.Post("/ghost/:username", becomeGhost) + friendGroup.Post("/alive/:username", becomeAlive) } - func getFriendRequests(c *fiber.Ctx) error { request_user := c.Locals("user").(models.User) return c.Status(fiber.StatusOK).JSON(serializers.FriendRequestsSerializer(request_user.GetFriendRequests(), request_user)) @@ -205,3 +208,35 @@ func removeFriend(c *fiber.Ctx) error { "detail": "Friend removed successfully", }) } + +func becomeGhost(c *fiber.Ctx) error { + request_user := c.Locals("user").(models.User) + username := c.Params("username") + + err := request_user.BecomeGost(username) + + if err != nil { + log.Println(err) + return c.Status(fiber.StatusInternalServerError).JSON(fiber.ErrInternalServerError) + } + + return c.Status(fiber.StatusOK).JSON(fiber.Map{ + "data": "profile hidden", + }) +} + +func becomeAlive(c *fiber.Ctx) error { + request_user := c.Locals("user").(models.User) + username := c.Params("username") + + err := request_user.BecomeAlive(username) + + if err != nil { + log.Println(err) + return c.Status(fiber.StatusInternalServerError).JSON(fiber.ErrInternalServerError) + } + + return c.Status(fiber.StatusOK).JSON(fiber.Map{ + "data": "profile visible", + }) +} diff --git a/vitty-backend-api/api/v2/userHandler.go b/vitty-backend-api/api/v2/userHandler.go index a0a5a76..52e2694 100644 --- a/vitty-backend-api/api/v2/userHandler.go +++ b/vitty-backend-api/api/v2/userHandler.go @@ -1,7 +1,9 @@ package v2 import ( + "errors" "fmt" + "log" "github.com/GDGVIT/vitty-backend/vitty-backend-api/api/middleware" "github.com/GDGVIT/vitty-backend/vitty-backend-api/api/serializers" @@ -9,6 +11,7 @@ import ( "github.com/GDGVIT/vitty-backend/vitty-backend-api/internal/models" "github.com/GDGVIT/vitty-backend/vitty-backend-api/internal/utils" "github.com/gofiber/fiber/v2" + "gorm.io/gorm" ) func userHandler(api fiber.Router) { @@ -60,10 +63,29 @@ func getUser(c *fiber.Ctx) error { fmt.Println("Friends", user.IsFriendsWith(request_user)) if (user.Username == request_user.Username) || - (user.IsFriendsWith(request_user)) || (request_user.Role == "admin") { return c.Status(fiber.StatusOK).JSON(serializers.UserSerializer(user, request_user)) } + + if user.IsFriendsWith(request_user) { + err, isGhosted := request_user.IsGhosted(username) + + if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { + log.Println(err) + return c.Status(fiber.StatusInternalServerError).JSON(fiber.ErrInternalServerError) + + } + + if isGhosted || errors.Is(err, gorm.ErrRecordNotFound) { + return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ + "code": "1811", + "error": "", + }) + } + + return c.Status(fiber.StatusOK).JSON(serializers.UserSerializer(user, request_user)) + } + return c.Status(fiber.StatusOK).JSON(serializers.UserCardSerializer(user, request_user)) } diff --git a/vitty-backend-api/internal/models/initialize.go b/vitty-backend-api/internal/models/initialize.go index a6df1bd..58650be 100644 --- a/vitty-backend-api/internal/models/initialize.go +++ b/vitty-backend-api/internal/models/initialize.go @@ -9,6 +9,7 @@ import ( func InitializeModels() { MODELS := map[string]interface{}{ "User": &User{}, + "UserFriends": &UserFriends{}, "Timetable": &Timetable{}, "Friend Requests": &FriendRequest{}, "Reminders": &Reminders{}, diff --git a/vitty-backend-api/internal/models/userFriends.go b/vitty-backend-api/internal/models/userFriends.go new file mode 100644 index 0000000..504697a --- /dev/null +++ b/vitty-backend-api/internal/models/userFriends.go @@ -0,0 +1,10 @@ +package models + +import "time" + +type UserFriends struct { + UserUsername string `gorm:"primaryKey"` + FriendUsername string `gorm:"primaryKey"` + Hide bool `gorm:"default:false"` + UpdatedAt *time.Time `gorm:"default:CURRENT_TIMESTAMP"` +} diff --git a/vitty-backend-api/internal/models/users.go b/vitty-backend-api/internal/models/users.go index 65621dd..e9bf7fe 100644 --- a/vitty-backend-api/internal/models/users.go +++ b/vitty-backend-api/internal/models/users.go @@ -27,11 +27,8 @@ func (u *User) GetCurrentStatus() map[string]interface{} { now := time.Now() currTime := time.Date(0, 1, 1, now.Hour(), now.Minute(), now.Second(), 0, time.Local) // Remove date part - fmt.Println("Current time: ", currTime) daySlots := u.GetTimeTable().GetDaySlots(time.Now().Weekday()) - fmt.Println("Day slots: ", daySlots) for _, slot := range daySlots[time.Now().Weekday().String()] { - fmt.Println("Slot: ", slot) if slot.StartTime.Before(currTime) && slot.EndTime.After(currTime) { return map[string]interface{}{ "status": "class", @@ -65,6 +62,55 @@ func (u *User) IsFriendsWith(user User) bool { return count != 0 } +func (u *User) BecomeGost(friendUserName string) error { + var userFriend UserFriends + + userFriend.UserUsername = u.Username + userFriend.FriendUsername = friendUserName + + err := database.DB.Model(&UserFriends{}).Where(&userFriend).UpdateColumn("hide", true).Error + return err +} + +func (u *User) BecomeAlive(friendUserName string) error { + var userFriend UserFriends + userFriend.UserUsername = u.Username + userFriend.FriendUsername = friendUserName + userFriend.Hide = false + + err := database.DB.Model(&UserFriends{}).Where(&userFriend).UpdateColumn("hide", false).Error + return err +} + +func (u *User) IsGhosted(friendUserName string) (error, bool) { + var userFriend UserFriends + + userFriend.UserUsername = u.Username + userFriend.FriendUsername = friendUserName + + threshold := time.Now().Add(-8*time.Hour - 48*time.Minute) + + fmt.Println(threshold) + + err := database.DB.Model(&UserFriends{}).Where("updated_at < ?", threshold).First(&userFriend).Error + + if err != nil { + return err, false + } + + if userFriend.Hide { + return nil, userFriend.Hide + } + + userFriend.UserUsername = friendUserName + userFriend.FriendUsername = u.Username + userFriend.UpdatedAt = nil + + err = database.DB.Model(&UserFriends{}).Where(&userFriend).First(&userFriend).Error + + return err, userFriend.Hide +} + func (u *User) HasSentFriendRequest(user User) bool { var count int64 database.DB.Model(&FriendRequest{}).Where("from_username = ? AND to_username = ?", u.Username, user.Username).Count(&count)