Skip to content

Commit

Permalink
Viewing reactions for anonymous users
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwoberts committed Oct 18, 2024
1 parent bb947dd commit eb04f98
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 54 deletions.
6 changes: 5 additions & 1 deletion app/services/sqlstore/postgres/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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)
}
Expand Down
49 changes: 0 additions & 49 deletions app/services/sqlstore/postgres/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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()

}
77 changes: 77 additions & 0 deletions app/services/sqlstore/postgres/post_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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()
}
13 changes: 9 additions & 4 deletions public/components/Reactions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -65,10 +66,14 @@ export const Reactions: React.FC<ReactionsProps> = ({ emojiSelectorRef, toggleRe
{reactions.map((reaction) => (
<span
key={reaction.emoji}
onClick={() => 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} <span className="ml-1 font-semibold">{reaction.count}</span>
</span>
Expand Down

0 comments on commit eb04f98

Please sign in to comment.