Skip to content

Commit

Permalink
XEP-0398: don't wait for server notification, modify vcard photo in a…
Browse files Browse the repository at this point in the history
…dvance

When we publishing something and server supports xep-0398, then we can modify our local vcard cache, not waiting while
server does this for us. Also nto all servers send notification when conversion is done (Prosody doesn't send),
so doing the conversion on client side is the only good choice
  • Loading branch information
Ri0n committed Jun 6, 2024
1 parent a7848ee commit 71d4904
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/avatars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "pixmaputil.h"
#include "psiaccount.h"
#include "vcardfactory.h"
#include "xmpp/xmpp-im/xmpp_serverinfomanager.h"

#include <QBuffer>
#include <QDateTime>
Expand Down Expand Up @@ -1064,7 +1065,15 @@ void AvatarFactory::publish_success(const QString &n, const PubSubItem &item)
info_el.setAttribute("type", image2type(d->selfAvatarData_));
meta_el.appendChild(info_el);
account()->pepManager()->publish(PEP_AVATAR_METADATA_NS, PubSubItem(d->selfAvatarHash_, meta_el));
if (account()->client()->serverInfoManager()->account_features().hasAvatarConversion()) {
VCardFactory::instance()->setPhoto(account()->jid(), d->selfAvatarData_, {});
}
d->selfAvatarData_.clear(); // we don't need it anymore
} else if (n == PEP_AVATAR_METADATA_NS) {
bool removed = item.payload().firstChildElement("metadata").firstChildElement("info").isNull();
if (account()->client()->serverInfoManager()->account_features().hasAvatarConversion() && removed) {
VCardFactory::instance()->deletePhoto(account()->jid(), {});
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/psiaccount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2374,7 +2374,7 @@ void PsiAccount::serverFeaturesChanged()
j->go(true);
}

if (d->client->serverInfoManager()->features().hasVCard() && !d->vcardChecked) {
if (d->client->serverInfoManager()->server_features().hasVCard() && !d->vcardChecked) {
// Get the vcard
const VCard vcard = VCardFactory::instance()->vcard(d->jid);
if (PsiOptions::instance()->getOption("options.vcard.query-own-vcard-on-login").toBool() || vcard.isEmpty()
Expand Down
34 changes: 34 additions & 0 deletions src/vcardfactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,40 @@ VCardRequest *VCardFactory::getVCard(PsiAccount *account, const Jid &jid, Flags
return queuedLoader_->enqueue(account, jid, flags, QueuedLoader::HighPriority);
}

void VCardFactory::setPhoto(const Jid &j, const QByteArray &photo, Flags flags)
{
VCard vc;
Jid sj;
if (flags & MucUser) {
sj = j;
vc = mucVcard(j);
} else {
sj = j.withResource({});
vc = vcard(sj);
}
if (vc && vc.photo() != photo) {
vc.setPhoto({});
saveVCard(sj, vc, flags);
}
}

void VCardFactory::deletePhoto(const Jid &j, Flags flags)
{
VCard vc;
Jid sj;
if (flags & MucUser) {
sj = j;
vc = mucVcard(j);
} else {
sj = j.withResource({});
vc = vcard(sj);
}
if (vc && !vc.photo().isEmpty()) {
vc.setPhoto({});
saveVCard(sj, vc, flags);
}
}

void VCardFactory::ensureVCardPhotoUpdated(PsiAccount *acc, const Jid &jid, Flags flags, const QByteArray &newPhotoHash)
{
VCard vc;
Expand Down
3 changes: 3 additions & 0 deletions src/vcardfactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ class VCardFactory : public QObject {
JT_VCard *setVCard(const PsiAccount *account, const VCard &v, const Jid &targetJid, VCardFactory::Flags flags);
VCardRequest *getVCard(PsiAccount *account, const Jid &, VCardFactory::Flags flags = {});

void setPhoto(const Jid &j, const QByteArray &photo, Flags flags);
void deletePhoto(const Jid &j, Flags flags);

// 1. check if it's needed to do a request,
// 2. enqueue request if necessary (no vcard, or if hash doesn't match)
// 3. vcardChanged() will be sent as usually when vcard is updated
Expand Down

0 comments on commit 71d4904

Please sign in to comment.