From 8391c95abcf80cff1e30ec1ca4cc6187f15b0946 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sun, 10 Mar 2024 23:35:38 +0800 Subject: [PATCH] :sparkles: Editable --- pkg/server/moments_api.go | 4 +- pkg/services/posts.go | 55 ++++++++++++++++ pkg/views/package.json | 1 + pkg/views/src/components/posts/PostItem.vue | 38 ++++++++++- .../src/components/publish/ArticleEditor.vue | 40 +++++++++--- .../src/components/publish/MomentEditor.vue | 63 ++++++++++--------- pkg/views/src/stores/editor.ts | 3 +- pkg/views/src/views/posts/articles.vue | 4 +- 8 files changed, 162 insertions(+), 46 deletions(-) diff --git a/pkg/server/moments_api.go b/pkg/server/moments_api.go index 6fe5839..250c93a 100644 --- a/pkg/server/moments_api.go +++ b/pkg/server/moments_api.go @@ -26,7 +26,7 @@ func createMoment(c *fiber.Ctx) error { var data struct { Alias string `json:"alias" form:"alias"` - Content string `json:"content" form:"content" validate:"required"` + Content string `json:"content" form:"content" validate:"required,max=1024"` Hashtags []models.Tag `json:"hashtags" form:"hashtags"` Categories []models.Category `json:"categories" form:"categories"` Attachments []models.Attachment `json:"attachments" form:"attachments"` @@ -89,7 +89,7 @@ func editMoment(c *fiber.Ctx) error { var data struct { Alias string `json:"alias" form:"alias" validate:"required"` - Content string `json:"content" form:"content" validate:"required"` + Content string `json:"content" form:"content" validate:"required,max=1024"` PublishedAt *time.Time `json:"published_at" form:"published_at"` Hashtags []models.Tag `json:"hashtags" form:"hashtags"` Categories []models.Category `json:"categories" form:"categories"` diff --git a/pkg/services/posts.go b/pkg/services/posts.go index 85f97cb..25177c1 100644 --- a/pkg/services/posts.go +++ b/pkg/services/posts.go @@ -83,6 +83,16 @@ func (v *PostTypeContext) GetViaAlias(alias string) (models.Feed, error) { return item, err } + var attachments []models.Attachment + if err := database.C. + Model(&models.Attachment{}). + Where(v.ColumnName+"_id = ?", item.ID). + Scan(&attachments).Error; err != nil { + return item, err + } else { + item.Attachments = attachments + } + return item, nil } @@ -96,6 +106,16 @@ func (v *PostTypeContext) Get(id uint, noComments ...bool) (models.Feed, error) return item, err } + var attachments []models.Attachment + if err := database.C. + Model(&models.Attachment{}). + Where(v.ColumnName+"_id = ?", id). + Scan(&attachments).Error; err != nil { + return item, err + } else { + item.Attachments = attachments + } + return item, nil } @@ -184,6 +204,41 @@ func (v *PostTypeContext) List(take int, offset int, noReact ...bool) ([]*models } } + { + var attachments []struct { + models.Attachment + + PostID uint `json:"post_id"` + } + + itemMap := lo.SliceToMap(items, func(item *models.Feed) (uint, *models.Feed) { + return item.ID, item + }) + + idx := lo.Map(items, func(item *models.Feed, index int) uint { + return item.ID + }) + + if err := database.C. + Model(&models.Attachment{}). + Select(v.ColumnName+"_id as post_id, *"). + Where(v.ColumnName+"_id IN (?)", idx). + Scan(&attachments).Error; err != nil { + return items, err + } + + 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 items, nil } diff --git a/pkg/views/package.json b/pkg/views/package.json index 50e6356..f466f41 100644 --- a/pkg/views/package.json +++ b/pkg/views/package.json @@ -22,6 +22,7 @@ "universal-cookie": "^7.1.0", "unocss": "^0.58.5", "vue": "^3.4.15", + "vue-easy-lightbox": "next", "vue-router": "^4.2.5", "vuetify": "^3.5.7" }, diff --git a/pkg/views/src/components/posts/PostItem.vue b/pkg/views/src/components/posts/PostItem.vue index 5708851..d46ab3c 100644 --- a/pkg/views/src/components/posts/PostItem.vue +++ b/pkg/views/src/components/posts/PostItem.vue @@ -9,7 +9,7 @@ /> -
+
{{ props.item?.author.nick }}
@@ -32,27 +32,61 @@ :reactions="props.item?.reaction_list ?? {}" @update="updateReactions" /> + +
+ Posted at {{ new Date(props.item?.created_at).toLocaleString() }} +
+ + + + + + + + + +
\ No newline at end of file + diff --git a/pkg/views/src/stores/editor.ts b/pkg/views/src/stores/editor.ts index 1445e25..06e5259 100644 --- a/pkg/views/src/stores/editor.ts +++ b/pkg/views/src/stores/editor.ts @@ -10,7 +10,8 @@ export const useEditor = defineStore("editor", () => { comment: false }) - const related = reactive<{ comment_to: any; reply_to: any; repost_to: any }>({ + const related = reactive<{ edit_to: any; comment_to: any; reply_to: any; repost_to: any }>({ + edit_to: null, comment_to: null, reply_to: null, repost_to: null diff --git a/pkg/views/src/views/posts/articles.vue b/pkg/views/src/views/posts/articles.vue index d9ae207..7698cc5 100644 --- a/pkg/views/src/views/posts/articles.vue +++ b/pkg/views/src/views/posts/articles.vue @@ -15,8 +15,8 @@
@@ -32,9 +32,9 @@