forked from gnoverse/dsocial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphql_client.go
181 lines (157 loc) · 4.23 KB
/
graphql_client.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
"github.com/Khan/genqlient/graphql"
"github.com/gorilla/websocket"
)
var gUserPostsByAddress = make(map[string]*UserPosts) // user's std.Address -> *UserPosts
type UserAndPostID struct {
UserPostAddr string
PostID int
}
type UserPosts struct {
homePosts []*UserAndPostID // Includes this user's threads posts plus posts of users being followed.
followers map[string]int // std.Address -> startedPostsCtr of the follower
}
func newUserPosts() *UserPosts {
return &UserPosts{
homePosts: []*UserAndPostID{},
followers: make(map[string]int),
}
}
func getOrCreateUserPosts(userAddr string) *UserPosts {
userPosts, ok := gUserPostsByAddress[userAddr]
if ok {
return userPosts
}
userPosts = newUserPosts()
gUserPostsByAddress[userAddr] = userPosts
return userPosts
}
func getPostID(response string) (int, error) {
const postIDSuffix = " gno.land/r/berty/social.PostID)"
if !strings.HasSuffix(response, postIDSuffix) {
return 0, errors.New("Expected PostID suffix: " + response)
}
postID, err := strconv.Atoi(response[1 : len(response)-len(postIDSuffix)])
if err != nil {
return 0, err
}
return postID, nil
}
func processCallInfo(messages []CallInfoMessagesTransactionMessage, response CallInfoResponseTransactionResponse) {
for _, m := range messages {
msgCall, ok := m.Value.(*CallInfoMessagesTransactionMessageValueMsgCall)
if !ok {
continue
}
if msgCall.Func == "Follow" {
followedUserPosts := getOrCreateUserPosts(msgCall.Args[0])
startedPostsCtr, err := getPostID(response.Data)
if err != nil {
fmt.Printf("Error getting PostID: %s\n", err.Error())
continue
}
if startedPostsCtr == 0 {
// Not a new following.
continue
}
followedUserPosts.followers[msgCall.Caller] = startedPostsCtr
} else if msgCall.Func == "PostMessage" || msgCall.Func == "RepostThread" {
userPosts := getOrCreateUserPosts(msgCall.Caller)
postID, err := getPostID(response.Data)
if err != nil {
fmt.Printf("Error getting PostID: %s\n", err.Error())
continue
}
userAndPostID := &UserAndPostID{
UserPostAddr: msgCall.Caller,
PostID: postID,
}
userPosts.homePosts = append(userPosts.homePosts, userAndPostID)
for followerAddr, startedPostsCtr := range userPosts.followers {
if postID > startedPostsCtr {
followerUserPosts := getOrCreateUserPosts(followerAddr)
followerUserPosts.homePosts = append(followerUserPosts.homePosts, userAndPostID)
}
}
}
}
}
func (s *indexerService) createGraphQLClient() error {
s.graphQLClient = graphql.NewClient(s.remoteAddr, http.DefaultClient)
resp, err := getTransactions(s.ctx, s.graphQLClient)
if err != nil {
return err
}
for _, t := range resp.Transactions {
processCallInfo(t.Messages, t.Response)
}
clientAddr, err := websocketURL(s.remoteAddr)
if err != nil {
return err
}
wsClient := graphql.NewClientUsingWebSocket(
clientAddr,
&MyDialer{Dialer: websocket.DefaultDialer},
nil,
)
errChan, err := wsClient.StartWebSocket(s.ctx)
if err != nil {
return err
}
dataChan, subscriptionID, err := subscribeTransactions(s.ctx, wsClient)
if err != nil {
return err
}
defer wsClient.CloseWebSocket()
for loop := true; loop; {
select {
case msg, more := <-dataChan:
if !more {
loop = false
break
}
if msg.Data != nil {
processCallInfo(msg.Data.Transactions.Messages, msg.Data.Transactions.Response)
}
if msg.Errors != nil {
fmt.Println("error:", msg.Errors)
loop = false
}
case err = <-errChan:
return err
case <-s.ctx.Done():
wsClient.Unsubscribe(subscriptionID)
loop = false
}
}
return nil
}
func websocketURL(addr string) (string, error) {
url, err := url.Parse(addr)
if err != nil {
return "", err
}
var wsScheme string
switch url.Scheme {
case "https", "wss":
wsScheme = "wss"
default:
wsScheme = "ws"
}
return fmt.Sprintf("%s://%s%s", wsScheme, url.Host, url.Path), nil
}
type MyDialer struct {
*websocket.Dialer
}
func (md *MyDialer) DialContext(ctx context.Context, urlStr string, requestHeader http.Header) (graphql.WSConn, error) {
conn, _, err := md.Dialer.DialContext(ctx, urlStr, requestHeader)
return graphql.WSConn(conn), err
}