Comparison

mod_muc_members_json/mod_muc_members_json.lua @ 5586:30b9f78b5058

mod_muc_members_json: New module to import MUC membership from a JSON URL
author Matthew Wild <mwild1@gmail.com>
date Fri, 07 Jul 2023 01:25:44 +0100
child 5587:dd4df71166ea
comparison
equal deleted inserted replaced
5585:5b316088bef5 5586:30b9f78b5058
1 local http = require "net.http";
2 local json = require "util.json";
3
4 local json_url = assert(module:get_option_string("muc_members_json_url"), "muc_members_json_url required");
5 local managed_mucs = module:get_option("muc_members_json_mucs");
6
7 local mod_muc = module:depends("muc");
8
9 --[[
10 {
11 xsf = {
12 team_hats = {
13 board = {
14 id = "xmpp:xmpp.org/hats/board";
15 title = "Board";
16 };
17 };
18 member_hat = {
19 id = "xmpp:xmpp.org/hats/member";
20 title = "XSF member";
21 };
22 };
23 iteam = {
24 team_hats = {
25 iteam = {
26 id = "xmpp:xmpp.org/hats/iteam";
27 title = "Infra team";
28 };
29 };
30 };
31 }
32 --]]
33
34 local function get_hats(member_info, muc_config)
35 local hats = {};
36 if muc_config.member_hat then
37 hats[muc_config.member_hat.id] = {
38 title = muc_config.member_hat.title;
39 };
40 end
41 if muc_config.team_hats and member_info.roles then
42 for _, role in ipairs(member_info.roles) do
43 local hat = muc_config.team_hats[role];
44 if hat then
45 hats[hat.id] = {
46 title = hat.title;
47 };
48 end
49 end
50 end
51 return hats;
52 end
53
54 function module.load()
55 http.request(json_url)
56 :next(function (result)
57 return json.decode(result.body);
58 end)
59 :next(function (data)
60 module:log("debug", "DATA: %s", require "util.serialization".serialize(data, "debug"));
61
62 for name, muc_config in pairs(managed_mucs) do
63 local muc_jid = name.."@"..module.host;
64 local muc = mod_muc.get_room_from_jid(muc_jid);
65 module:log("warn", "%s -> %s -> %s", name, muc_jid, muc);
66 if muc then
67 local jids = {};
68 for _, member_info in ipairs(data.members) do
69 for _, member_jid in ipairs(member_info.jids) do
70 jids[member_jid] = true;
71 local affiliation = muc:get_affiliation(member_jid);
72 if not affiliation then
73 muc:set_affiliation(true, member_jid, "member", "imported membership");
74 muc:set_affiliation_data(member_jid, "source", module.name);
75 end
76 muc:set_affiliation_data(member_jid, "hats", get_hats(member_info, muc_config));
77 end
78 end
79 -- Remove affiliation from folk who weren't in the source data but previously were
80 for jid, aff, data in muc:each_affiliation() do
81 if not jids[jid] and data.source == module.name then
82 muc:set_affiliation(true, jid, "none", "imported membership lost");
83 end
84 end
85 end
86 end
87
88 end):catch(function (err)
89 module:log("error", "FAILED: %s", err);
90 end);
91 end