logo

ap-client

CLI-based client / toolbox for ActivityPub Client-to-Servergit clone https://hacktivis.me/git/ap-client.git

ActivityPubClient.pm (1741B)


  1. #!/usr/bin/env perl
  2. # AP-Client: CLI-based client / toolbox for ActivityPub
  3. # Copyright © 2020-2023 AP-Client Authors <https://hacktivis.me/git/ap-client/>
  4. # SPDX-License-Identifier: BSD-3-Clause
  5. package App::ActivityPubClient;
  6. our $VERSION = 'v0.1.4';
  7. use strict;
  8. use utf8;
  9. use open ":std", ":encoding(UTF-8)";
  10. use Scalar::Util qw(reftype);
  11. use Exporter 'import';
  12. our @EXPORT_OK = qw(print_object);
  13. sub print_object_key {
  14. my ($indent, $object, $key) = @_;
  15. if ($object->{$key}) {
  16. print_ref($indent, $object->{$key}, $key);
  17. }
  18. }
  19. sub print_object {
  20. my ($indent, $object) = @_;
  21. my @regular_keys =
  22. qw{url subtitleLanguage context inbox outbox prev next published updated summary content bcc bto cc to object attachment tag orderedItems mediaType};
  23. printf "%*s %s", $indent, '⇒', $object->{"type"};
  24. printf ' id:<%s>', $object->{"id"} if $object->{"id"};
  25. printf ' href:<%s>', $object->{"href"} if $object->{"href"};
  26. printf ' “%s”', $object->{"name"} if $object->{"name"};
  27. printf ' @%s', $object->{"preferredUsername"}
  28. if $object->{"preferredUsername"};
  29. printf ' ⚠' if ($object->{"sensitive"} eq JSON->true);
  30. foreach (@regular_keys) {
  31. print_object_key($indent, $object, $_);
  32. }
  33. }
  34. sub print_ref {
  35. my ($indent, $object, $name) = @_;
  36. my $ref_type = reftype($object);
  37. if ($ref_type eq 'HASH') {
  38. printf "\n%*s%s: \n", $indent, ' ', $name;
  39. print_object($indent + 4, $object);
  40. } elsif ($ref_type eq 'ARRAY') {
  41. printf "\n%*s%s: ", $indent, ' ', $name if @{$object};
  42. foreach (@{$object}) {
  43. if (reftype($_) eq 'HASH') {
  44. print "\n";
  45. print_object($indent + 4, $_);
  46. } else {
  47. printf "%s ; ", $_;
  48. }
  49. }
  50. } else {
  51. printf "\n%*s%s: %s", $indent, ' ', $name, $object;
  52. }
  53. }
  54. 1;