forked from bigbluebutton-bot/bigbluebutton-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.go
More file actions
172 lines (136 loc) · 4.02 KB
/
bot.go
File metadata and controls
172 lines (136 loc) · 4.02 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
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
package bot
import (
"errors"
"net/http"
"sync"
"time"
api "github.com/bigbluebutton-bot/bigbluebutton-bot/api"
ddp "github.com/gopackage/ddp"
bbb "github.com/bigbluebutton-bot/bigbluebutton-bot/bbb"
pad "github.com/bigbluebutton-bot/bigbluebutton-bot/pad"
)
type StatusType string
const (
DISCONNECTING StatusType = "disconnecting"
DISCONNECTED StatusType = "disconnected"
CONNECTING StatusType = "connecting"
CONNECTED StatusType = "connected"
RECONNECTING StatusType = "reconnecting"
)
// Client represents a BigBlueButton client connection. The BigBlueButton client establish a BigBlueButton
// session and acts as a message pump for other tools.
type Client struct {
// Status is the current connection status of the client
Status StatusType
// BBB-urls the client is connected to
ClientURL string
ClientWSURL string
PadURL string
PadWSURL string
WebRTCWSURL string
// to make api requests to the BBB-server
API *api.ApiRequest
ddpClient *ddp.Client
// events will store all the functions executed on certain events. (events["OnStatus"][]func(StatusType))
events map[string][]interface{}
ddpEventHandler *ddpEventHandler
// after join there are the following informations
JoinURL string
SessionCookie []*http.Cookie
InternalUserID string
UserName string
AuthToken string
SessionToken string
ExternalMeetingID string
InternalMeetingID string
// Pads (Captures and shared notes)
padMutex *sync.Mutex
captures []*pad.Pad
}
func NewClient(clientURL string, clientWSURL string, padURL string, padWSURL string, apiURL string, apiSecret string, webRTCWSURL string) (*Client, error) {
api, err := api.NewRequest(apiURL, apiSecret, api.SHA256)
if err != nil {
return nil, err
}
ddpClient := ddp.NewClient(clientWSURL, clientURL)
c := &Client{
Status: DISCONNECTED,
ClientURL: clientURL,
ClientWSURL: clientWSURL,
PadURL: padURL,
PadWSURL: padWSURL,
WebRTCWSURL: webRTCWSURL,
ddpClient: ddpClient,
API: api,
events: make(map[string][]interface{}),
ddpEventHandler: nil,
padMutex: new(sync.Mutex),
captures: make([]*pad.Pad, 0),
}
c.ddpEventHandler = &ddpEventHandler{
client: c,
updater: make(map[string][]updaterfunc),
}
return c, nil
}
// Join a meeting
func (c *Client) Join(meetingID string, userName string, moderator bool) error {
if c.Status != DISCONNECTED {
c.Leave()
}
c.Status = CONNECTING
joinURL, coockie, internalUserID, authToken, sessionToken, internalMeetingID, err := c.API.Join(meetingID, userName, moderator)
if err != nil {
return err
}
c.JoinURL = joinURL
c.SessionCookie = coockie
c.InternalUserID = internalUserID
c.UserName = userName
c.AuthToken = authToken
c.SessionToken = sessionToken
c.ExternalMeetingID = meetingID
c.InternalMeetingID = internalMeetingID
// Connect to the DDP server
if err = c.ddpConnect(); err != nil {
c.Status = DISCONNECTED
return err
}
// Subscribe to the current user
if err = c.ddpSubscribe(bbb.CurrentUser, nil); err != nil {
c.Status = DISCONNECTED
return err
}
// Call the validateAuthToken method with the userID, authToken, and userName
_, err = c.ddpCall(bbb.ValidateAuthTokenCall, internalMeetingID, internalUserID, authToken, internalUserID)
if err != nil {
c.Status = DISCONNECTED
return errors.New("could not validateAuthToken")
}
c.Status = CONNECTED
return nil
}
// Leave the joined meeting
func (c *Client) Leave() error {
// If not connected, return an error
if c.Status != CONNECTED {
// If is connecting retry 5 times
if c.Status == CONNECTING {
i := 0
for i < 5 {
if c.Status == CONNECTED {
c.Leave()
}
time.Sleep(time.Second * 1)
i += 1
}
}
return errors.New("Client is in no meeting. First Join a meeting with: client.Join(meetingID string, userName string, moderator bool)")
}
c.ddpCall(bbb.UserLeftMeetingCall)
c.ddpCall(bbb.SetExitReasonCall, "logout")
// c.ddpClient.UnSubscribe("from all subs")
c.ddpDisconnect()
c.Status = DISCONNECTED
return nil
}