(Re)Add chatstate notification.
This commit is contained in:
parent
a4518d3f6b
commit
195063bc75
|
|
@ -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) {
|
||||||
|
|
|
||||||
|
|
@ -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{})
|
|
||||||
|
|
||||||
t := time.AfterFunc(100*time.Second, func() {
|
|
||||||
g.sendXmppMessage(from, "", "", &xmpp.Inactive{})
|
|
||||||
})
|
|
||||||
g.XMPP_Composing_Timers[from] = t
|
|
||||||
})
|
|
||||||
g.XMPP_Composing_Timers[from] = timer
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GatewayInfo) stopComposingTimer(from string) {
|
func (g *GatewayInfo) chatstatesNotification() {
|
||||||
if t, ok := g.XMPP_Composing_Timers[from]; ok {
|
inactiveTimers := make(map[string]*time.Timer)
|
||||||
// Delete previous timer if exist
|
pausedTimers := make(map[string]*time.Timer)
|
||||||
if !t.Stop() {
|
g.ChatstateNotificationData = make(chan string)
|
||||||
// Prevent firing after stop
|
|
||||||
<-t.C
|
for {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
delete(g.XMPP_Composing_Timers, from)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
package logger
|
package logger
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -12,7 +12,7 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
func Init(infoHandle io.Writer, warningHandle io.Writer, errorHandle io.Writer) {
|
func Init(infoHandle io.Writer, warningHandle io.Writer, errorHandle io.Writer) {
|
||||||
Info = log.New(infoHandle, "INFO : ", log.Ldate|log.Ltime|log.Lshortfile)
|
Info = log.New(infoHandle, "INFO : ", log.Ldate|log.Ltime|log.Lshortfile)
|
||||||
Debug = log.New(warningHandle, "DEBUG : ", log.Ldate|log.Ltime|log.Lshortfile)
|
Debug = log.New(warningHandle, "DEBUG : ", log.Ldate|log.Ltime|log.Lshortfile)
|
||||||
Error = log.New(errorHandle, "ERROR : ", log.Ldate|log.Ltime|log.Lshortfile)
|
Error = log.New(errorHandle, "ERROR : ", log.Ldate|log.Ltime|log.Lshortfile)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue