From 1c48aab8634615bbbcacae24d5813d7b48b46803 Mon Sep 17 00:00:00 2001 From: Asaf Gartner Date: Mon, 13 Dec 2021 18:58:26 +0200 Subject: [PATCH] Fixed admin queue --- src/hmndata/threads_and_posts_helper.go | 18 +++++++++++++----- src/website/admin.go | 16 ++++++++++------ 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/hmndata/threads_and_posts_helper.go b/src/hmndata/threads_and_posts_helper.go index 8011b86..cbe7b9f 100644 --- a/src/hmndata/threads_and_posts_helper.go +++ b/src/hmndata/threads_and_posts_helper.go @@ -696,20 +696,28 @@ func DeletePost( tx pgx.Tx, threadId, postId int, ) (threadDeleted bool) { - isFirstPost, err := db.QueryBool(ctx, tx, + type threadInfo struct { + FirstPostID int `db:"first_id"` + Deleted bool `db:"deleted"` + } + ti, err := db.QueryOne(ctx, tx, threadInfo{}, ` - SELECT thread.first_id = $1 + SELECT $columns FROM handmade_thread AS thread WHERE - thread.id = $2 + thread.id = $1 `, - postId, threadId, ) if err != nil { - panic(oops.New(err, "failed to check if post was the first post in the thread")) + panic(oops.New(err, "failed to fetch thread info")) } + info := ti.(*threadInfo) + if info.Deleted { + return true + } + isFirstPost := info.FirstPostID == postId if isFirstPost { // Just delete the whole thread and all its posts. diff --git a/src/website/admin.go b/src/website/admin.go index 8ac5af6..0771151 100644 --- a/src/website/admin.go +++ b/src/website/admin.go @@ -172,7 +172,7 @@ func AdminApprovalQueueSubmit(c *RequestContext) ResponseData { if errors.Is(err, db.NotFound) { return RejectRequest(c, "User not found") } else { - return c.ErrorResponse(http.StatusBadRequest, oops.New(err, "failed to fetch user")) + return c.ErrorResponse(http.StatusInternalServerError, oops.New(err, "failed to fetch user")) } } user := u.(*models.User) @@ -189,7 +189,7 @@ func AdminApprovalQueueSubmit(c *RequestContext) ResponseData { user.ID, ) if err != nil { - return c.ErrorResponse(http.StatusBadRequest, oops.New(err, "failed to set user to approved")) + return c.ErrorResponse(http.StatusInternalServerError, oops.New(err, "failed to set user to approved")) } whatHappened = fmt.Sprintf("%s approved successfully", user.Username) } else if action == ApprovalQueueActionSpammer { @@ -203,13 +203,16 @@ func AdminApprovalQueueSubmit(c *RequestContext) ResponseData { user.ID, ) if err != nil { - return c.ErrorResponse(http.StatusBadRequest, oops.New(err, "failed to set user to banned")) + return c.ErrorResponse(http.StatusInternalServerError, oops.New(err, "failed to set user to banned")) } err = auth.DeleteSessionForUser(c.Context(), c.Conn, user.Username) if err != nil { - return c.ErrorResponse(http.StatusBadRequest, oops.New(err, "failed to log out user")) + return c.ErrorResponse(http.StatusInternalServerError, oops.New(err, "failed to log out user")) } err = deleteAllPostsForUser(c.Context(), c.Conn, user.ID) + if err != nil { + return c.ErrorResponse(http.StatusInternalServerError, oops.New(err, "failed to delete spammer's posts")) + } whatHappened = fmt.Sprintf("%s banned successfully", user.Username) } else { whatHappened = fmt.Sprintf("Unrecognized action: %s", action) @@ -240,10 +243,11 @@ func fetchUnapprovedPosts(c *RequestContext) ([]*UnapprovedPost, error) { JOIN auth_user AS author ON author.id = post.author_id WHERE NOT thread.deleted - AND author.status = $1 + AND NOT post.deleted + AND author.status = ANY($1) ORDER BY post.postdate DESC `, - models.UserStatusConfirmed, + []models.UserStatus{models.UserStatusConfirmed}, ) if err != nil { return nil, oops.New(err, "failed to fetch unapproved posts")