Retrieve Bio / Description for vCard desc / note

This commit is contained in:
Bohdan Horbeshko 2022-06-29 09:56:27 -04:00
parent 76ac662366
commit 38ffdb9e47
3 changed files with 64 additions and 3 deletions

View file

@ -1023,6 +1023,47 @@ func (c *Client) OpenPhotoFile(photoFile *client.File, priority int32) (*os.File
return nil, path, err return nil, path, err
} }
// GetChatDescription obtains bio or description according to the chat type
func (c *Client) GetChatDescription(chat *client.Chat) string {
chatType := chat.Type.ChatTypeType()
if chatType == client.TypeChatTypePrivate {
privateType, _ := chat.Type.(*client.ChatTypePrivate)
fullInfo, err := c.client.GetUserFullInfo(&client.GetUserFullInfoRequest{
UserId: privateType.UserId,
})
if err == nil {
if fullInfo.Bio != "" {
return fullInfo.Bio
} else if fullInfo.Description != "" {
return fullInfo.Description
}
} else {
log.Warnf("Coudln't retrieve private chat info: %v", err.Error())
}
} else if chatType == client.TypeChatTypeBasicGroup {
basicGroupType, _ := chat.Type.(*client.ChatTypeBasicGroup)
fullInfo, err := c.client.GetBasicGroupFullInfo(&client.GetBasicGroupFullInfoRequest{
BasicGroupId: basicGroupType.BasicGroupId,
})
if err == nil {
return fullInfo.Description
} else {
log.Warnf("Coudln't retrieve basic group info: %v", err.Error())
}
} else if chatType == client.TypeChatTypeSupergroup {
supergroupType, _ := chat.Type.(*client.ChatTypeSupergroup)
fullInfo, err := c.client.GetSupergroupFullInfo(&client.GetSupergroupFullInfoRequest{
SupergroupId: supergroupType.SupergroupId,
})
if err == nil {
return fullInfo.Description
} else {
log.Warnf("Coudln't retrieve supergroup info: %v", err.Error())
}
}
return ""
}
// subscribe to a Telegram ID // subscribe to a Telegram ID
func (c *Client) subscribeToID(id int64, chat *client.Chat) { func (c *Client) subscribeToID(id int64, chat *client.Chat) {
var args []args.V var args []args.V

View file

@ -32,6 +32,7 @@ type IqVcardTemp struct {
N IqVcardN N IqVcardN
Tel IqVcardTel Tel IqVcardTel
Photo IqVcardPhoto Photo IqVcardPhoto
Desc IqVcardDesc
ResultSet *stanza.ResultSet `xml:"set,omitempty"` ResultSet *stanza.ResultSet `xml:"set,omitempty"`
} }
@ -104,6 +105,12 @@ type IqVcardPhotoBinval struct {
Text string `xml:",chardata"` Text string `xml:",chardata"`
} }
// IqVcardDesc is vCard/DESC
type IqVcardDesc struct {
XMLName xml.Name `xml:"DESC"`
Text string `xml:",chardata"`
}
// Namespace is a namespace! // Namespace is a namespace!
func (c PresenceNickExtension) Namespace() string { func (c PresenceNickExtension) Namespace() string {
return c.XMLName.Space return c.XMLName.Space

View file

@ -260,7 +260,7 @@ func handleGetVcardIq(s xmpp.Sender, iq *stanza.IQ, typ byte) {
return return
} }
var fn, photo, nickname, given, family, tel string var fn, photo, nickname, given, family, tel, info string
if chat != nil { if chat != nil {
fn = chat.Title fn = chat.Title
@ -284,6 +284,7 @@ func handleGetVcardIq(s xmpp.Sender, iq *stanza.IQ, typ byte) {
log.Errorf("PHOTO: %#v", err.Error()) log.Errorf("PHOTO: %#v", err.Error())
} }
} }
info = session.GetChatDescription(chat)
} }
if user != nil { if user != nil {
nickname = user.Username nickname = user.Username
@ -299,7 +300,7 @@ func handleGetVcardIq(s xmpp.Sender, iq *stanza.IQ, typ byte) {
Id: iq.Id, Id: iq.Id,
Type: "result", Type: "result",
}, },
Payload: makeVCardPayload(typ, iq.To, fn, photo, nickname, given, family, tel), Payload: makeVCardPayload(typ, iq.To, fn, photo, nickname, given, family, tel, info),
} }
log.Debugf("%#v", answer) log.Debugf("%#v", answer)
@ -371,7 +372,7 @@ func toToID(to string) (int64, bool) {
return toID, true return toID, true
} }
func makeVCardPayload(typ byte, id string, fn string, photo string, nickname string, given string, family string, tel string) stanza.IQPayload { func makeVCardPayload(typ byte, id, fn, photo, nickname, given, family, tel, info string) stanza.IQPayload {
if typ == TypeVCardTemp { if typ == TypeVCardTemp {
vcard := &extensions.IqVcardTemp{} vcard := &extensions.IqVcardTemp{}
@ -384,6 +385,7 @@ func makeVCardPayload(typ byte, id string, fn string, photo string, nickname str
vcard.N.Given.Text = given vcard.N.Given.Text = given
vcard.N.Family.Text = family vcard.N.Family.Text = family
vcard.Tel.Number.Text = tel vcard.Tel.Number.Text = tel
vcard.Desc.Text = info
return vcard return vcard
} else if typ == TypeVCard4 { } else if typ == TypeVCard4 {
@ -447,6 +449,17 @@ func makeVCardPayload(typ byte, id string, fn string, photo string, nickname str
}, },
}) })
} }
if info != "" {
nodes = append(nodes, stanza.Node{
XMLName: xml.Name{Local: "note"},
Nodes: []stanza.Node{
stanza.Node{
XMLName: xml.Name{Local: "text"},
Content: info,
},
},
})
}
pubsub := &stanza.PubSubGeneric{ pubsub := &stanza.PubSubGeneric{
Items: &stanza.Items{ Items: &stanza.Items{