Add WhatsApp->Matrix read receipts and phone connection notifications

This commit is contained in:
Tulir Asokan
2018-08-24 19:46:14 +03:00
parent 60529bf022
commit 1f87deb317
12 changed files with 178 additions and 69 deletions

View File

@ -17,8 +17,9 @@
package whatsapp_ext
import (
"github.com/Rhymen/go-whatsapp"
"encoding/json"
"github.com/Rhymen/go-whatsapp"
)
type ConnInfo struct {
@ -26,8 +27,8 @@ type ConnInfo struct {
BinaryVersion int `json:"binVersion"`
Phone struct {
WhatsAppVersion string `json:"wa_version"`
MCC int `json:"mcc"`
MNC int `json:"mnc"`
MCC string `json:"mcc"`
MNC string `json:"mnc"`
OSVersion string `json:"os_version"`
DeviceManufacturer string `json:"device_manufacturer"`
DeviceModel string `json:"device_model"`

View File

@ -18,6 +18,7 @@ package whatsapp_ext
import (
"encoding/json"
"github.com/Rhymen/go-whatsapp"
)
@ -27,6 +28,7 @@ type JSONMessageType string
const (
MessageMsgInfo JSONMessageType = "MsgInfo"
MessageMsg JSONMessageType = "Msg"
MessagePresence JSONMessageType = "Presence"
MessageStream JSONMessageType = "Stream"
MessageConn JSONMessageType = "Conn"
@ -76,11 +78,11 @@ func (ext *ExtendedConn) HandleJsonMessage(message string) {
case MessageStream:
ext.handleMessageStream(msg[1:])
case MessageConn:
ext.handleMessageProps(msg[1])
ext.handleMessageConn(msg[1])
case MessageProps:
ext.handleMessageProps(msg[1])
case MessageMsgInfo:
ext.handleMessageMsgInfo(msg[1])
case MessageMsgInfo, MessageMsg:
ext.handleMessageMsgInfo(msgType, msg[1])
default:
for _, handler := range ext.handlers {
ujmHandler, ok := handler.(UnhandledJSONMessageHandler)

View File

@ -17,25 +17,49 @@
package whatsapp_ext
import (
"github.com/Rhymen/go-whatsapp"
"encoding/json"
"strings"
"github.com/Rhymen/go-whatsapp"
)
type MsgInfoCommand string
const (
MsgInfoCommandAcknowledge MsgInfoCommand = "ack"
MsgInfoCommandAck MsgInfoCommand = "ack"
MsgInfoCommandAcks MsgInfoCommand = "acks"
)
type Acknowledgement int
const (
AckMessageSent Acknowledgement = 1
AckMessageDelivered Acknowledgement = 2
AckMessageRead Acknowledgement = 3
)
type JSONStringOrArray []string
func (jsoa *JSONStringOrArray) UnmarshalJSON(data []byte) error {
var str string
if json.Unmarshal(data, &str) == nil {
*jsoa = []string{str}
return nil
}
var strs []string
json.Unmarshal(data, &strs)
*jsoa = strs
return nil
}
type MsgInfo struct {
Command MsgInfoCommand `json:"cmd"`
ID string `json:"id"`
Acknowledgement int `json:"ack"`
MessageFromJID string `json:"from"`
SenderJID string `json:"participant"`
ToJID string `json:"to"`
Timestamp int64 `json:"t"`
Command MsgInfoCommand `json:"cmd"`
IDs JSONStringOrArray `json:"id"`
Acknowledgement Acknowledgement `json:"ack"`
MessageFromJID string `json:"from"`
SenderJID string `json:"participant"`
ToJID string `json:"to"`
Timestamp int64 `json:"t"`
}
type MsgInfoHandler interface {
@ -43,7 +67,7 @@ type MsgInfoHandler interface {
HandleMsgInfo(MsgInfo)
}
func (ext *ExtendedConn) handleMessageMsgInfo(message []byte) {
func (ext *ExtendedConn) handleMessageMsgInfo(msgType JSONMessageType, message []byte) {
var event MsgInfo
err := json.Unmarshal(message, &event)
if err != nil {
@ -53,11 +77,14 @@ func (ext *ExtendedConn) handleMessageMsgInfo(message []byte) {
event.MessageFromJID = strings.Replace(event.MessageFromJID, OldUserSuffix, NewUserSuffix, 1)
event.SenderJID = strings.Replace(event.SenderJID, OldUserSuffix, NewUserSuffix, 1)
event.ToJID = strings.Replace(event.ToJID, OldUserSuffix, NewUserSuffix, 1)
if msgType == MessageMsg {
event.SenderJID = event.MessageFromJID
}
for _, handler := range ext.handlers {
msgInfoHandler, ok := handler.(MsgInfoHandler)
if !ok {
continue
}
msgInfoHandler.HandleMsgInfo(event)
go msgInfoHandler.HandleMsgInfo(event)
}
}

View File

@ -53,6 +53,6 @@ func (ext *ExtendedConn) handleMessagePresence(message []byte) {
if !ok {
continue
}
presenceHandler.HandlePresence(event)
go presenceHandler.HandlePresence(event)
}
}

View File

@ -62,6 +62,6 @@ func (ext *ExtendedConn) handleMessageProps(message []byte) {
if !ok {
continue
}
protocolPropsHandler.HandleProtocolProps(event)
go protocolPropsHandler.HandleProtocolProps(event)
}
}

View File

@ -57,6 +57,6 @@ func (ext *ExtendedConn) handleMessageStream(message []json.RawMessage) {
if !ok {
continue
}
streamHandler.HandleStreamEvent(event)
go streamHandler.HandleStreamEvent(event)
}
}