Compare commits
2 Commits
a0a3ef09d0
...
9207a4f164
Author | SHA1 | Date | |
---|---|---|---|
9207a4f164 | |||
486bb69977 |
@ -52,5 +52,7 @@ func MapAPIs(app *fiber.App, baseURL string) {
|
|||||||
|
|
||||||
api.Get("/tags", listTags)
|
api.Get("/tags", listTags)
|
||||||
api.Get("/tags/:tag", getTag)
|
api.Get("/tags/:tag", getTag)
|
||||||
|
|
||||||
|
api.Get("/whats-new", getWhatsNew)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
65
pkg/internal/server/api/what_new_api.dart.go
Normal file
65
pkg/internal/server/api/what_new_api.dart.go
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
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,6 +41,34 @@ func ListAccountFriends(user models.Account) ([]models.Account, error) {
|
|||||||
return accounts, nil
|
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 {
|
func ModifyPosterVoteCount(user models.Account, isUpvote bool, delta int) error {
|
||||||
if isUpvote {
|
if isUpvote {
|
||||||
user.TotalUpvote += delta
|
user.TotalUpvote += delta
|
||||||
|
@ -32,12 +32,17 @@ func FilterPostWithUserContext(tx *gorm.DB, user *models.Account) *gorm.DB {
|
|||||||
friendAllowList := lo.Map(friends, func(item models.Account, index int) uint {
|
friendAllowList := lo.Map(friends, func(item models.Account, index int) uint {
|
||||||
return item.ID
|
return item.ID
|
||||||
})
|
})
|
||||||
|
blocked, _ := ListAccountBlockedUsers(*user)
|
||||||
|
blockedDisallowList := lo.Map(blocked, func(item models.Account, index int) uint {
|
||||||
|
return item.ID
|
||||||
|
})
|
||||||
|
|
||||||
tx = tx.Where(
|
tx = tx.Where(
|
||||||
"(visibility != ? OR (visibility != ? AND author_id IN ?) OR (visibility = ? AND ?) OR (visibility = ? AND NOT ?) OR author_id = ?)",
|
"(visibility != ? OR (visibility != ? AND author_id IN ? AND author_id NOT IN ?) OR (visibility = ? AND ?) OR (visibility = ? AND NOT ?) OR author_id = ?)",
|
||||||
NoneVisibility,
|
NoneVisibility,
|
||||||
FriendsVisibility,
|
FriendsVisibility,
|
||||||
friendAllowList,
|
friendAllowList,
|
||||||
|
blockedDisallowList,
|
||||||
SelectedVisibility,
|
SelectedVisibility,
|
||||||
datatypes.JSONQuery("visible_users").HasKey(strconv.Itoa(int(user.ID))),
|
datatypes.JSONQuery("visible_users").HasKey(strconv.Itoa(int(user.ID))),
|
||||||
FilteredVisibility,
|
FilteredVisibility,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user