Builtin-Prosody: use Peertube's avatar.

This commit is contained in:
John Livingston 2022-01-05 18:53:44 +01:00
parent be592aeacf
commit 6afb640df2
No known key found for this signature in database
GPG Key ID: B17B5640CE66CDBC
4 changed files with 94 additions and 8 deletions

View File

@ -0,0 +1,5 @@
# mod_vcard_peertubelivechat
This module is a custom module that allows Prosody to load vCards from Peertube.
This module is part of peertube-plugin-livechat, and is under the same LICENSE.

View File

@ -0,0 +1,73 @@
local st = require "util.stanza";
local http = require "net.http";
local async = require "util.async";
local b64 = require "util.encodings".base64.encode;
local jid_split = require "util.jid".split;
local json = require "util.json";
local uh = require "util.http";
module:add_feature("vcard-temp");
local peertube_url = assert(module:get_option_string("peertubelivechat_vcard_peertube_url", nil), "'peertubelivechat_vcard_peertube_url' is a required option");
if peertube_url:sub(-1,-1) == "/" then peertube_url = peertube_url:sub(1,-2); end
module:hook("iq-get/bare/vcard-temp:vCard", function (event)
local origin, stanza = event.origin, event.stanza;
local who = jid_split(stanza.attr.to) or origin.username
module:log("debug", "vCard request for %s", who);
local wait, done = async.waiter();
local url = peertube_url .. '/api/v1/accounts/' .. uh.urlencode(who);
module:log("debug", "Calling Peertube API: %s", url);
local ret;
http.request(url, { accept = "application/json" }, function (body, code)
if math.floor(code / 100) == 2 then
local parsed, parse_err = json.decode(body);
if not parsed then
module:log("debug", "Got invalid JSON from %s: %s", url, parse_err);
else
ret = parsed;
end
else
module:log("debug", "Rejected by API: ", body);
end
done();
end)
wait();
if not ret then
module:log("debug", "Peertube user not found, no vCard for %s", who);
origin.send(st.error_reply(stanza, "cancel", "item-not-found"));
return true;
end
local vcard_temp = st.stanza("vCard", { xmlns = "vcard-temp" });
vcard_temp:text_tag("NICKNAME", ret.displayName);
if ret.avatar and ret.avatar.path then
module:log("debug", "Downloading user avatar on %s", peertube_url .. ret.avatar.path);
local waitAvatar, doneAvatar = async.waiter();
http.request(peertube_url .. ret.avatar.path, {}, function (body, code, response)
if math.floor(code / 100) == 2 then
module:log("debug", "Avatar found for %s", who);
vcard_temp:tag("PHOTO")
if (response and response.headers and response.headers["Content-Type"]) then
module:log("debug", "Avatar Content-Type: %s", response.headers["Content-Type"]);
vcard_temp:text_tag("TYPE", response.headers["Content-Type"])
end
vcard_temp:text_tag("BINVAL", b64(body))
vcard_temp:up()
else
module:log("debug", "Cant load avatar: ", body);
end
doneAvatar();
end)
waitAvatar();
end
origin.send(st.reply(stanza):add_child(vcard_temp));
return true;
end, 1); -- TODO: Negative priority, so if the user has set a custom vCard (mod_vcard_legacy), it will be used?
-- TODO: cache results for N seconds

View File

@ -108,14 +108,14 @@ async function getProsodyConfig (options: RegisterServerOptions): Promise<Prosod
const apikey = await getAPIKey(options)
valuesToHideInDiagnostic.set('APIKey', apikey)
let baseApiUrl = settings['prosody-peertube-uri'] as string
if (baseApiUrl && !/^https?:\/\/[a-z0-9.-_]+(?::\d+)?$/.test(baseApiUrl)) {
let basePeertubeUrl = settings['prosody-peertube-uri'] as string
if (basePeertubeUrl && !/^https?:\/\/[a-z0-9.-_]+(?::\d+)?$/.test(basePeertubeUrl)) {
throw new Error('Invalid prosody-peertube-uri')
}
if (!baseApiUrl) {
baseApiUrl = options.peertubeHelpers.config.getWebserverUrl()
if (!basePeertubeUrl) {
basePeertubeUrl = options.peertubeHelpers.config.getWebserverUrl()
}
baseApiUrl += getBaseRouterRoute(options) + 'api/'
const baseApiUrl = basePeertubeUrl + getBaseRouterRoute(options) + 'api/'
const authApiUrl = baseApiUrl + 'user' // FIXME: should be protected by apikey, but mod_auth_http cant handle params
const roomApiUrl = baseApiUrl + 'room?apikey=' + apikey + '&jid={room.jid|jid_node}'
@ -152,6 +152,7 @@ async function getProsodyConfig (options: RegisterServerOptions): Promise<Prosod
config.useDefaultPersistent()
config.useListRoomsApi(apikey)
config.usePeertubeVCards(basePeertubeUrl)
config.useTestModule(apikey, testApiUrl)

View File

@ -157,9 +157,9 @@ class ProsodyConfigContent {
'uptime', // Report how long server has been running
'ping', // Replies to XMPP pings with pongs
'bosh', // Enable BOSH clients, aka "Jabber over HTTP"
'posix', // POSIX functionality, sends server to background, enables syslog, etc.
'pep', // Enables users to publish their avatar, mood, activity, playing music and more
'vcard_legacy' // Conversion between legacy vCard and PEP Avatar, vcard
'posix' // POSIX functionality, sends server to background, enables syslog, etc.
// 'pep', // Enables users to publish their avatar, mood, activity, playing music and more
// 'vcard_legacy' // Conversion between legacy vCard and PEP Avatar, vcard
// 'vcard4' // User profiles (stored in PEP)
])
@ -301,6 +301,13 @@ class ProsodyConfigContent {
this.muc.set('peertubelivechat_test_peertube_api_url', apiurl)
}
usePeertubeVCards (peertubeUrl: string): void {
if (this.authenticated) {
this.authenticated.add('modules_enabled', 'vcard_peertubelivechat')
this.authenticated.set('peertubelivechat_vcard_peertube_url', peertubeUrl)
}
}
setLog (level: ProsodyLogLevel, syslog?: ProsodyLogLevel[]): void {
let log = ''
log += 'log = {\n'