✨ Attachments
This commit is contained in:
@ -45,12 +45,12 @@ func createArticle(c *fiber.Ctx) error {
|
||||
item := &models.Article{
|
||||
PostBase: models.PostBase{
|
||||
Alias: data.Alias,
|
||||
Attachments: data.Attachments,
|
||||
PublishedAt: data.PublishedAt,
|
||||
AuthorID: user.ID,
|
||||
},
|
||||
Hashtags: data.Hashtags,
|
||||
Categories: data.Categories,
|
||||
Attachments: data.Attachments,
|
||||
Title: data.Title,
|
||||
Description: data.Description,
|
||||
Content: data.Content,
|
||||
|
@ -56,13 +56,12 @@ func createComment(c *fiber.Ctx) error {
|
||||
user := c.Locals("principal").(models.Account)
|
||||
|
||||
var data struct {
|
||||
Alias string `json:"alias" form:"alias"`
|
||||
Content string `json:"content" form:"content" validate:"required"`
|
||||
PublishedAt *time.Time `json:"published_at" form:"published_at"`
|
||||
Hashtags []models.Tag `json:"hashtags" form:"hashtags"`
|
||||
Categories []models.Category `json:"categories" form:"categories"`
|
||||
Attachments []models.Attachment `json:"attachments" form:"attachments"`
|
||||
ReplyTo uint `json:"reply_to" form:"reply_to"`
|
||||
Alias string `json:"alias" form:"alias"`
|
||||
Content string `json:"content" form:"content" validate:"required"`
|
||||
PublishedAt *time.Time `json:"published_at" form:"published_at"`
|
||||
Hashtags []models.Tag `json:"hashtags" form:"hashtags"`
|
||||
Categories []models.Category `json:"categories" form:"categories"`
|
||||
ReplyTo uint `json:"reply_to" form:"reply_to"`
|
||||
}
|
||||
|
||||
if err := BindAndValidate(c, &data); err != nil {
|
||||
@ -74,7 +73,6 @@ func createComment(c *fiber.Ctx) error {
|
||||
item := &models.Comment{
|
||||
PostBase: models.PostBase{
|
||||
Alias: data.Alias,
|
||||
Attachments: data.Attachments,
|
||||
PublishedAt: data.PublishedAt,
|
||||
AuthorID: user.ID,
|
||||
},
|
||||
@ -133,12 +131,11 @@ func editComment(c *fiber.Ctx) error {
|
||||
id, _ := c.ParamsInt("commentId", 0)
|
||||
|
||||
var data struct {
|
||||
Alias string `json:"alias" form:"alias" validate:"required"`
|
||||
Content string `json:"content" form:"content" validate:"required"`
|
||||
PublishedAt *time.Time `json:"published_at" form:"published_at"`
|
||||
Hashtags []models.Tag `json:"hashtags" form:"hashtags"`
|
||||
Categories []models.Category `json:"categories" form:"categories"`
|
||||
Attachments []models.Attachment `json:"attachments" form:"attachments"`
|
||||
Alias string `json:"alias" form:"alias" validate:"required"`
|
||||
Content string `json:"content" form:"content" validate:"required"`
|
||||
PublishedAt *time.Time `json:"published_at" form:"published_at"`
|
||||
Hashtags []models.Tag `json:"hashtags" form:"hashtags"`
|
||||
Categories []models.Category `json:"categories" form:"categories"`
|
||||
}
|
||||
|
||||
if err := BindAndValidate(c, &data); err != nil {
|
||||
@ -160,7 +157,6 @@ func editComment(c *fiber.Ctx) error {
|
||||
item.PublishedAt = data.PublishedAt
|
||||
item.Hashtags = data.Hashtags
|
||||
item.Categories = data.Categories
|
||||
item.Attachments = data.Attachments
|
||||
|
||||
if item, err := services.EditPost(item); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
|
@ -47,22 +47,28 @@ func listFeed(c *fiber.Ctx) error {
|
||||
commentTable := viper.GetString("database.prefix") + "comments"
|
||||
reactionTable := viper.GetString("database.prefix") + "reactions"
|
||||
|
||||
database.C.Raw(fmt.Sprintf(`SELECT feed.*, author.*,
|
||||
COALESCE(comment_count, 0) as comment_count,
|
||||
COALESCE(reaction_count, 0) as reaction_count
|
||||
FROM (? UNION ALL ?) as feed
|
||||
INNER JOIN %s as author ON author_id = author.id
|
||||
LEFT JOIN (SELECT article_id, moment_id, COUNT(*) as comment_count
|
||||
database.C.Raw(
|
||||
fmt.Sprintf(`SELECT feed.*, author.*,
|
||||
COALESCE(comment_count, 0) AS comment_count,
|
||||
COALESCE(reaction_count, 0) AS reaction_count
|
||||
FROM (? UNION ALL ?) AS feed
|
||||
INNER JOIN %s AS author ON author_id = author.id
|
||||
LEFT JOIN (SELECT article_id, moment_id, COUNT(*) AS comment_count
|
||||
FROM %s
|
||||
GROUP BY article_id, moment_id) as comments
|
||||
GROUP BY article_id, moment_id) AS comments
|
||||
ON (feed.model_type = 'article' AND feed.id = comments.article_id) OR
|
||||
(feed.model_type = 'moment' AND feed.id = comments.moment_id)
|
||||
LEFT JOIN (SELECT article_id, moment_id, COUNT(*) as reaction_count
|
||||
LEFT JOIN (SELECT article_id, moment_id, COUNT(*) AS reaction_count
|
||||
FROM %s
|
||||
GROUP BY article_id, moment_id) as reactions
|
||||
GROUP BY article_id, moment_id) AS reactions
|
||||
ON (feed.model_type = 'article' AND feed.id = reactions.article_id) OR
|
||||
(feed.model_type = 'moment' AND feed.id = reactions.moment_id)
|
||||
WHERE %s ORDER BY feed.created_at desc LIMIT ? OFFSET ?`, userTable, commentTable, reactionTable, whereCondition),
|
||||
WHERE %s ORDER BY feed.created_at desc LIMIT ? OFFSET ?`,
|
||||
userTable,
|
||||
commentTable,
|
||||
reactionTable,
|
||||
whereCondition,
|
||||
),
|
||||
database.C.Select(queryArticle).Model(&models.Article{}),
|
||||
database.C.Select(queryMoment).Model(&models.Moment{}),
|
||||
take,
|
||||
@ -122,6 +128,56 @@ func listFeed(c *fiber.Ctx) error {
|
||||
}
|
||||
}
|
||||
|
||||
if !c.QueryBool("noAttachment", false) {
|
||||
revertAttachment := func(dataset string) error {
|
||||
var attachments []struct {
|
||||
models.Attachment
|
||||
|
||||
PostID uint `json:"post_id"`
|
||||
}
|
||||
|
||||
itemMap := lo.SliceToMap(lo.FilterMap(result, func(item *models.Feed, index int) (*models.Feed, bool) {
|
||||
return item, item.ModelType == dataset
|
||||
}), func(item *models.Feed) (uint, *models.Feed) {
|
||||
return item.ID, item
|
||||
})
|
||||
|
||||
idx := lo.Map(lo.Filter(result, func(item *models.Feed, index int) bool {
|
||||
return item.ModelType == dataset
|
||||
}), func(item *models.Feed, index int) uint {
|
||||
return item.ID
|
||||
})
|
||||
|
||||
if err := database.C.
|
||||
Model(&models.Attachment{}).
|
||||
Select(dataset+"_id as post_id, *").
|
||||
Where(dataset+"_id IN (?)", idx).
|
||||
Scan(&attachments).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
list := map[uint][]models.Attachment{}
|
||||
for _, info := range attachments {
|
||||
list[info.PostID] = append(list[info.PostID], info.Attachment)
|
||||
}
|
||||
|
||||
for k, v := range list {
|
||||
if post, ok := itemMap[k]; ok {
|
||||
post.Attachments = v
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := revertAttachment("article"); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := revertAttachment("moment"); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
var count int64
|
||||
database.C.Raw(`SELECT COUNT(*) FROM (? UNION ALL ?) as feed`,
|
||||
database.C.Select(queryArticle).Model(&models.Article{}),
|
||||
|
@ -44,14 +44,14 @@ func createMoment(c *fiber.Ctx) error {
|
||||
item := &models.Moment{
|
||||
PostBase: models.PostBase{
|
||||
Alias: data.Alias,
|
||||
Attachments: data.Attachments,
|
||||
PublishedAt: data.PublishedAt,
|
||||
AuthorID: user.ID,
|
||||
},
|
||||
Hashtags: data.Hashtags,
|
||||
Categories: data.Categories,
|
||||
Content: data.Content,
|
||||
RealmID: data.RealmID,
|
||||
Hashtags: data.Hashtags,
|
||||
Categories: data.Categories,
|
||||
Attachments: data.Attachments,
|
||||
Content: data.Content,
|
||||
RealmID: data.RealmID,
|
||||
}
|
||||
|
||||
var relatedCount int64
|
||||
|
Reference in New Issue
Block a user