Make vCard requests asynchronous

This commit is contained in:
bodqhrohro 2019-12-19 22:58:20 +02:00
parent ab914b0ff7
commit 472d4b7bef

View file

@ -34,78 +34,7 @@ func HandleIq(s xmpp.Sender, p stanza.Packet) {
if iq.Type == "get" {
_, ok := iq.Payload.(*extensions.IqVcardTemp)
if ok {
log.WithFields(log.Fields{
"from": iq.From,
"to": iq.To,
}).Warn("VCard request")
fromJid, err := xmpp.NewJid(iq.From)
if err != nil {
log.Error("Invalid from JID!")
return
}
session, ok := sessions[fromJid.Bare()]
if !ok {
log.Error("IQ from stranger")
return
}
toParts := strings.Split(iq.To, "@")
toID, err := strconv.ParseInt(toParts[0], 10, 64)
if err != nil {
log.Error("Invalid IQ to")
return
}
chat, user, err := session.GetContactByID(toID, nil)
if err != nil {
log.Error(err)
return
}
vcard := extensions.IqVcardTemp{}
if chat != nil {
vcard.Fn.Text = chat.Title
if chat.Photo != nil {
path := chat.Photo.Small.Local.Path
file, err := os.Open(path)
if err == nil {
defer file.Close()
buf := new(bytes.Buffer)
binval := base64.NewEncoder(base64.StdEncoding, buf)
_, err = io.Copy(binval, file)
if err == nil {
vcard.Photo.Type.Text = "image/jpeg"
vcard.Photo.Binval.Text = buf.String()
} else {
log.Errorf("Error calculating hash: %v", path)
}
} else if path != "" {
log.Errorf("Photo does not exist: %v", path)
}
}
}
if user != nil {
vcard.Nickname.Text = user.Username
vcard.N.Given.Text = user.FirstName
vcard.N.Family.Text = user.LastName
vcard.Tel.Number.Text = user.PhoneNumber
}
answer := stanza.IQ{
Attrs: stanza.Attrs{
From: iq.To,
To: iq.From,
Id: iq.Id,
Type: "result",
},
Payload: vcard,
}
log.Debugf("%#v", answer)
_ = s.Send(answer)
go handleGetVcardTempIq(s, iq)
}
}
}
@ -247,3 +176,78 @@ func handlePresence(s xmpp.Sender, p stanza.Presence) {
}()
}
}
func handleGetVcardTempIq(s xmpp.Sender, iq stanza.IQ) {
log.WithFields(log.Fields{
"from": iq.From,
"to": iq.To,
}).Warn("VCard request")
fromJid, err := xmpp.NewJid(iq.From)
if err != nil {
log.Error("Invalid from JID!")
return
}
session, ok := sessions[fromJid.Bare()]
if !ok {
log.Error("IQ from stranger")
return
}
toParts := strings.Split(iq.To, "@")
toID, err := strconv.ParseInt(toParts[0], 10, 64)
if err != nil {
log.Error("Invalid IQ to")
return
}
chat, user, err := session.GetContactByID(toID, nil)
if err != nil {
log.Error(err)
return
}
vcard := extensions.IqVcardTemp{}
if chat != nil {
vcard.Fn.Text = chat.Title
if chat.Photo != nil {
path := chat.Photo.Small.Local.Path
file, err := os.Open(path)
if err == nil {
defer file.Close()
buf := new(bytes.Buffer)
binval := base64.NewEncoder(base64.StdEncoding, buf)
_, err = io.Copy(binval, file)
if err == nil {
vcard.Photo.Type.Text = "image/jpeg"
vcard.Photo.Binval.Text = buf.String()
} else {
log.Errorf("Error calculating hash: %v", path)
}
} else if path != "" {
log.Errorf("Photo does not exist: %v", path)
}
}
}
if user != nil {
vcard.Nickname.Text = user.Username
vcard.N.Given.Text = user.FirstName
vcard.N.Family.Text = user.LastName
vcard.Tel.Number.Text = user.PhoneNumber
}
answer := stanza.IQ{
Attrs: stanza.Attrs{
From: iq.To,
To: iq.From,
Id: iq.Id,
Type: "result",
},
Payload: vcard,
}
log.Debugf("%#v", answer)
_ = s.Send(answer)
}