comparison mod_invites/mod_invites.lua @ 4346:671bc55f0fc9

mod_invites: Add support to internal API for creating reusable and custom-TTL invites
author Matthew Wild <mwild1@gmail.com>
date Sun, 17 Jan 2021 17:42:49 +0000
parents 844cfc8c4039
children 0ec482e617bb
comparison
equal deleted inserted replaced
4345:1bb08e9ffa82 4346:671bc55f0fc9
2 local it = require "util.iterators"; 2 local it = require "util.iterators";
3 local url = require "socket.url"; 3 local url = require "socket.url";
4 local jid_node = require "util.jid".node; 4 local jid_node = require "util.jid".node;
5 local jid_split = require "util.jid".split; 5 local jid_split = require "util.jid".split;
6 6
7 local invite_ttl = module:get_option_number("invite_expiry", 86400 * 7); 7 local default_ttl = module:get_option_number("invite_expiry", 86400 * 7);
8 8
9 local token_storage; 9 local token_storage;
10 if prosody.process_type == "prosody" or prosody.shutdown then 10 if prosody.process_type == "prosody" or prosody.shutdown then
11 token_storage = module:open_store("invite_token", "map"); 11 token_storage = module:open_store("invite_token", "map");
12 end 12 end
17 path = jid, 17 path = jid,
18 query = action..";preauth="..token..(params and (";"..params) or ""), 18 query = action..";preauth="..token..(params and (";"..params) or ""),
19 }); 19 });
20 end 20 end
21 21
22 local function create_invite(invite_action, invite_jid, allow_registration, additional_data) 22 local function create_invite(invite_action, invite_jid, allow_registration, additional_data, ttl, reusable)
23 local token = id.medium(); 23 local token = id.medium();
24 24
25 local created_at = os.time(); 25 local created_at = os.time();
26 local expires = created_at + invite_ttl; 26 local expires = created_at + (ttl or default_ttl);
27 27
28 local invite_params = (invite_action == "roster" and allow_registration) and "ibr=y" or nil; 28 local invite_params = (invite_action == "roster" and allow_registration) and "ibr=y" or nil;
29 29
30 local invite = { 30 local invite = {
31 type = invite_action; 31 type = invite_action;
37 37
38 uri = get_uri(invite_action, invite_jid, token, invite_params); 38 uri = get_uri(invite_action, invite_jid, token, invite_params);
39 39
40 created_at = created_at; 40 created_at = created_at;
41 expires = expires; 41 expires = expires;
42
43 reusable = reusable;
42 }; 44 };
43 45
44 module:fire_event("invite-created", invite); 46 module:fire_event("invite-created", invite);
45 47
46 if allow_registration then 48 if allow_registration then
115 117
116 local valid_invite_methods = {}; 118 local valid_invite_methods = {};
117 local valid_invite_mt = { __index = valid_invite_methods }; 119 local valid_invite_mt = { __index = valid_invite_methods };
118 120
119 function valid_invite_methods:use() 121 function valid_invite_methods:use()
122 if self.reusable then
123 return true;
124 end
125
120 if self.username then 126 if self.username then
121 -- Also remove the contact invite if present, on the 127 -- Also remove the contact invite if present, on the
122 -- assumption that they now have a mutual subscription 128 -- assumption that they now have a mutual subscription
123 token_storage:set(self.username, self.token, nil); 129 token_storage:set(self.username, self.token, nil);
124 end 130 end
168 username = username; 174 username = username;
169 inviter = inviter; 175 inviter = inviter;
170 type = token_info and token_info.type or "roster"; 176 type = token_info and token_info.type or "roster";
171 uri = token_info and token_info.uri or get_uri("roster", username.."@"..module.host, token); 177 uri = token_info and token_info.uri or get_uri("roster", username.."@"..module.host, token);
172 additional_data = token_info and token_info.additional_data or nil; 178 additional_data = token_info and token_info.additional_data or nil;
179 reusable = token_info.reusable;
173 }, valid_invite_mt); 180 }, valid_invite_mt);
174 end 181 end
175 182
176 function use(token) --luacheck: ignore 131/use 183 function use(token) --luacheck: ignore 131/use
177 local invite = get(token); 184 local invite = get(token);