Retrieve Bio / Description for vCard desc / note

calls
Bohdan Horbeshko 2 years ago
parent 76ac662366
commit 38ffdb9e47

@ -1023,6 +1023,47 @@ func (c *Client) OpenPhotoFile(photoFile *client.File, priority int32) (*os.File
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
func (c *Client) subscribeToID(id int64, chat *client.Chat) {
var args []args.V

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

@ -260,7 +260,7 @@ func handleGetVcardIq(s xmpp.Sender, iq *stanza.IQ, typ byte) {
return
}
var fn, photo, nickname, given, family, tel string
var fn, photo, nickname, given, family, tel, info string
if chat != nil {
fn = chat.Title
@ -284,6 +284,7 @@ func handleGetVcardIq(s xmpp.Sender, iq *stanza.IQ, typ byte) {
log.Errorf("PHOTO: %#v", err.Error())
}
}
info = session.GetChatDescription(chat)
}
if user != nil {
nickname = user.Username
@ -299,7 +300,7 @@ func handleGetVcardIq(s xmpp.Sender, iq *stanza.IQ, typ byte) {
Id: iq.Id,
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)
@ -371,7 +372,7 @@ func toToID(to string) (int64, bool) {
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 {
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.Family.Text = family
vcard.Tel.Number.Text = tel
vcard.Desc.Text = info
return vcard
} 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{
Items: &stanza.Items{

Loading…
Cancel
Save