Skip to content

Commit

Permalink
feat: added user comment count support (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
wyattjoh authored Aug 24, 2021
1 parent 13bbc93 commit 3d0193d
Show file tree
Hide file tree
Showing 10 changed files with 333 additions and 108 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
fetch-depth: 0
- uses: actions/setup-go@v2
with:
go-version: "^1.15.6"
go-version: "^1.16.6"
- uses: golangci/golangci-lint-action@v2
with:
version: v1.35
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- uses: actions/[email protected]
- uses: actions/setup-go@v2
with:
go-version: "^1.15.6"
go-version: "^1.16.6"
- uses: golangci/golangci-lint-action@v2
with:
version: v1.35
Expand Down
9 changes: 9 additions & 0 deletions counts/comment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package counts

// Comment is a Comment in Coral.
type Comment struct {
AuthorID string `bson:"authorID"`
StoryID string `bson:"storyID"`
Status string `bson:"status"`
ActionCounts map[string]int `bson:"actionCounts"`
}
77 changes: 60 additions & 17 deletions counts/counts.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,63 @@
package counts

type CommentCounts struct {
Action map[string]int `bson:"action"`
Status struct {
Approved int `bson:"APPROVED"`
None int `bson:"NONE"`
Premod int `bson:"PREMOD"`
Rejected int `bson:"REJECTED"`
SystemWithheld int `bson:"SYSTEM_WITHHELD"`
} `bson:"status"`
ModerationQueue struct {
Total int `bson:"total"`
Queues struct {
Unmoderated int `bson:"unmoderated"`
Reported int `bson:"reported"`
Pending int `bson:"pending"`
} `bson:"queues"`
} `bson:"moderationQueue"`
type CommentStatusCounts struct {
Approved int `bson:"APPROVED"`
None int `bson:"NONE"`
Premod int `bson:"PREMOD"`
Rejected int `bson:"REJECTED"`
SystemWithheld int `bson:"SYSTEM_WITHHELD"`
}

func (csc *CommentStatusCounts) Increment(comment *Comment) {
switch comment.Status {
case "APPROVED":
csc.Approved++
case "NONE":
csc.None++
case "PREMOD":
csc.Premod++
case "REJECTED":
csc.Rejected++
case "SYSTEM_WITHHELD":
csc.SystemWithheld++
}
}

type CommentModerationQueue struct {
Total int `bson:"total"`
Queues struct {
Unmoderated int `bson:"unmoderated"`
Reported int `bson:"reported"`
Pending int `bson:"pending"`
} `bson:"queues"`
}

func (cmq *CommentModerationQueue) Increment(comment *Comment) {
switch comment.Status {
case "NONE":
cmq.Total++
cmq.Queues.Unmoderated++

// If this comment has a flag on it, then it should also be in the reported
// queue.
if count, ok := comment.ActionCounts["FLAG"]; ok && count > 0 {
cmq.Queues.Reported++
}
case "PREMOD":
cmq.Total++
cmq.Queues.Unmoderated++
cmq.Queues.Pending++
case "SYSTEM_WITHHELD":
cmq.Total++
cmq.Queues.Unmoderated++
cmq.Queues.Pending++
}
}

type CommentActionCounts map[string]int

func (cac CommentActionCounts) Increment(comment *Comment) {
for key, count := range comment.ActionCounts {
cac[key] += count
}
}
26 changes: 3 additions & 23 deletions counts/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,8 @@ import (
)

type Site struct {
ID string `bson:"id"`
CommentCounts CommentCounts `bson:"commentCounts"`
}

func (s *Site) Increment(story *Story) {
// Action
for key, count := range story.CommentCounts.Action {
s.CommentCounts.Action[key] += count
}

// Status
s.CommentCounts.Status.Approved += story.CommentCounts.Status.Approved
s.CommentCounts.Status.None += story.CommentCounts.Status.None
s.CommentCounts.Status.Premod += story.CommentCounts.Status.Premod
s.CommentCounts.Status.Rejected += story.CommentCounts.Status.Rejected
s.CommentCounts.Status.SystemWithheld += story.CommentCounts.Status.SystemWithheld

// ModerationQueue
s.CommentCounts.ModerationQueue.Total += story.CommentCounts.ModerationQueue.Total
s.CommentCounts.ModerationQueue.Queues.Unmoderated += story.CommentCounts.ModerationQueue.Queues.Unmoderated
s.CommentCounts.ModerationQueue.Queues.Reported += story.CommentCounts.ModerationQueue.Queues.Reported
s.CommentCounts.ModerationQueue.Queues.Pending += story.CommentCounts.ModerationQueue.Queues.Pending
ID string `bson:"id"`
CommentCounts StoryCommentCounts `bson:"commentCounts"`
}

// ProcessSite will update a given site's counts based on the story documents
Expand Down Expand Up @@ -81,7 +61,7 @@ func ProcessSite(ctx context.Context, db *mongo.Database, tenantID, siteID strin
}

// Increment the site document based on this story.
site.Increment(&story)
site.CommentCounts.Merge(&story.CommentCounts)
}

if err := cursor.Err(); err != nil {
Expand Down
70 changes: 29 additions & 41 deletions counts/stories.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,60 +12,48 @@ import (
"go.mongodb.org/mongo-driver/mongo/options"
)

// Comment is a Comment in Coral.
type Comment struct {
StoryID string `bson:"storyID"`
Status string `bson:"status"`
ActionCounts map[string]int `bson:"actionCounts"`
type StoryCommentCounts struct {
Action CommentActionCounts `bson:"action"`
Status CommentStatusCounts `bson:"status"`
ModerationQueue CommentModerationQueue `bson:"moderationQueue"`
}

func (scc *StoryCommentCounts) Merge(counts *StoryCommentCounts) {
// Action
for key, count := range counts.Action {
scc.Action[key] += count
}

// Status
scc.Status.Approved += counts.Status.Approved
scc.Status.None += counts.Status.None
scc.Status.Premod += counts.Status.Premod
scc.Status.Rejected += counts.Status.Rejected
scc.Status.SystemWithheld += counts.Status.SystemWithheld

// ModerationQueue
scc.ModerationQueue.Total += counts.ModerationQueue.Total
scc.ModerationQueue.Queues.Unmoderated += counts.ModerationQueue.Queues.Unmoderated
scc.ModerationQueue.Queues.Reported += counts.ModerationQueue.Queues.Reported
scc.ModerationQueue.Queues.Pending += counts.ModerationQueue.Queues.Pending
}

// Story is a Story in Coral.
type Story struct {
ID string `bson:"id"`
CommentCounts CommentCounts `bson:"commentCounts"`
ID string `bson:"id"`
CommentCounts StoryCommentCounts `bson:"commentCounts"`
}

// Increment will increment the comment counts based on the passed comment.
func (s *Story) Increment(comment *Comment) {
// Action
for key, count := range comment.ActionCounts {
s.CommentCounts.Action[key] += count
}
s.CommentCounts.Action.Increment(comment)

// Status
switch comment.Status {
case "APPROVED":
s.CommentCounts.Status.Approved++
case "NONE":
s.CommentCounts.Status.None++
case "PREMOD":
s.CommentCounts.Status.Premod++
case "REJECTED":
s.CommentCounts.Status.Rejected++
case "SYSTEM_WITHHELD":
s.CommentCounts.Status.SystemWithheld++
}
s.CommentCounts.Status.Increment(comment)

// ModerationQueue
switch comment.Status {
case "NONE":
s.CommentCounts.ModerationQueue.Total++
s.CommentCounts.ModerationQueue.Queues.Unmoderated++

// If this comment has a flag on it, then it should also be in the reported
// queue.
if count, ok := comment.ActionCounts["FLAG"]; ok && count > 0 {
s.CommentCounts.ModerationQueue.Queues.Reported++
}
case "PREMOD":
s.CommentCounts.ModerationQueue.Total++
s.CommentCounts.ModerationQueue.Queues.Unmoderated++
s.CommentCounts.ModerationQueue.Queues.Pending++
case "SYSTEM_WITHHELD":
s.CommentCounts.ModerationQueue.Total++
s.CommentCounts.ModerationQueue.Queues.Unmoderated++
s.CommentCounts.ModerationQueue.Queues.Pending++
}
s.CommentCounts.ModerationQueue.Increment(comment)
}

// ProcessStories will iterate over each stories comments and aggregate the
Expand Down
Loading

0 comments on commit 3d0193d

Please sign in to comment.