(Re)Add chatstate notification.

This commit is contained in:
chteufleur 2016-10-26 08:20:47 +02:00
parent a4518d3f6b
commit 195063bc75
4 changed files with 68 additions and 39 deletions

View File

@ -2,8 +2,6 @@ package gateway
import ( import (
"github.com/Philipp15b/go-steam" "github.com/Philipp15b/go-steam"
"time"
) )
const ( const (
@ -33,10 +31,10 @@ type GatewayInfo struct {
XMPP_JID_Client string XMPP_JID_Client string
XMPP_Out chan interface{} XMPP_Out chan interface{}
XMPP_Connected_Client map[string]bool XMPP_Connected_Client map[string]bool
XMPP_Composing_Timers map[string]*time.Timer
DebugMessage bool DebugMessage bool
XMPP_IQ_RemoteRoster_Request map[string]string XMPP_IQ_RemoteRoster_Request map[string]string
AllowEditRoster bool AllowEditRoster bool
ChatstateNotificationData chan string
} }
type StatusSteamFriend struct { type StatusSteamFriend struct {
@ -48,6 +46,7 @@ type StatusSteamFriend struct {
func (g *GatewayInfo) Run() { func (g *GatewayInfo) Run() {
go g.SteamRun() go g.SteamRun()
go g.chatstatesNotification()
} }
func (g *GatewayInfo) SetSteamAuthCode(authCode string) { func (g *GatewayInfo) SetSteamAuthCode(authCode string) {

View File

@ -249,49 +249,80 @@ func (g *GatewayInfo) removeAllUserFromRoster() {
func (g *GatewayInfo) SendXmppMessage(from, subject, message string) { func (g *GatewayInfo) SendXmppMessage(from, subject, message string) {
g.sendXmppMessage(from, subject, message, &xmpp.Active{}) g.sendXmppMessage(from, subject, message, &xmpp.Active{})
return g.ChatstateNotificationData <- from
g.ChatstateNotificationData <- "inactive"
g.stopComposingTimer(from)
// Make inactive after 2 min if nothing happen
t := time.AfterFunc(120*time.Second, func() {
g.sendXmppMessage(from, "", "", &xmpp.Inactive{})
})
g.XMPP_Composing_Timers[from] = t
} }
func (g *GatewayInfo) SendXmppMessageLeaveConversation(from string) { func (g *GatewayInfo) SendXmppMessageLeaveConversation(from string) {
g.sendXmppMessage(from, "", "", &xmpp.Gone{}) g.sendXmppMessage(from, "", "", &xmpp.Gone{})
return g.ChatstateNotificationData <- from
g.ChatstateNotificationData <- "stop"
g.stopComposingTimer(from)
} }
func (g *GatewayInfo) SendXmppMessageComposing(from string) { func (g *GatewayInfo) SendXmppMessageComposing(from string) {
g.sendXmppMessage(from, "", "", &xmpp.Composing{}) g.sendXmppMessage(from, "", "", &xmpp.Composing{})
return g.ChatstateNotificationData <- from
g.ChatstateNotificationData <- "paused"
g.stopComposingTimer(from) g.ChatstateNotificationData <- from
g.ChatstateNotificationData <- "inactive"
timer := time.AfterFunc(20*time.Second, func() { }
g.sendXmppMessage(from, "", "", &xmpp.Paused{})
func (g *GatewayInfo) chatstatesNotification() {
t := time.AfterFunc(100*time.Second, func() { inactiveTimers := make(map[string]*time.Timer)
g.sendXmppMessage(from, "", "", &xmpp.Inactive{}) pausedTimers := make(map[string]*time.Timer)
}) g.ChatstateNotificationData = make(chan string)
g.XMPP_Composing_Timers[from] = t
}) for {
g.XMPP_Composing_Timers[from] = timer jid := <-g.ChatstateNotificationData
chatstate := <-g.ChatstateNotificationData
timerInactive, okInactive := inactiveTimers[jid]
timerPaused, okPaused := pausedTimers[jid]
switch chatstate {
case "stop":
if okInactive {
if !timerInactive.Stop() {
<-timerInactive.C
}
delete(inactiveTimers, jid)
}
if okPaused {
if !timerPaused.Stop() {
<-timerPaused.C
}
delete(pausedTimers, jid)
}
case "paused":
if okInactive {
if !timerPaused.Stop() {
<-timerPaused.C
}
timerPaused.Reset(20 * time.Second)
} else {
timerPaused = time.AfterFunc(20*time.Second, func() {
g.sendXmppMessage(jid, "", "", &xmpp.Paused{})
delete(pausedTimers, jid)
})
pausedTimers[jid] = timerPaused
}
case "inactive":
if okInactive {
if !timerInactive.Stop() {
<-timerInactive.C
}
timerInactive.Reset(120 * time.Second)
} else {
timerInactive = time.AfterFunc(120*time.Second, func() {
g.sendXmppMessage(jid, "", "", &xmpp.Inactive{})
delete(inactiveTimers, jid)
})
inactiveTimers[jid] = timerInactive
} }
func (g *GatewayInfo) stopComposingTimer(from string) {
if t, ok := g.XMPP_Composing_Timers[from]; ok {
// Delete previous timer if exist
if !t.Stop() {
// Prevent firing after stop
<-t.C
} }
delete(g.XMPP_Composing_Timers, from)
} }
} }

View File

@ -280,7 +280,6 @@ func AddNewUser(jidUser, steamLogin, steamPwd string, debugMessage bool) {
g.XMPP_Out = comp.Out g.XMPP_Out = comp.Out
g.XMPP_Connected_Client = make(map[string]bool) g.XMPP_Connected_Client = make(map[string]bool)
g.XMPP_Composing_Timers = make(map[string]*time.Timer)
g.DebugMessage = debugMessage g.DebugMessage = debugMessage
g.XMPP_IQ_RemoteRoster_Request = make(map[string]string) g.XMPP_IQ_RemoteRoster_Request = make(map[string]string)
g.AllowEditRoster = false g.AllowEditRoster = false