Skip to content

Commit

Permalink
feat(server): add feed for users
Browse files Browse the repository at this point in the history
  • Loading branch information
lareii committed Sep 13, 2024
1 parent 0f9f189 commit 80b7e20
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 24 deletions.
80 changes: 80 additions & 0 deletions server/handlers/auth/feed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package auth

import (
"github.com/gofiber/fiber/v2"
"github.com/lareii/copl.uk/server/models"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
)

type PostsResponse struct {
Message string `json:"message"`
Posts []models.PostResponseContent `json:"posts"`
}

func GetFeed(c *fiber.Ctx) error {
limit := c.QueryInt("limit", 10)
offset := c.QueryInt("offset", 0)

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

posts, err := models.GetPosts(
int64(limit),
int64(offset),
bson.M{"author": bson.M{"$in": user.Following}},
bson.M{"created_at": -1},
)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"message": "Error fetching posts.",
})
}

var authorIDs []primitive.ObjectID
for _, post := range posts {
authorIDs = append(authorIDs, post.Author)
}

authorMap := make(map[primitive.ObjectID]models.User)
for _, authorID := range authorIDs {
author, err := models.GetUserByID(authorID)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"message": "Error fetching post authors.",
})
}
authorMap[author.ID] = author
}

var responsePosts []models.PostResponseContent
for _, post := range posts {
author := authorMap[post.Author]

responsePosts = append(responsePosts, models.PostResponseContent{
ID: post.ID,
CreatedAt: post.CreatedAt,
UpdatedAt: post.UpdatedAt,
Author: models.PostResponseAuthor{
ID: author.ID,
CreatedAt: author.CreatedAt,
DisplayName: author.DisplayName,
Username: author.Username,
About: author.About,
Points: author.Points,
},
Content: post.Content,
Likes: post.Likes,
Comments: post.Comments,
})
}

return c.Status(fiber.StatusOK).JSON(PostsResponse{
Message: "Posts found.",
Posts: responsePosts,
})
}
3 changes: 2 additions & 1 deletion server/handlers/posts/get_posts.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package posts
import (
"github.com/gofiber/fiber/v2"
"github.com/lareii/copl.uk/server/models"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
)

Expand All @@ -15,7 +16,7 @@ func GetPosts(c *fiber.Ctx) error {
limit := c.QueryInt("limit", 10)
offset := c.QueryInt("offset", 0)

posts, err := models.GetPosts(int64(limit), int64(offset))
posts, err := models.GetPosts(int64(limit), int64(offset), nil, bson.M{"created_at": -1})
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"message": "Error fetching posts.",
Expand Down
3 changes: 2 additions & 1 deletion server/handlers/users/get_user_posts.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package users
import (
"github.com/gofiber/fiber/v2"
"github.com/lareii/copl.uk/server/models"
"go.mongodb.org/mongo-driver/bson"
)

func GetUserPosts(c *fiber.Ctx) error {
Expand All @@ -22,7 +23,7 @@ func GetUserPosts(c *fiber.Ctx) error {
limit := c.QueryInt("limit", 10)
offset := c.QueryInt("offset", 0)

posts, err := models.GetPostsByUser(user, int64(limit), int64(offset))
posts, err := models.GetPosts(int64(limit), int64(offset), bson.M{"author": user.ID}, bson.M{"created_at": -1})
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"message": "Error fetching user posts.",
Expand Down
25 changes: 3 additions & 22 deletions server/models/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,31 +55,12 @@ func GetPostByID(postID primitive.ObjectID) (Post, error) {
return post, nil
}

func GetPosts(limit, offset int64) ([]Post, error) {
func GetPosts(limit, offset int64, filter, sort bson.M) ([]Post, error) {
var posts []Post
cursor, err := database.Posts.Find(context.Background(), bson.M{}, &options.FindOptions{
cursor, err := database.Posts.Find(context.Background(), filter, &options.FindOptions{
Limit: &limit,
Skip: &offset,
Sort: bson.M{"created_at": -1},
})
if err != nil {
return posts, fmt.Errorf("error fetching posts: %v", err)
}

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

return posts, nil
}

func GetPostsByUser(user User, limit, offset int64) ([]Post, error) {
var posts []Post
cursor, err := database.Posts.Find(context.Background(), bson.M{"author": user.ID}, &options.FindOptions{
Limit: &limit,
Skip: &offset,
Sort: bson.M{"created_at": -1},
Sort: sort,
})
if err != nil {
return posts, fmt.Errorf("error fetching posts: %v", err)
Expand Down
1 change: 1 addition & 0 deletions server/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func SetupRouter(app *fiber.App) {
authGroup.Post("/logout", auth.Logout)
authGroup.Get("/me", middlewares.AuthMiddleware(), auth.User)
authGroup.Patch("/me", middlewares.AuthMiddleware(), auth.UpdateUser)
authGroup.Get("/me/feed", middlewares.AuthMiddleware(), auth.GetFeed)

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

0 comments on commit 80b7e20

Please sign in to comment.