Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide #45

Merged
merged 4 commits into from
Jan 6, 2025
Merged

Hide #45

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion vitty-backend-api/api/v2/friendHandler.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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))
Expand Down Expand Up @@ -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",
})
}
24 changes: 23 additions & 1 deletion vitty-backend-api/api/v2/userHandler.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
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"
"github.com/GDGVIT/vitty-backend/vitty-backend-api/internal/database"
"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) {
Expand Down Expand Up @@ -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))
}

Expand Down
1 change: 1 addition & 0 deletions vitty-backend-api/internal/models/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
func InitializeModels() {
MODELS := map[string]interface{}{
"User": &User{},
"UserFriends": &UserFriends{},
"Timetable": &Timetable{},
"Friend Requests": &FriendRequest{},
"Reminders": &Reminders{},
Expand Down
10 changes: 10 additions & 0 deletions vitty-backend-api/internal/models/userFriends.go
Original file line number Diff line number Diff line change
@@ -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"`
}
52 changes: 49 additions & 3 deletions vitty-backend-api/internal/models/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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)
Expand Down
Loading