Add WhatsApp->Matrix typing notifications and presence

This commit is contained in:
Tulir Asokan 2018-08-24 20:02:38 +03:00
parent 1f87deb317
commit a7f7295528
4 changed files with 33 additions and 3 deletions

View file

@ -26,8 +26,8 @@
* [x] Media/files * [x] Media/files
* [ ] Message deletions * [ ] Message deletions
* [x] Avatars * [x] Avatars
* [ ] Presence * [x] Presence
* [ ] Typing notifications * [x] Typing notifications
* [x] Read receipts * [x] Read receipts
* [ ] Admin/superadmin status * [ ] Admin/superadmin status
* [ ] Membership actions * [ ] Membership actions

View file

@ -132,6 +132,8 @@ type Puppet struct {
bridge *Bridge bridge *Bridge
log log.Logger log log.Logger
typingIn types.MatrixRoomID
MXID types.MatrixUserID MXID types.MatrixUserID
} }

19
user.go
View file

@ -261,6 +261,25 @@ func (user *User) HandleConnInfo(info whatsapp_ext.ConnInfo) {
} }
} }
func (user *User) HandlePresence(info whatsapp_ext.Presence) {
puppet := user.GetPuppetByJID(info.SenderJID)
switch info.Status {
case whatsapp_ext.PresenceUnavailable:
puppet.Intent().SetPresence("offline")
case whatsapp_ext.PresenceAvailable:
if len(puppet.typingIn) > 0 {
puppet.Intent().UserTyping(puppet.typingIn, false, 0)
puppet.typingIn = ""
} else {
puppet.Intent().SetPresence("online")
}
case whatsapp_ext.PresenceComposing:
portal := user.GetPortalByJID(info.JID)
puppet.typingIn = portal.MXID
puppet.Intent().UserTyping(portal.MXID, true, 15 * 1000)
}
}
func (user *User) HandleMsgInfo(info whatsapp_ext.MsgInfo) { func (user *User) HandleMsgInfo(info whatsapp_ext.MsgInfo) {
if (info.Command == whatsapp_ext.MsgInfoCommandAck || info.Command == whatsapp_ext.MsgInfoCommandAcks) && info.Acknowledgement == whatsapp_ext.AckMessageRead { if (info.Command == whatsapp_ext.MsgInfoCommandAck || info.Command == whatsapp_ext.MsgInfoCommandAcks) && info.Acknowledgement == whatsapp_ext.AckMessageRead {
portal := user.GetPortalByJID(info.ToJID) portal := user.GetPortalByJID(info.ToJID)

View file

@ -17,9 +17,10 @@
package whatsapp_ext package whatsapp_ext
import ( import (
"github.com/Rhymen/go-whatsapp"
"encoding/json" "encoding/json"
"strings" "strings"
"github.com/Rhymen/go-whatsapp"
) )
type PresenceType string type PresenceType string
@ -27,12 +28,15 @@ type PresenceType string
const ( const (
PresenceUnavailable PresenceType = "unavailable" PresenceUnavailable PresenceType = "unavailable"
PresenceAvailable PresenceType = "available" PresenceAvailable PresenceType = "available"
PresenceComposing PresenceType = "composing"
) )
type Presence struct { type Presence struct {
JID string `json:"id"` JID string `json:"id"`
SenderJID string `json:"participant"`
Status PresenceType `json:"type"` Status PresenceType `json:"type"`
Timestamp int64 `json:"t"` Timestamp int64 `json:"t"`
Deny bool `json:"deny"`
} }
type PresenceHandler interface { type PresenceHandler interface {
@ -48,6 +52,11 @@ func (ext *ExtendedConn) handleMessagePresence(message []byte) {
return return
} }
event.JID = strings.Replace(event.JID, OldUserSuffix, NewUserSuffix, 1) event.JID = strings.Replace(event.JID, OldUserSuffix, NewUserSuffix, 1)
if len(event.SenderJID) == 0 {
event.SenderJID = event.JID
} else {
event.SenderJID = strings.Replace(event.SenderJID, OldUserSuffix, NewUserSuffix, 1)
}
for _, handler := range ext.handlers { for _, handler := range ext.handlers {
presenceHandler, ok := handler.(PresenceHandler) presenceHandler, ok := handler.(PresenceHandler)
if !ok { if !ok {