2019-02-21 20:28:13 +01:00
|
|
|
package bwhatsapp
|
|
|
|
|
|
|
|
import (
|
2019-06-01 00:53:49 +02:00
|
|
|
"fmt"
|
2020-11-27 00:34:44 +01:00
|
|
|
"strings"
|
2019-02-21 20:28:13 +01:00
|
|
|
|
2022-01-31 00:13:37 +01:00
|
|
|
"go.mau.fi/whatsmeow/store"
|
|
|
|
"go.mau.fi/whatsmeow/store/sqlstore"
|
|
|
|
"go.mau.fi/whatsmeow/types"
|
2019-02-21 20:28:13 +01:00
|
|
|
)
|
|
|
|
|
2019-06-01 00:53:49 +02:00
|
|
|
type ProfilePicInfo struct {
|
2020-11-27 00:34:44 +01:00
|
|
|
URL string `json:"eurl"`
|
|
|
|
Tag string `json:"tag"`
|
|
|
|
Status int16 `json:"status"`
|
2019-06-01 00:53:49 +02:00
|
|
|
}
|
|
|
|
|
2022-01-31 00:13:37 +01:00
|
|
|
func (b *Bwhatsapp) getSenderName(senderJid types.JID) string {
|
|
|
|
if sender, exists := b.contacts[senderJid]; exists {
|
|
|
|
if sender.FullName != "" {
|
|
|
|
return sender.FullName
|
|
|
|
}
|
|
|
|
// if user is not in phone contacts
|
|
|
|
// it is the most obvious scenario unless you sync your phone contacts with some remote updated source
|
|
|
|
// users can change it in their WhatsApp settings -> profile -> click on Avatar
|
|
|
|
if sender.PushName != "" {
|
|
|
|
return sender.PushName
|
2019-02-21 20:28:13 +01:00
|
|
|
}
|
|
|
|
|
2022-01-31 00:13:37 +01:00
|
|
|
if sender.FirstName != "" {
|
|
|
|
return sender.FirstName
|
|
|
|
}
|
2019-02-21 20:28:13 +01:00
|
|
|
}
|
|
|
|
|
2022-01-31 00:13:37 +01:00
|
|
|
// try to reload this contact
|
|
|
|
if _, err := b.wc.Store.Contacts.GetAllContacts(); err != nil {
|
|
|
|
b.Log.Errorf("error on update of contacts: %v", err)
|
2019-02-21 20:28:13 +01:00
|
|
|
}
|
2020-11-27 00:34:44 +01:00
|
|
|
|
2022-01-31 00:13:37 +01:00
|
|
|
allcontacts, err := b.wc.Store.Contacts.GetAllContacts()
|
2020-11-27 00:34:44 +01:00
|
|
|
if err != nil {
|
2022-01-31 00:13:37 +01:00
|
|
|
b.Log.Errorf("error on update of contacts: %v", err)
|
2020-11-27 00:34:44 +01:00
|
|
|
}
|
|
|
|
|
2022-01-31 00:13:37 +01:00
|
|
|
if len(allcontacts) > 0 {
|
|
|
|
b.contacts = allcontacts
|
2020-11-27 00:34:44 +01:00
|
|
|
}
|
|
|
|
|
2022-01-31 00:13:37 +01:00
|
|
|
if sender, exists := b.contacts[senderJid]; exists {
|
|
|
|
if sender.FullName != "" {
|
|
|
|
return sender.FullName
|
2019-02-21 20:28:13 +01:00
|
|
|
}
|
|
|
|
// if user is not in phone contacts
|
|
|
|
// it is the most obvious scenario unless you sync your phone contacts with some remote updated source
|
|
|
|
// users can change it in their WhatsApp settings -> profile -> click on Avatar
|
2022-01-31 00:13:37 +01:00
|
|
|
if sender.PushName != "" {
|
|
|
|
return sender.PushName
|
2020-06-25 00:35:49 +02:00
|
|
|
}
|
|
|
|
|
2022-01-31 00:13:37 +01:00
|
|
|
if sender.FirstName != "" {
|
|
|
|
return sender.FirstName
|
2020-06-25 00:35:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-31 00:13:37 +01:00
|
|
|
return "Someone"
|
2019-02-21 20:28:13 +01:00
|
|
|
}
|
|
|
|
|
2022-01-31 00:13:37 +01:00
|
|
|
func (b *Bwhatsapp) getSenderNotify(senderJid types.JID) string {
|
|
|
|
if sender, exists := b.contacts[senderJid]; exists {
|
|
|
|
return sender.PushName
|
2019-02-21 20:28:13 +01:00
|
|
|
}
|
2020-11-27 00:34:44 +01:00
|
|
|
|
2019-02-21 20:28:13 +01:00
|
|
|
return ""
|
|
|
|
}
|
2019-06-01 00:53:49 +02:00
|
|
|
|
2022-01-31 00:13:37 +01:00
|
|
|
func (b *Bwhatsapp) GetProfilePicThumb(jid string) (*types.ProfilePictureInfo, error) {
|
|
|
|
pjid, _ := types.ParseJID(jid)
|
|
|
|
info, err := b.wc.GetProfilePictureInfo(pjid, true)
|
2019-06-01 00:53:49 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to get avatar: %v", err)
|
|
|
|
}
|
2020-11-27 00:34:44 +01:00
|
|
|
|
2019-06-01 00:53:49 +02:00
|
|
|
return info, nil
|
|
|
|
}
|
2020-11-27 00:34:44 +01:00
|
|
|
|
|
|
|
func isGroupJid(identifier string) bool {
|
|
|
|
return strings.HasSuffix(identifier, "@g.us") ||
|
|
|
|
strings.HasSuffix(identifier, "@temp") ||
|
|
|
|
strings.HasSuffix(identifier, "@broadcast")
|
|
|
|
}
|
2022-01-31 00:13:37 +01:00
|
|
|
|
|
|
|
func (b *Bwhatsapp) getDevice() (*store.Device, error) {
|
|
|
|
device := &store.Device{}
|
|
|
|
|
2022-03-13 01:01:03 +01:00
|
|
|
storeContainer, err := sqlstore.New("sqlite", "file:"+b.Config.GetString("sessionfile")+".db?_foreign_keys=on&_pragma=busy_timeout=10000", nil)
|
2022-01-31 00:13:37 +01:00
|
|
|
if err != nil {
|
|
|
|
return device, fmt.Errorf("failed to connect to database: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
device, err = storeContainer.GetFirstDevice()
|
|
|
|
if err != nil {
|
|
|
|
return device, fmt.Errorf("failed to get device: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return device, nil
|
|
|
|
}
|