logo

ap-client

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

ap-represent.pl (1777B)


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