2015-10-25 00:44:25 -07:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2015-10-26 14:55:20 -07:00
|
|
|
"fmt"
|
2015-10-25 00:44:25 -07:00
|
|
|
"github.com/satori/go.uuid"
|
2015-10-26 10:06:45 -07:00
|
|
|
"golang.org/x/net/websocket"
|
2015-10-25 00:44:25 -07:00
|
|
|
"log"
|
2015-10-25 00:58:05 -07:00
|
|
|
"strconv"
|
2015-10-26 10:06:45 -07:00
|
|
|
"sync"
|
2015-10-25 03:21:50 -07:00
|
|
|
"time"
|
2015-10-25 00:44:25 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
var ResponseSuccess = ClientMessage{Command: SuccessCommand}
|
|
|
|
var ResponseFailure = ClientMessage{Command: "False"}
|
|
|
|
|
2015-10-25 03:21:50 -07:00
|
|
|
const ChannelInfoDelay = 2 * time.Second
|
|
|
|
|
2015-10-25 00:44:25 -07:00
|
|
|
func HandleCommand(conn *websocket.Conn, client *ClientInfo, msg ClientMessage) {
|
|
|
|
handler, ok := CommandHandlers[msg.Command]
|
|
|
|
if !ok {
|
2015-10-26 14:55:20 -07:00
|
|
|
log.Println("[!] Unknown command", msg.Command, "- sent by client", client.ClientID, "@", conn.RemoteAddr())
|
|
|
|
FFZCodec.Send(conn, ClientMessage{
|
|
|
|
MessageID: msg.MessageID,
|
|
|
|
Command: "error",
|
|
|
|
Arguments: fmt.Sprintf("Unknown command %s", msg.Command),
|
|
|
|
})
|
2015-10-25 00:44:25 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := CallHandler(handler, conn, client, msg)
|
|
|
|
|
|
|
|
if err == nil {
|
2015-10-26 14:55:20 -07:00
|
|
|
if response.Command == AsyncResponseCommand {
|
|
|
|
// Don't send anything
|
|
|
|
// The response will be delivered over client.MessageChannel / serverMessageChan
|
|
|
|
} else {
|
|
|
|
response.MessageID = msg.MessageID
|
|
|
|
FFZCodec.Send(conn, response)
|
|
|
|
}
|
2015-10-25 00:44:25 -07:00
|
|
|
} else {
|
|
|
|
FFZCodec.Send(conn, ClientMessage{
|
|
|
|
MessageID: msg.MessageID,
|
2015-10-26 10:06:45 -07:00
|
|
|
Command: "error",
|
2015-10-25 00:44:25 -07:00
|
|
|
Arguments: err.Error(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func HandleHello(conn *websocket.Conn, client *ClientInfo, msg ClientMessage) (rmsg ClientMessage, err error) {
|
|
|
|
version, clientId, err := msg.ArgumentsAsTwoStrings()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
client.Version = version
|
|
|
|
client.ClientID = uuid.FromStringOrNil(clientId)
|
|
|
|
if client.ClientID == uuid.Nil {
|
|
|
|
client.ClientID = uuid.NewV4()
|
|
|
|
}
|
|
|
|
|
2015-10-26 10:07:15 -07:00
|
|
|
SubscribeGlobal(client)
|
|
|
|
|
2015-10-25 00:44:25 -07:00
|
|
|
return ClientMessage{
|
|
|
|
Arguments: client.ClientID.String(),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2015-10-26 14:55:20 -07:00
|
|
|
func HandleReady(conn *websocket.Conn, client *ClientInfo, msg ClientMessage) (rmsg ClientMessage, err error) {
|
|
|
|
disconnectAt, err := msg.ArgumentsAsInt()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
client.Mutex.Lock()
|
|
|
|
if client.MakePendingRequests != nil {
|
|
|
|
if !client.MakePendingRequests.Stop() {
|
|
|
|
// Timer already fired, GetSubscriptionBacklog() has started
|
|
|
|
rmsg.Command = SuccessCommand
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
client.PendingSubscriptionsBacklog = nil
|
|
|
|
client.MakePendingRequests = nil
|
|
|
|
client.Mutex.Unlock()
|
|
|
|
|
|
|
|
if disconnectAt == 0 {
|
|
|
|
// backlog only
|
|
|
|
go func() {
|
|
|
|
client.MessageChannel <- ClientMessage{MessageID: msg.MessageID, Command: SuccessCommand}
|
|
|
|
SendBacklogForNewClient(client)
|
|
|
|
}()
|
|
|
|
return ClientMessage{Command: AsyncResponseCommand}, nil
|
|
|
|
} else {
|
|
|
|
// backlog and timed
|
|
|
|
go func() {
|
|
|
|
client.MessageChannel <- ClientMessage{MessageID: msg.MessageID, Command: SuccessCommand}
|
|
|
|
SendBacklogForNewClient(client)
|
|
|
|
SendTimedBacklogMessages(client, time.Unix(disconnectAt, 0))
|
|
|
|
}()
|
|
|
|
return ClientMessage{Command: AsyncResponseCommand}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-25 00:44:25 -07:00
|
|
|
func HandleSetUser(conn *websocket.Conn, client *ClientInfo, msg ClientMessage) (rmsg ClientMessage, err error) {
|
|
|
|
username, err := msg.ArgumentsAsString()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-10-25 03:21:50 -07:00
|
|
|
client.Mutex.Lock()
|
2015-10-25 00:44:25 -07:00
|
|
|
client.TwitchUsername = username
|
|
|
|
client.UsernameValidated = false
|
2015-10-25 03:21:50 -07:00
|
|
|
client.Mutex.Unlock()
|
2015-10-25 00:44:25 -07:00
|
|
|
|
|
|
|
return ResponseSuccess, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func HandleSub(conn *websocket.Conn, client *ClientInfo, msg ClientMessage) (rmsg ClientMessage, err error) {
|
|
|
|
channel, err := msg.ArgumentsAsString()
|
|
|
|
|
2015-10-25 20:17:17 -07:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-10-25 03:21:50 -07:00
|
|
|
client.Mutex.Lock()
|
|
|
|
|
2015-10-25 00:44:25 -07:00
|
|
|
AddToSliceS(&client.CurrentChannels, channel)
|
2015-10-26 12:13:28 -07:00
|
|
|
client.PendingSubscriptionsBacklog = append(client.PendingSubscriptionsBacklog, channel)
|
2015-10-25 00:44:25 -07:00
|
|
|
|
2015-10-27 21:21:06 -07:00
|
|
|
// if client.MakePendingRequests == nil {
|
|
|
|
// client.MakePendingRequests = time.AfterFunc(ChannelInfoDelay, GetSubscriptionBacklogFor(conn, client))
|
|
|
|
// } else {
|
|
|
|
// if !client.MakePendingRequests.Reset(ChannelInfoDelay) {
|
|
|
|
// client.MakePendingRequests = time.AfterFunc(ChannelInfoDelay, GetSubscriptionBacklogFor(conn, client))
|
|
|
|
// }
|
|
|
|
// }
|
2015-10-25 03:21:50 -07:00
|
|
|
|
|
|
|
client.Mutex.Unlock()
|
|
|
|
|
2015-10-25 20:17:17 -07:00
|
|
|
SubscribeChat(client, channel)
|
2015-10-25 00:44:25 -07:00
|
|
|
|
|
|
|
return ResponseSuccess, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func HandleUnsub(conn *websocket.Conn, client *ClientInfo, msg ClientMessage) (rmsg ClientMessage, err error) {
|
|
|
|
channel, err := msg.ArgumentsAsString()
|
|
|
|
|
2015-10-25 20:17:17 -07:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-10-25 03:21:50 -07:00
|
|
|
client.Mutex.Lock()
|
2015-10-25 00:44:25 -07:00
|
|
|
RemoveFromSliceS(&client.CurrentChannels, channel)
|
2015-10-25 03:21:50 -07:00
|
|
|
client.Mutex.Unlock()
|
|
|
|
|
|
|
|
UnsubscribeSingleChat(client, channel)
|
2015-10-25 00:44:25 -07:00
|
|
|
|
|
|
|
return ResponseSuccess, nil
|
|
|
|
}
|
|
|
|
|
2015-10-25 03:21:50 -07:00
|
|
|
func GetSubscriptionBacklogFor(conn *websocket.Conn, client *ClientInfo) func() {
|
|
|
|
return func() {
|
|
|
|
GetSubscriptionBacklog(conn, client)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// On goroutine
|
|
|
|
func GetSubscriptionBacklog(conn *websocket.Conn, client *ClientInfo) {
|
2015-10-26 12:13:28 -07:00
|
|
|
var subs []string
|
2015-10-25 03:21:50 -07:00
|
|
|
|
|
|
|
// Lock, grab the data, and reset it
|
|
|
|
client.Mutex.Lock()
|
2015-10-26 12:13:28 -07:00
|
|
|
subs = client.PendingSubscriptionsBacklog
|
|
|
|
client.PendingSubscriptionsBacklog = nil
|
2015-10-25 03:21:50 -07:00
|
|
|
client.MakePendingRequests = nil
|
|
|
|
client.Mutex.Unlock()
|
|
|
|
|
2015-10-26 12:13:28 -07:00
|
|
|
if len(subs) == 0 {
|
2015-10-25 03:21:50 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-10-25 20:17:17 -07:00
|
|
|
if backendUrl == "" {
|
|
|
|
return // for testing runs
|
|
|
|
}
|
2015-10-26 12:13:28 -07:00
|
|
|
messages, err := FetchBacklogData(subs)
|
2015-10-25 03:21:50 -07:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
// Oh well.
|
|
|
|
log.Print("error in GetSubscriptionBacklog:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deliver to client
|
|
|
|
for _, msg := range messages {
|
|
|
|
client.MessageChannel <- msg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type SurveySubmission struct {
|
|
|
|
User string
|
|
|
|
Json string
|
|
|
|
}
|
2015-10-26 10:06:45 -07:00
|
|
|
|
2015-10-25 03:21:50 -07:00
|
|
|
var SurveySubmissions []SurveySubmission
|
|
|
|
var SurveySubmissionLock sync.Mutex
|
|
|
|
|
2015-10-25 00:44:25 -07:00
|
|
|
func HandleSurvey(conn *websocket.Conn, client *ClientInfo, msg ClientMessage) (rmsg ClientMessage, err error) {
|
2015-10-25 03:21:50 -07:00
|
|
|
SurveySubmissionLock.Lock()
|
|
|
|
SurveySubmissions = append(SurveySubmissions, SurveySubmission{client.TwitchUsername, msg.origArguments})
|
|
|
|
SurveySubmissionLock.Unlock()
|
|
|
|
|
2015-10-25 00:44:25 -07:00
|
|
|
return ResponseSuccess, nil
|
|
|
|
}
|
|
|
|
|
2015-10-25 03:21:50 -07:00
|
|
|
type FollowEvent struct {
|
2015-10-26 10:06:45 -07:00
|
|
|
User string
|
|
|
|
Channel string
|
2015-10-25 03:21:50 -07:00
|
|
|
NowFollowing bool
|
2015-10-26 10:06:45 -07:00
|
|
|
Timestamp time.Time
|
2015-10-25 03:21:50 -07:00
|
|
|
}
|
2015-10-26 10:06:45 -07:00
|
|
|
|
2015-10-25 03:21:50 -07:00
|
|
|
var FollowEvents []FollowEvent
|
|
|
|
var FollowEventsLock sync.Mutex
|
|
|
|
|
2015-10-25 00:44:25 -07:00
|
|
|
func HandleTrackFollow(conn *websocket.Conn, client *ClientInfo, msg ClientMessage) (rmsg ClientMessage, err error) {
|
2015-10-25 03:21:50 -07:00
|
|
|
channel, following, err := msg.ArgumentsAsStringAndBool()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
FollowEventsLock.Lock()
|
|
|
|
FollowEvents = append(FollowEvents, FollowEvent{client.TwitchUsername, channel, following, now})
|
|
|
|
FollowEventsLock.Unlock()
|
2015-10-25 00:44:25 -07:00
|
|
|
|
|
|
|
return ResponseSuccess, nil
|
|
|
|
}
|
|
|
|
|
2015-10-25 00:58:05 -07:00
|
|
|
var AggregateEmoteUsage map[int]map[string]int = make(map[int]map[string]int)
|
|
|
|
var AggregateEmoteUsageLock sync.Mutex
|
|
|
|
|
2015-10-25 00:44:25 -07:00
|
|
|
func HandleEmoticonUses(conn *websocket.Conn, client *ClientInfo, msg ClientMessage) (rmsg ClientMessage, err error) {
|
2015-10-25 00:58:05 -07:00
|
|
|
// arguments is [1]map[EmoteId]map[RoomName]float64
|
|
|
|
|
|
|
|
mapRoot := msg.Arguments.([]interface{})[0].(map[string]interface{})
|
|
|
|
|
|
|
|
AggregateEmoteUsageLock.Lock()
|
|
|
|
defer AggregateEmoteUsageLock.Unlock()
|
|
|
|
|
|
|
|
for strEmote, val1 := range mapRoot {
|
|
|
|
var emoteId int
|
|
|
|
emoteId, err = strconv.Atoi(strEmote)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
destMapInner, ok := AggregateEmoteUsage[emoteId]
|
|
|
|
if !ok {
|
|
|
|
destMapInner = make(map[string]int)
|
|
|
|
AggregateEmoteUsage[emoteId] = destMapInner
|
|
|
|
}
|
|
|
|
|
|
|
|
mapInner := val1.(map[string]interface{})
|
|
|
|
for roomName, val2 := range mapInner {
|
|
|
|
var count int = int(val2.(float64))
|
|
|
|
destMapInner[roomName] += count
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-25 00:44:25 -07:00
|
|
|
return ResponseSuccess, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func HandleRemoteCommand(conn *websocket.Conn, client *ClientInfo, msg ClientMessage) (rmsg ClientMessage, err error) {
|
|
|
|
go func(conn *websocket.Conn, msg ClientMessage, authInfo AuthInfo) {
|
|
|
|
resp, err := RequestRemoteDataCached(string(msg.Command), msg.origArguments, authInfo)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
FFZCodec.Send(conn, ClientMessage{MessageID: msg.MessageID, Command: ErrorCommand, Arguments: err.Error()})
|
|
|
|
} else {
|
|
|
|
FFZCodec.Send(conn, ClientMessage{MessageID: msg.MessageID, Command: SuccessCommand, origArguments: resp})
|
|
|
|
}
|
|
|
|
}(conn, msg, client.AuthInfo)
|
|
|
|
|
|
|
|
return ClientMessage{Command: AsyncResponseCommand}, nil
|
|
|
|
}
|