From 7c915fe134afc9484fef9cebdd5151e69fe8bb81 Mon Sep 17 00:00:00 2001 From: Michael Li Date: Sat, 30 Jul 2022 14:23:34 +0800 Subject: [PATCH] optimize #176 change reload page logic just for siimple --- internal/service/post.go | 17 ++++++++++++----- web/src/views/Home.vue | 21 ++++++++++++++++++--- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/internal/service/post.go b/internal/service/post.go index 37d1334d..1f8cb632 100644 --- a/internal/service/post.go +++ b/internal/service/post.go @@ -103,8 +103,11 @@ func tagsFrom(originTags []string) []string { // CreatePost 创建文章 // TODO: 推文+推文内容需要在一个事务中添加,后续优化 -func CreatePost(c *gin.Context, userID int64, param PostCreationReq) (post *model.Post, err error) { - var mediaContents []string +func CreatePost(c *gin.Context, userID int64, param PostCreationReq) (*model.PostFormated, error) { + var ( + err error + mediaContents []string + ) defer func() { if err != nil { @@ -113,12 +116,12 @@ func CreatePost(c *gin.Context, userID int64, param PostCreationReq) (post *mode }() if mediaContents, err = persistMediaContents(param.Contents); err != nil { - return + return nil, err } ip := c.ClientIP() tags := tagsFrom(param.Tags) - post = &model.Post{ + post := &model.Post{ UserID: userID, Tags: strings.Join(tags, ","), IP: ip, @@ -187,7 +190,11 @@ func CreatePost(c *gin.Context, userID int64, param PostCreationReq) (post *mode // 推送Search PushPostToSearch(post) - return post, nil + formatedPosts, err := ds.RevampPosts([]*model.PostFormated{post.Format()}) + if err != nil { + return nil, err + } + return formatedPosts[0], nil } func DeletePost(user *model.User, id int64) *errcode.Error { diff --git a/web/src/views/Home.vue b/web/src/views/Home.vue index d40f7e85..7dc390cf 100644 --- a/web/src/views/Home.vue +++ b/web/src/views/Home.vue @@ -84,9 +84,24 @@ const loadPosts = () => { }; const onPostSuccess = (post: Item.PostProps) => { - setTimeout(() => { - loadPosts(); - }, 50); + let items = []; + let length = list.value.length; + if (length == pageSize.value) { + length-- + } + var i = 0 + for (;i < length; i++) { + let item: Item.PostProps = list.value[i]; + if (!item.is_top) { + break; + } + items.push(item); + } + items.push(post); + for (;i < length; i++) { + items.push(list.value[i]); + } + list.value = items; }; const updatePage = (p: number) => {