🗑️ Clean up code
This commit is contained in:
		| @@ -28,7 +28,6 @@ func MapAPIs(app *fiber.App, baseURL string) { | |||||||
| 		recommendations := api.Group("/recommendations").Name("Recommendations API") | 		recommendations := api.Group("/recommendations").Name("Recommendations API") | ||||||
| 		{ | 		{ | ||||||
| 			recommendations.Get("/", listRecommendation) | 			recommendations.Get("/", listRecommendation) | ||||||
| 			recommendations.Get("/friends", listRecommendationFriends) |  | ||||||
| 			recommendations.Get("/shuffle", listRecommendationShuffle) | 			recommendations.Get("/shuffle", listRecommendationShuffle) | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
|   | |||||||
| @@ -2,12 +2,8 @@ package api | |||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"git.solsynth.dev/hypernet/interactive/pkg/internal/database" | 	"git.solsynth.dev/hypernet/interactive/pkg/internal/database" | ||||||
| 	"git.solsynth.dev/hypernet/interactive/pkg/internal/gap" |  | ||||||
| 	"git.solsynth.dev/hypernet/interactive/pkg/internal/models" | 	"git.solsynth.dev/hypernet/interactive/pkg/internal/models" | ||||||
| 	"git.solsynth.dev/hypernet/interactive/pkg/internal/services" | 	"git.solsynth.dev/hypernet/interactive/pkg/internal/services" | ||||||
| 	"git.solsynth.dev/hypernet/nexus/pkg/nex/sec" |  | ||||||
| 	"git.solsynth.dev/hypernet/nexus/pkg/proto" |  | ||||||
| 	"git.solsynth.dev/hypernet/passport/pkg/authkit" |  | ||||||
| 	authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models" | 	authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models" | ||||||
| 	"github.com/gofiber/fiber/v2" | 	"github.com/gofiber/fiber/v2" | ||||||
| 	"github.com/samber/lo" | 	"github.com/samber/lo" | ||||||
| @@ -47,64 +43,6 @@ func listRecommendation(c *fiber.Ctx) error { | |||||||
| 	return c.JSON(posts) | 	return c.JSON(posts) | ||||||
| } | } | ||||||
|  |  | ||||||
| func listRecommendationFriends(c *fiber.Ctx) error { |  | ||||||
| 	if err := sec.EnsureAuthenticated(c); err != nil { |  | ||||||
| 		return err |  | ||||||
| 	} |  | ||||||
| 	user := c.Locals("user").(authm.Account) |  | ||||||
|  |  | ||||||
| 	take := c.QueryInt("take", 0) |  | ||||||
| 	offset := c.QueryInt("offset", 0) |  | ||||||
|  |  | ||||||
| 	tx := database.C |  | ||||||
|  |  | ||||||
| 	var err error |  | ||||||
| 	if tx, err = UniversalPostFilter(c, tx); err != nil { |  | ||||||
| 		return err |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	friends, _ := authkit.ListRelative(gap.Nx, user.ID, int32(authm.RelationshipFriend), true) |  | ||||||
| 	friendList := lo.Map(friends, func(item *proto.UserInfo, index int) uint { |  | ||||||
| 		return uint(item.GetId()) |  | ||||||
| 	}) |  | ||||||
|  |  | ||||||
| 	tx = tx.Where("publisher_id IN ?", friendList) |  | ||||||
|  |  | ||||||
| 	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()) |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	order := "published_at DESC" |  | ||||||
| 	if c.QueryBool("featured", false) { |  | ||||||
| 		order = "published_at DESC, (COALESCE(total_upvote, 0) - COALESCE(total_downvote, 0)) DESC" |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	items, err := services.ListPost(tx, take, offset, order, 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)) |  | ||||||
| 			} |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	return c.JSON(fiber.Map{ |  | ||||||
| 		"count": count, |  | ||||||
| 		"data":  items, |  | ||||||
| 	}) |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func listRecommendationShuffle(c *fiber.Ctx) error { | func listRecommendationShuffle(c *fiber.Ctx) error { | ||||||
| 	take := c.QueryInt("take", 0) | 	take := c.QueryInt("take", 0) | ||||||
| 	offset := c.QueryInt("offset", 0) | 	offset := c.QueryInt("offset", 0) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user