Add back the ability to mark everything on the site as unread

This commit is contained in:
Ben Visness 2021-07-29 23:48:30 -05:00
parent 8ecb4a7173
commit 958aeb45e4
5 changed files with 107 additions and 43 deletions

View File

@ -0,0 +1,44 @@
package migrations
import (
"context"
"time"
"git.handmade.network/hmn/hmn/src/migration/types"
"git.handmade.network/hmn/hmn/src/oops"
"github.com/jackc/pgx/v4"
)
func init() {
registerMigration(AddUserLastMarkedRead{})
}
type AddUserLastMarkedRead struct{}
func (m AddUserLastMarkedRead) Version() types.MigrationVersion {
return types.MigrationVersion(time.Date(2021, 7, 30, 3, 44, 32, 0, time.UTC))
}
func (m AddUserLastMarkedRead) Name() string {
return "AddUserLastMarkedRead"
}
func (m AddUserLastMarkedRead) Description() string {
return "Add a field to users that tracks when they last read EVERYTHING."
}
func (m AddUserLastMarkedRead) Up(ctx context.Context, tx pgx.Tx) error {
_, err := tx.Exec(ctx, `
ALTER TABLE auth_user
ADD marked_all_read_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT 'epoch';
`)
if err != nil {
return oops.New(err, "failed to insert new column")
}
return nil
}
func (m AddUserLastMarkedRead) Down(ctx context.Context, tx pgx.Tx) error {
panic("Implement me")
}

View File

@ -36,4 +36,6 @@ type User struct {
DiscordSaveShowcase bool `db:"discord_save_showcase"` DiscordSaveShowcase bool `db:"discord_save_showcase"`
DiscordDeleteSnippetOnMessageDelete bool `db:"discord_delete_snippet_on_message_delete"` DiscordDeleteSnippetOnMessageDelete bool `db:"discord_delete_snippet_on_message_delete"`
MarkedAllReadAt time.Time `db:"marked_all_read_at"`
} }

View File

