-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathactivity_handler.go
More file actions
56 lines (52 loc) · 1.23 KB
/
activity_handler.go
File metadata and controls
56 lines (52 loc) · 1.23 KB
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
package server
import (
"github.com/labstack/echo"
)
type jsonResponseActivity struct {
Type string `json:"type"`
Data Activity `json:"data"`
}
type jsonResponse struct {
Activity []*jsonResponseActivity `json:"activity"`
Next string `json:"next"`
}
func ActivityHandler(db Database) echo.HandlerFunc {
return func(c echo.Context) error {
locale := LocaleForRequest(c.Request())
var filter func(Activity) bool
if c.QueryParams().Has("nohelp") && c.QueryParam("nohelp") != "false" {
filter = func(a Activity) bool {
if fp, ok := a.(*ForumPost); ok {
if fp.ForumId == locale.HelpForumId {
return false
}
}
return true
}
}
activity, next, err := db.Activity(locale, c.QueryParam("next"), 50, filter)
if err != nil {
return err
}
response := jsonResponse{
Next: next,
}
for _, a := range activity {
t := ""
switch a.(type) {
case *ForumPost:
t = "forum_post"
case *RedditComment:
t = "reddit_comment"
case *RedditPost:
t = "reddit_post"
}
response.Activity = append(response.Activity, &jsonResponseActivity{
Type: t,
Data: a,
})
}
c.Response().Header().Add("Cache-Control", "max-age=120")
return c.JSON(200, response)
}
}