♻️ Globally apply the V2 api
This commit is contained in:
@ -65,7 +65,7 @@ func apUserOutbox(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
var activities []activitypub.Item
|
||||
if posts, err := services.ListPost(tx, limit, (page-1)*limit, "published_at DESC", nil); err != nil {
|
||||
if posts, err := services.ListPostV1(tx, limit, (page-1)*limit, "published_at DESC", nil); err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
} else {
|
||||
for _, post := range posts {
|
||||
|
@ -59,7 +59,6 @@ func MapControllers(app *fiber.App, baseURL string) {
|
||||
posts := api.Group("/posts").Name("Posts API")
|
||||
{
|
||||
posts.Get("/", listPost)
|
||||
posts.Get("/v2", listPostV2)
|
||||
posts.Get("/search", searchPost)
|
||||
posts.Get("/minimal", listPostMinimal)
|
||||
posts.Get("/drafts", listDraftPost)
|
||||
|
@ -26,17 +26,26 @@ func getPost(c *fiber.Ctx) error {
|
||||
var item models.Post
|
||||
var err error
|
||||
|
||||
tx := database.C
|
||||
var userId *uint
|
||||
if user, authenticated := c.Locals("user").(authm.Account); authenticated {
|
||||
userId = &user.ID
|
||||
}
|
||||
|
||||
tx := database.C
|
||||
if tx, err = services.UniversalPostFilter(c, tx, services.UniversalPostFilterConfig{
|
||||
ShowReply: true,
|
||||
ShowDraft: true,
|
||||
ShowReply: true,
|
||||
ShowDraft: true,
|
||||
ShowCollapsed: true,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if numericId, paramErr := strconv.Atoi(id); paramErr == nil {
|
||||
item, err = services.GetPost(tx, uint(numericId))
|
||||
if c.Get("X-API-Version", "1") == "2" {
|
||||
item, err = queries.GetPost(tx, uint(numericId), userId)
|
||||
} else {
|
||||
item, err = services.GetPost(tx, uint(numericId))
|
||||
}
|
||||
} else {
|
||||
segments := strings.Split(id, ":")
|
||||
if len(segments) != 2 {
|
||||
@ -44,7 +53,11 @@ func getPost(c *fiber.Ctx) error {
|
||||
}
|
||||
area := segments[0]
|
||||
alias := segments[1]
|
||||
item, err = services.GetPostByAlias(tx, alias, area)
|
||||
if c.Get("X-API-Version", "1") == "2" {
|
||||
item, err = queries.GetPostByAlias(tx, alias, area, userId)
|
||||
} else {
|
||||
item, err = services.GetPostByAlias(tx, alias, area)
|
||||
}
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
@ -88,22 +101,27 @@ func searchPost(c *fiber.Ctx) error {
|
||||
userId = &user.ID
|
||||
}
|
||||
|
||||
var count int64
|
||||
countTx := tx
|
||||
count, err := services.CountPost(countTx)
|
||||
count, err = services.CountPost(countTx)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
items, err := services.ListPost(tx, take, offset, "published_at DESC", userId)
|
||||
var items []models.Post
|
||||
|
||||
if c.Get("X-API-Version", "1") == "2" {
|
||||
items, err = queries.ListPost(tx, take, offset, "published_at DESC", userId)
|
||||
} else {
|
||||
items, err = services.ListPostV1(tx, take, offset, "published_at DESC", userId)
|
||||
}
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
if c.QueryBool("truncate", true) {
|
||||
for _, item := range items {
|
||||
if item != nil {
|
||||
item = lo.ToPtr(services.TruncatePostContent(*item))
|
||||
}
|
||||
item = services.TruncatePostContent(item)
|
||||
}
|
||||
}
|
||||
|
||||
@ -129,54 +147,20 @@ func listPost(c *fiber.Ctx) error {
|
||||
userId = &user.ID
|
||||
}
|
||||
|
||||
var count int64
|
||||
countTx := tx
|
||||
count, err := services.CountPost(countTx)
|
||||
count, err = services.CountPost(countTx)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
items, err := services.ListPost(tx, take, offset, "published_at DESC", userId)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
var items []models.Post
|
||||
|
||||
if c.Get("X-API-Version", "1") == "2" {
|
||||
items, err = queries.ListPost(tx, take, offset, "published_at DESC", userId)
|
||||
} else {
|
||||
items, err = services.ListPostV1(tx, take, offset, "published_at DESC", userId)
|
||||
}
|
||||
|
||||
if c.QueryBool("truncate", true) {
|
||||
for _, item := range items {
|
||||
if item != nil {
|
||||
item = lo.ToPtr(services.TruncatePostContent(*item))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return c.JSON(fiber.Map{
|
||||
"count": count,
|
||||
"data": items,
|
||||
})
|
||||
}
|
||||
|
||||
func listPostV2(c *fiber.Ctx) error {
|
||||
take := c.QueryInt("take", 10)
|
||||
offset := c.QueryInt("offset", 0)
|
||||
|
||||
tx := database.C
|
||||
|
||||
var err error
|
||||
if tx, err = services.UniversalPostFilter(c, tx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var userId *uint
|
||||
if user, authenticated := c.Locals("user").(authm.Account); authenticated {
|
||||
userId = &user.ID
|
||||
}
|
||||
|
||||
countTx := tx
|
||||
count, err := services.CountPost(countTx)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
items, err := queries.ListPostV2(tx, take, offset, "published_at DESC", userId)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
@ -238,6 +222,7 @@ func listDraftPost(c *fiber.Ctx) error {
|
||||
}
|
||||
user := c.Locals("user").(authm.Account)
|
||||
|
||||
var err error
|
||||
tx := services.FilterPostWithAuthorDraft(database.C, user.ID)
|
||||
|
||||
var userId *uint
|
||||
@ -245,21 +230,27 @@ func listDraftPost(c *fiber.Ctx) error {
|
||||
userId = &user.ID
|
||||
}
|
||||
|
||||
count, err := services.CountPost(tx)
|
||||
var count int64
|
||||
countTx := tx
|
||||
count, err = services.CountPost(countTx)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
items, err := services.ListPost(tx, take, offset, "created_at DESC", userId, true)
|
||||
var items []models.Post
|
||||
|
||||
if c.Get("X-API-Version", "1") == "2" {
|
||||
items, err = queries.ListPost(tx, take, offset, "published_at DESC", userId)
|
||||
} else {
|
||||
items, err = services.ListPostV1(tx, take, offset, "published_at DESC", userId)
|
||||
}
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
if c.QueryBool("truncate", true) {
|
||||
for _, item := range items {
|
||||
if item != nil {
|
||||
item = lo.ToPtr(services.TruncatePostContent(*item))
|
||||
}
|
||||
item = services.TruncatePostContent(item)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -33,7 +33,7 @@ func listPinnedPost(c *fiber.Ctx) error {
|
||||
userId = &user.ID
|
||||
}
|
||||
|
||||
items, err := services.ListPost(tx, 100, 0, "published_at DESC", userId)
|
||||
items, err := services.ListPostV1(tx, 100, 0, "published_at DESC", userId)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
@ -29,7 +29,7 @@ func listRecommendation(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
tx := database.C.Where("id IN ?", postIdx)
|
||||
newPosts, err := services.ListPost(tx, featuredMax, 0, "id ASC", userId)
|
||||
newPosts, err := services.ListPostV1(tx, featuredMax, 0, "id ASC", userId)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
@ -67,7 +67,7 @@ func listRecommendationShuffle(c *fiber.Ctx) error {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
items, err := services.ListPost(tx, take, offset, "RANDOM()", userId)
|
||||
items, err := services.ListPostV1(tx, take, offset, "RANDOM()", userId)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
@ -92,7 +92,7 @@ func getRecommendationFeed(c *fiber.Ctx) error {
|
||||
|
||||
var cursorTime *time.Time
|
||||
if cursor > 0 {
|
||||
cursorTime = lo.ToPtr(time.UnixMilli(int64(cursor) + 1))
|
||||
cursorTime = lo.ToPtr(time.UnixMilli(int64(cursor - 1)))
|
||||
}
|
||||
|
||||
var userId *uint
|
||||
|
@ -47,7 +47,7 @@ func listPostReplies(c *fiber.Ctx) error {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
items, err := services.ListPost(tx, take, offset, "published_at DESC", userId)
|
||||
items, err := services.ListPostV1(tx, take, offset, "published_at DESC", userId)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
@ -90,7 +90,7 @@ func listPostFeaturedReply(c *fiber.Ctx) error {
|
||||
tx = services.FilterPostWithTag(tx, c.Query("tag"))
|
||||
}
|
||||
|
||||
items, err := services.ListPost(tx, take, 0, "(COALESCE(total_upvote, 0) - COALESCE(total_downvote, 0)) DESC, published_at DESC", userId)
|
||||
items, err := services.ListPostV1(tx, take, 0, "(COALESCE(total_upvote, 0) - COALESCE(total_downvote, 0)) DESC, published_at DESC", userId)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
@ -40,7 +40,7 @@ func getWhatsNew(c *fiber.Ctx) error {
|
||||
order = "published_at DESC, (COALESCE(total_upvote, 0) - COALESCE(total_downvote, 0)) DESC"
|
||||
}
|
||||
|
||||
items, err := services.ListPost(tx, 10, 0, order, userId)
|
||||
items, err := services.ListPostV1(tx, 10, 0, order, userId)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
Reference in New Issue
Block a user