Comparison

mod_muc_hats_adhoc/mod_muc_hats_adhoc.lua @ 3948:11825788a452

mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
author Matthew Wild <mwild1@gmail.com>
date Thu, 19 Mar 2020 14:51:44 +0000
comparison
equal deleted inserted replaced
3947:1f90e333b1d8 3948:11825788a452
1 module:depends("adhoc");
2 local adhoc_new = module:require("adhoc").new;
3 local hats_api = module:depends("muc_hats_api");
4
5 local dataforms_new = require "util.dataforms".new;
6 local adhoc_simple = require "util.adhoc".new_simple_form;
7
8 local function generate_error_message(errors)
9 local errmsg = {};
10 for name, err in pairs(errors) do
11 errmsg[#errmsg + 1] = name .. ": " .. err;
12 end
13 return { status = "completed", error = { message = table.concat(errmsg, "\n") } };
14 end
15
16 local add_hat_form_layout = dataforms_new {
17 title = "Add a hat";
18 instructions = "Assign a hat to a room member";
19
20 { name = "user", type = "jid-single", label = "User JID", required = true };
21 { name = "room", type = "jid-single", label = "Room JID", required = true };
22 { name = "title", type = "text-single", label = "Hat title" };
23 { name = "uri", type = "text-single", label = "Hat URI", required = true };
24 };
25
26 local add_hat_handler = adhoc_simple(add_hat_form_layout, function (fields, errs)
27 if errs then
28 return generate_error_message(errs);
29 end
30
31 local ok, err_cond, err_text = hats_api.add_user_hat(fields.user, fields.room, fields.uri, {
32 active = true;
33 required = true;
34 title = fields.title;
35 });
36
37 return {
38 status = "completed";
39 info = ok and "The hat has been added successfully" or ("There was an error adding the hat: "..(err_text or err_cond));
40 };
41
42 end);
43
44 local remove_hat_form_layout = dataforms_new {
45 title = "Remove a hat";
46 instructions = "Remove a hat from a room member";
47
48 { name = "user", type = "jid-single", label = "User JID", required = true };
49 { name = "room", type = "jid-single", label = "Room JID", required = true };
50 { name = "uri", type = "text-single", label = "Hat URI", required = true };
51 };
52
53 local remove_hat_handler = adhoc_simple(remove_hat_form_layout, function (fields, errs)
54 if errs then
55 return generate_error_message(errs);
56 end
57
58 local ok, err_cond, err_text = hats_api.remove_user_hat(fields.user, fields.room, fields.uri);
59
60 return {
61 status = "completed";
62 info = ok and "The hat has been removed successfully" or ("There was an error removing the hat: "..(err_text or err_cond));
63 };
64
65 end);
66
67 local add_hat_desc = adhoc_new("Add hat to a user", "http://prosody.im/protocol/hats#add", add_hat_handler, "admin");
68 local remove_hat_desc = adhoc_new("Remove hat from a user", "http://prosody.im/protocol/hats#remove", remove_hat_handler, "admin");
69
70 module:provides("adhoc", add_hat_desc);
71 module:provides("adhoc", remove_hat_desc);