Software /
code /
prosody-modules
Annotate
mod_groups_internal/mod_groups_internal.lua @ 4385:dfb34cc97028
mod_groups_internal: allow specifying a group_id on create
It will refuse to create that group if it exists already, as it
should.
author | Jonas Schäfer <jonas@wielicki.name> |
---|---|
date | Mon, 25 Jan 2021 21:32:53 +0100 |
parent | 4383:1e7406b85add |
child | 4386:6357ac65b4eb |
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 |
4385
dfb34cc97028
mod_groups_internal: allow specifying a group_id on create
Jonas Schäfer <jonas@wielicki.name>
parents:
4383
diff
changeset
|
87 function create(group_info, create_muc, group_id) |
4383
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 |
4385
dfb34cc97028
mod_groups_internal: allow specifying a group_id on create
Jonas Schäfer <jonas@wielicki.name>
parents:
4383
diff
changeset
|
91 if group_id then |
dfb34cc97028
mod_groups_internal: allow specifying a group_id on create
Jonas Schäfer <jonas@wielicki.name>
parents:
4383
diff
changeset
|
92 if exists(group_id) then |
dfb34cc97028
mod_groups_internal: allow specifying a group_id on create
Jonas Schäfer <jonas@wielicki.name>
parents:
4383
diff
changeset
|
93 return nil, "conflict" |
dfb34cc97028
mod_groups_internal: allow specifying a group_id on create
Jonas Schäfer <jonas@wielicki.name>
parents:
4383
diff
changeset
|
94 end |
dfb34cc97028
mod_groups_internal: allow specifying a group_id on create
Jonas Schäfer <jonas@wielicki.name>
parents:
4383
diff
changeset
|
95 else |
dfb34cc97028
mod_groups_internal: allow specifying a group_id on create
Jonas Schäfer <jonas@wielicki.name>
parents:
4383
diff
changeset
|
96 group_id = id.short(); |
dfb34cc97028
mod_groups_internal: allow specifying a group_id on create
Jonas Schäfer <jonas@wielicki.name>
parents:
4383
diff
changeset
|
97 end |
4383
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
98 |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
99 if create_muc then |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
100 return nil, "not-implemented"; |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
101 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
102 |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
103 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
|
104 name = group_info.name; |
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 if not ok then |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
107 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
|
108 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
109 return group_id; |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
110 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
111 |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
112 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
|
113 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
|
114 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
115 |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
116 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
|
117 if not info then |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
118 return nil, "bad-request" |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
119 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
120 |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
121 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
|
122 return nil, "bad-request" |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
123 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
124 |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
125 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
|
126 if not ok then |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
127 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
|
128 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
129 return true |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
130 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
131 |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
132 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
|
133 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
|
134 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
135 |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
136 function exists(group_id) |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
137 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
|
138 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
139 |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
140 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
|
141 local groups = {}; |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
142 do |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
143 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
|
144 if group_set then |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
145 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
|
146 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
|
147 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
148 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
149 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
150 return groups; |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
151 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
152 |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
153 function delete(group_id) |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
154 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
|
155 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
|
156 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
157 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
|
158 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
159 |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
160 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
|
161 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
|
162 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
|
163 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
|
164 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
165 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
|
166 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
|
167 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
168 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
|
169 return true; |
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 |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
172 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
|
173 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
|
174 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
|
175 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
|
176 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
177 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
|
178 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
|
179 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
180 return true; |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
181 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
182 |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
183 -- 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
|
184 function groups() |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
185 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
|
186 end |