-
Notifications
You must be signed in to change notification settings - Fork 0
/
drisqus_d3js.go
160 lines (131 loc) · 4.01 KB
/
drisqus_d3js.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright Piero de Salvia.
// All Rights Reserved
package drisqus
import (
"strconv"
"github.com/pierods/gisqus"
)
// RepliesInThread is used by MakeReplySlice
type RepliesInThread struct {
ID AuthorID
UserName AuthorUsername
Replies ReplyCount
}
/*
MakeReplySlice is a d3.js-friendly method, makes a slice of author-replies
*/
func (d *Drisqus) MakeReplySlice(authorStatsMap *map[AuthorID]*AuthorStats) []RepliesInThread {
var replies []RepliesInThread
for _, authorStats := range *authorStatsMap {
replies = append(replies, RepliesInThread{
authorStats.ID,
authorStats.Username,
authorStats.Replies,
})
}
return replies
}
/*
MakeReplyWrittenSlice is a d3.js-friendly method, makes a slice of author-replies (written)
*/
func (d *Drisqus) MakeReplyWrittenSlice(authorStatsMap *map[AuthorID]*AuthorStats) []RepliesInThread {
var replies []RepliesInThread
for _, authorStats := range *authorStatsMap {
replies = append(replies, RepliesInThread{
authorStats.ID,
authorStats.Username,
authorStats.RepliesWritten,
})
}
return replies
}
// PostsByAuthor is used by MakePostCountSlice
type PostsByAuthor struct {
ID AuthorID
UserName AuthorUsername
Posts PostCount
}
/*
MakePostCountSlice is a d3.js-friendly method, makes a slice of author-post count
*/
func (d *Drisqus) MakePostCountSlice(authorStatsMap *map[AuthorID]*AuthorStats) []PostsByAuthor {
var posts []PostsByAuthor
for _, authorStats := range *authorStatsMap {
posts = append(posts, PostsByAuthor{
authorStats.ID,
authorStats.Username,
authorStats.Posts,
})
}
return posts
}
//LikesByAuthor is used by MakeLikeCountSlice
type LikesByAuthor struct {
ID AuthorID
UserName AuthorUsername
Likes LikeCount
Dislikes DislikeCount
}
/*
MakeLikeCountSlice is a d3.js-friendly method, makes a slice of author-likes-dislikes count
*/
func (d *Drisqus) MakeLikeCountSlice(authorStatsMap *map[AuthorID]*AuthorStats) []LikesByAuthor {
var likes []LikesByAuthor
for _, authorStats := range *authorStatsMap {
likes = append(likes, LikesByAuthor{
authorStats.ID,
authorStats.Username,
authorStats.Likes,
authorStats.Dislikes,
})
}
return likes
}
// AuthorReplyCount is used by MakeReplyGroups
type AuthorReplyCount struct {
ReplierName AuthorUsername `json:"replierName"`
AuthorName AuthorUsername `json:"authorName"`
Replies ReplyCount `json:"replies"`
}
// MarshalJSON is used to coerce an AuthorReplyCount into a mixed type Javascript array of the form [string, string, int]
func (arc *AuthorReplyCount) MarshalJSON() ([]byte, error) {
replies := strconv.Itoa(int(arc.Replies))
oneEl := `["` + string(arc.ReplierName) + `","` + string(arc.AuthorName) + `",` + replies + `]`
return []byte(oneEl), nil
}
/*
MakeReplyGroups is a d3.js-friendly method, makes a slice of author-to-author reply counts
*/
func (d *Drisqus) MakeReplyGroups(posts []*gisqus.Post) *[]AuthorReplyCount {
postMap := make(map[PostID]*gisqus.Post)
for _, post := range posts {
postMap[PostID(post.ID)] = post
}
authorMap := make(map[AuthorUsername]map[AuthorUsername]int)
for _, post := range postMap {
// let's count replies.
if post.Parent != 0 { // roots don't have parents (are not replies)
parentID := strconv.Itoa(post.Parent)
// it's not an orphan post, otherwise could not attribute reply to parent
if parentPost, exists := postMap[PostID(parentID)]; exists {
var parentAuthorsMap map[AuthorUsername]int
if parentAuthorsMap, exists = authorMap[AuthorUsername(post.Author.Username)]; !exists {
parentAuthorsMap = make(map[AuthorUsername]int)
authorMap[AuthorUsername(post.Author.Username)] = parentAuthorsMap
}
parentAuthorsMap[AuthorUsername(parentPost.Author.Username)]++
}
}
}
var replySlice []AuthorReplyCount
for replier, parentMap := range authorMap {
for author, replies := range parentMap {
replySlice = append(replySlice, AuthorReplyCount{
ReplierName: replier,
AuthorName: author,
Replies: ReplyCount(replies),
})
}
}
return &replySlice
}