New story & article create & edit api

This commit is contained in:
2024-07-22 00:49:36 +08:00
parent 18dedfc493
commit 3a5a84ae56
7 changed files with 317 additions and 206 deletions

View File

@ -1,21 +0,0 @@
package services
import (
"context"
"git.solsynth.dev/hydrogen/dealer/pkg/hyper"
"git.solsynth.dev/hydrogen/interactive/pkg/internal/gap"
"git.solsynth.dev/hydrogen/paperclip/pkg/proto"
"github.com/samber/lo"
)
func CheckAttachmentByIDExists(id uint, usage string) bool {
pc, err := gap.H.GetServiceGrpcConn(hyper.ServiceTypeFileProvider)
if err != nil {
return false
}
_, err = proto.NewAttachmentsClient(pc).CheckAttachmentExists(context.Background(), &proto.AttachmentLookupRequest{
Id: lo.ToPtr(uint64(id)),
Usage: &usage,
})
return err == nil
}

View File

@ -55,33 +55,6 @@ func FilterPostDraft(tx *gorm.DB) *gorm.DB {
return tx.Where("is_draft = ? OR is_draft IS NULL", false)
}
func GetPostWithAlias(tx *gorm.DB, alias string, ignoreLimitation ...bool) (models.Post, error) {
if len(ignoreLimitation) == 0 || !ignoreLimitation[0] {
tx = FilterPostWithPublishedAt(tx, time.Now())
}
var item models.Post
if err := tx.
Where("alias = ?", alias).
Preload("Tags").
Preload("Categories").
Preload("Realm").
Preload("Author").
Preload("ReplyTo").
Preload("ReplyTo.Author").
Preload("ReplyTo.Tags").
Preload("ReplyTo.Categories").
Preload("RepostTo").
Preload("RepostTo.Author").
Preload("RepostTo.Tags").
Preload("RepostTo.Categories").
First(&item).Error; err != nil {
return item, err
}
return item, nil
}
func GetPost(tx *gorm.DB, id uint, ignoreLimitation ...bool) (models.Post, error) {
if len(ignoreLimitation) == 0 || !ignoreLimitation[0] {
tx = FilterPostWithPublishedAt(tx, time.Now())
@ -243,8 +216,6 @@ func EnsurePostCategoriesAndTags(item models.Post) (models.Post, error) {
}
func NewPost(user models.Account, item models.Post) (models.Post, error) {
item.Language = DetectLanguage(item.Content)
item, err := EnsurePostCategoriesAndTags(item)
if err != nil {
return item, err
@ -272,7 +243,7 @@ func NewPost(user models.Account, item models.Post) (models.Post, error) {
err = NotifyPosterAccount(
op.Author,
"Post got replied",
fmt.Sprintf("%s (%s) replied your post #%s.", user.Nick, user.Name, op.Alias),
fmt.Sprintf("%s (%s) replied your post.", user.Nick, user.Name),
lo.ToPtr(fmt.Sprintf("%s replied you", user.Nick)),
)
if err != nil {
@ -286,7 +257,6 @@ func NewPost(user models.Account, item models.Post) (models.Post, error) {
}
func EditPost(item models.Post) (models.Post, error) {
item.Language = DetectLanguage(item.Content)
item, err := EnsurePostCategoriesAndTags(item)
if err != nil {
return item, err
@ -312,9 +282,9 @@ func ReactPost(user models.Account, reaction models.Reaction) (bool, models.Reac
if op.Author.ID != user.ID {
err = NotifyPosterAccount(
op.Author,
"Post got replied",
fmt.Sprintf("%s (%s) replied your post #%s.", user.Nick, user.Name, op.Alias),
lo.ToPtr(fmt.Sprintf("%s replied you", user.Nick)),
"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)),
)
if err != nil {
log.Error().Err(err).Msg("An error occurred when notifying user...")