class XMPP::UserAvatar

Overview

UserAvatar is a high-level client for XEP-0084 - User Avatar and XEP-0398 - User Avatar to vCard-Based Avatars.

Avatars are published to two PEP nodes: a "urn:xmpp:avatar:data" node holding the base64 image bytes and a "urn:xmpp:avatar:metadata" node describing the formats (size, MIME type, and an id hashing the image). Subscribers that receive the metadata fetch the data only if it differs from what they already hold. XEP-0398 bridges this to the legacy vCard-based avatar (XEP-0153) by converting between AvatarData and a vCard PHOTO.

avatar = XMPP::UserAvatar.new(client) avatar.publish(base64_bytes, "image/png", 2048, width: 64, height: 64) avatar.subscribe("[email protected]")

client.on("message") do |s, p| if metadata = XMPP::UserAvatar.metadata_from_message(p.as(XMPP::Stanza::Message)) metadata.infos.first.try(&.id) # fetch the matching data node end end

See: https://xmpp.org/extensions/xep-0084.html and https://xmpp.org/extensions/xep-0398.html

Defined in:

xmpp/user_avatar.cr

Constant Summary

NS_DATA = "urn:xmpp:avatar:data"

The PEP node that holds the avatar image bytes.

NS_METADATA = "urn:xmpp:avatar:metadata"

The PEP node that describes the avatar formats.

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.new(client : Client) #

[View source]

Class Method Detail

def self.avatar_from_photo(photo : Stanza::Photo) : Stanza::AvatarData #

avatar_from_photo converts a vCard PHOTO into an AvatarData payload, per XEP-0398 §3.


[View source]
def self.metadata_from_message(message : Stanza::Message) : Stanza::AvatarMetadata | Nil #

metadata_from_message extracts avatar metadata from a PEP notification message, or nil when the message does not carry one.


[View source]
def self.to_vcard_photo(data : String, type : String) : Stanza::Photo #

to_vcard_photo builds the vCard PHOTO (XEP-0153) equivalent of an avatar data payload, per XEP-0398 §3.


[View source]

Instance Method Detail

def publish(data : String, type : String, bytes : Int32, id : String | Nil = nil, width : Int32 = 0, height : Int32 = 0) #

publish stores an avatar image on the client's data and metadata PEP nodes. data is the base64-encoded image, type its MIME type, and bytes the decoded size in bytes. id hashes the image (defaults to the hex SHA-1 of the base64 text).


[View source]
def subscribe(jid : String) #

subscribe subscribes the client to another user's avatar metadata node.


[View source]
def unsubscribe(jid : String) #

unsubscribe removes the client's subscription to a user's avatar metadata node.


[View source]