Skip to content

Commit

Permalink
feat: added option for users to hide timetable from friends
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSilentSage committed Jan 1, 2025
1 parent 9c18ea6 commit 2172f50
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 5 deletions.
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/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
firebase.google.com/go v3.13.0+incompatible
github.com/gofiber/fiber/v2 v2.46.0
github.com/golang-jwt/jwt/v4 v4.5.0
github.com/joho/godotenv v1.5.1
github.com/labstack/echo/v4 v4.11.2
github.com/urfave/cli/v2 v2.25.6
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1
Expand Down
2 changes: 2 additions & 0 deletions vitty-backend-api/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/klauspost/compress v1.16.3 h1:XuJt9zzcnaz6a16/OU53ZjWp/v7/42WcR5t2a0PcNQY=
github.com/klauspost/compress v1.16.3/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
github.com/labstack/echo/v4 v4.11.2 h1:T+cTLQxWCDfqDEoydYm5kCobjmHwOwcv4OJAPHilmdE=
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{},
}
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

0 comments on commit 2172f50

Please sign in to comment.