-
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.
- Loading branch information
Showing
5 changed files
with
88 additions
and
24 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
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, | ||
}) | ||
} |
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
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