From 2d8bc96a090f3de17023b277774c49ce1893e7b6 Mon Sep 17 00:00:00 2001 From: Marco Antonio Alvarez Date: Thu, 26 Mar 2020 18:31:56 +0100 Subject: [PATCH] updated to latest mautrix-go and mautrix-appservice-go (#145) --- custompuppet.go | 55 ++++++++++++++++++++++++++++++++++++++----------- go.mod | 3 ++- go.sum | 4 ++++ 3 files changed, 49 insertions(+), 13 deletions(-) diff --git a/custompuppet.go b/custompuppet.go index b7f4a3c..ac98ee9 100644 --- a/custompuppet.go +++ b/custompuppet.go @@ -22,6 +22,7 @@ import ( "encoding/hex" "encoding/json" "fmt" + "os" "strings" "time" @@ -29,7 +30,7 @@ import ( "github.com/Rhymen/go-whatsapp" "maunium.net/go/mautrix" - "maunium.net/go/mautrix-appservice" + appservice "maunium.net/go/mautrix-appservice" ) var ( @@ -117,7 +118,9 @@ func (puppet *Puppet) StartCustomMXID() error { return err } urlPath := intent.BuildURL("account", "whoami") - var resp struct{ UserID string `json:"user_id"` } + var resp struct { + UserID string `json:"user_id"` + } _, err = intent.MakeRequest("GET", urlPath, nil, &resp) if err != nil { puppet.clearCustomMXID() @@ -155,6 +158,28 @@ func (puppet *Puppet) stopSyncing() { puppet.customIntent.StopSync() } +func parseEvent(roomID string, data json.RawMessage) *mautrix.Event { + event := &mautrix.Event{} + err := json.Unmarshal(data, event) + if err != nil { + // TODO add separate handler for these + _, _ = fmt.Fprintf(os.Stderr, "Failed to unmarshal event: %v\n%s\n", err, string(data)) + return nil + } + return event +} + +func parsePresenceEvent(data json.RawMessage) *mautrix.Event { + event := &mautrix.Event{} + err := json.Unmarshal(data, event) + if err != nil { + // TODO add separate handler for these + _, _ = fmt.Fprintf(os.Stderr, "Failed to unmarshal event: %v\n%s\n", err, string(data)) + return nil + } + return event +} + func (puppet *Puppet) ProcessResponse(resp *mautrix.RespSync, since string) error { if !puppet.customUser.IsConnected() { puppet.log.Debugln("Skipping sync processing: custom user not connected to whatsapp") @@ -165,20 +190,26 @@ func (puppet *Puppet) ProcessResponse(resp *mautrix.RespSync, since string) erro if portal == nil { continue } - for _, event := range events.Ephemeral.Events { - switch event.Type { - case mautrix.EphemeralEventReceipt: - go puppet.handleReceiptEvent(portal, event) - case mautrix.EphemeralEventTyping: - go puppet.handleTypingEvent(portal, event) + for _, data := range events.Ephemeral.Events { + event := parseEvent(roomID, data) + if event != nil { + switch event.Type { + case mautrix.EphemeralEventReceipt: + go puppet.handleReceiptEvent(portal, event) + case mautrix.EphemeralEventTyping: + go puppet.handleTypingEvent(portal, event) + } } } } - for _, event := range resp.Presence.Events { - if event.Sender != puppet.CustomMXID { - continue + for _, data := range resp.Presence.Events { + event := parsePresenceEvent(data) + if event != nil { + if event.Sender != puppet.CustomMXID { + continue + } + go puppet.handlePresenceEvent(event) } - go puppet.handlePresenceEvent(event) } return nil } diff --git a/go.mod b/go.mod index 89e1142..4656de7 100644 --- a/go.mod +++ b/go.mod @@ -10,11 +10,12 @@ require ( github.com/lib/pq v1.1.1 github.com/mattn/go-sqlite3 v1.10.0 github.com/pkg/errors v0.8.1 + github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect github.com/skip2/go-qrcode v0.0.0-20190110000554-dc11ecdae0a9 gopkg.in/yaml.v2 v2.2.2 maunium.net/go/mauflag v1.0.0 maunium.net/go/maulogger/v2 v2.0.0 - maunium.net/go/mautrix v0.1.0-alpha.3.0.20200209182939-9df6760d40d2 + maunium.net/go/mautrix v0.1.0-alpha.3.0.20200221234248-8a868bce2854 maunium.net/go/mautrix-appservice v0.1.0-alpha.3.0.20200209183024-a3d12dc80898 ) diff --git a/go.sum b/go.sum index a95c3cb..35aa020 100644 --- a/go.sum +++ b/go.sum @@ -58,6 +58,8 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190603091049-60506f45cf65 h1:+rhAzEzT3f4JtomfC371qB+0Ola2caSKcY69NUBZrRQ= golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= +golang.org/x/net v0.0.0-20200202094626-16171245cfb2 h1:CCH4IOTTfewWjGOlSp+zGcjutRKlBEZQ6wTn8ozI/nI= +golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -84,6 +86,8 @@ maunium.net/go/mautrix v0.1.0-alpha.3.0.20191230181907-055c3acd81cd h1:stoHlgxDA maunium.net/go/mautrix v0.1.0-alpha.3.0.20191230181907-055c3acd81cd/go.mod h1:O+QWJP3H7BZEzIBSrECKpnpRnEKBwaoWVEu/yZwVwxg= maunium.net/go/mautrix v0.1.0-alpha.3.0.20200209182939-9df6760d40d2 h1:Ewvf0/4z0OQ7IyySxP5rjhzpL01Slxhup7ugHieAlrI= maunium.net/go/mautrix v0.1.0-alpha.3.0.20200209182939-9df6760d40d2/go.mod h1:O+QWJP3H7BZEzIBSrECKpnpRnEKBwaoWVEu/yZwVwxg= +maunium.net/go/mautrix v0.1.0-alpha.3.0.20200221234248-8a868bce2854 h1:NKj52IqLkfscNMSbSVepaXHM6M/x3iNe/iWSAcmAilo= +maunium.net/go/mautrix v0.1.0-alpha.3.0.20200221234248-8a868bce2854/go.mod h1:g10T1fh2Q2HkJWycVs93eBXdWpqD67f1YVQhNxdIDr4= maunium.net/go/mautrix-appservice v0.1.0-alpha.3.0.20190618052224-6e6c9bb47548 h1:ni1nqs+2AOO+g1ND6f2W0pMcb6sIDVqzerXosO+pI2g= maunium.net/go/mautrix-appservice v0.1.0-alpha.3.0.20190618052224-6e6c9bb47548/go.mod h1:yVWU0gvIHIXClgyVnShiufiDksFbFrBqHG9lDAYcmGI= maunium.net/go/mautrix-appservice v0.1.0-alpha.3.0.20190822210104-3e49344e186b h1:/03X0PPgtk4pqXcdH86xMzOl891whG5A1hFXQ+xXons=