# HG changeset patch # User Matthew Wild # Date 1610887667 0 # Node ID a104440c20a49a37fd1dd4528684d612493238ef # Parent 7cd3b7ec59e921f78a0aeb039608fa60e9891947 mod_invites: Add internal API to list, read and delete account invites diff -r 7cd3b7ec59e9 -r a104440c20a4 mod_invites/mod_invites.lua --- a/mod_invites/mod_invites.lua Sat Jan 16 19:47:22 2021 +0000 +++ b/mod_invites/mod_invites.lua Sun Jan 17 12:47:47 2021 +0000 @@ -1,4 +1,5 @@ local id = require "util.id"; +local it = require "util.iterators"; local url = require "socket.url"; local jid_node = require "util.jid".node; local jid_split = require "util.jid".split; @@ -78,6 +79,39 @@ return create_invite("roster", username.."@"..module.host, allow_registration, additional_data); end +-- Iterates pending (non-expired, unused) invites that allow registration +function pending_account_invites() --luacheck: ignore 131/pending_account_invites + local store = module:open_store("invite_token"); + local now = os.time(); + local function is_valid_invite(_, invite) + return invite.expires > now; + end + return it.filter(is_valid_invite, pairs(store:get(nil) or {})); +end + +function get_account_invite_info(token) + if not token then + return nil, "no-token"; + end + + -- Fetch from host store (account invite) + local token_info = token_storage:get(nil, token); + if not token_info then + return nil, "token-invalid"; + elseif os.time() > token_info.expires then + return nil, "token-expired"; + end + + return token_info; +end + +function delete_account_invite(token) + if not token then + return nil, "no-token"; + end + + return token_storage:set(nil, token, nil); +end local valid_invite_methods = {}; local valid_invite_mt = { __index = valid_invite_methods };