-
Notifications
You must be signed in to change notification settings - Fork 0
/
feeders.go
114 lines (94 loc) · 3.2 KB
/
feeders.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
package views
import (
"fmt"
"github.com/horkruxes/hkxserver/client"
"github.com/horkruxes/hkxserver/model"
"github.com/horkruxes/hkxserver/query"
"github.com/horkruxes/hkxserver/service"
)
type ClientData struct {
PublicPods bool
PrivatePods bool
PodsString string
}
// PageData represent information sent to the page
type PageData struct {
Server service.GeneralConfig
Client ClientData // Filters to feed back loop
TopMessage model.Message
Messages []model.Message
PageInfo PageInfo
}
type PageInfo struct {
MainPage bool
CommentsPage bool
Title string
SubTitle string
PostToMessageID string
}
// Get Local and online messages, checks validity and return view
func GetMessagesAndMainPageInfo(s service.Service) PageData {
// Get local messages
messages := query.GetMessages(s)
// Get other pods messages
if s.ClientConfig.PublicPods {
remoteMessages := client.GetMessagesFrom(s.GeneralConfig.TrustedPods, "/api/message")
messages = append(messages, remoteMessages...)
}
messages = model.SortByDate(messages)
messages = client.CleanMessagesClientSide(messages)
for i, msg := range messages {
if len(msg.Content) > 250 {
messages[i].Content = msg.Content[:250] + "..."
}
}
// Inject view
return PageData{
Messages: messages,
Server: s.GeneralConfig,
// Client: ClientData{PublicPods: s.ClientConfig.PublicPods, PrivatePods: s.ClientConfig.SpecificPods, PodsString: s.ClientConfig.SpecificPodsListString},
PageInfo: PageInfo{MainPage: true, Title: "All Messages"},
}
}
// Get Local and online messages, checks validity and return view
// Pubkey is in base64 form
func GetAuthorMessagesAndMainPageInfo(s service.Service, pubKey string) PageData {
// Get local messages
messages := query.GetMessagesFromAuthor(s, pubKey)
// Get other pods messages
remoteMessages := client.GetMessagesFrom(s.GeneralConfig.TrustedPods, "/api/user/"+pubKey)
messages = append(messages, remoteMessages...)
messages = model.SortByDate(messages)
// Inject view
return PageData{
Messages: client.CleanMessagesClientSide(messages),
Server: s.GeneralConfig,
PageInfo: PageInfo{Title: "Author", SubTitle: pubKey},
}
}
// Get Local and online messages, checks validity and return view
func GetCommentsAndMainPageInfo(s service.Service, messageID string) PageData {
messages := []model.Message{}
// Get local comments
messages = append(messages, query.GetCommentsTo(s, messageID)...)
// Get other pods comments
remoteMessages := client.GetMessagesFrom(s.GeneralConfig.TrustedPods, "/api/comments/"+messageID)
messages = append(messages, remoteMessages...)
messages = model.SortByDate(messages)
messages = client.CleanMessagesClientSide(messages)
// Try to get local OP
op, err := query.GetMessage(s, messageID)
if err != nil {
fmt.Println("err:", err)
// Asks other pods to get comment
op = client.GetSingleMessageFromEachPod(s.GeneralConfig.TrustedPods, "/api/message/"+messageID)
}
op = client.CleanSingleMessageClientSide(op)
// Inject view
return PageData{
TopMessage: op,
Messages: messages,
Server: s.GeneralConfig,
PageInfo: PageInfo{Title: "Comments", SubTitle: messageID, PostToMessageID: messageID, CommentsPage: true},
}
}