Comparison

mod_invites/mod_invites.lua @ 4341:a104440c20a4

mod_invites: Add internal API to list, read and delete account invites
author Matthew Wild <mwild1@gmail.com>
date Sun, 17 Jan 2021 12:47:47 +0000
parent 4096:24f4eb35ab60
child 4342:84e60c3d6e61
comparison
equal deleted inserted replaced
4340:7cd3b7ec59e9 4341:a104440c20a4
1 local id = require "util.id"; 1 local id = require "util.id";
2 local it = require "util.iterators";
2 local url = require "socket.url"; 3 local url = require "socket.url";
3 local jid_node = require "util.jid".node; 4 local jid_node = require "util.jid".node;
4 local jid_split = require "util.jid".split; 5 local jid_split = require "util.jid".split;
5 6
6 local invite_ttl = module:get_option_number("invite_expiry", 86400 * 7); 7 local invite_ttl = module:get_option_number("invite_expiry", 86400 * 7);
76 -- Create invitation to become a contact of a local user 77 -- Create invitation to become a contact of a local user
77 function create_contact(username, allow_registration, additional_data) --luacheck: ignore 131/create_contact 78 function create_contact(username, allow_registration, additional_data) --luacheck: ignore 131/create_contact
78 return create_invite("roster", username.."@"..module.host, allow_registration, additional_data); 79 return create_invite("roster", username.."@"..module.host, allow_registration, additional_data);
79 end 80 end
80 81
82 -- Iterates pending (non-expired, unused) invites that allow registration
83 function pending_account_invites() --luacheck: ignore 131/pending_account_invites
84 local store = module:open_store("invite_token");
85 local now = os.time();
86 local function is_valid_invite(_, invite)
87 return invite.expires > now;
88 end
89 return it.filter(is_valid_invite, pairs(store:get(nil) or {}));
90 end
91
92 function get_account_invite_info(token)
93 if not token then
94 return nil, "no-token";
95 end
96
97 -- Fetch from host store (account invite)
98 local token_info = token_storage:get(nil, token);
99 if not token_info then
100 return nil, "token-invalid";
101 elseif os.time() > token_info.expires then
102 return nil, "token-expired";
103 end
104
105 return token_info;
106 end
107
108 function delete_account_invite(token)
109 if not token then
110 return nil, "no-token";
111 end
112
113 return token_storage:set(nil, token, nil);
114 end
81 115
82 local valid_invite_methods = {}; 116 local valid_invite_methods = {};
83 local valid_invite_mt = { __index = valid_invite_methods }; 117 local valid_invite_mt = { __index = valid_invite_methods };
84 118
85 function valid_invite_methods:use() 119 function valid_invite_methods:use()