comparison mod_muc_hats_api/mod_muc_hats_api.lua @ 3947:1f90e333b1d8

mod_muc_hats_api: New API-only module for managing user hats in MUCs
author Matthew Wild <mwild1@gmail.com>
date Thu, 19 Mar 2020 14:39:14 +0000
parents
children
comparison
equal deleted inserted replaced
3946:2a5b42e4db07 3947:1f90e333b1d8
1 local mod_muc = module:depends("muc");
2
3 function add_user_hat(user_jid, room_jid, hat_id, attachment)
4 local room = mod_muc.get_room_from_jid(room_jid);
5 if not room then
6 return nil, "item-not-found", "no such room";
7 end
8 local user_aff = room:get_affiliation(user_jid);
9 if not user_aff then
10 return nil, "item-not-found", "user not affiliated with room";
11 end
12 local aff_data = room:get_affiliation_data(user_jid) or {};
13 local hats = aff_data.hats;
14 if not hats then
15 hats = {};
16 aff_data.hats = hats;
17 end
18
19 hats[hat_id] = {
20 active = attachment.active;
21 required = attachment.required;
22 title = attachment.title;
23 };
24
25 local ok, err = room:set_affiliation(true, user_jid, user_aff, nil, aff_data);
26 if not ok then
27 return nil, err;
28 end
29 return true;
30 end
31
32 function remove_user_hat(user_jid, room_jid, hat_id)
33 local room = mod_muc.get_room_from_jid(room_jid);
34 if not room then
35 return nil, "item-not-found", "no such room";
36 end
37 local user_aff = room:get_affiliation(user_jid);
38 if not user_aff then
39 return nil, "item-not-found", "user not affiliated with room";
40 end
41 local aff_data = room:get_affiliation_data(user_jid);
42 local hats = aff_data and aff_data.hats;
43 if not hats then
44 return true;
45 end
46
47 hats[hat_id] = nil;
48
49 local ok, err = room:set_affiliation(true, user_jid, user_aff, nil, aff_data);
50 if not ok then
51 return nil, err;
52 end
53 return true;
54 end
55
56 function set_user_hats(user_jid, room_jid, new_hats)
57 local room = mod_muc.get_room_from_jid(room_jid);
58 if not room then
59 return nil, "item-not-found", "no such room";
60 end
61 local user_aff = room:get_affiliation(user_jid);
62 if not user_aff then
63 return nil, "item-not-found", "user not affiliated with room";
64 end
65 local aff_data = room:get_affiliation_data(user_jid) or {};
66
67 aff_data.hats = new_hats;
68
69 local ok, err = room:set_affiliation(true, user_jid, user_aff, nil, aff_data);
70 if not ok then
71 return nil, err;
72 end
73 return true;
74 end
75