Comparison

mod_invites_api/mod_invites_api.lua @ 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
parent 5638:4379e0e84ee5
comparison
equal deleted inserted replaced
5638:4379e0e84ee5 5639:1664bd4c274b
327 module.host = domain; --luacheck: ignore 122/module 327 module.host = domain; --luacheck: ignore 122/module
328 usermanager.initialize_host(module.host); 328 usermanager.initialize_host(module.host);
329 api_key_store = module:open_store("invite_api_keys", "map"); 329 api_key_store = module:open_store("invite_api_keys", "map");
330 api_key_store_kv = module:open_store("invite_api_keys"); 330 api_key_store_kv = module:open_store("invite_api_keys");
331 331
332 local found = false;
333 local function error_found(username, name)
334 if name then
335 print("Error: Could not find "..name.." in "..domain);
336 print("");
337 print("To make this API key, run:");
338 print("> prosodyctl "..module.name.." create "..domain.." "..name);
339 return 1;
340 end
341 if username then
342 print("Error: Could not find "..username.."@"..domain);
343 print("");
344 print("To make this API key, run:");
345 print("> prosodyctl "..module.name.." create "..username.."@"..domain);
346 return 1;
347 end
348 end
349
332 if command == "create" then 350 if command == "create" then
333 local util_id = require "util.id".short(); 351 local util_id = require "util.id".short();
334 local util_token = require "util.id".long(); 352 local util_token = require "util.id".long();
335 local found = false;
336 local os_time = os.time(); 353 local os_time = os.time();
337 if username then 354 if username then
338 if usermanager.user_exists(username, module.host) then 355 if usermanager.user_exists(username, module.host) then
339 get_value(function (id, info) 356 get_value(function (id, info)
340 if username.."@"..module.host == info.jid then 357 if username.."@"..module.host == info.jid then
368 local arg_name = table.remove(arg, 1); 385 local arg_name = table.remove(arg, 1);
369 if not arg_name or arg_name == "" then 386 if not arg_name or arg_name == "" then
370 print("Error: key for host needs a `NAME`."); 387 print("Error: key for host needs a `NAME`.");
371 return; 388 return;
372 end 389 end
373 found = false;
374 get_value(function (id, info) 390 get_value(function (id, info)
375 if domain == info.jid and arg_name == info.name then 391 if domain == info.jid and arg_name == info.name then
376 date = datetime.datetime(info.created_at); 392 date = datetime.datetime(info.created_at);
377 print("Found:"); 393 print("Found:");
378 print(date, id, info.jid, info.name); 394 print(date, id, info.jid, info.name);
398 end 414 end
399 elseif command == "rename" then 415 elseif command == "rename" then
400 local util_token = require "util.id".long(); 416 local util_token = require "util.id".long();
401 local os_time = os.time(); 417 local os_time = os.time();
402 if username then 418 if username then
403 local found;
404 if usermanager.user_exists(username, module.host) then 419 if usermanager.user_exists(username, module.host) then
405 get_value(function (id, info) 420 get_value(function (id, info)
406 if username.."@"..module.host == info.jid then 421 if username.."@"..module.host == info.jid then
407 print("Found:"); 422 print("Found:");
408 print(date, id.."/"..util_token, info.jid); 423 print(date, id.."/"..util_token, info.jid);
409 found = true; 424 found = true;
410 end 425 end
411 end); 426 end);
412 if not found then 427 if not found then
413 print("Error: Could not find the key for "..username.."@"..domain); 428 error_found(username);
414 return 1; 429 return 1;
415 end 430 end
416 print("Error: not allow to rename user's account."); 431 print("Error: not allow to rename user's account.");
417 return 1; 432 return 1;
418 else 433 else
419 print("Error: "..username.."@"..domain.." does not exists."); 434 print("Error: "..username.."@"..domain.." doesn't exists!");
420 return 1; 435 return 1;
421 end 436 end
422 elseif domain then 437 elseif domain then
423 local arg_name_orig = table.remove(arg, 1); 438 local arg_name_orig = table.remove(arg, 1);
424 if not arg_name_orig or arg_name_orig == "" then 439 if not arg_name_orig or arg_name_orig == "" then
428 local arg_name_new = table.remove(arg, 1); 443 local arg_name_new = table.remove(arg, 1);
429 if not arg_name_new or arg_name_new == "" then 444 if not arg_name_new or arg_name_new == "" then
430 print("Error: need a `NEW_NAME` to replace "..arg_name_orig); 445 print("Error: need a `NEW_NAME` to replace "..arg_name_orig);
431 return 1; 446 return 1;
432 end 447 end
433
434 found = false;
435 get_value(function (id, info) 448 get_value(function (id, info)
436 if domain == info.jid and arg_name_orig == info.name then 449 if domain == info.jid and arg_name_orig == info.name then
437 api_key_store:set(nil, id, { 450 api_key_store:set(nil, id, {
438 id = id; 451 id = id;
439 token = info.token; 452 token = info.token;
447 print(date, id.."/"..info.token, info.jid, arg_name_new); 460 print(date, id.."/"..info.token, info.jid, arg_name_new);
448 found = true; 461 found = true;
449 end 462 end
450 end); 463 end);
451 if not found then 464 if not found then
452 date = datetime.datetime(os_time); 465 error_found(nil, arg_name_orig);
453 print("Error: Could not find "..arg_name_orig.." in "..domain);
454 print("");
455 print("To make this API key, run:");
456 print("> prosodyctl "..module.name.." create "..domain.." "..arg_name_orig);
457 return 1; 466 return 1;
458 end 467 end
459 return; 468 return;
460 end 469 end
461 elseif command == "renew" then 470 elseif command == "renew" then
462 local util_token = require "util.id".long(); 471 local util_token = require "util.id".long();
463 local os_time = os.time(); 472 local os_time = os.time();
464 if username then 473 if username then
465 local found;
466 if usermanager.user_exists(username, module.host) then 474 if usermanager.user_exists(username, module.host) then
467 get_value(function (id, info) 475 get_value(function (id, info)
468 if username.."@"..module.host == info.jid then 476 if username.."@"..module.host == info.jid then
469 api_key_store:set(nil, id, { 477 api_key_store:set(nil, id, {
470 id = id; 478 id = id;
479 print("Re-newed:"); 487 print("Re-newed:");
480 print(date, id.."/"..util_token, info.jid); 488 print(date, id.."/"..util_token, info.jid);
481 end 489 end
482 end); 490 end);
483 if not found then 491 if not found then
484 print("Error: Could not find the key for "..username.."@"..domain); 492 error_found(username);
485 print("");
486 print("To make this API key, run:");
487 print("> prosodyctl "..module.name.." create "..username.."@"..domain);
488 return 1; 493 return 1;
489 end 494 end
490 return; 495 return;
491 else 496 else
492 print("Error: "..username.."@"..domain.." does not exists."); 497 print("Error: "..username.."@"..domain.." does not exists.");
496 local arg_name = table.remove(arg, 1); 501 local arg_name = table.remove(arg, 1);
497 if not arg_name or arg_name == "" then 502 if not arg_name or arg_name == "" then
498 print("Error: key for host needs a `NAME`."); 503 print("Error: key for host needs a `NAME`.");
499 return 1; 504 return 1;
500 end 505 end
501 found = false;
502 get_value(function (id, info) 506 get_value(function (id, info)
503 if domain == info.jid and arg_name == info.name then 507 if domain == info.jid and arg_name == info.name then
504 api_key_store:set(nil, id, { 508 api_key_store:set(nil, id, {
505 id = id; 509 id = id;
506 token = util_token; 510 token = util_token;
514 print(date, id.."/"..util_token, info.jid, info.name); 518 print(date, id.."/"..util_token, info.jid, info.name);
515 found = true; 519 found = true;
516 end 520 end
517 end); 521 end);
518 if not found then 522 if not found then
519 print("Error: Could not find "..arg_name.." in "..domain); 523 error_found(nil, arg_name);
520 print("");
521 print("To make this API key, run:");
522 print("> prosodyctl "..module.name.." create "..domain.." "..arg_name);
523 return 1; 524 return 1;
524 end 525 end
525 return; 526 return;
526 end 527 end
527 elseif command == "get" then 528 elseif command == "get" then
528 local name = table.remove(arg, 1); 529 local name = table.remove(arg, 1);
529 local found = false;
530 if name and not username then 530 if name and not username then
531 get_value(function (id, info) 531 get_value(function (id, info)
532 if info.name == name then 532 if info.name == name then
533 print(date, id, get_url(id, info.token)); 533 print(date, id, get_url(id, info.token));
534 found = true; 534 found = true;
535 end 535 end
536 end); 536 end);
537 if found == false then 537 if found == false then
538 print("Error: could not find "..name.." in "..domain); 538 error_found(nil, name);
539 print("");
540 print("You can create it with:");
541 print("");
542 print("> prosodyctl "..module.name.." create "..domain.." "..name);
543 end 539 end
544 return; 540 return;
545 elseif username then 541 elseif username then
546 get_value(function (id, info) 542 get_value(function (id, info)
547 local j = jid.prepped_split(info.jid); 543 local j = jid.prepped_split(info.jid);
549 print(date, id, get_url(id, info.token)); 545 print(date, id, get_url(id, info.token));
550 found = true; 546 found = true;
551 end 547 end
552 end); 548 end);
553 if found == false then 549 if found == false then
554 print("Error: could not find the key for "..username.."@"..domain); 550 error_found(username);
555 print("");
556 print("You can create it with:");
557 print("");
558 print("> prosodyctl "..module.name.." create "..username.."@"..domain);
559 end 551 end
560 return; 552 return;
561 else 553 else
562 get_value(function (id, info) 554 get_value(function (id, info)
563 if info.jid == module.host then 555 if info.jid == module.host then
599 local name = table.remove(arg, 1); 591 local name = table.remove(arg, 1);
600 if name and not username then 592 if name and not username then
601 get_value(function (id, info) 593 get_value(function (id, info)
602 if info.name == name then 594 if info.name == name then
603 print(id); 595 print(id);
604 end 596 found = true;
605 end); 597 end
598 end);
599 if not found then
600 error_found(nil, name);
601 return 1;
602 end
606 return; 603 return;
607 elseif username then 604 elseif username then
608 get_value(function (id, info) 605 get_value(function (id, info)
609 local j = jid.prepped_split(info.jid); 606 local j = jid.prepped_split(info.jid);
610 if j == username then 607 if j == username then
611 print(id); 608 print(id);
612 end 609 found = true
613 end); 610 end
611 end);
612 if not found then
613 error_found(username);
614 return 1;
615 end
614 return; 616 return;
615 else 617 else
616 get_value(function (id, info) 618 get_value(function (id, info)
617 if info.jid == module.host then 619 if info.jid == module.host then
618 print(id, info.jid, info.name or "<unknown>"); 620 print(id, info.jid, info.name or "<unknown>");
626 local name = table.remove(arg, 1); 628 local name = table.remove(arg, 1);
627 if name and not username then 629 if name and not username then
628 get_value(function (id, info) 630 get_value(function (id, info)
629 if info.name == name then 631 if info.name == name then
630 print(id.."/"..info.token); 632 print(id.."/"..info.token);
631 end 633 found = true;
632 end); 634 end
635 end);
636 if not found then
637 error_found(nil, name);
638 return 1;
639 end
633 return; 640 return;
634 elseif username then 641 elseif username then
635 get_value(function (id, info) 642 get_value(function (id, info)
636 local j = jid.prepped_split(info.jid); 643 local j = jid.prepped_split(info.jid);
637 if j == username then 644 if j == username then
638 print(id.."/"..info.token); 645 print(id.."/"..info.token);
639 end 646 found = true;
640 end); 647 end
648 end);
649 if not found then
650 error_found(username);
651 return 1;
652 end
641 return; 653 return;
642 else 654 else
643 get_value(function (id, info) 655 get_value(function (id, info)
644 if info.jid == module.host then 656 if info.jid == module.host then
645 print(id.."/"..info.token, info.jid, info.name or "<unknown>"); 657 print(id.."/"..info.token, info.jid, info.name or "<unknown>");
648 end 660 end
649 end); 661 end);
650 return; 662 return;
651 end 663 end
652 elseif command == "url" then 664 elseif command == "url" then
653 found = false;
654 local name = table.remove(arg, 1); 665 local name = table.remove(arg, 1);
655 if name and not username then 666 if name and not username then
656 get_value(function (id, info) 667 get_value(function (id, info)
657 if info.name == name then 668 if info.name == name then
658 print(get_url(id, info.token)); 669 print(get_url(id, info.token));
659 found = true; 670 found = true;
660 end 671 end
661 end); 672 end);
662 if not found then 673 if not found then
663 date = datetime.datetime(os_time); 674 error_found(nil, name);
664 print("Error: Could not find "..name.." in "..domain);
665 print("");
666 print("To make this API key, run:");
667 print("> prosodyctl "..module.name.." create "..domain.." "..name);
668 return 1; 675 return 1;
669 end 676 end
670 return; 677 return;
671 elseif username then 678 elseif username then
672 get_value(function (id, info) 679 get_value(function (id, info)
675 print(get_url(id, info.token)); 682 print(get_url(id, info.token));
676 found = true; 683 found = true;
677 end 684 end
678 end); 685 end);
679 if not found then 686 if not found then
680 date = datetime.datetime(os_time); 687 error_found(username);
681 print("Error: Could not find "..username.."@"..domain);
682 print("");
683 print("To make this API key, run:");
684 print("> prosodyctl "..module.name.." create "..username.."@"..domain);
685 return 1; 688 return 1;
686 end 689 end
687 return; 690 return;
688 else 691 else
689 get_value(function (id, info) 692 get_value(function (id, info)
724 print("Error: Input must be a valid host - cannot be a user's account."); 727 print("Error: Input must be a valid host - cannot be a user's account.");
725 return 1; 728 return 1;
726 end 729 end
727 local arg_id = table.remove(arg, 1); 730 local arg_id = table.remove(arg, 1);
728 if not api_key_store:get(nil, arg_id) then 731 if not api_key_store:get(nil, arg_id) then
729 print("Error: key not found"); 732 print("Error: key not found!");
730 return 1; 733 return 1;
731 else 734 else
732 get_value(function (id, info) 735 get_value(function (id, info)
733 if arg_id == id then 736 if arg_id == id then
734 api_key_store:set(nil, id, nil); 737 api_key_store:set(nil, id, nil);