This repository has been archived by the owner on Feb 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathcomments.go
147 lines (118 loc) · 3.35 KB
/
comments.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
package strava
import (
"encoding/json"
"fmt"
"time"
)
type CommentDetailed struct {
CommentSummary
}
type CommentSummary struct {
Id int64 `json:"id"`
ActivityId int64 `json:"activity_id"`
Text string `json:"text"`
Athlete AthleteSummary `json:"athlete"`
CreatedAt time.Time `json:"-"`
CreatedAtString string `json:"created_at"` // the ISO 8601 encoding of when the comment was posted
}
type ActivityCommentsService struct {
client *Client
activityId int64
}
func NewActivityCommentsService(client *Client, activityId int64) *ActivityCommentsService {
return &ActivityCommentsService{client: client, activityId: activityId}
}
/*********************************************************/
type ActivitiesCommentsListCall struct {
service *ActivityCommentsService
ops map[string]interface{}
}
func (s *ActivityCommentsService) List() *ActivitiesCommentsListCall {
return &ActivitiesCommentsListCall{
service: s,
ops: make(map[string]interface{}),
}
}
func (c *ActivitiesCommentsListCall) IncludeMarkdown() *ActivitiesCommentsListCall {
c.ops["markdown"] = true
return c
}
func (c *ActivitiesCommentsListCall) Page(page int) *ActivitiesCommentsListCall {
c.ops["page"] = page
return c
}
func (c *ActivitiesCommentsListCall) PerPage(perPage int) *ActivitiesCommentsListCall {
c.ops["per_page"] = perPage
return c
}
func (c *ActivitiesCommentsListCall) Do() ([]*CommentSummary, error) {
data, err := c.service.client.run("GET", fmt.Sprintf("/activities/%d/comments", c.service.activityId), c.ops)
if err != nil {
return nil, err
}
comments := make([]*CommentSummary, 0)
err = json.Unmarshal(data, &comments)
if err != nil {
return nil, err
}
for _, c := range comments {
c.postProcessSummary()
}
return comments, nil
}
/*********************************************************/
type ActivityCommentsPostCall struct {
service *ActivityCommentsService
text string
}
func (s *ActivityCommentsService) Create(text string) *ActivityCommentsPostCall {
return &ActivityCommentsPostCall{
service: s,
text: text,
}
}
func (c *ActivityCommentsPostCall) Do() (*CommentDetailed, error) {
data, err := c.service.client.run(
"POST",
fmt.Sprintf("/activities/%d/comments", c.service.activityId),
map[string]interface{}{"text": c.text},
)
if err != nil {
return nil, err
}
var comment CommentDetailed
err = json.Unmarshal(data, &comment)
if err != nil {
return nil, err
}
comment.postProcessDetailed()
return &comment, nil
}
/*********************************************************/
type ActivityCommentsDeleteCall struct {
service *ActivityCommentsService
activityId int64
commentId int64
}
func (s *ActivityCommentsService) Delete(commentId int64) *ActivityCommentsDeleteCall {
return &ActivityCommentsDeleteCall{
service: s,
commentId: commentId,
}
}
func (c *ActivityCommentsDeleteCall) Do() error {
_, err := c.service.client.run(
"DELETE",
fmt.Sprintf("/activities/%d/comments/%d", c.service.activityId, c.commentId),
nil,
)
return err
}
/*********************************************************/
func (c *CommentDetailed) postProcessDetailed() {
c.postProcessSummary()
}
func (c *CommentSummary) postProcessSummary() {
c.Athlete.postProcessSummary()
c.CreatedAt, _ = time.Parse(timeFormat, c.CreatedAtString)
}