Skip to content

Commit

Permalink
#475 WA users' avatars!
Browse files Browse the repository at this point in the history
  • Loading branch information
Krzysztof Madejski committed Feb 12, 2019
1 parent 9605dda commit c8bb087
Showing 1 changed file with 30 additions and 22 deletions.
52 changes: 30 additions & 22 deletions bridge/whatsapp/whatsapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ import (

"github.com/Baozisoftware/qrcode-terminal-go"
"github.com/Rhymen/go-whatsapp"
// "maunium.net/go/mautrix-whatsapp/whatsapp-ext"
// "@c.us" -> "@s.whatsapp.net"
// "github.com/tulir/mautrix-whatsapp"
"maunium.net/go/mautrix-whatsapp/whatsapp-ext"
)

const (
Expand All @@ -29,11 +27,13 @@ type Bwhatsapp struct {

// https://github.com/Rhymen/go-whatsapp/blob/c31092027237441cffba1b9cb148eadf7c83c3d2/session.go#L18-L21
session *whatsapp.Session
// connExt *whatsappExt.ExtendedConn // https://github.com/tulir/mautrix-whatsapp/blob/master/whatsapp-ext/whatsapp.go
conn *whatsapp.Conn
conn *whatsapp.Conn
// https://github.com/tulir/mautrix-whatsapp/blob/master/whatsapp-ext/whatsapp.go
connExt *whatsappExt.ExtendedConn
startedAt uint64

users map[string]whatsapp.Contact
users map[string]whatsapp.Contact
userAvatars map[string]string
}

func New(cfg *bridge.Config) bridge.Bridger {
Expand All @@ -50,7 +50,8 @@ func New(cfg *bridge.Config) bridge.Bridger {
b := &Bwhatsapp{
Config: cfg,

users: make(map[string]whatsapp.Contact),
users: make(map[string]whatsapp.Contact),
userAvatars: make(map[string]string),

//uuid: xid.New().String(),
//users: map[string]*slack.User{},
Expand Down Expand Up @@ -85,8 +86,9 @@ func (b *Bwhatsapp) Connect() error {
}

b.conn = conn
//b.connExt = whatsappExt.ExtendConn(b.conn)
//b.connExt.SetClientName("Matterbridge WhatsApp bridge", "mb-wa")
b.connExt = whatsappExt.ExtendConn(b.conn)
// TODO do we want to use it? b.connExt.SetClientName("Matterbridge WhatsApp bridge", "mb-wa")

b.conn.AddHandler(b)
b.Log.Debugln("WhatsApp connection successful")

Expand Down Expand Up @@ -133,18 +135,21 @@ func (b *Bwhatsapp) Connect() error {
}
}

// get avatar URLs
// https://web.whatsapp.com/pp
// ?e=https%3A%2F%2Fpps.whatsapp.net%2Fv%2Ft61.11540-24%2F16489067_255781518198245_6804985813445640192_n.jpg%3Foe%3D5C66A1FA%26oh%3D20cd213793de891d1e256b27a1cf1075
// &t=s
// &u=remotenumber%40c.us
// &i=1485652465
// https://web.whatsapp.com/pp
// ?e=https://pps.whatsapp.net/v/t61.11540-24/16489067_255781518198245_6804985813445640192_n.jpg?oe=5C66A1FA&oh=20cd213793de891d1e256b27a1cf1075 (the actual photo)
// &t=s
// &u=remotenumber@c.us
// &i=1485652465
GetProfilePicThumb
// get user avatar asynchronously
go func() {
b.Log.Debug("Getting user avatars..")

for jid := range b.users {
info, err := b.connExt.GetProfilePicThumb(jid)
if err != nil {
b.Log.Warnf("Could not get profile photo of %s: %v", jid, err)

} else {
b.userAvatars[jid] = info.URL
}
}
b.Log.Debug("Finished getting avatars..")
}()

return nil
}
Expand Down Expand Up @@ -412,12 +417,15 @@ func (b *Bwhatsapp) HandleTextMessage(message whatsapp.TextMessage) {
Account: b.Account,
Protocol: b.Protocol,
Extra: make(map[string][]interface{}),
// Avatar: b.getAvatar(ev.Message.From.Username), // TODO get avatar
// ParentID: TODO, // TODO handle thread replies // map from Info.QuotedMessageID string
// Event string `json:"event"`
// Gateway string // will be added during message processing
ID: message.Info.Id}

if avatarUrl, exists := b.userAvatars[senderJid]; exists {
rmsg.Avatar = avatarUrl
}

b.Log.Debugf("<= Message is %#v", rmsg)
b.Remote <- rmsg
}
Expand Down

0 comments on commit c8bb087

Please sign in to comment.