68
|
1 |
|
|
2 |
-- USER AVATAR (XEP-0084) |
|
3 |
|
|
4 |
-- library |
|
5 |
|
|
6 |
local sha1 = require 'sha1' |
|
7 |
local base64 = require 'base64' |
|
8 |
local lm = require 'lm' |
|
9 |
local pep = require 'lm.pep' |
|
10 |
local pubsub = require 'lm.pubsub' |
|
11 |
|
|
12 |
-- |
|
13 |
|
|
14 |
local F = { } |
|
15 |
|
|
16 |
-- TODO 'temporary disabling' |
|
17 |
-- however I cannot see a method to enable it back without republishing avatar :( |
|
18 |
-- this requires client to know, what is published on the server now |
|
19 |
-- maybe we can do that by requesting item without payload from server |
|
20 |
-- pubsub metadata notification handler |
|
21 |
|
|
22 |
function F.publish ( conn, data, success, fail, height, width ) |
|
23 |
local id = sha1.digest ( data ) |
|
24 |
pep.publish ( conn, 'urn:xmpp:avatar:data', |
|
25 |
{ id = id, |
|
26 |
data = { xmlns = 'urn:xmpp:avatar:data', |
|
27 |
base64.encode ( data ), |
|
28 |
}, |
|
29 |
}, |
|
30 |
function () |
|
31 |
pep.publish ( conn, 'urn:xmpp:avatar:metadata', |
|
32 |
{ id = id, |
|
33 |
info = { bytes = data:len (), id = id, type = 'image/png', height = height, width = width }, |
|
34 |
}, success, fail ) |
|
35 |
end, fail ) |
|
36 |
end |
|
37 |
|
|
38 |
function F.publish_url ( conn, url, iid, size, id, mtype, success, fail, height, width ) |
|
39 |
pep.publish ( conn, 'urn:xmpp:avatar:metadata', |
|
40 |
{ id = iid, |
|
41 |
info = { bytes = size, id = id, type = mtype, url = url, height = height, width = width }, |
|
42 |
}, success, fail ) |
|
43 |
end |
|
44 |
|
|
45 |
function F.get ( conn, from, id, success, fail ) |
|
46 |
pubsub.retrieve ( conn, from, 'urn:xmpp:avatar:data', |
|
47 |
function ( from, node, item ) |
|
48 |
local data = item:child ( 'data' ) |
|
49 |
if data then |
|
50 |
success ( data:value () ) |
|
51 |
else |
|
52 |
-- XXX |
|
53 |
end |
|
54 |
end, fail, nil, { id } ) |
|
55 |
end |
|
56 |
|
|
57 |
return F |
|
58 |
|
|
59 |
-- vim: se ts=4: -- |