@ -354,7 +354,9 @@ func fetchAllPosts(c *RequestContext, lineageBuilder *models.SubforumLineageBuil
postResult := iPostResult.(*feedPostQuery) postResult := iPostResult.(*feedPostQuery)
hasRead := false hasRead := false
if postResult.ThreadLastReadTime != nil && postResult.ThreadLastReadTime.After(postResult.Post.PostDate) { if c.CurrentUser != nil && c.CurrentUser.MarkedAllReadAt.After(postResult.Post.PostDate) {
hasRead = true
} else if postResult.ThreadLastReadTime != nil && postResult.ThreadLastReadTime.After(postResult.Post.PostDate) {
hasRead = true hasRead = true
} else if postResult.SubforumLastReadTime != nil && postResult.SubforumLastReadTime.After(postResult.Post.PostDate) { } else if postResult.SubforumLastReadTime != nil && postResult.SubforumLastReadTime.After(postResult.Post.PostDate) {
hasRead = true hasRead = true

View File

@ -306,40 +306,53 @@ func ForumMarkRead(c *RequestContext) ResponseData {
} }
defer tx.Rollback(c.Context()) defer tx.Rollback(c.Context())
// TODO(ben): Rework this logic when we rework blogs, threads, etc.
sfIds := []int{sfId} sfIds := []int{sfId}
if sfId == 0 { if sfId == 0 {
// Select all categories // Mark literally everything as read
type sfIdResult struct { _, err := tx.Exec(c.Context(),
SubforumID int `db:"id"`
}
cats, err := db.Query(c.Context(), tx, sfIdResult{},
` `
SELECT $columns UPDATE auth_user
FROM handmade_subforum SET marked_all_read_at = NOW()
WHERE kind = ANY ($1) WHERE id = $1
`, `,
[]models.ThreadType{models.ThreadTypeProjectArticle, models.ThreadTypeForumPost}, c.CurrentUser.ID,
) )
if err != nil { if err != nil {
return ErrorResponse(http.StatusInternalServerError, oops.New(err, "failed to fetch category IDs for CLRI")) return ErrorResponse(http.StatusInternalServerError, oops.New(err, "failed to mark all posts as read"))
} }
catIdResults := cats.ToSlice() // Delete thread unread info
sfIds = make([]int, len(catIdResults))
for i, res := range catIdResults {
sfIds[i] = res.(*sfIdResult).SubforumID
}
}
c.Perf.StartBlock("SQL", "Update CLRIs")
_, err = tx.Exec(c.Context(), _, err = tx.Exec(c.Context(),
` `
INSERT INTO handmade_categorylastreadinfo (category_id, user_id, lastread) DELETE FROM handmade_threadlastreadinfo
WHERE user_id = $1;
`,
c.CurrentUser.ID,
)
if err != nil {
return ErrorResponse(http.StatusInternalServerError, oops.New(err, "failed to delete thread unread info"))
}
// Delete subforum unread info
_, err = tx.Exec(c.Context(),
`
DELETE FROM handmade_subforumlastreadinfo
WHERE user_id = $1;
`,
c.CurrentUser.ID,
)
if err != nil {
return ErrorResponse(http.StatusInternalServerError, oops.New(err, "failed to delete subforum unread info"))
}
} else {
c.Perf.StartBlock("SQL", "Update SLRIs")
_, err = tx.Exec(c.Context(),
`
INSERT INTO handmade_subforumlastreadinfo (subforum_id, user_id, lastread)
SELECT id, $2, $3 SELECT id, $2, $3
FROM handmade_category FROM handmade_subforum
WHERE id = ANY ($1) WHERE id = ANY ($1)
ON CONFLICT (category_id, user_id) DO UPDATE ON CONFLICT (subforum_id, user_id) DO UPDATE
SET lastread = EXCLUDED.lastread SET lastread = EXCLUDED.lastread
`, `,
sfIds, sfIds,
@ -348,7 +361,7 @@ func ForumMarkRead(c *RequestContext) ResponseData {
) )
c.Perf.EndBlock() c.Perf.EndBlock()
if err != nil { if err != nil {
return ErrorResponse(http.StatusInternalServerError, oops.New(err, "failed to update forum clris")) return ErrorResponse(http.StatusInternalServerError, oops.New(err, "failed to update forum slris"))
} }
c.Perf.StartBlock("SQL", "Delete TLRIs") c.Perf.StartBlock("SQL", "Delete TLRIs")
@ -361,7 +374,7 @@ func ForumMarkRead(c *RequestContext) ResponseData {
SELECT id SELECT id
FROM handmade_thread FROM handmade_thread
WHERE WHERE
category_id = ANY ($1) subforum_id = ANY ($1)
) )
`, `,
sfIds, sfIds,
@ -371,10 +384,11 @@ func ForumMarkRead(c *RequestContext) ResponseData {
if err != nil { if err != nil {
return ErrorResponse(http.StatusInternalServerError, oops.New(err, "failed to delete unnecessary tlris")) return ErrorResponse(http.StatusInternalServerError, oops.New(err, "failed to delete unnecessary tlris"))
} }
}
err = tx.Commit(c.Context()) err = tx.Commit(c.Context())
if err != nil { if err != nil {
return ErrorResponse(http.StatusInternalServerError, oops.New(err, "failed to commit CLRI/TLRI updates")) return ErrorResponse(http.StatusInternalServerError, oops.New(err, "failed to commit SLRI/TLRI updates"))
} }
var redirUrl string var redirUrl string

View File

@ -146,7 +146,9 @@ func Index(c *RequestContext) ResponseData {
projectPost := projectPostRow.(*projectPostQuery) projectPost := projectPostRow.(*projectPostQuery)
hasRead := false hasRead := false
if projectPost.ThreadLastReadTime != nil && projectPost.ThreadLastReadTime.After(projectPost.Post.PostDate) { if c.CurrentUser != nil && c.CurrentUser.MarkedAllReadAt.After(projectPost.Post.PostDate) {
hasRead = true
} else if projectPost.ThreadLastReadTime != nil && projectPost.ThreadLastReadTime.After(projectPost.Post.PostDate) {
hasRead = true hasRead = true
} else if projectPost.SubforumLastReadTime != nil && projectPost.SubforumLastReadTime.After(projectPost.Post.PostDate) { } else if projectPost.SubforumLastReadTime != nil && projectPost.SubforumLastReadTime.After(projectPost.Post.PostDate) {
hasRead = true hasRead = true