Software /
code /
prosody-modules
Changeset
5639:1664bd4c274b
mod_invites_api: refactor to keep consistent error message.
author | Trần H. Trung <xmpp:trần.h.trung@trung.fun> |
---|---|
date | Mon, 31 Jul 2023 16:41:48 +0700 |
parents | 5638:4379e0e84ee5 |
children | 5640:5e7e609fae0c |
files | mod_invites_api/mod_invites_api.lua |
diffstat | 1 files changed, 48 insertions(+), 45 deletions(-) [+] |
line wrap: on
line diff
--- a/mod_invites_api/mod_invites_api.lua Mon Jul 31 13:15:54 2023 +0700 +++ b/mod_invites_api/mod_invites_api.lua Mon Jul 31 16:41:48 2023 +0700 @@ -329,10 +329,27 @@ api_key_store = module:open_store("invite_api_keys", "map"); api_key_store_kv = module:open_store("invite_api_keys"); + local found = false; + local function error_found(username, name) + if name then + print("Error: Could not find "..name.." in "..domain); + print(""); + print("To make this API key, run:"); + print("> prosodyctl "..module.name.." create "..domain.." "..name); + return 1; + end + if username then + print("Error: Could not find "..username.."@"..domain); + print(""); + print("To make this API key, run:"); + print("> prosodyctl "..module.name.." create "..username.."@"..domain); + return 1; + end + end + if command == "create" then local util_id = require "util.id".short(); local util_token = require "util.id".long(); - local found = false; local os_time = os.time(); if username then if usermanager.user_exists(username, module.host) then @@ -370,7 +387,6 @@ print("Error: key for host needs a `NAME`."); return; end - found = false; get_value(function (id, info) if domain == info.jid and arg_name == info.name then date = datetime.datetime(info.created_at); @@ -400,7 +416,6 @@ local util_token = require "util.id".long(); local os_time = os.time(); if username then - local found; if usermanager.user_exists(username, module.host) then get_value(function (id, info) if username.."@"..module.host == info.jid then @@ -410,13 +425,13 @@ end end); if not found then - print("Error: Could not find the key for "..username.."@"..domain); + error_found(username); return 1; end print("Error: not allow to rename user's account."); return 1; else - print("Error: "..username.."@"..domain.." does not exists."); + print("Error: "..username.."@"..domain.." doesn't exists!"); return 1; end elseif domain then @@ -430,8 +445,6 @@ print("Error: need a `NEW_NAME` to replace "..arg_name_orig); return 1; end - - found = false; get_value(function (id, info) if domain == info.jid and arg_name_orig == info.name then api_key_store:set(nil, id, { @@ -449,11 +462,7 @@ end end); if not found then - date = datetime.datetime(os_time); - print("Error: Could not find "..arg_name_orig.." in "..domain); - print(""); - print("To make this API key, run:"); - print("> prosodyctl "..module.name.." create "..domain.." "..arg_name_orig); + error_found(nil, arg_name_orig); return 1; end return; @@ -462,7 +471,6 @@ local util_token = require "util.id".long(); local os_time = os.time(); if username then - local found; if usermanager.user_exists(username, module.host) then get_value(function (id, info) if username.."@"..module.host == info.jid then @@ -481,10 +489,7 @@ end end); if not found then - print("Error: Could not find the key for "..username.."@"..domain); - print(""); - print("To make this API key, run:"); - print("> prosodyctl "..module.name.." create "..username.."@"..domain); + error_found(username); return 1; end return; @@ -498,7 +503,6 @@ print("Error: key for host needs a `NAME`."); return 1; end - found = false; get_value(function (id, info) if domain == info.jid and arg_name == info.name then api_key_store:set(nil, id, { @@ -516,17 +520,13 @@ end end); if not found then - print("Error: Could not find "..arg_name.." in "..domain); - print(""); - print("To make this API key, run:"); - print("> prosodyctl "..module.name.." create "..domain.." "..arg_name); + error_found(nil, arg_name); return 1; end return; end elseif command == "get" then local name = table.remove(arg, 1); - local found = false; if name and not username then get_value(function (id, info) if info.name == name then @@ -535,11 +535,7 @@ end end); if found == false then - print("Error: could not find "..name.." in "..domain); - print(""); - print("You can create it with:"); - print(""); - print("> prosodyctl "..module.name.." create "..domain.." "..name); + error_found(nil, name); end return; elseif username then @@ -551,11 +547,7 @@ end end); if found == false then - print("Error: could not find the key for "..username.."@"..domain); - print(""); - print("You can create it with:"); - print(""); - print("> prosodyctl "..module.name.." create "..username.."@"..domain); + error_found(username); end return; else @@ -601,16 +593,26 @@ get_value(function (id, info) if info.name == name then print(id); + found = true; end end); + if not found then + error_found(nil, name); + return 1; + end return; elseif username then get_value(function (id, info) local j = jid.prepped_split(info.jid); if j == username then print(id); + found = true end end); + if not found then + error_found(username); + return 1; + end return; else get_value(function (id, info) @@ -628,16 +630,26 @@ get_value(function (id, info) if info.name == name then print(id.."/"..info.token); + found = true; end end); + if not found then + error_found(nil, name); + return 1; + end return; elseif username then get_value(function (id, info) local j = jid.prepped_split(info.jid); if j == username then print(id.."/"..info.token); + found = true; end end); + if not found then + error_found(username); + return 1; + end return; else get_value(function (id, info) @@ -650,7 +662,6 @@ return; end elseif command == "url" then - found = false; local name = table.remove(arg, 1); if name and not username then get_value(function (id, info) @@ -660,11 +671,7 @@ end end); if not found then - date = datetime.datetime(os_time); - print("Error: Could not find "..name.." in "..domain); - print(""); - print("To make this API key, run:"); - print("> prosodyctl "..module.name.." create "..domain.." "..name); + error_found(nil, name); return 1; end return; @@ -677,11 +684,7 @@ end end); if not found then - date = datetime.datetime(os_time); - print("Error: Could not find "..username.."@"..domain); - print(""); - print("To make this API key, run:"); - print("> prosodyctl "..module.name.." create "..username.."@"..domain); + error_found(username); return 1; end return; @@ -726,7 +729,7 @@ end local arg_id = table.remove(arg, 1); if not api_key_store:get(nil, arg_id) then - print("Error: key not found"); + print("Error: key not found!"); return 1; else get_value(function (id, info)