Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

Messing about with reactions

Matt Robertsa876384dcd7d
diff --git a/app/models/entity/comment.go b/app/models/entity/comment.go
index 43be67fdd..12912b488 100644
--- a/app/models/entity/comment.go
+++ b/app/models/entity/comment.go
@@ -2,13 +2,20 @@ package entity
import "time"
-//Comment represents an user comment on an post
+type ReactionCounts struct {
+ Emoji string `json:"emoji"`
+ Count int `json:"count"`
+}
+
+// Comment represents an user comment on an post
type Comment struct {
- ID int `json:"id"`
- Content string `json:"content"`
- CreatedAt time.Time `json:"createdAt"`
- User *User `json:"user"`
- Attachments []string `json:"attachments,omitempty"`
- EditedAt *time.Time `json:"editedAt,omitempty"`
- EditedBy *User `json:"editedBy,omitempty"`
+ ID int `json:"id"`
+ Content string `json:"content"`
+ CreatedAt time.Time `json:"createdAt"`
+ User *User `json:"user"`
+ Attachments []string `json:"attachments,omitempty"`
+ EditedAt *time.Time `json:"editedAt,omitempty"`
+ EditedBy *User `json:"editedBy,omitempty"`
+ ReactionCounts map[string]int `json:"reactionCounts,omitempty"`
+ Reactions string `json:"reactions,omitempty"`
}
diff --git a/app/models/entity/reaction.go b/app/models/entity/reaction.go
new file mode 100644
index 000000000..3fbbd5a61
--- /dev/null
+++ b/app/models/entity/reaction.go
@@ -0,0 +1,12 @@
+package entity
+
+import "time"
+
+// Reaction represents a user's emoji reaction to a comment
+type Reaction struct {
+ ID int `json:"id"`
+ Emoji string `json:"emoji"`
+ Comment *Comment `json:"-"`
+ User *User `json:"user"`
+ CreatedAt time.Time `json:"createdAt"`
+}
diff --git a/app/services/sqlstore/postgres/comment.go b/app/services/sqlstore/postgres/comment.go
index 868eef81e..84163a521 100644
--- a/app/services/sqlstore/postgres/comment.go
+++ b/app/services/sqlstore/postgres/comment.go
@@ -2,6 +2,7 @@ package postgres
import (
"context"
+ "encoding/json"
"time"
"github.com/getfider/fider/app/models/cmd"
@@ -11,14 +12,21 @@ import (
"github.com/getfider/fider/app/pkg/errors"
)
+type ReactionCounts struct {
+ Emoji string `db:"emoji"`
+ Count int `db:"count"`
+}
+
type dbComment struct {
- ID int `db:"id"`
- Content string `db:"content"`
- CreatedAt time.Time `db:"created_at"`
- User *dbUser `db:"user"`
- Attachments []string `db:"attachment_bkeys"`
- EditedAt dbx.NullTime `db:"edited_at"`
- EditedBy *dbUser `db:"edited_by"`
+ ID int `db:"id"`
+ Content string `db:"content"`
+ CreatedAt time.Time `db:"created_at"`
+ User *dbUser `db:"user"`
+ Attachments []string `db:"attachment_bkeys"`
+ EditedAt dbx.NullTime `db:"edited_at"`
+ EditedBy *dbUser `db:"edited_by"`
+ ReactionCounts dbx.NullString `db:"reaction_counts"`
+ // Reactions dbx.NullString `db:"reactions"`
}
func (c *dbComment) toModel(ctx context.Context) *entity.Comment {
@@ -28,11 +36,22 @@ func (c *dbComment) toModel(ctx context.Context) *entity.Comment {
CreatedAt: c.CreatedAt,
User: c.User.toModel(ctx),
Attachments: c.Attachments,
+ // Reactions: c.Reactions.String,
}
if c.EditedAt.Valid {
comment.EditedBy = c.EditedBy.toModel(ctx)
comment.EditedAt = &c.EditedAt.Time
}
+
+ comment.ReactionCounts = make(map[string]int)
+ if c.ReactionCounts.Valid {
+ var reactionCounts map[string]int
+ err := json.Unmarshal([]byte(c.ReactionCounts.String), &reactionCounts)
+ if err == nil {
+ comment.ReactionCounts = reactionCounts
+ }
+ }
+
return comment
}
@@ -131,7 +150,8 @@ func getCommentsByPost(ctx context.Context, q *query.GetCommentsByPost) error {
comments := []*dbComment{}
err := trx.Select(&comments,
- `WITH agg_attachments AS (
+ `
+ WITH agg_attachments AS (
SELECT
c.id as comment_id,
ARRAY_REMOVE(ARRAY_AGG(at.attachment_bkey), NULL) as attachment_bkeys
@@ -144,6 +164,18 @@ func getCommentsByPost(ctx context.Context, q *query.GetCommentsByPost) error {
AND at.tenant_id = $2
AND at.comment_id IS NOT NULL
GROUP BY c.id
+ ),
+ agg_reactions AS (
+ SELECT
+ comment_id,
+ json_object_agg(emoji, count) as reaction_counts
+ FROM (
+ SELECT comment_id, emoji, COUNT(*) as count
+ FROM reactions
+ WHERE comment_id IN (SELECT id FROM comments WHERE post_id = $1)
+ GROUP BY comment_id, emoji
+ ) r
+ GROUP BY comment_id
)
SELECT c.id,
c.content,
@@ -163,7 +195,8 @@ func getCommentsByPost(ctx context.Context, q *query.GetCommentsByPost) error {
e.status AS edited_by_status,
e.avatar_type AS edited_by_avatar_type,
e.avatar_bkey AS edited_by_avatar_bkey,
- at.attachment_bkeys
+ at.attachment_bkeys,
+ ar.reaction_counts
FROM comments c
INNER JOIN posts p
ON p.id = c.post_id
@@ -176,6 +209,8 @@ func getCommentsByPost(ctx context.Context, q *query.GetCommentsByPost) error {
AND e.tenant_id = c.tenant_id
LEFT JOIN agg_attachments at
ON at.comment_id = c.id
+ LEFT JOIN agg_reactions ar
+ ON ar.comment_id = c.id
WHERE p.id = $1
AND p.tenant_id = $2
AND c.deleted_at IS NULL
diff --git a/migrations/202410122105_create_reactions_up.sql b/migrations/202410122105_create_reactions_up.sql
new file mode 100644
index 000000000..7f3307f55
--- /dev/null
+++ b/migrations/202410122105_create_reactions_up.sql
@@ -0,0 +1,9 @@
+create table if not exists reactions (
+ id serial primary key,
+ emoji varchar(8) not null,
+ comment_id int not null,
+ user_id int not null,
+ created_on timestamptz not null,
+ foreign key (comment_id) references comments(id),
+ foreign key (user_id) references users(id)
+);
diff --git a/public/models/post.ts b/public/models/post.ts
index 57bc2b372..efc454580 100644
--- a/public/models/post.ts
+++ b/public/models/post.ts
@@ -57,6 +57,9 @@ export interface Comment {
createdAt: string
user: User
attachments?: string[]
+ reactionCounts: {
+ [key: string]: number
+ }
editedAt?: string
editedBy?: User
}
diff --git a/public/pages/ShowPost/components/ShowComment.tsx b/public/pages/ShowPost/components/ShowComment.tsx
index 429ee42e2..94349bb0d 100644
--- a/public/pages/ShowPost/components/ShowComment.tsx
+++ b/public/pages/ShowPost/components/ShowComment.tsx
@@ -178,6 +178,15 @@ export const ShowComment = (props: ShowCommentProps) => {
<>
<Markdown text={comment.content} style="full" />
{comment.attachments && comment.attachments.map((x) => <ImageViewer key={x} bkey={x} />)}
+ {comment.reactionCounts !== undefined && (
+ <div className="mt-2 flex space-x-2">
+ {Object.entries(comment.reactionCounts).map(([emoji, count]) => (
+ <span key={emoji} className="inline-flex items-center px-2 py-1 rounded-full text-xs bg-gray-100 mr-2">
+ {emoji} <span className="ml-1 font-semibold">{count}</span>
+ </span>
+ ))}
+ </div>
+ )}
</>
)}
</div>