-
Notifications
You must be signed in to change notification settings - Fork 0
/
federatedclient.go
164 lines (135 loc) · 3.74 KB
/
federatedclient.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
package shibshib
import (
"fmt"
"net/url"
"reflect"
"strings"
authv1 "github.com/harmony-development/shibshib/gen/auth/v1"
chatv1 "github.com/harmony-development/shibshib/gen/chat/v1"
)
type FederatedClient struct {
Client
homeserver string
subclients map[string]*Client
streams map[<-chan *LocatedMessage]*Client
listening map[*Client]<-chan *LocatedMessage
}
type FederatedEvent struct {
Client *Client
Event *LocatedMessage
}
func NewFederatedClient(homeserver, token string, userID uint64) (*FederatedClient, error) {
url, err := url.Parse(homeserver)
if err != nil {
return nil, err
}
it := "wss"
if url.Scheme == "http" {
it = "ws"
}
self := &FederatedClient{}
self.homeserver = homeserver
self.Client.homeserver = homeserver
self.init(homeserver, it, url.Host)
self.authed(token, userID)
self.subclients = make(map[string]*Client)
self.streams = make(map[<-chan *LocatedMessage]*Client)
self.listening = make(map[*Client]<-chan *LocatedMessage)
err = self.StreamEvents()
if err != nil {
return nil, fmt.Errorf("NewFederatedClient: own streamevents failed: %w", err)
}
return self, nil
}
func (f *FederatedClient) clientFor(homeserver string) (*Client, error) {
if f.homeserver == homeserver || strings.Split(homeserver, ":")[0] == "localhost" || homeserver == "" {
return &f.Client, nil
}
if val, ok := f.subclients[homeserver]; ok {
return val, nil
}
session, err := f.AuthKit.Federate(&authv1.FederateRequest{
ServerId: homeserver,
})
if err != nil {
return nil, fmt.Errorf("ClientFor: homeserver federation step failed: %w", err)
}
url, err := url.Parse(homeserver)
if err != nil {
return nil, err
}
scheme := "wss"
if url.Scheme == "http" {
scheme = "ws"
}
c := new(Client)
c.init(homeserver, scheme, url.Host)
data, err := c.AuthKit.LoginFederated(&authv1.LoginFederatedRequest{
AuthToken: session.Token,
ServerId: f.homeserver,
})
if err != nil {
return nil, fmt.Errorf("ClientFor: failed to log into foreignserver: %w", err)
}
c.authed(data.Session.SessionToken, data.Session.UserId)
err = c.StreamEvents()
if err != nil {
return nil, fmt.Errorf("ClientFor: failed to stream events for foreign server: %w", err)
}
f.subclients[homeserver] = c
return c, nil
}
func (f *FederatedClient) Start() (<-chan FederatedEvent, error) {
list, err := f.ChatKit.GetGuildList(&chatv1.GetGuildListRequest{})
if err != nil {
return nil, fmt.Errorf("Start: failed to get guild list on homeserver: %w", err)
}
cases := []reflect.SelectCase{}
for _, g := range list.Guilds {
client, err := f.clientFor(g.ServerId)
if err != nil {
return nil, fmt.Errorf("Start: failed to get client for guild %s/%d: %w", g.ServerId, g.GuildId, err)
}
stream, ok := f.listening[client]
if !ok {
stream = client.EventsStream()
cases = append(cases, reflect.SelectCase{
Dir: reflect.SelectRecv,
Chan: reflect.ValueOf(stream),
})
client.ErrorHandler = func(e error) {
if e == ErrEndOfStream {
err := client.StreamEvents()
if err != nil {
panic(fmt.Errorf("client.ErrorHandler: could not restart stream: %w", err))
}
cp := client.subscribedGuilds
client.subscribedGuilds = make([]uint64, 0)
for _, gg := range cp {
client.SubscribeToGuild(gg)
}
} else {
panic(err)
}
}
f.listening[client] = stream
f.streams[stream] = client
}
client.SubscribeToGuild(g.GuildId)
}
channel := make(chan FederatedEvent)
go func() {
for {
i, v, ok := reflect.Select(cases)
if !ok {
cases = append(cases[:i], cases[i+1:]...)
}
val := v.Interface().(*LocatedMessage)
channel <- FederatedEvent{
Event: val,
Client: f.streams[cases[i].Chan.Interface().(<-chan *LocatedMessage)],
}
}
}()
return channel, nil
}