/
types.go
69 lines (62 loc) · 1.22 KB
/
types.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
package lobsters
import "time"
type Thread struct {
op Op
comments []Comment
CommentWidth int
LeftPadding int
LineWidth int
ShowDate bool
ShowAuthor bool
}
type Op struct {
createdAt time.Time
id string
message string
nComments int
self string
score int
title string
thread *Thread
url string
username string
}
type Comment struct {
createdAt time.Time
depth int
id string
message string
parent string
replies []Comment
score int
thread *Thread
username string
}
func (t *Thread) insert(comment Comment) {
if comment.parent == "" {
t.comments = append(t.comments, comment)
} else {
parentComment := t.findComment(comment.parent)
comment.depth = parentComment.depth + 1
parentComment.replies = append(parentComment.replies, comment)
}
}
func (t *Thread) findComment(id string) *Comment {
for i := range t.comments {
if found := t.comments[i].findComment(id); found != nil {
return found
}
}
return nil
}
func (c *Comment) findComment(id string) *Comment {
if c.id == id {
return c
}
for i := range c.replies {
if found := c.replies[i].findComment(id); found != nil {
return found
}
}
return nil
}