Don't treat replies to the OP as replies

This commit is contained in:
Ben Visness 2021-09-06 16:39:28 -05:00
parent 193bbca28c
commit f1e2e99663
3 changed files with 69 additions and 5 deletions

View File

@ -0,0 +1,56 @@
package migrations
import (
"context"
"fmt"
"time"
"git.handmade.network/hmn/hmn/src/migration/types"
"git.handmade.network/hmn/hmn/src/oops"
"github.com/jackc/pgx/v4"
)
func init() {
registerMigration(DeleteIncorrectReplies{})
}
type DeleteIncorrectReplies struct{}
func (m DeleteIncorrectReplies) Version() types.MigrationVersion {
return types.MigrationVersion(time.Date(2021, 9, 6, 21, 32, 26, 0, time.UTC))
}
func (m DeleteIncorrectReplies) Name() string {
return "DeleteIncorrectReplies"
}
func (m DeleteIncorrectReplies) Description() string {
return "Remove reply ID from posts that are replying to the OP"
}
func (m DeleteIncorrectReplies) Up(ctx context.Context, tx pgx.Tx) error {
tag, err := tx.Exec(ctx, `
UPDATE handmade_post
SET reply_id = NULL
WHERE id IN (
SELECT post.id
FROM
handmade_post AS post
JOIN handmade_thread AS thread ON post.thread_id = thread.id
WHERE
post.reply_id = thread.first_id
)
`)
if err != nil {
return oops.New(err, "failed to delete bad reply IDs")
}
fmt.Printf("Cleared the reply id on %d posts.\n", tag.RowsAffected())
return nil
}
func (m DeleteIncorrectReplies) Down(ctx context.Context, tx pgx.Tx) error {
fmt.Println("This migration was just a fixup and we don't really need a reverse.")
return nil
}

View File

@ -98,9 +98,9 @@
{{ end }}
</div>
</div>
{{ if .ReplyPost }}
{{ with .ReplyPost }}
<div class="i c--dim f7 pb2">
Replying to {{ if .Author }}{{ .Author.Username }}{{ else }}deleted user{{ end }} (<a href="{{ .ReplyPost.Url }}">#{{ .ReplyPost.ID }}</a>)
Replying to {{ with .Author }}{{ .Username }}{{ else }}deleted user{{ end }} (<a href="{{ .Url }}">#{{ .ID }}</a>)
</div>
{{ end }}
<div class="post-content overflow-x-auto">
@ -123,7 +123,7 @@
{{ else if .User }}
<a class="button" href="{{ .ReplyUrl }}">&#10551; Reply to Thread</a>
{{ else }}
<span><a href="{{ .LoginPageUrl }}">Log in</a> to reply</span>
<span class="pa2"><a href="{{ .LoginPageUrl }}">Log in</a> to reply</span>
{{ end }}
</div>
<div class="options order-0 order-last-ns">

View File

@ -465,7 +465,7 @@ func ForumThread(c *RequestContext) ResponseData {
if p.ReplyPost != nil {
reply := templates.PostToTemplate(p.ReplyPost, p.ReplyAuthor, c.Theme)
addForumUrlsToPost(&reply, c.CurrentProject.Slug, currentSubforumSlugs, thread.ID, post.ID)
addForumUrlsToPost(&reply, c.CurrentProject.Slug, currentSubforumSlugs, thread.ID, reply.ID)
post.ReplyPost = &reply
}
@ -701,7 +701,15 @@ func ForumPostReplySubmit(c *RequestContext) ResponseData {
return RejectRequest(c, "Your reply cannot be empty.")
}
newPostId, _ := CreateNewPost(c.Context(), tx, c.CurrentProject.ID, cd.ThreadID, models.ThreadTypeForumPost, c.CurrentUser.ID, &cd.PostID, unparsed, c.Req.Host)
thread := FetchThread(c.Context(), tx, cd.ThreadID)
// Replies to the OP should not be considered replies
var replyPostId *int
if cd.PostID != thread.FirstID {
replyPostId = &cd.PostID
}
newPostId, _ := CreateNewPost(c.Context(), tx, c.CurrentProject.ID, cd.ThreadID, models.ThreadTypeForumPost, c.CurrentUser.ID, replyPostId, unparsed, c.Req.Host)
err = tx.Commit(c.Context())
if err != nil {