♻️ Moved account-based post to publisher-based post
This commit is contained in:
@ -6,8 +6,7 @@ import (
|
||||
)
|
||||
|
||||
var AutoMaintainRange = []any{
|
||||
&models.Account{},
|
||||
&models.Realm{},
|
||||
&models.Publisher{},
|
||||
&models.Category{},
|
||||
&models.Tag{},
|
||||
&models.Post{},
|
||||
|
@ -1,24 +1,26 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/gap"
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/cruda"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/samber/lo"
|
||||
"github.com/spf13/viper"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
"gorm.io/gorm/schema"
|
||||
)
|
||||
|
||||
var C *gorm.DB
|
||||
|
||||
func NewSource() error {
|
||||
var err error
|
||||
func NewGorm() error {
|
||||
dsn, err := cruda.NewCrudaConn(gap.Nx).AllocDatabase("passport")
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to alloc database from nexus: %v", err)
|
||||
}
|
||||
|
||||
dialector := postgres.Open(viper.GetString("database.dsn"))
|
||||
C, err = gorm.Open(dialector, &gorm.Config{NamingStrategy: schema.NamingStrategy{
|
||||
TablePrefix: viper.GetString("database.prefix"),
|
||||
}, Logger: logger.New(&log.Logger, logger.Config{
|
||||
C, err = gorm.Open(postgres.Open(dsn), &gorm.Config{Logger: logger.New(&log.Logger, logger.Config{
|
||||
Colorful: true,
|
||||
IgnoreRecordNotFoundError: true,
|
||||
LogLevel: lo.Ternary(viper.GetBool("debug.database"), logger.Info, logger.Silent),
|
||||
|
@ -1,15 +0,0 @@
|
||||
package gap
|
||||
|
||||
import "net"
|
||||
|
||||
func GetOutboundIP() (net.IP, error) {
|
||||
conn, err := net.Dial("udp", "1.1.1.1:80")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
defer conn.Close()
|
||||
}
|
||||
|
||||
localAddr := conn.LocalAddr().(*net.UDPAddr)
|
||||
return localAddr.IP, nil
|
||||
}
|
@ -2,42 +2,51 @@ package gap
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex"
|
||||
"git.solsynth.dev/hypernet/pusher/pkg/pushkit/pushcon"
|
||||
"github.com/samber/lo"
|
||||
"strings"
|
||||
|
||||
"git.solsynth.dev/hydrogen/dealer/pkg/hyper"
|
||||
"git.solsynth.dev/hydrogen/dealer/pkg/proto"
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/proto"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var H *hyper.HyperConn
|
||||
var Nx *nex.Conn
|
||||
var Px *pushcon.Conn
|
||||
|
||||
func RegisterService() error {
|
||||
func InitializeToNexus() error {
|
||||
grpcBind := strings.SplitN(viper.GetString("grpc_bind"), ":", 2)
|
||||
httpBind := strings.SplitN(viper.GetString("bind"), ":", 2)
|
||||
|
||||
outboundIp, _ := GetOutboundIP()
|
||||
outboundIp, _ := nex.GetOutboundIP()
|
||||
|
||||
grpcOutbound := fmt.Sprintf("%s:%s", outboundIp, grpcBind[1])
|
||||
httpOutbound := fmt.Sprintf("%s:%s", outboundIp, httpBind[1])
|
||||
|
||||
var err error
|
||||
H, err = hyper.NewHyperConn(viper.GetString("dealer.addr"), &proto.ServiceInfo{
|
||||
Nx, err = nex.NewNexusConn(viper.GetString("nexus_addr"), &proto.ServiceInfo{
|
||||
Id: viper.GetString("id"),
|
||||
Type: hyper.ServiceTypeInteractiveProvider,
|
||||
Label: "Passport",
|
||||
Type: "co",
|
||||
Label: "Interactive",
|
||||
GrpcAddr: grpcOutbound,
|
||||
HttpAddr: &httpOutbound,
|
||||
HttpAddr: lo.ToPtr("http://" + httpOutbound + "/api"),
|
||||
})
|
||||
if err == nil {
|
||||
go func() {
|
||||
err := H.KeepRegisterService()
|
||||
err := Nx.RunRegistering()
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("An error occurred while registering service...")
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
Px, err = pushcon.NewConn(Nx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error during initialize pushcon: %v", err)
|
||||
}
|
||||
|
||||
return err
|
||||
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
package grpc
|
||||
|
||||
import (
|
||||
"git.solsynth.dev/hydrogen/dealer/pkg/proto"
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/proto"
|
||||
"github.com/spf13/viper"
|
||||
"google.golang.org/grpc"
|
||||
health "google.golang.org/grpc/health/grpc_health_v1"
|
||||
@ -10,25 +10,29 @@ import (
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
proto.UnimplementedServiceDirectoryServer
|
||||
proto.UnimplementedDirectoryServiceServer
|
||||
|
||||
srv *grpc.Server
|
||||
}
|
||||
|
||||
var S *grpc.Server
|
||||
func NewGrpc() *Server {
|
||||
server := &Server{
|
||||
srv: grpc.NewServer(),
|
||||
}
|
||||
|
||||
func NewGRPC() {
|
||||
S = grpc.NewServer()
|
||||
health.RegisterHealthServer(server.srv, server)
|
||||
proto.RegisterDirectoryServiceServer(server.srv, server)
|
||||
|
||||
health.RegisterHealthServer(S, &Server{})
|
||||
proto.RegisterServiceDirectoryServer(S, &Server{})
|
||||
reflection.Register(server.srv)
|
||||
|
||||
reflection.Register(S)
|
||||
return server
|
||||
}
|
||||
|
||||
func ListenGRPC() error {
|
||||
func (v *Server) Listen() error {
|
||||
listener, err := net.Listen("tcp", viper.GetString("grpc_bind"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return S.Serve(listener)
|
||||
return v.srv.Serve(listener)
|
||||
}
|
||||
|
@ -23,7 +23,7 @@ func (v *Server) BroadcastDeletion(ctx context.Context, request *proto.DeletionR
|
||||
database.C.Delete(model, "account_id = ?", numericId)
|
||||
}
|
||||
}
|
||||
database.C.Delete(&models.Account{}, "id = ?", numericId)
|
||||
database.C.Delete(&models.Publisher{}, "id = ?", numericId)
|
||||
}
|
||||
|
||||
return &proto.DeletionResponse{}, nil
|
||||
|
@ -1,15 +1,17 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/cruda"
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
|
||||
"git.solsynth.dev/hypernet/passport/pkg/authkit"
|
||||
authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"git.solsynth.dev/hydrogen/dealer/pkg/hyper"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/database"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/gap"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/http/exts"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/models"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/server/exts"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/services"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
@ -17,12 +19,13 @@ import (
|
||||
)
|
||||
|
||||
func createArticle(c *fiber.Ctx) error {
|
||||
if err := gap.H.EnsureGrantedPerm(c, "CreatePosts", true); err != nil {
|
||||
if err := sec.EnsureGrantedPerm(c, "CreatePosts", true); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
user := c.Locals("user").(authm.Account)
|
||||
|
||||
var data struct {
|
||||
Publisher uint `json:"publisher"`
|
||||
Alias *string `json:"alias"`
|
||||
Title string `json:"title" validate:"required,max=1024"`
|
||||
Description *string `json:"description"`
|
||||
@ -37,13 +40,17 @@ func createArticle(c *fiber.Ctx) error {
|
||||
InvisibleUsers []uint `json:"invisible_users_list"`
|
||||
Visibility *int8 `json:"visibility"`
|
||||
IsDraft bool `json:"is_draft"`
|
||||
RealmAlias *string `json:"realm"`
|
||||
}
|
||||
|
||||
if err := exts.BindAndValidate(c, &data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
publisher, err := services.GetPublisher(data.Publisher, user.ID)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
body := models.PostArticleBody{
|
||||
Thumbnail: data.Thumbnail,
|
||||
Title: data.Title,
|
||||
@ -68,7 +75,7 @@ func createArticle(c *fiber.Ctx) error {
|
||||
PublishedUntil: data.PublishedUntil,
|
||||
VisibleUsers: data.VisibleUsers,
|
||||
InvisibleUsers: data.InvisibleUsers,
|
||||
AuthorID: user.ID,
|
||||
PublisherID: publisher.ID,
|
||||
}
|
||||
|
||||
if item.PublishedAt == nil {
|
||||
@ -81,27 +88,15 @@ func createArticle(c *fiber.Ctx) error {
|
||||
item.Visibility = models.PostVisibilityAll
|
||||
}
|
||||
|
||||
if data.RealmAlias != nil {
|
||||
if realm, err := services.GetRealmWithAlias(*data.RealmAlias); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
} else if _, err = services.GetRealmMember(realm.ID, user.ID); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to post in the realm, access denied: %v", err))
|
||||
} else {
|
||||
item.RealmID = &realm.ID
|
||||
item.Realm = &realm
|
||||
}
|
||||
}
|
||||
|
||||
item, err := services.NewPost(user, item)
|
||||
item, err = services.NewPost(publisher, item)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
} else {
|
||||
_ = gap.H.RecordAuditLog(
|
||||
user.ID,
|
||||
_ = authkit.AddEventExt(
|
||||
gap.Nx,
|
||||
"posts.new",
|
||||
strconv.Itoa(int(item.ID)),
|
||||
c.IP(),
|
||||
c.Get(fiber.HeaderUserAgent),
|
||||
c,
|
||||
)
|
||||
}
|
||||
|
||||
@ -110,12 +105,13 @@ func createArticle(c *fiber.Ctx) error {
|
||||
|
||||
func editArticle(c *fiber.Ctx) error {
|
||||
id, _ := c.ParamsInt("postId", 0)
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
if err := sec.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
user := c.Locals("user").(authm.Account)
|
||||
|
||||
var data struct {
|
||||
Publisher uint `json:"publisher"`
|
||||
Alias *string `json:"alias"`
|
||||
Title string `json:"title" validate:"required,max=1024"`
|
||||
Description *string `json:"description"`
|
||||
@ -130,17 +126,21 @@ func editArticle(c *fiber.Ctx) error {
|
||||
InvisibleUsers []uint `json:"invisible_users_list"`
|
||||
Visibility *int8 `json:"visibility"`
|
||||
IsDraft bool `json:"is_draft"`
|
||||
RealmAlias *string `json:"realm"`
|
||||
}
|
||||
|
||||
if err := exts.BindAndValidate(c, &data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
publisher, err := services.GetPublisher(data.Publisher, user.ID)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
var item models.Post
|
||||
if err := database.C.Where(models.Post{
|
||||
BaseModel: hyper.BaseModel{ID: uint(id)},
|
||||
AuthorID: user.ID,
|
||||
BaseModel: cruda.BaseModel{ID: uint(id)},
|
||||
PublisherID: publisher.ID,
|
||||
}).First(&item).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||
}
|
||||
@ -180,33 +180,22 @@ func editArticle(c *fiber.Ctx) error {
|
||||
item.PublishedUntil = data.PublishedUntil
|
||||
item.VisibleUsers = data.VisibleUsers
|
||||
item.InvisibleUsers = data.InvisibleUsers
|
||||
item.Author = user
|
||||
|
||||
// Preload publisher data
|
||||
item.Publisher = publisher
|
||||
|
||||
if data.Visibility != nil {
|
||||
item.Visibility = *data.Visibility
|
||||
}
|
||||
|
||||
if data.RealmAlias != nil {
|
||||
if realm, err := services.GetRealmWithAlias(*data.RealmAlias); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
} else if _, err = services.GetRealmMember(realm.ID, user.ID); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to post in the realm, access denied: %v", err))
|
||||
} else {
|
||||
item.RealmID = &realm.ID
|
||||
item.Realm = &realm
|
||||
}
|
||||
}
|
||||
|
||||
var err error
|
||||
if item, err = services.EditPost(item); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
} else {
|
||||
_ = gap.H.RecordAuditLog(
|
||||
user.ID,
|
||||
_ = authkit.AddEventExt(
|
||||
gap.Nx,
|
||||
"posts.edit",
|
||||
strconv.Itoa(int(item.ID)),
|
||||
c.IP(),
|
||||
c.Get(fiber.HeaderUserAgent),
|
||||
c,
|
||||
)
|
||||
}
|
||||
|
@ -1,9 +1,9 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/gap"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/server/exts"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/http/exts"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/services"
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
@ -28,7 +28,7 @@ func listCategories(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
func newCategory(c *fiber.Ctx) error {
|
||||
if err := gap.H.EnsureGrantedPerm(c, "CreatePostCategories", true); err != nil {
|
||||
if err := sec.EnsureGrantedPerm(c, "CreatePostCategories", true); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@ -51,7 +51,7 @@ func newCategory(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
func editCategory(c *fiber.Ctx) error {
|
||||
if err := gap.H.EnsureGrantedPerm(c, "CreatePostCategories", true); err != nil {
|
||||
if err := sec.EnsureGrantedPerm(c, "CreatePostCategories", true); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@ -80,7 +80,7 @@ func editCategory(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
func deleteCategory(c *fiber.Ctx) error {
|
||||
if err := gap.H.EnsureGrantedPerm(c, "CreatePostCategories", true); err != nil {
|
||||
if err := sec.EnsureGrantedPerm(c, "CreatePostCategories", true); err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -7,9 +7,7 @@ import (
|
||||
func MapAPIs(app *fiber.App, baseURL string) {
|
||||
api := app.Group(baseURL).Name("API")
|
||||
{
|
||||
api.Get("/users/me", getUserinfo)
|
||||
api.Get("/users/:account", getOthersInfo)
|
||||
api.Get("/users/:account/pin", listOthersPinnedPost)
|
||||
api.Get("/users/:account/pins", listUserPinnedPost)
|
||||
|
||||
api.Get("/publishers/:name", getPublisher)
|
||||
|
||||
@ -51,15 +49,12 @@ func MapAPIs(app *fiber.App, baseURL string) {
|
||||
subscriptions.Get("/users/:userId", getSubscriptionOnUser)
|
||||
subscriptions.Get("/tags/:tagId", getSubscriptionOnTag)
|
||||
subscriptions.Get("/categories/:categoryId", getSubscriptionOnCategory)
|
||||
subscriptions.Get("/realms/:realmId", getSubscriptionOnRealm)
|
||||
subscriptions.Post("/users/:userId", subscribeToUser)
|
||||
subscriptions.Post("/tags/:tagId", subscribeToTag)
|
||||
subscriptions.Post("/categories/:categoryId", subscribeToCategory)
|
||||
subscriptions.Post("/realms/:realmId", subscribeToRealm)
|
||||
subscriptions.Delete("/users/:userId", unsubscribeFromUser)
|
||||
subscriptions.Delete("/tags/:tagId", unsubscribeFromTag)
|
||||
subscriptions.Delete("/categories/:categoryId", unsubscribeFromCategory)
|
||||
subscriptions.Delete("/realms/:realmId", unsubscribeFromRealm)
|
||||
}
|
||||
|
||||
api.Get("/categories", listCategories)
|
@ -2,6 +2,10 @@ package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/cruda"
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
|
||||
"git.solsynth.dev/hypernet/passport/pkg/authkit"
|
||||
authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models"
|
||||
"gorm.io/gorm"
|
||||
"strconv"
|
||||
"strings"
|
||||
@ -9,38 +13,28 @@ import (
|
||||
"git.solsynth.dev/hydrogen/dealer/pkg/hyper"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/database"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/gap"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/http/exts"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/models"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/server/exts"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/services"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
func universalPostFilter(c *fiber.Ctx, tx *gorm.DB) (*gorm.DB, error) {
|
||||
realm := c.Query("realm")
|
||||
|
||||
tx = services.FilterPostDraft(tx)
|
||||
|
||||
if user, authenticated := c.Locals("user").(models.Account); authenticated {
|
||||
if user, authenticated := c.Locals("user").(authm.Account); authenticated {
|
||||
tx = services.FilterPostWithUserContext(tx, &user)
|
||||
} else {
|
||||
tx = services.FilterPostWithUserContext(tx, nil)
|
||||
}
|
||||
|
||||
if len(realm) > 0 {
|
||||
if realm, err := services.GetRealmWithAlias(realm); err != nil {
|
||||
return tx, fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("realm was not found: %v", err))
|
||||
} else {
|
||||
tx = services.FilterPostWithRealm(tx, realm.ID)
|
||||
}
|
||||
}
|
||||
|
||||
if c.QueryBool("noReply", true) {
|
||||
tx = services.FilterPostReply(tx)
|
||||
}
|
||||
|
||||
if len(c.Query("author")) > 0 {
|
||||
var author models.Account
|
||||
var author models.Publisher
|
||||
if err := database.C.Where(&hyper.BaseUser{Name: c.Query("author")}).First(&author).Error; err != nil {
|
||||
return tx, fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||
}
|
||||
@ -65,7 +59,7 @@ func getPost(c *fiber.Ctx) error {
|
||||
|
||||
tx := services.FilterPostDraft(database.C)
|
||||
|
||||
if user, authenticated := c.Locals("user").(models.Account); authenticated {
|
||||
if user, authenticated := c.Locals("user").(authm.Account); authenticated {
|
||||
tx = services.FilterPostWithUserContext(tx, &user)
|
||||
} else {
|
||||
tx = services.FilterPostWithUserContext(tx, nil)
|
||||
@ -218,10 +212,10 @@ func listDraftPost(c *fiber.Ctx) error {
|
||||
take := c.QueryInt("take", 0)
|
||||
offset := c.QueryInt("offset", 0)
|
||||
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
if err := sec.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
user := c.Locals("user").(authm.Account)
|
||||
|
||||
tx := services.FilterPostWithAuthorDraft(database.C, user.ID)
|
||||
|
||||
@ -250,16 +244,26 @@ func listDraftPost(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
func deletePost(c *fiber.Ctx) error {
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
if err := sec.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
user := c.Locals("user").(authm.Account)
|
||||
id, _ := c.ParamsInt("postId", 0)
|
||||
|
||||
publisherId := c.QueryInt("publisherId", 0)
|
||||
if publisherId <= 0 {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "missing publisher id in request")
|
||||
}
|
||||
|
||||
publisher, err := services.GetPublisher(uint(publisherId), user.ID)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
var item models.Post
|
||||
if err := database.C.Where(models.Post{
|
||||
BaseModel: hyper.BaseModel{ID: uint(id)},
|
||||
AuthorID: user.ID,
|
||||
BaseModel: cruda.BaseModel{ID: uint(id)},
|
||||
PublisherID: publisher.ID,
|
||||
}).First(&item).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||
}
|
||||
@ -267,12 +271,11 @@ func deletePost(c *fiber.Ctx) error {
|
||||
if err := services.DeletePost(item); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
} else {
|
||||
_ = gap.H.RecordAuditLog(
|
||||
user.ID,
|
||||
_ = authkit.AddEventExt(
|
||||
gap.Nx,
|
||||
"posts.delete",
|
||||
strconv.Itoa(int(item.ID)),
|
||||
c.IP(),
|
||||
c.Get(fiber.HeaderUserAgent),
|
||||
c,
|
||||
)
|
||||
}
|
||||
|
||||
@ -280,10 +283,10 @@ func deletePost(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
func reactPost(c *fiber.Ctx) error {
|
||||
if err := gap.H.EnsureGrantedPerm(c, "CreateReactions", true); err != nil {
|
||||
if err := sec.EnsureGrantedPerm(c, "CreateReactions", true); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
user := c.Locals("user").(authm.Account)
|
||||
|
||||
var data struct {
|
||||
Symbol string `json:"symbol"`
|
||||
@ -304,18 +307,17 @@ func reactPost(c *fiber.Ctx) error {
|
||||
if err := database.C.Where("id = ?", c.Params("postId")).Select("id").First(&res).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to find post to react: %v", err))
|
||||
} else {
|
||||
reaction.PostID = &res.ID
|
||||
reaction.PostID = res.ID
|
||||
}
|
||||
|
||||
if positive, reaction, err := services.ReactPost(user, reaction); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
} else {
|
||||
_ = gap.H.RecordAuditLog(
|
||||
user.ID,
|
||||
_ = authkit.AddEventExt(
|
||||
gap.Nx,
|
||||
"posts.react",
|
||||
strconv.Itoa(int(res.ID)),
|
||||
c.IP(),
|
||||
c.Get(fiber.HeaderUserAgent),
|
||||
c,
|
||||
)
|
||||
|
||||
return c.Status(lo.Ternary(positive, fiber.StatusCreated, fiber.StatusNoContent)).JSON(reaction)
|
||||
@ -323,10 +325,10 @@ func reactPost(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
func pinPost(c *fiber.Ctx) error {
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
if err := sec.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
user := c.Locals("user").(authm.Account)
|
||||
|
||||
var res models.Post
|
||||
if err := database.C.Where("id = ? AND author_id = ?", c.Params("postId"), user.ID).First(&res).Error; err != nil {
|
||||
@ -336,24 +338,20 @@ func pinPost(c *fiber.Ctx) error {
|
||||
if status, err := services.PinPost(res); err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
} else if status {
|
||||
_ = gap.H.RecordAuditLog(
|
||||
user.ID,
|
||||
_ = authkit.AddEventExt(
|
||||
gap.Nx,
|
||||
"posts.pin",
|
||||
strconv.Itoa(int(res.ID)),
|
||||
c.IP(),
|
||||
c.Get(fiber.HeaderUserAgent),
|
||||
c,
|
||||
)
|
||||
|
||||
return c.SendStatus(fiber.StatusOK)
|
||||
} else {
|
||||
_ = gap.H.RecordAuditLog(
|
||||
user.ID,
|
||||
_ = authkit.AddEventExt(
|
||||
gap.Nx,
|
||||
"posts.unpin",
|
||||
strconv.Itoa(int(res.ID)),
|
||||
c.IP(),
|
||||
c.Get(fiber.HeaderUserAgent),
|
||||
c,
|
||||
)
|
||||
|
||||
return c.SendStatus(fiber.StatusNoContent)
|
||||
}
|
||||
}
|
9
pkg/internal/http/api/publishers_api.go
Normal file
9
pkg/internal/http/api/publishers_api.go
Normal file
@ -0,0 +1,9 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func getPublisher(c *fiber.Ctx) error {
|
||||
panic("TODO")
|
||||
}
|
@ -3,8 +3,11 @@ package api
|
||||
import (
|
||||
"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"
|
||||
"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"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
@ -51,10 +54,10 @@ func listRecommendationNews(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
func listRecommendationFriends(c *fiber.Ctx) error {
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
if err := sec.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
user := c.Locals("user").(authm.Account)
|
||||
|
||||
take := c.QueryInt("take", 0)
|
||||
offset := c.QueryInt("offset", 0)
|
||||
@ -66,9 +69,9 @@ func listRecommendationFriends(c *fiber.Ctx) error {
|
||||
return err
|
||||
}
|
||||
|
||||
friends, _ := services.ListAccountFriends(user)
|
||||
friendList := lo.Map(friends, func(item models.Account, index int) uint {
|
||||
return item.ID
|
||||
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("author_id IN ?", friendList)
|
@ -23,7 +23,7 @@ func listPostReplies(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
if len(c.Query("author")) > 0 {
|
||||
var author models.Account
|
||||
var author models.Publisher
|
||||
if err := database.C.Where(&hyper.BaseUser{Name: c.Query("author")}).First(&author).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||
}
|
||||
@ -66,7 +66,7 @@ func listPostFeaturedReply(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
if len(c.Query("author")) > 0 {
|
||||
var author models.Account
|
||||
var author models.Publisher
|
||||
if err := database.C.Where(&hyper.BaseUser{Name: c.Query("author")}).First(&author).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||
}
|
@ -2,14 +2,17 @@ package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/cruda"
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
|
||||
"git.solsynth.dev/hypernet/passport/pkg/authkit"
|
||||
authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"git.solsynth.dev/hydrogen/dealer/pkg/hyper"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/database"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/gap"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/http/exts"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/models"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/server/exts"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/services"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
@ -17,12 +20,13 @@ import (
|
||||
)
|
||||
|
||||
func createStory(c *fiber.Ctx) error {
|
||||
if err := gap.H.EnsureGrantedPerm(c, "CreatePosts", true); err != nil {
|
||||
if err := sec.EnsureGrantedPerm(c, "CreatePosts", true); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
user := c.Locals("user").(authm.Account)
|
||||
|
||||
var data struct {
|
||||
Publisher uint `json:"publisher"`
|
||||
Alias *string `json:"alias"`
|
||||
Title *string `json:"title"`
|
||||
Content string `json:"content" validate:"required,max=4096"`
|
||||
@ -37,7 +41,6 @@ func createStory(c *fiber.Ctx) error {
|
||||
InvisibleUsers []uint `json:"invisible_users_list"`
|
||||
Visibility *int8 `json:"visibility"`
|
||||
IsDraft bool `json:"is_draft"`
|
||||
RealmAlias *string `json:"realm"`
|
||||
ReplyTo *uint `json:"reply_to"`
|
||||
RepostTo *uint `json:"repost_to"`
|
||||
}
|
||||
@ -46,6 +49,11 @@ func createStory(c *fiber.Ctx) error {
|
||||
return err
|
||||
}
|
||||
|
||||
publisher, err := services.GetPublisher(data.Publisher, user.ID)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
body := models.PostStoryBody{
|
||||
Thumbnail: data.Thumbnail,
|
||||
Title: data.Title,
|
||||
@ -70,7 +78,7 @@ func createStory(c *fiber.Ctx) error {
|
||||
IsDraft: data.IsDraft,
|
||||
VisibleUsers: data.VisibleUsers,
|
||||
InvisibleUsers: data.InvisibleUsers,
|
||||
AuthorID: user.ID,
|
||||
PublisherID: publisher.ID,
|
||||
}
|
||||
|
||||
if item.PublishedAt == nil {
|
||||
@ -100,27 +108,15 @@ func createStory(c *fiber.Ctx) error {
|
||||
}
|
||||
}
|
||||
|
||||
if data.RealmAlias != nil {
|
||||
if realm, err := services.GetRealmWithAlias(*data.RealmAlias); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
} else if _, err = services.GetRealmMember(realm.ID, user.ID); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to post in the realm, access denied: %v", err))
|
||||
} else {
|
||||
item.RealmID = &realm.ID
|
||||
item.Realm = &realm
|
||||
}
|
||||
}
|
||||
|
||||
item, err := services.NewPost(user, item)
|
||||
item, err = services.NewPost(publisher, item)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
} else {
|
||||
_ = gap.H.RecordAuditLog(
|
||||
user.ID,
|
||||
_ = authkit.AddEventExt(
|
||||
gap.Nx,
|
||||
"posts.new",
|
||||
strconv.Itoa(int(item.ID)),
|
||||
c.IP(),
|
||||
c.Get(fiber.HeaderUserAgent),
|
||||
c,
|
||||
)
|
||||
}
|
||||
|
||||
@ -129,12 +125,13 @@ func createStory(c *fiber.Ctx) error {
|
||||
|
||||
func editStory(c *fiber.Ctx) error {
|
||||
id, _ := c.ParamsInt("postId", 0)
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
if err := sec.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
user := c.Locals("user").(authm.Account)
|
||||
|
||||
var data struct {
|
||||
Publisher uint `json:"publisher"`
|
||||
Alias *string `json:"alias"`
|
||||
Title *string `json:"title"`
|
||||
Content string `json:"content" validate:"required,max=4096"`
|
||||
@ -149,17 +146,21 @@ func editStory(c *fiber.Ctx) error {
|
||||
InvisibleUsers []uint `json:"invisible_users_list"`
|
||||
Visibility *int8 `json:"visibility"`
|
||||
IsDraft bool `json:"is_draft"`
|
||||
RealmAlias *string `json:"realm"`
|
||||
}
|
||||
|
||||
if err := exts.BindAndValidate(c, &data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
publisher, err := services.GetPublisher(data.Publisher, user.ID)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
var item models.Post
|
||||
if err := database.C.Where(models.Post{
|
||||
BaseModel: hyper.BaseModel{ID: uint(id)},
|
||||
AuthorID: user.ID,
|
||||
BaseModel: cruda.BaseModel{ID: uint(id)},
|
||||
PublisherID: publisher.ID,
|
||||
}).First(&item).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||
}
|
||||
@ -199,33 +200,22 @@ func editStory(c *fiber.Ctx) error {
|
||||
item.IsDraft = data.IsDraft
|
||||
item.VisibleUsers = data.VisibleUsers
|
||||
item.InvisibleUsers = data.InvisibleUsers
|
||||
item.Author = user
|
||||
|
||||
// Preload publisher data
|
||||
item.Publisher = publisher
|
||||
|
||||
if data.Visibility != nil {
|
||||
item.Visibility = *data.Visibility
|
||||
}
|
||||
|
||||
if data.RealmAlias != nil {
|
||||
if realm, err := services.GetRealmWithAlias(*data.RealmAlias); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
} else if _, err = services.GetRealmMember(realm.ID, user.ID); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to post in the realm, access denied: %v", err))
|
||||
} else {
|
||||
item.RealmID = &realm.ID
|
||||
item.Realm = &realm
|
||||
}
|
||||
}
|
||||
|
||||
var err error
|
||||
if item, err = services.EditPost(item); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
} else {
|
||||
_ = gap.H.RecordAuditLog(
|
||||
user.ID,
|
||||
_ = authkit.AddEventExt(
|
||||
gap.Nx,
|
||||
"posts.edit",
|
||||
strconv.Itoa(int(item.ID)),
|
||||
c.IP(),
|
||||
c.Get(fiber.HeaderUserAgent),
|
||||
c,
|
||||
)
|
||||
}
|
||||
|
@ -2,19 +2,21 @@ package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
|
||||
"git.solsynth.dev/hypernet/passport/pkg/authkit"
|
||||
authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models"
|
||||
"strconv"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
func getSubscriptionOnUser(c *fiber.Ctx) error {
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
if err := sec.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
user := c.Locals("user").(authm.Account)
|
||||
|
||||
otherUserId, err := c.ParamsInt("userId", 0)
|
||||
otherUser, err := services.GetAccountWithID(uint(otherUserId))
|
||||
@ -33,10 +35,10 @@ func getSubscriptionOnUser(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
func getSubscriptionOnTag(c *fiber.Ctx) error {
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
if err := sec.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
user := c.Locals("user").(authm.Account)
|
||||
|
||||
tagId, err := c.ParamsInt("tagId", 0)
|
||||
tag, err := services.GetTagWithID(uint(tagId))
|
||||
@ -55,10 +57,10 @@ func getSubscriptionOnTag(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
func getSubscriptionOnCategory(c *fiber.Ctx) error {
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
if err := sec.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
user := c.Locals("user").(authm.Account)
|
||||
|
||||
categoryId, err := c.ParamsInt("categoryId", 0)
|
||||
category, err := services.GetCategoryWithID(uint(categoryId))
|
||||
@ -76,33 +78,11 @@ func getSubscriptionOnCategory(c *fiber.Ctx) error {
|
||||
return c.JSON(subscription)
|
||||
}
|
||||
|
||||
func getSubscriptionOnRealm(c *fiber.Ctx) error {
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
|
||||
realmId, err := c.ParamsInt("realmId", 0)
|
||||
realm, err := services.GetRealmWithID(uint(realmId))
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to get realm: %v", err))
|
||||
}
|
||||
|
||||
subscription, err := services.GetSubscriptionOnRealm(user, realm)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to get subscription: %v", err))
|
||||
} else if subscription == nil {
|
||||
return fiber.NewError(fiber.StatusNotFound, "subscription does not exist")
|
||||
}
|
||||
|
||||
return c.JSON(subscription)
|
||||
}
|
||||
|
||||
func subscribeToUser(c *fiber.Ctx) error {
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
if err := sec.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
user := c.Locals("user").(authm.Account)
|
||||
|
||||
otherUserId, err := c.ParamsInt("userId", 0)
|
||||
otherUser, err := services.GetAccountWithID(uint(otherUserId))
|
||||
@ -115,22 +95,21 @@ func subscribeToUser(c *fiber.Ctx) error {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to subscribe to user: %v", err))
|
||||
}
|
||||
|
||||
_ = gap.H.RecordAuditLog(
|
||||
user.ID,
|
||||
_ = authkit.AddEventExt(
|
||||
gap.Nx,
|
||||
"posts.subscribe.users",
|
||||
strconv.Itoa(int(otherUser.ID)),
|
||||
c.IP(),
|
||||
c.Get(fiber.HeaderUserAgent),
|
||||
c,
|
||||
)
|
||||
|
||||
return c.JSON(subscription)
|
||||
}
|
||||
|
||||
func subscribeToTag(c *fiber.Ctx) error {
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
if err := sec.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
user := c.Locals("user").(authm.Account)
|
||||
|
||||
tagId, err := c.ParamsInt("tagId", 0)
|
||||
tag, err := services.GetTagWithID(uint(tagId))
|
||||
@ -143,22 +122,21 @@ func subscribeToTag(c *fiber.Ctx) error {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to subscribe to tag: %v", err))
|
||||
}
|
||||
|
||||
_ = gap.H.RecordAuditLog(
|
||||
user.ID,
|
||||
_ = authkit.AddEventExt(
|
||||
gap.Nx,
|
||||
"posts.subscribe.tags",
|
||||
strconv.Itoa(int(tag.ID)),
|
||||
c.IP(),
|
||||
c.Get(fiber.HeaderUserAgent),
|
||||
c,
|
||||
)
|
||||
|
||||
return c.JSON(subscription)
|
||||
}
|
||||
|
||||
func subscribeToCategory(c *fiber.Ctx) error {
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
if err := sec.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
user := c.Locals("user").(authm.Account)
|
||||
|
||||
categoryId, err := c.ParamsInt("categoryId", 0)
|
||||
category, err := services.GetCategoryWithID(uint(categoryId))
|
||||
@ -171,50 +149,21 @@ func subscribeToCategory(c *fiber.Ctx) error {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to subscribe to category: %v", err))
|
||||
}
|
||||
|
||||
_ = gap.H.RecordAuditLog(
|
||||
user.ID,
|
||||
_ = authkit.AddEventExt(
|
||||
gap.Nx,
|
||||
"posts.subscribe.categories",
|
||||
strconv.Itoa(int(category.ID)),
|
||||
c.IP(),
|
||||
c.Get(fiber.HeaderUserAgent),
|
||||
)
|
||||
|
||||
return c.JSON(subscription)
|
||||
}
|
||||
|
||||
func subscribeToRealm(c *fiber.Ctx) error {
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
|
||||
realmId, err := c.ParamsInt("realmId", 0)
|
||||
realm, err := services.GetRealmWithID(uint(realmId))
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to get realm: %v", err))
|
||||
}
|
||||
|
||||
subscription, err := services.SubscribeToRealm(user, realm)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to subscribe to realm: %v", err))
|
||||
}
|
||||
|
||||
_ = gap.H.RecordAuditLog(
|
||||
user.ID,
|
||||
"posts.subscribe.realms",
|
||||
strconv.Itoa(int(realm.ID)),
|
||||
c.IP(),
|
||||
c.Get(fiber.HeaderUserAgent),
|
||||
c,
|
||||
)
|
||||
|
||||
return c.JSON(subscription)
|
||||
}
|
||||
|
||||
func unsubscribeFromUser(c *fiber.Ctx) error {
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
if err := sec.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
user := c.Locals("user").(authm.Account)
|
||||
|
||||
otherUserId, err := c.ParamsInt("userId", 0)
|
||||
otherUser, err := services.GetAccountWithID(uint(otherUserId))
|
||||
@ -227,22 +176,21 @@ func unsubscribeFromUser(c *fiber.Ctx) error {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to unsubscribe from user: %v", err))
|
||||
}
|
||||
|
||||
_ = gap.H.RecordAuditLog(
|
||||
user.ID,
|
||||
_ = authkit.AddEventExt(
|
||||
gap.Nx,
|
||||
"posts.unsubscribe.users",
|
||||
strconv.Itoa(int(otherUser.ID)),
|
||||
c.IP(),
|
||||
c.Get(fiber.HeaderUserAgent),
|
||||
c,
|
||||
)
|
||||
|
||||
return c.SendStatus(fiber.StatusOK)
|
||||
}
|
||||
|
||||
func unsubscribeFromTag(c *fiber.Ctx) error {
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
if err := sec.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
user := c.Locals("user").(authm.Account)
|
||||
|
||||
tagId, err := c.ParamsInt("tagId", 0)
|
||||
tag, err := services.GetTagWithID(uint(tagId))
|
||||
@ -255,22 +203,21 @@ func unsubscribeFromTag(c *fiber.Ctx) error {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to unsubscribe from tag: %v", err))
|
||||
}
|
||||
|
||||
_ = gap.H.RecordAuditLog(
|
||||
user.ID,
|
||||
_ = authkit.AddEventExt(
|
||||
gap.Nx,
|
||||
"posts.unsubscribe.tags",
|
||||
strconv.Itoa(int(tag.ID)),
|
||||
c.IP(),
|
||||
c.Get(fiber.HeaderUserAgent),
|
||||
c,
|
||||
)
|
||||
|
||||
return c.SendStatus(fiber.StatusOK)
|
||||
}
|
||||
|
||||
func unsubscribeFromCategory(c *fiber.Ctx) error {
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
if err := sec.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
user := c.Locals("user").(authm.Account)
|
||||
|
||||
categoryId, err := c.ParamsInt("categoryId", 0)
|
||||
category, err := services.GetCategoryWithID(uint(categoryId))
|
||||
@ -283,40 +230,11 @@ func unsubscribeFromCategory(c *fiber.Ctx) error {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to unsubscribe from category: %v", err))
|
||||
}
|
||||
|
||||
_ = gap.H.RecordAuditLog(
|
||||
user.ID,
|
||||
_ = authkit.AddEventExt(
|
||||
gap.Nx,
|
||||
"posts.unsubscribe.categories",
|
||||
strconv.Itoa(int(category.ID)),
|
||||
c.IP(),
|
||||
c.Get(fiber.HeaderUserAgent),
|
||||
)
|
||||
|
||||
return c.SendStatus(fiber.StatusOK)
|
||||
}
|
||||
|
||||
func unsubscribeFromRealm(c *fiber.Ctx) error {
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
|
||||
realmId, err := c.ParamsInt("realmId", 0)
|
||||
realm, err := services.GetRealmWithID(uint(realmId))
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to get realm: %v", err))
|
||||
}
|
||||
|
||||
err = services.UnsubscribeFromRealm(user, realm)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to unsubscribe from realm: %v", err))
|
||||
}
|
||||
|
||||
_ = gap.H.RecordAuditLog(
|
||||
user.ID,
|
||||
"posts.unsubscribe.realms",
|
||||
strconv.Itoa(int(realm.ID)),
|
||||
c.IP(),
|
||||
c.Get(fiber.HeaderUserAgent),
|
||||
c,
|
||||
)
|
||||
|
||||
return c.SendStatus(fiber.StatusOK)
|
31
pkg/internal/http/api/users_api.go
Normal file
31
pkg/internal/http/api/users_api.go
Normal file
@ -0,0 +1,31 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"git.solsynth.dev/hydrogen/dealer/pkg/hyper"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/database"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/models"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/services"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func listUserPinnedPost(c *fiber.Ctx) error {
|
||||
account := c.Params("account")
|
||||
|
||||
var user models.Publisher
|
||||
if err := database.C.
|
||||
Where(&hyper.BaseUser{Name: account}).
|
||||
First(&user).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
tx := services.FilterPostDraft(database.C)
|
||||
tx = tx.Where("author_id = ?", user.ID)
|
||||
tx = tx.Where("pinned_at IS NOT NULL")
|
||||
|
||||
items, err := services.ListPost(tx, 100, 0, "published_at DESC")
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(items)
|
||||
}
|
@ -1,40 +1,29 @@
|
||||
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"
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
|
||||
authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func getWhatsNew(c *fiber.Ctx) error {
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
if err := sec.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
user := c.Locals("user").(authm.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)
|
||||
|
||||
tx = tx.Where("id > ?", pivot)
|
||||
|
||||
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 {
|
@ -1,13 +1,11 @@
|
||||
package server
|
||||
package http
|
||||
|
||||
import (
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
|
||||
"git.solsynth.dev/hypernet/passport/pkg/authkit"
|
||||
"strings"
|
||||
|
||||
"git.solsynth.dev/hydrogen/dealer/pkg/hyper"
|
||||
"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/server/api"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/http/api"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/middleware/cors"
|
||||
"github.com/gofiber/fiber/v2/middleware/idempotency"
|
||||
@ -17,10 +15,14 @@ import (
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var app *fiber.App
|
||||
var IReader *sec.InternalTokenReader
|
||||
|
||||
func NewServer() {
|
||||
app = fiber.New(fiber.Config{
|
||||
type App struct {
|
||||
app *fiber.App
|
||||
}
|
||||
|
||||
func NewServer() *App {
|
||||
app := fiber.New(fiber.Config{
|
||||
DisableStartupMessage: true,
|
||||
EnableIPValidation: true,
|
||||
ServerHeader: "Hydrogen.Interactive",
|
||||
@ -54,23 +56,18 @@ func NewServer() {
|
||||
Output: log.Logger,
|
||||
}))
|
||||
|
||||
tablePrefix := viper.GetString("database.prefix")
|
||||
app.Use(gap.H.AuthMiddleware)
|
||||
app.Use(hyper.LinkAccountMiddleware[models.Account](
|
||||
database.C,
|
||||
tablePrefix+"accounts",
|
||||
func(u hyper.BaseUser) models.Account {
|
||||
return models.Account{
|
||||
BaseUser: u,
|
||||
}
|
||||
},
|
||||
))
|
||||
app.Use(sec.ContextMiddleware(IReader))
|
||||
app.Use(authkit.GetAccountFromUserInfo)
|
||||
|
||||
api.MapAPIs(app, "/api")
|
||||
}
|
||||
|
||||
func Listen() {
|
||||
if err := app.Listen(viper.GetString("bind")); err != nil {
|
||||
log.Fatal().Err(err).Msg("An error occurred when starting server...")
|
||||
return &App{
|
||||
app: app,
|
||||
}
|
||||
}
|
||||
|
||||
func (v *App) Listen() {
|
||||
if err := v.app.Listen(viper.GetString("bind")); err != nil {
|
||||
log.Fatal().Err(err).Msg("An error occurred when starting http...")
|
||||
}
|
||||
}
|
@ -1,14 +0,0 @@
|
||||
package models
|
||||
|
||||
import "git.solsynth.dev/hydrogen/dealer/pkg/hyper"
|
||||
|
||||
type Account struct {
|
||||
hyper.BaseUser
|
||||
|
||||
Posts []Post `json:"posts" gorm:"foreignKey:AuthorID"`
|
||||
Reactions []Reaction `json:"reactions"`
|
||||
Subscriptions []Subscription `json:"subscriptions" gorm:"foreginKey:FollowerID"`
|
||||
|
||||
TotalUpvote int `json:"total_upvote"`
|
||||
TotalDownvote int `json:"total_downvote"`
|
||||
}
|
@ -1,9 +1,9 @@
|
||||
package models
|
||||
|
||||
import "git.solsynth.dev/hydrogen/dealer/pkg/hyper"
|
||||
import "git.solsynth.dev/hypernet/nexus/pkg/nex/cruda"
|
||||
|
||||
type Tag struct {
|
||||
hyper.BaseModel
|
||||
cruda.BaseModel
|
||||
|
||||
Alias string `json:"alias" gorm:"uniqueIndex" validate:"lowercase"`
|
||||
Name string `json:"name"`
|
||||
@ -12,7 +12,7 @@ type Tag struct {
|
||||
}
|
||||
|
||||
type Category struct {
|
||||
hyper.BaseModel
|
||||
cruda.BaseModel
|
||||
|
||||
Alias string `json:"alias" gorm:"uniqueIndex" validate:"lowercase,alphanum"`
|
||||
Name string `json:"name"`
|
||||
|
@ -1,9 +1,10 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/cruda"
|
||||
authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models"
|
||||
"time"
|
||||
|
||||
"git.solsynth.dev/hydrogen/dealer/pkg/hyper"
|
||||
"gorm.io/datatypes"
|
||||
)
|
||||
|
||||
@ -23,23 +24,21 @@ const (
|
||||
)
|
||||
|
||||
type Post struct {
|
||||
hyper.BaseModel
|
||||
cruda.BaseModel
|
||||
|
||||
Type string `json:"type"`
|
||||
Body datatypes.JSONMap `json:"body" gorm:"index:,type:gin"`
|
||||
Language string `json:"language"`
|
||||
Alias *string `json:"alias"`
|
||||
AreaAlias *string `json:"area_alias"`
|
||||
Tags []Tag `json:"tags" gorm:"many2many:post_tags"`
|
||||
Categories []Category `json:"categories" gorm:"many2many:post_categories"`
|
||||
Reactions []Reaction `json:"reactions"`
|
||||
Replies []Post `json:"replies" gorm:"foreignKey:ReplyID"`
|
||||
ReplyID *uint `json:"reply_id"`
|
||||
RepostID *uint `json:"repost_id"`
|
||||
RealmID *uint `json:"realm_id"`
|
||||
ReplyTo *Post `json:"reply_to" gorm:"foreignKey:ReplyID"`
|
||||
RepostTo *Post `json:"repost_to" gorm:"foreignKey:RepostID"`
|
||||
Realm *Realm `json:"realm"`
|
||||
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"`
|
||||
Tags []Tag `json:"tags" gorm:"many2many:post_tags"`
|
||||
Categories []Category `json:"categories" gorm:"many2many:post_categories"`
|
||||
Reactions []Reaction `json:"reactions"`
|
||||
Replies []Post `json:"replies" gorm:"foreignKey:ReplyID"`
|
||||
ReplyID *uint `json:"reply_id"`
|
||||
RepostID *uint `json:"repost_id"`
|
||||
ReplyTo *Post `json:"reply_to" gorm:"foreignKey:ReplyID"`
|
||||
RepostTo *Post `json:"repost_to" gorm:"foreignKey:RepostID"`
|
||||
|
||||
VisibleUsers datatypes.JSONSlice[uint] `json:"visible_users_list"`
|
||||
InvisibleUsers datatypes.JSONSlice[uint] `json:"invisible_users_list"`
|
||||
@ -56,8 +55,11 @@ type Post struct {
|
||||
TotalUpvote int `json:"total_upvote"`
|
||||
TotalDownvote int `json:"total_downvote"`
|
||||
|
||||
AuthorID uint `json:"author_id"`
|
||||
Author Account `json:"author"`
|
||||
RealmID *uint `json:"realm_id"`
|
||||
Realm *authm.Realm `json:"realm" gorm:"-"`
|
||||
|
||||
PublisherID uint `json:"publisher_id"`
|
||||
Publisher Publisher `json:"publisher"`
|
||||
|
||||
Metric PostMetric `json:"metric" gorm:"-"`
|
||||
}
|
||||
|
31
pkg/internal/models/publishers.go
Normal file
31
pkg/internal/models/publishers.go
Normal file
@ -0,0 +1,31 @@
|
||||
package models
|
||||
|
||||
import "git.solsynth.dev/hypernet/nexus/pkg/nex/cruda"
|
||||
|
||||
const (
|
||||
PublisherTypePersonal = iota
|
||||
PublisherTypeOrganization
|
||||
PublisherTypeAnonymous
|
||||
)
|
||||
|
||||
type Publisher struct {
|
||||
cruda.BaseModel
|
||||
|
||||
Type int `json:"type"`
|
||||
|
||||
Name string `json:"name" gorm:"uniqueIndex"`
|
||||
Nick string `json:"nick"`
|
||||
Description string `json:"description"`
|
||||
Avatar string `json:"avatar"`
|
||||
Banner string `json:"banner"`
|
||||
|
||||
Posts []Post `json:"posts" gorm:"foreignKey:AuthorID"`
|
||||
Reactions []Reaction `json:"reactions"`
|
||||
Subscriptions []Subscription `json:"subscriptions" gorm:"foreginKey:FollowerID"`
|
||||
|
||||
TotalUpvote int `json:"total_upvote"`
|
||||
TotalDownvote int `json:"total_downvote"`
|
||||
|
||||
RealmID *uint `json:"realm_id"`
|
||||
AccountID *uint `json:"account_id"`
|
||||
}
|
@ -20,6 +20,6 @@ type Reaction struct {
|
||||
Symbol string `json:"symbol"`
|
||||
Attitude ReactionAttitude `json:"attitude"`
|
||||
|
||||
PostID *uint `json:"post_id"`
|
||||
AccountID uint `json:"account_id"`
|
||||
PostID uint `json:"post_id"`
|
||||
AccountID uint `json:"account_id"`
|
||||
}
|
||||
|
@ -1,9 +0,0 @@
|
||||
package models
|
||||
|
||||
import "git.solsynth.dev/hydrogen/dealer/pkg/hyper"
|
||||
|
||||
type Realm struct {
|
||||
hyper.BaseRealm
|
||||
|
||||
Posts []Post `json:"posts"`
|
||||
}
|
@ -1,18 +1,16 @@
|
||||
package models
|
||||
|
||||
import "git.solsynth.dev/hydrogen/dealer/pkg/hyper"
|
||||
import "git.solsynth.dev/hypernet/nexus/pkg/nex/cruda"
|
||||
|
||||
type Subscription struct {
|
||||
hyper.BaseModel
|
||||
cruda.BaseModel
|
||||
|
||||
FollowerID uint `json:"follower_id"`
|
||||
Follower Account `json:"follower"`
|
||||
AccountID *uint `json:"account_id,omitempty"`
|
||||
Account *Account `json:"account,omitempty"`
|
||||
TagID *uint `json:"tag_id,omitempty"`
|
||||
Tag Tag `json:"tag,omitempty"`
|
||||
CategoryID *uint `json:"category_id,omitempty"`
|
||||
Category Category `json:"category,omitempty"`
|
||||
RealmID *uint `json:"realm_id,omitempty"`
|
||||
Realm Realm `json:"realm,omitempty"`
|
||||
FollowerID uint `json:"follower_id"`
|
||||
Follower Publisher `json:"follower"`
|
||||
AccountID *uint `json:"account_id,omitempty"`
|
||||
Account *Publisher `json:"account,omitempty"`
|
||||
TagID *uint `json:"tag_id,omitempty"`
|
||||
Tag Tag `json:"tag,omitempty"`
|
||||
CategoryID *uint `json:"category_id,omitempty"`
|
||||
Category Category `json:"category,omitempty"`
|
||||
}
|
||||
|
@ -1,15 +0,0 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/services"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func getPublisher(c *fiber.Ctx) error {
|
||||
alias := c.Params("name")
|
||||
if out, err := services.GetPublisher(alias); err != nil {
|
||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||
} else {
|
||||
return c.JSON(out)
|
||||
}
|
||||
}
|
@ -1,61 +0,0 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"git.solsynth.dev/hydrogen/dealer/pkg/hyper"
|
||||
"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"
|
||||
)
|
||||
|
||||
func getUserinfo(c *fiber.Ctx) error {
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
|
||||
var data models.Account
|
||||
if err := database.C.
|
||||
Where(&hyper.BaseModel{ID: user.ID}).
|
||||
First(&data).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(data)
|
||||
}
|
||||
|
||||
func getOthersInfo(c *fiber.Ctx) error {
|
||||
account := c.Params("account")
|
||||
|
||||
var data models.Account
|
||||
if err := database.C.
|
||||
Where(&hyper.BaseUser{Name: account}).
|
||||
First(&data).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(data)
|
||||
}
|
||||
|
||||
func listOthersPinnedPost(c *fiber.Ctx) error {
|
||||
account := c.Params("account")
|
||||
|
||||
var user models.Account
|
||||
if err := database.C.
|
||||
Where(&hyper.BaseUser{Name: account}).
|
||||
First(&user).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
tx := services.FilterPostDraft(database.C)
|
||||
tx = tx.Where("author_id = ?", user.ID)
|
||||
tx = tx.Where("pinned_at IS NOT NULL")
|
||||
|
||||
items, err := services.ListPost(tx, 100, 0, "published_at DESC")
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(items)
|
||||
}
|
@ -1,84 +1,24 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.solsynth.dev/hydrogen/dealer/pkg/hyper"
|
||||
"git.solsynth.dev/hydrogen/dealer/pkg/proto"
|
||||
"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/hypernet/passport/pkg/authkit"
|
||||
"git.solsynth.dev/hypernet/pusher/pkg/pushkit"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
func GetAccountWithID(id uint) (models.Account, error) {
|
||||
var account models.Account
|
||||
func GetAccountWithID(id uint) (models.Publisher, error) {
|
||||
var account models.Publisher
|
||||
if err := database.C.Where("id = ?", id).First(&account).Error; err != nil {
|
||||
return account, fmt.Errorf("unable to get account by id: %v", err)
|
||||
}
|
||||
return account, nil
|
||||
}
|
||||
|
||||
func ListAccountFriends(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 friends: %v", err)
|
||||
}
|
||||
result, err := proto.NewAuthClient(pc).ListUserFriends(ctx, &proto.ListUserRelativeRequest{
|
||||
UserId: uint64(user.ID),
|
||||
IsRelated: true,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to listing account friends: %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 account friends: %v", err)
|
||||
}
|
||||
|
||||
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.Publisher, isUpvote bool, delta int) error {
|
||||
if isUpvote {
|
||||
user.TotalUpvote += delta
|
||||
} else {
|
||||
@ -88,32 +28,29 @@ func ModifyPosterVoteCount(user models.Account, isUpvote bool, delta int) error
|
||||
return database.C.Save(&user).Error
|
||||
}
|
||||
|
||||
func NotifyPosterAccount(user models.Account, post models.Post, title, body string, subtitle *string) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||
defer cancel()
|
||||
|
||||
pc, err := gap.H.GetServiceGrpcConn(hyper.ServiceTypeAuthProvider)
|
||||
if err != nil {
|
||||
return err
|
||||
func NotifyPosterAccount(pub models.Publisher, post models.Post, title, body string, subtitle ...string) error {
|
||||
if pub.AccountID == nil {
|
||||
return nil
|
||||
}
|
||||
_, err = proto.NewNotifierClient(pc).NotifyUser(ctx, &proto.NotifyUserRequest{
|
||||
UserId: uint64(user.ID),
|
||||
Notify: &proto.NotifyRequest{
|
||||
Topic: "interactive.feedback",
|
||||
Title: title,
|
||||
Subtitle: subtitle,
|
||||
Body: body,
|
||||
Metadata: hyper.EncodeMap(map[string]any{
|
||||
"related_post": TruncatePostContent(post),
|
||||
}),
|
||||
IsRealtime: false,
|
||||
IsForcePush: true,
|
||||
|
||||
if len(subtitle) == 0 {
|
||||
subtitle = append(subtitle, "")
|
||||
}
|
||||
|
||||
err := authkit.NotifyUser(gap.Nx, uint64(*pub.AccountID), pushkit.Notification{
|
||||
Topic: "interactive.feedback",
|
||||
Title: title,
|
||||
Subtitle: subtitle[0],
|
||||
Body: body,
|
||||
Priority: 4,
|
||||
Metadata: map[string]any{
|
||||
"related_post": TruncatePostContent(post),
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Msg("An error occurred when notify account...")
|
||||
} else {
|
||||
log.Debug().Uint("uid", user.ID).Msg("Notified account.")
|
||||
log.Debug().Uint("uid", pub.ID).Msg("Notified account.")
|
||||
}
|
||||
|
||||
return err
|
||||
|
@ -2,9 +2,9 @@ package services
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/cruda"
|
||||
"strings"
|
||||
|
||||
"git.solsynth.dev/hydrogen/dealer/pkg/hyper"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/database"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/models"
|
||||
"gorm.io/gorm"
|
||||
@ -28,7 +28,7 @@ func GetCategory(alias string) (models.Category, error) {
|
||||
func GetCategoryWithID(id uint) (models.Category, error) {
|
||||
var category models.Category
|
||||
if err := database.C.Where(models.Category{
|
||||
BaseModel: hyper.BaseModel{ID: id},
|
||||
BaseModel: cruda.BaseModel{ID: id},
|
||||
}).First(&category).Error; err != nil {
|
||||
return category, err
|
||||
}
|
||||
@ -64,7 +64,7 @@ func DeleteCategory(category models.Category) error {
|
||||
func GetTagWithID(id uint) (models.Tag, error) {
|
||||
var tag models.Tag
|
||||
if err := database.C.Where(models.Tag{
|
||||
BaseModel: hyper.BaseModel{ID: id},
|
||||
BaseModel: cruda.BaseModel{ID: id},
|
||||
}).First(&tag).Error; err != nil {
|
||||
return tag, err
|
||||
}
|
||||
|
@ -3,6 +3,10 @@ package services
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/gap"
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/proto"
|
||||
"git.solsynth.dev/hypernet/passport/pkg/authkit"
|
||||
authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"time"
|
||||
@ -11,12 +15,11 @@ import (
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/models"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/samber/lo"
|
||||
"github.com/spf13/viper"
|
||||
"gorm.io/datatypes"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func FilterPostWithUserContext(tx *gorm.DB, user *models.Account) *gorm.DB {
|
||||
func FilterPostWithUserContext(tx *gorm.DB, user *authm.Account) *gorm.DB {
|
||||
if user == nil {
|
||||
return tx.Where("visibility = ?", models.PostVisibilityAll)
|
||||
}
|
||||
@ -28,13 +31,13 @@ func FilterPostWithUserContext(tx *gorm.DB, user *models.Account) *gorm.DB {
|
||||
NoneVisibility = models.PostVisibilityNone
|
||||
)
|
||||
|
||||
friends, _ := ListAccountFriends(*user)
|
||||
allowlist := lo.Map(friends, func(item models.Account, index int) uint {
|
||||
return item.ID
|
||||
friends, _ := authkit.ListRelative(gap.Nx, user.ID, int32(authm.RelationshipFriend), true)
|
||||
allowlist := lo.Map(friends, func(item *proto.UserInfo, index int) uint {
|
||||
return uint(item.GetId())
|
||||
})
|
||||
blocked, _ := ListAccountBlockedUsers(*user)
|
||||
blocklist := lo.Map(blocked, func(item models.Account, index int) uint {
|
||||
return item.ID
|
||||
blocked, _ := authkit.ListRelative(gap.Nx, user.ID, int32(authm.RelationshipBlocked), true)
|
||||
blocklist := lo.Map(blocked, func(item *proto.UserInfo, index int) uint {
|
||||
return uint(item.GetId())
|
||||
})
|
||||
|
||||
tx = tx.Where(
|
||||
@ -54,17 +57,15 @@ func FilterPostWithUserContext(tx *gorm.DB, user *models.Account) *gorm.DB {
|
||||
}
|
||||
|
||||
func FilterPostWithCategory(tx *gorm.DB, alias string) *gorm.DB {
|
||||
prefix := viper.GetString("database.prefix")
|
||||
return tx.Joins(fmt.Sprintf("JOIN %spost_categories ON %sposts.id = %spost_categories.post_id", prefix, prefix, prefix)).
|
||||
Joins(fmt.Sprintf("JOIN %scategories ON %scategories.id = %spost_categories.category_id", prefix, prefix, prefix)).
|
||||
Where(fmt.Sprintf("%scategories.alias = ?", prefix), alias)
|
||||
return tx.Joins("JOIN post_categories ON posts.id = post_categories.post_id").
|
||||
Joins("JOIN categories ON categories.id = post_categories.category_id").
|
||||
Where("categories.alias = ?", alias)
|
||||
}
|
||||
|
||||
func FilterPostWithTag(tx *gorm.DB, alias string) *gorm.DB {
|
||||
prefix := viper.GetString("database.prefix")
|
||||
return tx.Joins(fmt.Sprintf("JOIN %spost_tags ON %sposts.id = %spost_tags.post_id", prefix, prefix, prefix)).
|
||||
Joins(fmt.Sprintf("JOIN %stags ON %stags.id = %spost_tags.tag_id", prefix, prefix, prefix)).
|
||||
Where(fmt.Sprintf("%stags.alias = ?", prefix), alias)
|
||||
return tx.Joins("JOIN post_tags ON posts.id = post_tags.post_id").
|
||||
Joins("JOIN tags ON tags.id = post_tags.tag_id").
|
||||
Where("tags.alias = ?", alias)
|
||||
}
|
||||
|
||||
func FilterPostWithRealm(tx *gorm.DB, id uint) *gorm.DB {
|
||||
@ -289,7 +290,7 @@ func EnsurePostCategoriesAndTags(item models.Post) (models.Post, error) {
|
||||
return item, nil
|
||||
}
|
||||
|
||||
func NewPost(user models.Account, item models.Post) (models.Post, error) {
|
||||
func NewPost(user models.Publisher, item models.Post) (models.Post, error) {
|
||||
if item.Alias != nil && len(*item.Alias) == 0 {
|
||||
item.Alias = nil
|
||||
}
|
||||
@ -302,9 +303,9 @@ func NewPost(user models.Account, item models.Post) (models.Post, error) {
|
||||
}
|
||||
|
||||
if item.Realm != nil {
|
||||
item.AreaAlias = &item.Realm.Alias
|
||||
item.AliasPrefix = &item.Realm.Alias
|
||||
} else {
|
||||
item.AreaAlias = &user.Name
|
||||
item.AliasPrefix = &user.Name
|
||||
}
|
||||
|
||||
log.Debug().Any("body", item.Body).Msg("Posting a post...")
|
||||
@ -316,16 +317,6 @@ func NewPost(user models.Account, item models.Post) (models.Post, error) {
|
||||
return item, err
|
||||
}
|
||||
|
||||
if item.RealmID != nil {
|
||||
log.Debug().Uint("id", *item.RealmID).Msg("Looking for post author realm...")
|
||||
member, err := GetRealmMember(*item.RealmID, user.ID)
|
||||
if err != nil {
|
||||
return item, fmt.Errorf("you aren't a part of that realm: %v", err)
|
||||
} else if !item.Realm.IsCommunity && member.PowerLevel < 25 {
|
||||
return item, fmt.Errorf("you need has power level above 25 of a realm or in a community realm to post")
|
||||
}
|
||||
}
|
||||
|
||||
log.Debug().Msg("Saving post record into database...")
|
||||
if err := database.C.Save(&item).Error; err != nil {
|
||||
return item, err
|
||||
@ -338,14 +329,14 @@ func NewPost(user models.Account, item models.Post) (models.Post, error) {
|
||||
Where("id = ?", item.ReplyID).
|
||||
Preload("Author").
|
||||
First(&op).Error; err == nil {
|
||||
if op.Author.ID != user.ID {
|
||||
log.Debug().Uint("user", op.AuthorID).Msg("Notifying the original poster their post got replied...")
|
||||
if op.Publisher.AccountID != nil && op.Publisher.ID != user.ID {
|
||||
log.Debug().Uint("user", *op.Publisher.AccountID).Msg("Notifying the original poster their post got replied...")
|
||||
err = NotifyPosterAccount(
|
||||
op.Author,
|
||||
op.Publisher,
|
||||
op,
|
||||
"Post got replied",
|
||||
fmt.Sprintf("%s (%s) replied your post (#%d).", user.Nick, user.Name, op.ID),
|
||||
lo.ToPtr(fmt.Sprintf("%s replied you", user.Nick)),
|
||||
fmt.Sprintf("%s replied you", user.Nick),
|
||||
)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("An error occurred when notifying user...")
|
||||
@ -392,9 +383,9 @@ func EditPost(item models.Post) (models.Post, error) {
|
||||
}
|
||||
|
||||
if item.Realm != nil {
|
||||
item.AreaAlias = &item.Realm.Alias
|
||||
item.AliasPrefix = &item.Realm.Alias
|
||||
} else {
|
||||
item.AreaAlias = &item.Author.Name
|
||||
item.AliasPrefix = &item.Publisher.Name
|
||||
}
|
||||
|
||||
item, err := EnsurePostCategoriesAndTags(item)
|
||||
@ -411,7 +402,7 @@ func DeletePost(item models.Post) error {
|
||||
return database.C.Delete(&item).Error
|
||||
}
|
||||
|
||||
func ReactPost(user models.Account, reaction models.Reaction) (bool, models.Reaction, error) {
|
||||
func ReactPost(user authm.Account, reaction models.Reaction) (bool, models.Reaction, error) {
|
||||
var op models.Post
|
||||
if err := database.C.
|
||||
Where("id = ?", reaction.PostID).
|
||||
@ -422,13 +413,13 @@ func ReactPost(user models.Account, reaction models.Reaction) (bool, models.Reac
|
||||
|
||||
if err := database.C.Where(reaction).First(&reaction).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
if op.Author.ID != user.ID {
|
||||
if op.Publisher.AccountID != nil && *op.Publisher.AccountID != user.ID {
|
||||
err = NotifyPosterAccount(
|
||||
op.Author,
|
||||
op.Publisher,
|
||||
op,
|
||||
"Post got reacted",
|
||||
fmt.Sprintf("%s (%s) reacted your post a %s.", user.Nick, user.Name, reaction.Symbol),
|
||||
lo.ToPtr(fmt.Sprintf("%s reacted you", user.Nick)),
|
||||
fmt.Sprintf("%s reacted you", user.Nick),
|
||||
)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("An error occurred when notifying user...")
|
||||
@ -437,7 +428,7 @@ func ReactPost(user models.Account, reaction models.Reaction) (bool, models.Reac
|
||||
|
||||
err = database.C.Save(&reaction).Error
|
||||
if err == nil && reaction.Attitude != models.AttitudeNeutral {
|
||||
_ = ModifyPosterVoteCount(op.Author, reaction.Attitude == models.AttitudePositive, 1)
|
||||
_ = ModifyPosterVoteCount(op.Publisher, reaction.Attitude == models.AttitudePositive, 1)
|
||||
|
||||
if reaction.Attitude == models.AttitudePositive {
|
||||
op.TotalUpvote++
|
||||
@ -454,7 +445,7 @@ func ReactPost(user models.Account, reaction models.Reaction) (bool, models.Reac
|
||||
} else {
|
||||
err = database.C.Delete(&reaction).Error
|
||||
if err == nil && reaction.Attitude != models.AttitudeNeutral {
|
||||
_ = ModifyPosterVoteCount(op.Author, reaction.Attitude == models.AttitudePositive, -1)
|
||||
_ = ModifyPosterVoteCount(op.Publisher, reaction.Attitude == models.AttitudePositive, -1)
|
||||
|
||||
if reaction.Attitude == models.AttitudePositive {
|
||||
op.TotalUpvote--
|
||||
|
@ -1,26 +1,15 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/database"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/models"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func GetPublisher(alias string) (any, error) {
|
||||
realm, err := GetRealmWithAlias(alias)
|
||||
if err == nil {
|
||||
return fiber.Map{
|
||||
"type": "realm",
|
||||
"data": realm,
|
||||
}, nil
|
||||
func GetPublisher(id uint, userID uint) (models.Publisher, error) {
|
||||
var publisher models.Publisher
|
||||
if err := database.C.Where("id = ? AND account_id = ?", id, userID).First(&publisher).Error; err != nil {
|
||||
return publisher, fmt.Errorf("unable to get publisher: %v", err)
|
||||
}
|
||||
|
||||
var account models.Account
|
||||
if err = database.C.Where("name = ?", alias).First(&account).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return fiber.Map{
|
||||
"type": "account",
|
||||
"data": account,
|
||||
}, nil
|
||||
return publisher, nil
|
||||
}
|
||||
|
@ -1,67 +0,0 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.solsynth.dev/hydrogen/dealer/pkg/hyper"
|
||||
"git.solsynth.dev/hydrogen/dealer/pkg/proto"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/database"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/gap"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/models"
|
||||
"github.com/samber/lo"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
func GetRealmWithID(id uint) (models.Realm, error) {
|
||||
var realm models.Realm
|
||||
pc, err := gap.H.GetServiceGrpcConn(hyper.ServiceTypeAuthProvider)
|
||||
if err != nil {
|
||||
return realm, err
|
||||
}
|
||||
response, err := proto.NewRealmClient(pc).GetRealm(context.Background(), &proto.LookupRealmRequest{
|
||||
Id: lo.ToPtr(uint64(id)),
|
||||
})
|
||||
if err != nil {
|
||||
return realm, err
|
||||
}
|
||||
prefix := viper.GetString("database.prefix")
|
||||
rm, err := hyper.LinkRealm(database.C, prefix+"realms", response)
|
||||
return models.Realm{BaseRealm: rm}, err
|
||||
}
|
||||
|
||||
func GetRealmWithAlias(alias string) (models.Realm, error) {
|
||||
var realm models.Realm
|
||||
pc, err := gap.H.GetServiceGrpcConn(hyper.ServiceTypeAuthProvider)
|
||||
if err != nil {
|
||||
return realm, err
|
||||
}
|
||||
response, err := proto.NewRealmClient(pc).GetRealm(context.Background(), &proto.LookupRealmRequest{
|
||||
Alias: &alias,
|
||||
})
|
||||
if err != nil {
|
||||
return realm, err
|
||||
}
|
||||
prefix := viper.GetString("database.prefix")
|
||||
rm, err := hyper.LinkRealm(database.C, prefix+"realms", response)
|
||||
return models.Realm{BaseRealm: rm}, err
|
||||
}
|
||||
|
||||
func GetRealmMember(realmId uint, userId uint) (*proto.RealmMemberInfo, error) {
|
||||
var realm models.Realm
|
||||
if err := database.C.Where("id = ?", realmId).First(&realm).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pc, err := gap.H.GetServiceGrpcConn(hyper.ServiceTypeAuthProvider)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
response, err := proto.NewRealmClient(pc).GetRealmMember(context.Background(), &proto.RealmMemberLookupRequest{
|
||||
RealmId: lo.ToPtr(uint64(realm.ID)),
|
||||
UserId: lo.ToPtr(uint64(userId)),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return response, nil
|
||||
}
|
||||
}
|
@ -1,20 +1,18 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.solsynth.dev/hydrogen/dealer/pkg/hyper"
|
||||
"git.solsynth.dev/hydrogen/dealer/pkg/proto"
|
||||
"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/hypernet/passport/pkg/authkit"
|
||||
authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models"
|
||||
"git.solsynth.dev/hypernet/pusher/pkg/pushkit"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func GetSubscriptionOnUser(user models.Account, target models.Account) (*models.Subscription, error) {
|
||||
func GetSubscriptionOnUser(user authm.Account, target models.Publisher) (*models.Subscription, error) {
|
||||
var subscription models.Subscription
|
||||
if err := database.C.Where("follower_id = ? AND account_id = ?", user.ID, target.ID).First(&subscription).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
@ -25,7 +23,7 @@ func GetSubscriptionOnUser(user models.Account, target models.Account) (*models.
|
||||
return &subscription, nil
|
||||
}
|
||||
|
||||
func GetSubscriptionOnTag(user models.Account, target models.Tag) (*models.Subscription, error) {
|
||||
func GetSubscriptionOnTag(user authm.Account, target models.Tag) (*models.Subscription, error) {
|
||||
var subscription models.Subscription
|
||||
if err := database.C.Where("follower_id = ? AND tag_id = ?", user.ID, target.ID).First(&subscription).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
@ -36,7 +34,7 @@ func GetSubscriptionOnTag(user models.Account, target models.Tag) (*models.Subsc
|
||||
return &subscription, nil
|
||||
}
|
||||
|
||||
func GetSubscriptionOnCategory(user models.Account, target models.Category) (*models.Subscription, error) {
|
||||
func GetSubscriptionOnCategory(user authm.Account, target models.Category) (*models.Subscription, error) {
|
||||
var subscription models.Subscription
|
||||
if err := database.C.Where("follower_id = ? AND category_id = ?", user.ID, target.ID).First(&subscription).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
@ -47,18 +45,7 @@ func GetSubscriptionOnCategory(user models.Account, target models.Category) (*mo
|
||||
return &subscription, nil
|
||||
}
|
||||
|
||||
func GetSubscriptionOnRealm(user models.Account, target models.Realm) (*models.Subscription, error) {
|
||||
var subscription models.Subscription
|
||||
if err := database.C.Where("follower_id = ? AND realm_id = ?", user.ID, target.ID).First(&subscription).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, fmt.Errorf("unable to get subscription: %v", err)
|
||||
}
|
||||
return &subscription, nil
|
||||
}
|
||||
|
||||
func SubscribeToUser(user models.Account, target models.Account) (models.Subscription, error) {
|
||||
func SubscribeToUser(user authm.Account, target models.Publisher) (models.Subscription, error) {
|
||||
var subscription models.Subscription
|
||||
if err := database.C.Where("follower_id = ? AND account_id = ?", user.ID, target.ID).First(&subscription).Error; err != nil {
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
@ -75,7 +62,7 @@ func SubscribeToUser(user models.Account, target models.Account) (models.Subscri
|
||||
return subscription, err
|
||||
}
|
||||
|
||||
func SubscribeToTag(user models.Account, target models.Tag) (models.Subscription, error) {
|
||||
func SubscribeToTag(user authm.Account, target models.Tag) (models.Subscription, error) {
|
||||
var subscription models.Subscription
|
||||
if err := database.C.Where("follower_id = ? AND tag_id = ?", user.ID, target.ID).First(&subscription).Error; err != nil {
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
@ -92,7 +79,7 @@ func SubscribeToTag(user models.Account, target models.Tag) (models.Subscription
|
||||
return subscription, err
|
||||
}
|
||||
|
||||
func SubscribeToCategory(user models.Account, target models.Category) (models.Subscription, error) {
|
||||
func SubscribeToCategory(user authm.Account, target models.Category) (models.Subscription, error) {
|
||||
var subscription models.Subscription
|
||||
if err := database.C.Where("follower_id = ? AND category_id = ?", user.ID, target.ID).First(&subscription).Error; err != nil {
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
@ -109,24 +96,7 @@ func SubscribeToCategory(user models.Account, target models.Category) (models.Su
|
||||
return subscription, err
|
||||
}
|
||||
|
||||
func SubscribeToRealm(user models.Account, target models.Realm) (models.Subscription, error) {
|
||||
var subscription models.Subscription
|
||||
if err := database.C.Where("follower_id = ? AND realm_id = ?", user.ID, target.ID).First(&subscription).Error; err != nil {
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return subscription, fmt.Errorf("subscription already exists")
|
||||
}
|
||||
}
|
||||
|
||||
subscription = models.Subscription{
|
||||
FollowerID: user.ID,
|
||||
RealmID: &target.ID,
|
||||
}
|
||||
|
||||
err := database.C.Save(&subscription).Error
|
||||
return subscription, err
|
||||
}
|
||||
|
||||
func UnsubscribeFromUser(user models.Account, target models.Account) error {
|
||||
func UnsubscribeFromUser(user authm.Account, target models.Publisher) error {
|
||||
var subscription models.Subscription
|
||||
if err := database.C.Where("follower_id = ? AND account_id = ?", user.ID, target.ID).First(&subscription).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
@ -139,7 +109,7 @@ func UnsubscribeFromUser(user models.Account, target models.Account) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func UnsubscribeFromTag(user models.Account, target models.Tag) error {
|
||||
func UnsubscribeFromTag(user authm.Account, target models.Tag) error {
|
||||
var subscription models.Subscription
|
||||
if err := database.C.Where("follower_id = ? AND tag_id = ?", user.ID, target.ID).First(&subscription).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
@ -152,7 +122,7 @@ func UnsubscribeFromTag(user models.Account, target models.Tag) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func UnsubscribeFromCategory(user models.Account, target models.Category) error {
|
||||
func UnsubscribeFromCategory(user authm.Account, target models.Category) error {
|
||||
var subscription models.Subscription
|
||||
if err := database.C.Where("follower_id = ? AND category_id = ?", user.ID, target.ID).First(&subscription).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
@ -165,20 +135,7 @@ func UnsubscribeFromCategory(user models.Account, target models.Category) error
|
||||
return err
|
||||
}
|
||||
|
||||
func UnsubscribeFromRealm(user models.Account, target models.Realm) error {
|
||||
var subscription models.Subscription
|
||||
if err := database.C.Where("follower_id = ? AND realm_id = ?", user.ID, target.ID).First(&subscription).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return fmt.Errorf("subscription does not exist")
|
||||
}
|
||||
return fmt.Errorf("unable to check subscription is exists or not: %v", err)
|
||||
}
|
||||
|
||||
err := database.C.Delete(&subscription).Error
|
||||
return err
|
||||
}
|
||||
|
||||
func NotifyUserSubscription(poster models.Account, content string, title *string) error {
|
||||
func NotifyUserSubscription(poster models.Publisher, content string, title *string) error {
|
||||
var subscriptions []models.Subscription
|
||||
if err := database.C.Where("account_id = ?", poster.ID).Preload("Follower").Find(&subscriptions).Error; err != nil {
|
||||
return fmt.Errorf("unable to get subscriptions: %v", err)
|
||||
@ -197,30 +154,18 @@ func NotifyUserSubscription(poster models.Account, content string, title *string
|
||||
userIDs = append(userIDs, uint64(subscription.Follower.ID))
|
||||
}
|
||||
|
||||
pc, err := gap.H.GetServiceGrpcConn(hyper.ServiceTypeAuthProvider)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||
defer cancel()
|
||||
|
||||
_, err = proto.NewNotifierClient(pc).NotifyUserBatch(ctx, &proto.NotifyUserBatchRequest{
|
||||
UserId: userIDs,
|
||||
Notify: &proto.NotifyRequest{
|
||||
Topic: "interactive.subscription",
|
||||
Title: nTitle,
|
||||
Subtitle: &nSubtitle,
|
||||
Body: body,
|
||||
IsRealtime: false,
|
||||
IsForcePush: true,
|
||||
},
|
||||
err := authkit.NotifyUserBatch(gap.Nx, userIDs, pushkit.Notification{
|
||||
Topic: "interactive.subscription",
|
||||
Title: nTitle,
|
||||
Subtitle: nSubtitle,
|
||||
Body: body,
|
||||
Priority: 3,
|
||||
})
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func NotifyTagSubscription(poster models.Tag, og models.Account, content string, title *string) error {
|
||||
func NotifyTagSubscription(poster models.Tag, og models.Publisher, content string, title *string) error {
|
||||
var subscriptions []models.Subscription
|
||||
if err := database.C.Where("tag_id = ?", poster.ID).Preload("Follower").Find(&subscriptions).Error; err != nil {
|
||||
return fmt.Errorf("unable to get subscriptions: %v", err)
|
||||
@ -239,30 +184,18 @@ func NotifyTagSubscription(poster models.Tag, og models.Account, content string,
|
||||
userIDs = append(userIDs, uint64(subscription.Follower.ID))
|
||||
}
|
||||
|
||||
pc, err := gap.H.GetServiceGrpcConn(hyper.ServiceTypeAuthProvider)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||
defer cancel()
|
||||
|
||||
_, err = proto.NewNotifierClient(pc).NotifyUserBatch(ctx, &proto.NotifyUserBatchRequest{
|
||||
UserId: userIDs,
|
||||
Notify: &proto.NotifyRequest{
|
||||
Topic: "interactive.subscription",
|
||||
Title: nTitle,
|
||||
Subtitle: &nSubtitle,
|
||||
Body: body,
|
||||
IsRealtime: false,
|
||||
IsForcePush: true,
|
||||
},
|
||||
err := authkit.NotifyUserBatch(gap.Nx, userIDs, pushkit.Notification{
|
||||
Topic: "interactive.subscription",
|
||||
Title: nTitle,
|
||||
Subtitle: nSubtitle,
|
||||
Body: body,
|
||||
Priority: 3,
|
||||
})
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func NotifyCategorySubscription(poster models.Category, og models.Account, content string, title *string) error {
|
||||
func NotifyCategorySubscription(poster models.Category, og models.Publisher, content string, title *string) error {
|
||||
var subscriptions []models.Subscription
|
||||
if err := database.C.Where("category_id = ?", poster.ID).Preload("Follower").Find(&subscriptions).Error; err != nil {
|
||||
return fmt.Errorf("unable to get subscriptions: %v", err)
|
||||
@ -281,66 +214,12 @@ func NotifyCategorySubscription(poster models.Category, og models.Account, conte
|
||||
userIDs = append(userIDs, uint64(subscription.Follower.ID))
|
||||
}
|
||||
|
||||
pc, err := gap.H.GetServiceGrpcConn(hyper.ServiceTypeAuthProvider)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||
defer cancel()
|
||||
|
||||
_, err = proto.NewNotifierClient(pc).NotifyUserBatch(ctx, &proto.NotifyUserBatchRequest{
|
||||
UserId: userIDs,
|
||||
Notify: &proto.NotifyRequest{
|
||||
Topic: "interactive.subscription",
|
||||
Title: nTitle,
|
||||
Subtitle: &nSubtitle,
|
||||
Body: body,
|
||||
IsRealtime: false,
|
||||
IsForcePush: true,
|
||||
},
|
||||
})
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func NotifyRealmSubscription(poster models.Realm, og models.Account, content string, title *string) error {
|
||||
var subscriptions []models.Subscription
|
||||
if err := database.C.Where("realm_id = ?", poster.ID).Preload("Follower").Find(&subscriptions).Error; err != nil {
|
||||
return fmt.Errorf("unable to get subscriptions: %v", err)
|
||||
}
|
||||
|
||||
nTitle := fmt.Sprintf("New post in %s by %s (%s)", poster.Name, og.Nick, og.Name)
|
||||
nSubtitle := "From your subscription"
|
||||
|
||||
body := TruncatePostContentShort(content)
|
||||
if title != nil {
|
||||
body = fmt.Sprintf("%s\n%s", *title, body)
|
||||
}
|
||||
|
||||
userIDs := make([]uint64, 0, len(subscriptions))
|
||||
for _, subscription := range subscriptions {
|
||||
userIDs = append(userIDs, uint64(subscription.Follower.ID))
|
||||
}
|
||||
|
||||
pc, err := gap.H.GetServiceGrpcConn(hyper.ServiceTypeAuthProvider)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||
defer cancel()
|
||||
|
||||
_, err = proto.NewNotifierClient(pc).NotifyUserBatch(ctx, &proto.NotifyUserBatchRequest{
|
||||
UserId: userIDs,
|
||||
Notify: &proto.NotifyRequest{
|
||||
Topic: "interactive.subscription",
|
||||
Title: nTitle,
|
||||
Subtitle: &nSubtitle,
|
||||
Body: body,
|
||||
IsRealtime: false,
|
||||
IsForcePush: true,
|
||||
},
|
||||
err := authkit.NotifyUserBatch(gap.Nx, userIDs, pushkit.Notification{
|
||||
Topic: "interactive.subscription",
|
||||
Title: nTitle,
|
||||
Subtitle: nSubtitle,
|
||||
Body: body,
|
||||
Priority: 3,
|
||||
})
|
||||
|
||||
return err
|
||||
|
45
pkg/main.go
45
pkg/main.go
@ -1,15 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
pkg "git.solsynth.dev/hydrogen/interactive/pkg/internal"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/gap"
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
|
||||
"github.com/fatih/color"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
pkg "git.solsynth.dev/hydrogen/interactive/pkg/internal"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/database"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/gap"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/grpc"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/server"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/http"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/services"
|
||||
"github.com/robfig/cron/v3"
|
||||
"github.com/rs/zerolog"
|
||||
@ -23,6 +26,12 @@ func init() {
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Booting screen
|
||||
fmt.Println(color.YellowString(" ___ _ _ _\n|_ _|_ __ | |_ ___ _ __ __ _ ___| |_(_)_ _____\n | || '_ \\| __/ _ \\ '__/ _` |/ __| __| \\ \\ / / _ \\\n | || | | | || __/ | | (_| | (__| |_| |\\ V / __/\n|___|_| |_|\\__\\___|_| \\__,_|\\___|\\__|_| \\_/ \\___|"))
|
||||
fmt.Printf("%s v%s\n", color.New(color.FgHiYellow).Add(color.Bold).Sprintf("Hypernet.Interactive"), pkg.AppVersion)
|
||||
fmt.Printf("The social networking service in Hypernet\n")
|
||||
color.HiBlack("=====================================================\n")
|
||||
|
||||
// Configure settings
|
||||
viper.AddConfigPath(".")
|
||||
viper.AddConfigPath("..")
|
||||
@ -34,38 +43,40 @@ func main() {
|
||||
log.Panic().Err(err).Msg("An error occurred when loading settings.")
|
||||
}
|
||||
|
||||
// Connect to nexus
|
||||
if err := gap.InitializeToNexus(); err != nil {
|
||||
log.Fatal().Err(err).Msg("An error occurred when connecting to nexus...")
|
||||
}
|
||||
|
||||
// Load keypair
|
||||
if reader, err := sec.NewInternalTokenReader(viper.GetString("security.internal_public_key")); err != nil {
|
||||
log.Error().Err(err).Msg("An error occurred when reading internal public key for jwt. Authentication related features will be disabled.")
|
||||
} else {
|
||||
http.IReader = reader
|
||||
log.Info().Msg("Internal jwt public key loaded.")
|
||||
}
|
||||
|
||||
// Connect to database
|
||||
if err := database.NewSource(); err != nil {
|
||||
if err := database.NewGorm(); err != nil {
|
||||
log.Fatal().Err(err).Msg("An error occurred when connect to database.")
|
||||
} else if err := database.RunMigration(database.C); err != nil {
|
||||
log.Fatal().Err(err).Msg("An error occurred when running database auto migration.")
|
||||
}
|
||||
|
||||
// Connect other services
|
||||
if err := gap.RegisterService(); err != nil {
|
||||
log.Fatal().Err(err).Msg("An error occurred when connecting to consul...")
|
||||
}
|
||||
|
||||
// Configure timed tasks
|
||||
quartz := cron.New(cron.WithLogger(cron.VerbosePrintfLogger(&log.Logger)))
|
||||
quartz.AddFunc("@every 60m", services.DoAutoDatabaseCleanup)
|
||||
quartz.Start()
|
||||
|
||||
// Server
|
||||
server.NewServer()
|
||||
go server.Listen()
|
||||
go http.NewServer().Listen()
|
||||
|
||||
grpc.NewGRPC()
|
||||
go grpc.ListenGRPC()
|
||||
go grpc.NewGrpc().Listen()
|
||||
|
||||
// Messages
|
||||
log.Info().Msgf("Interactive v%s is started...", pkg.AppVersion)
|
||||
|
||||
quit := make(chan os.Signal, 1)
|
||||
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
||||
<-quit
|
||||
|
||||
log.Info().Msgf("Interactive v%s is quitting...", pkg.AppVersion)
|
||||
|
||||
quartz.Stop()
|
||||
}
|
||||
|
Reference in New Issue
Block a user