-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(server): add notification system
- Loading branch information
Showing
4 changed files
with
102 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), ¬ifications) | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters