#475 WhatsApp: Deleting and editing messages stub

This commit is contained in:
Krzysztof Madejski
2019-02-14 12:39:00 +01:00
parent 7b247a32b8
commit 525494013f

View File

@@ -1,6 +1,8 @@
package bwhatsapp package bwhatsapp
import ( import (
"crypto/rand"
"encoding/hex"
"errors" "errors"
"fmt" "fmt"
"os" "os"
@@ -133,6 +135,7 @@ func (b *Bwhatsapp) Connect() error {
b.Log.Warnf("Could not get profile photo of %s: %v", jid, err) b.Log.Warnf("Could not get profile photo of %s: %v", jid, err)
} else { } else {
// TODO any race conditions here?
b.userAvatars[jid] = info.URL b.userAvatars[jid] = info.URL
} }
} }
@@ -146,8 +149,6 @@ func (b *Bwhatsapp) Connect() error {
func (b *Bwhatsapp) Login() error { func (b *Bwhatsapp) Login() error {
b.Log.Debugln("Logging in..") b.Log.Debugln("Logging in..")
// TODO qrCode, err := qrcode.Encode(code, qrcode.Low, 256) to encode as image/png
// and possibly send it to connected channels (to admin) to authorize the app
invert := b.GetBoolOrDefault(qrOnWhiteTerminal, false) invert := b.GetBoolOrDefault(qrOnWhiteTerminal, false)
qrChan := qrFromTerminal(invert) qrChan := qrFromTerminal(invert)
@@ -175,7 +176,8 @@ func (b *Bwhatsapp) Login() error {
return nil return nil
} }
// Disconnect TODO What does it mean // Disconnect is called while reconnecting to the bridge
// TODO 42wim Documentation would be helpful on when reconnects happen and what should be done in this function
// Required implementation of the Bridger interface // Required implementation of the Bridger interface
// https://github.com/42wim/matterbridge/blob/2cfd880cdb0df29771bf8f31df8d990ab897889d/bridge/bridge.go#L11-L16 // https://github.com/42wim/matterbridge/blob/2cfd880cdb0df29771bf8f31df8d990ab897889d/bridge/bridge.go#L11-L16
func (b *Bwhatsapp) Disconnect() error { func (b *Bwhatsapp) Disconnect() error {
@@ -241,37 +243,30 @@ func (b *Bwhatsapp) JoinChannel(channel config.ChannelInfo) error {
func (b *Bwhatsapp) Send(msg config.Message) (string, error) { func (b *Bwhatsapp) Send(msg config.Message) (string, error) {
b.Log.Debugf("=> Receiving %#v", msg) b.Log.Debugf("=> Receiving %#v", msg)
text := whatsapp.TextMessage{ // Delete message
Info: whatsapp.MessageInfo{ if msg.Event == config.EventMsgDelete {
// Id: "", // TODO id if msg.ID == "" {
// TODO Timestamp // No message ID in case action is executed on a message sent before the bridge was started
RemoteJid: msg.Channel, // which equals to group id // and then the bridge cache doesn't have this message ID mapped
}, // TODO 42wim Doesn't the app get clogged with a ton of IDs after some time of running?
Text: msg.Username + msg.Text, // WhatsApp allows to set any ID so in that case we could use external IDs and don't do mapping
// but external IDs are not set
return "", nil
}
// TODO delete message on WhatsApp https://github.com/Rhymen/go-whatsapp/issues/100
return "", nil
} }
// TODO adapt gitter code for edits, delete and some extra commands // Edit message
//roomID := b.getRoomID(msg.Channel) if msg.ID != "" {
//if roomID == "" { b.Log.Debugf("updating message with id %s", msg.ID)
// b.Log.Errorf("Could not find roomID for %v", msg.Channel)
// return "", nil msg.Text = msg.Text + " (edited)"
//} // TODO handle edit as a message reply with updated text
// }
//// Delete message
//if msg.Event == config.EventMsgDelete { //// TODO Handle Upload a file
// if msg.ID == "" {
// return "", nil
// }
// // gitter has no delete message api so we edit message to ""
// _, err := b.c.UpdateMessage(roomID, msg.ID, "")
// if err != nil {
// return "", err
// }
// return "", nil
//}
//
//// Upload a file (in gitter case send the upload URL because gitter has no native upload support)
//if msg.Extra != nil { //if msg.Extra != nil {
// for _, rmsg := range helper.HandleExtra(&msg, b.General) { // for _, rmsg := range helper.HandleExtra(&msg, b.General) {
// b.c.SendMessage(roomID, rmsg.Username+rmsg.Text) // b.c.SendMessage(roomID, rmsg.Username+rmsg.Text)
@@ -280,30 +275,26 @@ func (b *Bwhatsapp) Send(msg config.Message) (string, error) {
// return b.handleUploadFile(&msg, roomID) // return b.handleUploadFile(&msg, roomID)
// } // }
//} //}
//
//// Edit message // Post text message
//if msg.ID != "" { text := whatsapp.TextMessage{
// b.Log.Debugf("updating message with id %s", msg.ID) Info: whatsapp.MessageInfo{
// _, err := b.c.UpdateMessage(roomID, msg.ID, msg.Username+msg.Text) RemoteJid: msg.Channel, // which equals to group id
// if err != nil { },
// return "", err Text: msg.Username + msg.Text,
// } }
// return "", nil
//}
//
//// Post normal message
//resp, err := b.c.SendMessage(roomID, msg.Username+msg.Text)
//if err != nil {
// return "", err
//}
//return resp.ID, nil
b.Log.Debugf("=> Sending %#v", msg) b.Log.Debugf("=> Sending %#v", msg)
// create message ID
// TODO follow and act if https://github.com/Rhymen/go-whatsapp/issues/101 implemented
bytes := make([]byte, 10)
rand.Read(bytes)
text.Info.Id = strings.ToUpper(hex.EncodeToString(bytes))
err := b.conn.Send(text) err := b.conn.Send(text)
// TODO return message id return text.Info.Id, err
return "", err
} }
// TODO do we want that? to allow login with QR code from a bridged channel? https://github.com/tulir/mautrix-whatsapp/blob/513eb18e2d59bada0dd515ee1abaaf38a3bfe3d5/commands.go#L76 // TODO do we want that? to allow login with QR code from a bridged channel? https://github.com/tulir/mautrix-whatsapp/blob/513eb18e2d59bada0dd515ee1abaaf38a3bfe3d5/commands.go#L76