-
Notifications
You must be signed in to change notification settings - Fork 1
/
explore.go
84 lines (77 loc) · 2.07 KB
/
explore.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"github.com/go-chi/chi/v5"
"github.com/rkonfj/lln/state"
"github.com/rkonfj/lln/tools"
"github.com/sirupsen/logrus"
)
func explore(w http.ResponseWriter, r *http.Request) {
opts, err := tools.URLPaginationOptions(r)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, err.Error())
return
}
user := currentSessionUser(r)
ss, more := state.Recommendations(user, opts)
var ret []*Status
for _, s := range ss {
status := castStatus(s, user)
if s.Comments > 0 {
meta, err := state.NewCommentsRecommandMeta(s.ID)
if err != nil {
logrus.Errorf("create comments recommand meta error: %s", err)
continue
}
next := meta.Recommand()
if next != nil {
status.Next = castStatus(next, user)
}
}
ret = append(ret, status)
}
json.NewEncoder(w).Encode(L{V: ret, More: more})
}
type NewsProbeResponse struct {
News int `json:"news"`
CommentsMap map[string]int64 `json:"cm"`
LikesMap map[string]int64 `json:"lm"`
}
func exploreNewsProbe(w http.ResponseWriter, r *http.Request) {
maxCreateRev, err := tools.URLQueryInt64(r, "max")
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, err.Error())
return
}
minCreateRev, err := tools.URLQueryInt64(r, "min")
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, err.Error())
return
}
user := currentSessionUser(r)
probeResp := NewsProbeResponse{
News: state.RecommendCount(user, maxCreateRev),
CommentsMap: state.CommentsMap(minCreateRev, maxCreateRev),
LikesMap: state.LikesMap(minCreateRev, maxCreateRev),
}
json.NewEncoder(w).Encode(R{V: probeResp})
}
func exploreStatusComment(w http.ResponseWriter, r *http.Request) {
meta, err := state.NewCommentsRecommandMeta(chi.URLParam(r, tools.StatusID))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, err.Error())
return
}
s := meta.Recommand()
if s != nil {
json.NewEncoder(w).Encode(R{V: castStatus(s, currentSessionUser(r))})
return
}
json.NewEncoder(w).Encode(R{})
}