From eb04f98cb145da0ab273b1dec6739fe8eb792f63 Mon Sep 17 00:00:00 2001 From: Matt Roberts Date: Fri, 18 Oct 2024 14:05:31 +0100 Subject: [PATCH] Viewing reactions for anonymous users --- app/services/sqlstore/postgres/comment.go | 6 +- app/services/sqlstore/postgres/common_test.go | 49 ------------ app/services/sqlstore/postgres/post_test.go | 77 +++++++++++++++++++ public/components/Reactions.tsx | 13 +++- 4 files changed, 91 insertions(+), 54 deletions(-) diff --git a/app/services/sqlstore/postgres/comment.go b/app/services/sqlstore/postgres/comment.go index f2deb495b..74e238794 100644 --- a/app/services/sqlstore/postgres/comment.go +++ b/app/services/sqlstore/postgres/comment.go @@ -168,6 +168,10 @@ func getCommentsByPost(ctx context.Context, q *query.GetCommentsByPost) error { q.Result = make([]*entity.Comment, 0) comments := []*dbComment{} + userId := 0 + if user != nil { + userId = user.ID + } err := trx.Select(&comments, ` WITH agg_attachments AS ( @@ -241,7 +245,7 @@ func getCommentsByPost(ctx context.Context, q *query.GetCommentsByPost) error { WHERE p.id = $1 AND p.tenant_id = $2 AND c.deleted_at IS NULL - ORDER BY c.created_at ASC`, q.Post.ID, tenant.ID, user.ID) + ORDER BY c.created_at ASC`, q.Post.ID, tenant.ID, userId) if err != nil { return errors.Wrap(err, "failed get comments of post with id '%d'", q.Post.ID) } diff --git a/app/services/sqlstore/postgres/common_test.go b/app/services/sqlstore/postgres/common_test.go index 078658b3e..15ffee531 100644 --- a/app/services/sqlstore/postgres/common_test.go +++ b/app/services/sqlstore/postgres/common_test.go @@ -6,11 +6,8 @@ import ( "github.com/getfider/fider/app" - "github.com/getfider/fider/app/models/cmd" "github.com/getfider/fider/app/models/entity" - "github.com/getfider/fider/app/models/query" . "github.com/getfider/fider/app/pkg/assert" - "github.com/getfider/fider/app/pkg/bus" "github.com/getfider/fider/app/services/sqlstore/postgres" ) @@ -48,49 +45,3 @@ func withUser(ctx context.Context, user *entity.User) context.Context { ctx = context.WithValue(ctx, app.UserCtxKey, user) return ctx } - -func TestToggleReaction_Add(t *testing.T) { - SetupDatabaseTest(t) - defer TeardownDatabaseTest() - - newPost := &cmd.AddNewPost{Title: "My new post", Description: "with this description"} - err := bus.Dispatch(jonSnowCtx, newPost) - Expect(err).IsNil() - - newComment := &cmd.AddNewComment{Post: newPost.Result, Content: "This is my comment"} - err = bus.Dispatch(jonSnowCtx, newComment) - Expect(err).IsNil() - - // Now add a reaction - reaction := &cmd.ToggleCommentReaction{Comment: newComment.Result, Emoji: "👍", User: jonSnow} - err = bus.Dispatch(jonSnowCtx, reaction) - Expect(err).IsNil() - Expect(reaction.Result).IsTrue() - - // Get the comment, and check that the reaction was added - commentByID := &query.GetCommentsByPost{Post: &entity.Post{ID: newPost.Result.ID}} - err = bus.Dispatch(jonSnowCtx, commentByID) - Expect(err).IsNil() - - Expect(commentByID.Result).IsNotNil() - Expect(commentByID.Result[0].ReactionCounts).IsNotNil() - Expect(len(commentByID.Result[0].ReactionCounts)).Equals(1) - Expect(commentByID.Result[0].ReactionCounts[0].Emoji).Equals("👍") - Expect(commentByID.Result[0].ReactionCounts[0].Count).Equals(1) - Expect(commentByID.Result[0].ReactionCounts[0].IncludesMe).IsTrue() - - // Now remove the reaction - reaction = &cmd.ToggleCommentReaction{Comment: newComment.Result, Emoji: "👍", User: jonSnow} - err = bus.Dispatch(jonSnowCtx, reaction) - Expect(err).IsNil() - Expect(reaction.Result).IsFalse() - - // Get the comment, and check that the reaction was removed - commentByID = &query.GetCommentsByPost{Post: &entity.Post{ID: newPost.Result.ID}} - err = bus.Dispatch(jonSnowCtx, commentByID) - Expect(err).IsNil() - - Expect(commentByID.Result).IsNotNil() - Expect(commentByID.Result[0].ReactionCounts).IsNil() - -} diff --git a/app/services/sqlstore/postgres/post_test.go b/app/services/sqlstore/postgres/post_test.go index 554f38e42..81f2ab7f4 100644 --- a/app/services/sqlstore/postgres/post_test.go +++ b/app/services/sqlstore/postgres/post_test.go @@ -6,6 +6,7 @@ import ( "time" "github.com/getfider/fider/app/models/dto" + "github.com/getfider/fider/app/models/entity" "github.com/getfider/fider/app/models/enum" "github.com/getfider/fider/app/models/query" @@ -651,3 +652,79 @@ func TestPostStorage_Attachments(t *testing.T) { Expect(err).IsNil() Expect(getAttachments1.Result).HasLen(0) } + +func TestToggleReaction_Add(t *testing.T) { + SetupDatabaseTest(t) + defer TeardownDatabaseTest() + + newPost := &cmd.AddNewPost{Title: "My new post", Description: "with this description"} + err := bus.Dispatch(jonSnowCtx, newPost) + Expect(err).IsNil() + + newComment := &cmd.AddNewComment{Post: newPost.Result, Content: "This is my comment"} + err = bus.Dispatch(jonSnowCtx, newComment) + Expect(err).IsNil() + + // Now add a reaction + reaction := &cmd.ToggleCommentReaction{Comment: newComment.Result, Emoji: "👍", User: jonSnow} + err = bus.Dispatch(jonSnowCtx, reaction) + Expect(err).IsNil() + Expect(reaction.Result).IsTrue() + + // Get the comment, and check that the reaction was added + commentByID := &query.GetCommentsByPost{Post: &entity.Post{ID: newPost.Result.ID}} + err = bus.Dispatch(jonSnowCtx, commentByID) + Expect(err).IsNil() + + Expect(commentByID.Result).IsNotNil() + Expect(commentByID.Result[0].ReactionCounts).IsNotNil() + Expect(len(commentByID.Result[0].ReactionCounts)).Equals(1) + Expect(commentByID.Result[0].ReactionCounts[0].Emoji).Equals("👍") + Expect(commentByID.Result[0].ReactionCounts[0].Count).Equals(1) + Expect(commentByID.Result[0].ReactionCounts[0].IncludesMe).IsTrue() + + // Now remove the reaction + reaction = &cmd.ToggleCommentReaction{Comment: newComment.Result, Emoji: "👍", User: jonSnow} + err = bus.Dispatch(jonSnowCtx, reaction) + Expect(err).IsNil() + Expect(reaction.Result).IsFalse() + + // Get the comment, and check that the reaction was removed + commentByID = &query.GetCommentsByPost{Post: &entity.Post{ID: newPost.Result.ID}} + err = bus.Dispatch(jonSnowCtx, commentByID) + Expect(err).IsNil() + + Expect(commentByID.Result).IsNotNil() + Expect(commentByID.Result[0].ReactionCounts).IsNil() +} + +func TestViewReactions_AnonymousUser(t *testing.T) { + SetupDatabaseTest(t) + defer TeardownDatabaseTest() + + newPost := &cmd.AddNewPost{Title: "My new post", Description: "with this description"} + err := bus.Dispatch(jonSnowCtx, newPost) + Expect(err).IsNil() + + newComment := &cmd.AddNewComment{Post: newPost.Result, Content: "This is my comment"} + err = bus.Dispatch(jonSnowCtx, newComment) + Expect(err).IsNil() + + // Now add a reaction + reaction := &cmd.ToggleCommentReaction{Comment: newComment.Result, Emoji: "👍", User: jonSnow} + err = bus.Dispatch(jonSnowCtx, reaction) + Expect(err).IsNil() + Expect(reaction.Result).IsTrue() + + // Get the comment as an anonymous user, and check that the reaction was added + commentByID := &query.GetCommentsByPost{Post: &entity.Post{ID: newPost.Result.ID}} + err = bus.Dispatch(demoTenantCtx, commentByID) + Expect(err).IsNil() + + Expect(commentByID.Result).IsNotNil() + Expect(commentByID.Result[0].ReactionCounts).IsNotNil() + Expect(len(commentByID.Result[0].ReactionCounts)).Equals(1) + Expect(commentByID.Result[0].ReactionCounts[0].Emoji).Equals("👍") + Expect(commentByID.Result[0].ReactionCounts[0].Count).Equals(1) + Expect(commentByID.Result[0].ReactionCounts[0].IncludesMe).IsFalse() +} diff --git a/public/components/Reactions.tsx b/public/components/Reactions.tsx index a06a0233e..613030a75 100644 --- a/public/components/Reactions.tsx +++ b/public/components/Reactions.tsx @@ -3,6 +3,7 @@ import { ReactionCount } from "@fider/models" import { Icon } from "@fider/components" import ReactionAdd from "@fider/assets/images/reaction-add.svg" import { HStack } from "@fider/components/layout" +import { classSet } from "@fider/services" import { useFider } from "@fider/hooks" import "./Reactions.scss" @@ -65,10 +66,14 @@ export const Reactions: React.FC = ({ emojiSelectorRef, toggleRe {reactions.map((reaction) => ( toggleReaction(reaction.emoji)} - className={`clickable inline-flex items-center px-2 py-1 rounded-full text-xs ${ - reaction.includesMe ? "bg-blue-100 hover:bg-blue-200" : "bg-gray-100 hover:bg-gray-200" - }`} + {...(fider.session.isAuthenticated && { onClick: () => toggleReaction(reaction.emoji) })} + className={classSet({ + "inline-flex items-center px-2 py-1 rounded-full text-xs": true, + "bg-blue-100": reaction.includesMe, + "bg-gray-100": !reaction.includesMe, + "clickable hover:bg-blue-200": fider.session.isAuthenticated && reaction.includesMe, + "clickable hover:bg-gray-200": fider.session.isAuthenticated && !reaction.includesMe, + })} > {reaction.emoji} {reaction.count}