Compare commits
3 Commits
5ce0e33359
...
ed8afe8324
Author | SHA1 | Date | |
---|---|---|---|
ed8afe8324 | |||
ecc2bff9a0 | |||
e1f1cd5130 |
@ -1,6 +1,7 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
@ -41,6 +42,7 @@ func createArticle(c *fiber.Ctx) error {
|
||||
InvisibleUsers []uint `json:"invisible_users_list"`
|
||||
Visibility *int8 `json:"visibility"`
|
||||
IsDraft bool `json:"is_draft"`
|
||||
Realm *uint `json:"realm"`
|
||||
}
|
||||
|
||||
if err := exts.BindAndValidate(c, &data); err != nil {
|
||||
@ -83,6 +85,13 @@ func createArticle(c *fiber.Ctx) error {
|
||||
item.PublishedAt = lo.ToPtr(time.Now())
|
||||
}
|
||||
|
||||
if data.Realm != nil {
|
||||
if _, err := authkit.GetRealmMember(gap.Nx, *data.Realm, user.ID); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("you are not a member of realm #%d", *data.Realm))
|
||||
}
|
||||
item.RealmID = data.Realm
|
||||
}
|
||||
|
||||
if data.Visibility != nil {
|
||||
item.Visibility = *data.Visibility
|
||||
} else {
|
||||
|
@ -55,6 +55,10 @@ func UniversalPostFilter(c *fiber.Ctx, tx *gorm.DB) (*gorm.DB, error) {
|
||||
tx = services.FilterPostWithType(tx, c.Query("type"))
|
||||
}
|
||||
|
||||
if len(c.Query("realm")) > 0 {
|
||||
tx = services.FilterPostWithRealm(tx, c.Query("realm"))
|
||||
}
|
||||
|
||||
return tx, nil
|
||||
}
|
||||
|
||||
|
@ -43,6 +43,7 @@ func createQuestion(c *fiber.Ctx) error {
|
||||
InvisibleUsers []uint `json:"invisible_users_list"`
|
||||
Visibility *int8 `json:"visibility"`
|
||||
IsDraft bool `json:"is_draft"`
|
||||
Realm *uint `json:"realm"`
|
||||
Reward float64 `json:"reward"`
|
||||
}
|
||||
|
||||
@ -109,6 +110,13 @@ func createQuestion(c *fiber.Ctx) error {
|
||||
item.PublishedAt = lo.ToPtr(time.Now())
|
||||
}
|
||||
|
||||
if data.Realm != nil {
|
||||
if _, err := authkit.GetRealmMember(gap.Nx, *data.Realm, user.ID); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("you are not a member of realm #%d", *data.Realm))
|
||||
}
|
||||
item.RealmID = data.Realm
|
||||
}
|
||||
|
||||
if data.Visibility != nil {
|
||||
item.Visibility = *data.Visibility
|
||||
} else {
|
||||
|
@ -45,6 +45,7 @@ func createStory(c *fiber.Ctx) error {
|
||||
ReplyTo *uint `json:"reply_to"`
|
||||
RepostTo *uint `json:"repost_to"`
|
||||
Poll *uint `json:"poll"`
|
||||
Realm *uint `json:"realm"`
|
||||
}
|
||||
|
||||
if err := exts.BindAndValidate(c, &data); err != nil {
|
||||
@ -96,6 +97,13 @@ func createStory(c *fiber.Ctx) error {
|
||||
item.Visibility = models.PostVisibilityAll
|
||||
}
|
||||
|
||||
if data.Realm != nil {
|
||||
if _, err := authkit.GetRealmMember(gap.Nx, *data.Realm, user.ID); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("you are not a member of realm #%d", *data.Realm))
|
||||
}
|
||||
item.RealmID = data.Realm
|
||||
}
|
||||
|
||||
if data.ReplyTo != nil {
|
||||
var replyTo models.Post
|
||||
if err := database.C.Where("id = ?", data.ReplyTo).First(&replyTo).Error; err != nil {
|
||||
|
@ -1,6 +1,7 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
@ -41,6 +42,7 @@ func createVideo(c *fiber.Ctx) error {
|
||||
InvisibleUsers []uint `json:"invisible_users_list"`
|
||||
Visibility *int8 `json:"visibility"`
|
||||
IsDraft bool `json:"is_draft"`
|
||||
Realm *uint `json:"realm"`
|
||||
}
|
||||
|
||||
if err := exts.BindAndValidate(c, &data); err != nil {
|
||||
@ -84,6 +86,13 @@ func createVideo(c *fiber.Ctx) error {
|
||||
item.PublishedAt = lo.ToPtr(time.Now())
|
||||
}
|
||||
|
||||
if data.Realm != nil {
|
||||
if _, err := authkit.GetRealmMember(gap.Nx, *data.Realm, user.ID); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("you are not a member of realm #%d", *data.Realm))
|
||||
}
|
||||
item.RealmID = data.Realm
|
||||
}
|
||||
|
||||
if data.Visibility != nil {
|
||||
item.Visibility = *data.Visibility
|
||||
} else {
|
||||
|
@ -32,8 +32,8 @@ type Post struct {
|
||||
Type string `json:"type"`
|
||||
Body datatypes.JSONMap `json:"body" gorm:"index:,type:gin"`
|
||||
Language string `json:"language"`
|
||||
Alias *string `json:"alias"`
|
||||
AliasPrefix *string `json:"alias_prefix"`
|
||||
Alias *string `json:"alias" gorm:"index"`
|
||||
AliasPrefix *string `json:"alias_prefix" gorm:"index"`
|
||||
Tags []Tag `json:"tags" gorm:"many2many:post_tags"`
|
||||
Categories []Category `json:"categories" gorm:"many2many:post_categories"`
|
||||
Reactions []Reaction `json:"reactions"`
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
@ -15,6 +16,7 @@ import (
|
||||
pproto "git.solsynth.dev/hypernet/paperclip/pkg/proto"
|
||||
"git.solsynth.dev/hypernet/passport/pkg/authkit"
|
||||
authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models"
|
||||
aproto "git.solsynth.dev/hypernet/passport/pkg/proto"
|
||||
"github.com/eko/gocache/lib/v4/cache"
|
||||
"github.com/eko/gocache/lib/v4/marshaler"
|
||||
"github.com/eko/gocache/lib/v4/store"
|
||||
@ -29,7 +31,7 @@ import (
|
||||
|
||||
func FilterPostWithUserContext(tx *gorm.DB, user *authm.Account) *gorm.DB {
|
||||
if user == nil {
|
||||
return tx.Where("visibility = ?", models.PostVisibilityAll)
|
||||
return tx.Where("visibility = ? AND realm_id IS NULL", models.PostVisibilityAll)
|
||||
}
|
||||
|
||||
const (
|
||||
@ -40,15 +42,16 @@ func FilterPostWithUserContext(tx *gorm.DB, user *authm.Account) *gorm.DB {
|
||||
)
|
||||
|
||||
type userContextState struct {
|
||||
Allowlist []uint
|
||||
InvisibleList []uint
|
||||
Allowlist []uint `json:"allow"`
|
||||
InvisibleList []uint `json:"invisible"`
|
||||
RealmList []uint `json:"realm"`
|
||||
}
|
||||
|
||||
cacheManager := cache.New[any](localCache.S)
|
||||
marshal := marshaler.New(cacheManager)
|
||||
ctx := context.Background()
|
||||
|
||||
var allowlist, invisibleList []uint
|
||||
var allowlist, invisibleList, realmList []uint
|
||||
|
||||
statusCacheKey := fmt.Sprintf("post-user-context-query#%d", user.ID)
|
||||
statusCache, err := marshal.Get(ctx, statusCacheKey, new(userContextState))
|
||||
@ -56,10 +59,31 @@ func FilterPostWithUserContext(tx *gorm.DB, user *authm.Account) *gorm.DB {
|
||||
state := statusCache.(*userContextState)
|
||||
allowlist = state.Allowlist
|
||||
invisibleList = state.InvisibleList
|
||||
realmList = state.RealmList
|
||||
} else {
|
||||
// Getting the relationships
|
||||
userFriends, _ := authkit.ListRelative(gap.Nx, user.ID, int32(authm.RelationshipFriend), true)
|
||||
userGotBlocked, _ := authkit.ListRelative(gap.Nx, user.ID, int32(authm.RelationshipBlocked), true)
|
||||
userBlocked, _ := authkit.ListRelative(gap.Nx, user.ID, int32(authm.RelationshipBlocked), false)
|
||||
|
||||
// Getting the realm list
|
||||
{
|
||||
conn, err := gap.Nx.GetClientGrpcConn(nex.ServiceTypeAuth)
|
||||
if err != nil {
|
||||
ac := aproto.NewRealmServiceClient(conn)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||
defer cancel()
|
||||
resp, err := ac.ListAvailableRealm(ctx, &aproto.LookupUserRealmRequest{
|
||||
UserId: uint64(user.ID),
|
||||
})
|
||||
if err == nil {
|
||||
realmList = lo.Map(resp.GetData(), func(item *aproto.RealmInfo, index int) uint {
|
||||
return uint(item.GetId())
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
userFriendList := lo.Map(userFriends, func(item *proto.UserInfo, index int) uint {
|
||||
return uint(item.GetId())
|
||||
})
|
||||
@ -101,6 +125,7 @@ func FilterPostWithUserContext(tx *gorm.DB, user *authm.Account) *gorm.DB {
|
||||
userContextState{
|
||||
Allowlist: allowlist,
|
||||
InvisibleList: invisibleList,
|
||||
RealmList: realmList,
|
||||
},
|
||||
store.WithExpiration(5*time.Minute),
|
||||
store.WithTags([]string{"post-user-context-query", fmt.Sprintf("user#%d", user.ID)}),
|
||||
@ -125,10 +150,28 @@ func FilterPostWithUserContext(tx *gorm.DB, user *authm.Account) *gorm.DB {
|
||||
if len(invisibleList) > 0 {
|
||||
tx = tx.Where("publisher_id NOT IN ?", invisibleList)
|
||||
}
|
||||
if len(realmList) > 0 {
|
||||
tx = tx.Where("realm_id IN ?", realmList)
|
||||
} else {
|
||||
tx = tx.Where("realm_id IS NULL")
|
||||
}
|
||||
|
||||
return tx
|
||||
}
|
||||
|
||||
func FilterPostWithRealm(tx *gorm.DB, probe string) *gorm.DB {
|
||||
if numericId, err := strconv.Atoi(probe); err == nil {
|
||||
return tx.Where("realm_id = ?", uint(numericId))
|
||||
}
|
||||
|
||||
realm, err := authkit.GetRealmByAlias(gap.Nx, probe)
|
||||
if err != nil {
|
||||
return tx
|
||||
}
|
||||
|
||||
return tx.Where("realm_id = ?", realm.ID)
|
||||
}
|
||||
|
||||
func FilterPostWithCategory(tx *gorm.DB, alias string) *gorm.DB {
|
||||
aliases := strings.Split(alias, ",")
|
||||
return tx.Joins("JOIN post_categories ON posts.id = post_categories.post_id").
|
||||
|
Loading…
x
Reference in New Issue
Block a user