Compare commits
No commits in common. "9207a4f1640e95f32f1ace3f7146f157310d339c" and "a0a3ef09d0db9ca41fbb0686fab3b88feceb9e28" have entirely different histories.
9207a4f164
...
a0a3ef09d0
@ -52,7 +52,5 @@ func MapAPIs(app *fiber.App, baseURL string) {
|
||||
|
||||
api.Get("/tags", listTags)
|
||||
api.Get("/tags/:tag", getTag)
|
||||
|
||||
api.Get("/whats-new", getWhatsNew)
|
||||
}
|
||||
}
|
||||
|
@ -1,65 +0,0 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/database"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/gap"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/models"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/services"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
func getWhatsNew(c *fiber.Ctx) error {
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
|
||||
pivot := c.QueryInt("pivot", 0)
|
||||
if pivot < 0 {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "pivot must be greater than zero")
|
||||
}
|
||||
|
||||
realm := c.Query("realm")
|
||||
|
||||
tx := services.FilterPostDraft(database.C)
|
||||
tx = services.FilterPostWithUserContext(tx, &user)
|
||||
|
||||
friends, _ := services.ListAccountFriends(user)
|
||||
friendList := lo.Map(friends, func(item models.Account, index int) uint {
|
||||
return item.ID
|
||||
})
|
||||
|
||||
tx = tx.Where("id > ?", pivot)
|
||||
tx = tx.Where("author_id IN ?", friendList)
|
||||
|
||||
if len(realm) > 0 {
|
||||
if realm, err := services.GetRealmWithAlias(realm); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("realm was not found: %v", err))
|
||||
} else {
|
||||
tx = services.FilterPostWithRealm(tx, realm.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, 10, 0, order)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(fiber.Map{
|
||||
"count": count,
|
||||
"data": items,
|
||||
})
|
||||
}
|
@ -41,34 +41,6 @@ func ListAccountFriends(user models.Account) ([]models.Account, error) {
|
||||
return accounts, nil
|
||||
}
|
||||
|
||||
func ListAccountBlockedUsers(user models.Account) ([]models.Account, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||
defer cancel()
|
||||
|
||||
pc, err := gap.H.GetServiceGrpcConn(hyper.ServiceTypeAuthProvider)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to listing account blocked users: %v", err)
|
||||
}
|
||||
result, err := proto.NewAuthClient(pc).ListUserBlocklist(ctx, &proto.ListUserRelativeRequest{
|
||||
UserId: uint64(user.ID),
|
||||
IsRelated: true,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to listing account blocked users: %v", err)
|
||||
}
|
||||
|
||||
out := lo.Map(result.Data, func(item *proto.SimpleUserInfo, index int) uint {
|
||||
return uint(item.Id)
|
||||
})
|
||||
|
||||
var accounts []models.Account
|
||||
if err = database.C.Where("id IN ?", out).Find(&accounts).Error; err != nil {
|
||||
return nil, fmt.Errorf("failed to linking listed blocked users: %v", err)
|
||||
}
|
||||
|
||||
return accounts, nil
|
||||
}
|
||||
|
||||
func ModifyPosterVoteCount(user models.Account, isUpvote bool, delta int) error {
|
||||
if isUpvote {
|
||||
user.TotalUpvote += delta
|
||||
|
@ -32,17 +32,12 @@ func FilterPostWithUserContext(tx *gorm.DB, user *models.Account) *gorm.DB {
|
||||
friendAllowList := lo.Map(friends, func(item models.Account, index int) uint {
|
||||
return item.ID
|
||||
})
|
||||
blocked, _ := ListAccountBlockedUsers(*user)
|
||||
blockedDisallowList := lo.Map(blocked, func(item models.Account, index int) uint {
|
||||
return item.ID
|
||||
})
|
||||
|
||||
tx = tx.Where(
|
||||
"(visibility != ? OR (visibility != ? AND author_id IN ? AND author_id NOT IN ?) OR (visibility = ? AND ?) OR (visibility = ? AND NOT ?) OR author_id = ?)",
|
||||
"(visibility != ? OR (visibility != ? AND author_id IN ?) OR (visibility = ? AND ?) OR (visibility = ? AND NOT ?) OR author_id = ?)",
|
||||
NoneVisibility,
|
||||
FriendsVisibility,
|
||||
friendAllowList,
|
||||
blockedDisallowList,
|
||||
SelectedVisibility,
|
||||
datatypes.JSONQuery("visible_users").HasKey(strconv.Itoa(int(user.ID))),
|
||||
FilteredVisibility,
|
||||
|
Loading…
x
Reference in New Issue
Block a user