Mercurial > prosody-modules
annotate mod_groups_internal/mod_groups_internal.lua @ 4383:1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Mon, 25 Jan 2021 18:43:20 +0100 |
parents | |
children | dfb34cc97028 |
rev | line source |
---|---|
4383
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 local rostermanager = require"core.rostermanager"; |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 local id = require "util.id"; |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 local jid = require "util.jid"; |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 local jid_join = jid.join; |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 local host = module.host; |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 local group_info_store = module:open_store("group_info"); |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 local group_members_store = module:open_store("groups"); |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 local group_memberships = module:open_store("groups", "map"); |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 local is_contact_subscribed = rostermanager.is_contact_subscribed; |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 -- Make a *one-way* subscription. User will see when contact is online, |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 -- contact will not see when user is online. |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 local function subscribe(user, user_jid, contact, contact_jid) |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 -- Update user's roster to say subscription request is pending... |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 rostermanager.set_contact_pending_out(user, host, contact_jid); |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 -- Update contact's roster to say subscription request is pending... |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 rostermanager.set_contact_pending_in(contact, host, user_jid); |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 -- Update contact's roster to say subscription request approved... |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 rostermanager.subscribed(contact, host, user_jid); |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 -- Update user's roster to say subscription request approved... |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 rostermanager.process_inbound_subscription_approval(user, host, contact_jid); |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 -- Push updates to both rosters |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 rostermanager.roster_push(user, host, contact_jid); |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 rostermanager.roster_push(contact, host, user_jid); |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 local function user_groups(username) |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 return pairs(group_memberships:get_all(username) or {}); |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 local function do_single_group_subscriptions(username, group_id) |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 local members = group_members_store:get(group_id); |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 if not members then return; end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 local user_jid = jid_join(username, host); |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 for membername in pairs(members) do |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 if membername ~= username then |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 local member_jid = jid_join(membername, host); |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 if not is_contact_subscribed(username, host, member_jid) then |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 module:log("debug", "[group %s] Subscribing %s to %s", member_jid, user_jid); |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 subscribe(membername, member_jid, username, user_jid); |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 if not is_contact_subscribed(membername, host, user_jid) then |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 module:log("debug", "[group %s] Subscribing %s to %s", user_jid, member_jid); |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 subscribe(username, user_jid, membername, member_jid); |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 local function do_all_group_subscriptions_by_user(username) |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 for group_id in user_groups(username) do |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 do_single_group_subscriptions(username, group_id); |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 local function do_all_group_subscriptions_by_group(group_id) |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 for membername in pairs(get_members(group_id)) do |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 do_single_group_subscriptions(membername, group_id); |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 module:hook("resource-bind", function(event) |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 module:log("debug", "Updating group subscriptions..."); |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 do_all_group_subscriptions_by_user(event.session.username); |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 end); |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 local function get_group_muc(group_id) |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 -- Group MUC |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 local group_info = group_info_store:get(group_id); |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 if group_info and group_info.muc_jid then |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 local muc_jid = group_info.muc_jid; |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 local mod_muc = hosts[jid.host(muc_jid)].modules.muc; |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 if mod_muc then |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 local room = mod_muc.get_room_from_jid(muc_jid); |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 if not room then |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 room = mod_muc.create_room(muc_jid); |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 return room; |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 --luacheck: ignore 131 |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 function create(group_info, create_muc) |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 if not group_info.name then |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 return nil, "group-name-required"; |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 local group_id = id.short(); |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
93 if create_muc then |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
94 return nil, "not-implemented"; |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
95 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
96 |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
97 local ok = group_info_store:set(group_id, { |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
98 name = group_info.name; |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
99 }); |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
100 if not ok then |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
101 return nil, "internal-server-error"; |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
102 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
103 return group_id; |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
104 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
105 |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
106 function get_info(group_id) |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
107 return group_info_store:get(group_id); |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
108 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
109 |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
110 function set_info(group_id, info) |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
111 if not info then |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
112 return nil, "bad-request" |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
113 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
114 |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
115 if not info.name or #info.name == 0 then |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
116 return nil, "bad-request" |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
117 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
118 |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
119 local ok = group_info_store:set(group_id, info); |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
120 if not ok then |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
121 return nil, "internal-server-error"; |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
122 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
123 return true |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
124 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
125 |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
126 function get_members(group_id) |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
127 return group_members_store:get(group_id); |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
128 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
129 |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
130 function exists(group_id) |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
131 return not not get_info(group_id); |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
132 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
133 |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
134 function get_user_groups(username) |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
135 local groups = {}; |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
136 do |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
137 local group_set = group_memberships:get_all(username); |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
138 if group_set then |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
139 for group_id in pairs(group_set) do |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
140 table.insert(groups, group_id); |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
141 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
142 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
143 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
144 return groups; |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
145 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
146 |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
147 function delete(group_id) |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
148 if group_members_store:set(group_id, nil) then |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
149 return group_info_store:set(group_id, nil); |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
150 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
151 return nil, "internal-server-error"; |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
152 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
153 |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
154 function add_member(group_id, username) |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
155 local group_info = group_info_store:get(group_id); |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
156 if not group_info then |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
157 return nil, "group-not-found"; |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
158 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
159 if not group_memberships:set(group_id, username, {}) then |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
160 return nil, "internal-server-error"; |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
161 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
162 do_all_group_subscriptions_by_group(group_id); |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
163 return true; |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
164 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
165 |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
166 function remove_member(group_id, username) |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
167 local group_info = group_info_store:get(group_id); |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
168 if not group_info then |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
169 return nil, "group-not-found"; |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
170 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
171 if not group_memberships:set(group_id, username, nil) then |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
172 return nil, "internal-server-error"; |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
173 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
174 return true; |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
175 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
176 |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
177 -- Returns iterator over group ids |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
178 function groups() |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
179 return group_info_store:users(); |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
180 end |