Skip to content

Commit

Permalink
feat(server): add notification system
Browse files Browse the repository at this point in the history
  • Loading branch information
lareii committed Sep 18, 2024
1 parent dfa4dad commit 17bfacd
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 7 deletions.
17 changes: 10 additions & 7 deletions server/database/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ import (
)

const (
UsersCollectionName = "users"
PostsCollectionName = "posts"
CommentsCollectionName = "comments"
UsersCollectionName = "users"
PostsCollectionName = "posts"
CommentsCollectionName = "comments"
NofiticationsCollectionName = "notifications"
)

var (
Users *mongo.Collection
Posts *mongo.Collection
Comments *mongo.Collection
Users *mongo.Collection
Posts *mongo.Collection
Comments *mongo.Collection
Notifications *mongo.Collection
)

func createDatabase(ctx context.Context, databaseName string) error {
Expand All @@ -33,7 +35,7 @@ func createDatabase(ctx context.Context, databaseName string) error {
}
}

collections := []string{UsersCollectionName, PostsCollectionName, CommentsCollectionName}
collections := []string{UsersCollectionName, PostsCollectionName, CommentsCollectionName, NofiticationsCollectionName}
for _, coll := range collections {
if err := Client.Database(databaseName).CreateCollection(ctx, coll); err != nil {
return err
Expand All @@ -48,4 +50,5 @@ func initCollections(database *mongo.Database) {
Users = database.Collection(UsersCollectionName)
Posts = database.Collection(PostsCollectionName)
Comments = database.Collection(CommentsCollectionName)
Notifications = database.Collection(NofiticationsCollectionName)
}
36 changes: 36 additions & 0 deletions server/handlers/me/notifications.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package me

import (
"github.com/gofiber/fiber/v2"
"github.com/lareii/copl.uk/server/models"
)

func GetNotifications(c *fiber.Ctx) error {
user, ok := c.Locals("user").(models.User)
if !ok {
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
"message": "User not authenticated.",
})
}

limit := c.QueryInt("limit", 10)
offset := c.QueryInt("offset", 0)

if limit > 30 {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"message": "Limit must be less than 30.",
})
}

notifications, err := models.GetNotifications(int64(limit), int64(offset), user.ID)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"message": err.Error(),
})
}

return c.JSON(fiber.Map{
"message": "Notifications found.",
"notifications": notifications,
})
}
55 changes: 55 additions & 0 deletions server/models/notifications.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package models

import (
"context"
"fmt"
"time"

"github.com/lareii/copl.uk/server/database"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo/options"
)

type Notification struct {
ID primitive.ObjectID `bson:"_id" json:"id"`
CreatedAt primitive.Timestamp `bson:"created_at" json:"created_at"`
RecipientID primitive.ObjectID `bson:"recipient_id" json:"recipient_id"`
Content string `bson:"content" json:"content"`
Read bool `bson:"read" json:"read"`
Link string `bson:"link" json:"link"`
}

func GetNotifications(limit, offset int64, userID primitive.ObjectID) ([]Notification, error) {
var notifications []Notification
cursor, err := database.Notifications.Find(context.Background(),
bson.M{"recipient_id": userID},
&options.FindOptions{
Limit: &limit,
Skip: &offset,
Sort: bson.M{"created_at": -1},
})
if err != nil {
return notifications, fmt.Errorf("error fetching notifications: %v", err)
}

err = cursor.All(context.Background(), &notifications)
if err != nil {
return notifications, fmt.Errorf("error decoding notifications: %v", err)
}

return notifications, nil
}

func CreateNotification(notification Notification) (Notification, error) {
notification.ID = primitive.NewObjectID()
notification.CreatedAt = primitive.Timestamp{T: uint32(time.Now().Unix())}
notification.Read = false

_, err := database.Notifications.InsertOne(context.Background(), notification)
if err != nil {
return Notification{}, err
}

return notification, nil
}
1 change: 1 addition & 0 deletions server/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func SetupRouter(app *fiber.App) {
meGroup.Get("/", middlewares.AuthMiddleware(), me.User)
meGroup.Patch("/", middlewares.AuthMiddleware(), middlewares.RateLimiterMiddleware(10, 60), me.UpdateUser)
meGroup.Get("/feed", middlewares.AuthMiddleware(), middlewares.RateLimiterMiddleware(20, 60), me.GetFeed)
meGroup.Get("/notifications", middlewares.AuthMiddleware(), middlewares.RateLimiterMiddleware(20, 60), me.GetNotifications)

userGroup := app.Group("/users")
userGroup.Get("/", middlewares.AuthMiddleware(), middlewares.RateLimiterMiddleware(20, 60), users.GetUsers)
Expand Down

0 comments on commit 17bfacd

Please sign in to comment.