JSON.pm 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package Trog::Routes::JSON;
  2. use strict;
  3. use warnings;
  4. no warnings 'experimental';
  5. use feature qw{signatures state};
  6. use Clone qw{clone};
  7. use JSON::MaybeXS();
  8. use Scalar::Util();
  9. use Trog::Utils();
  10. use Trog::Config();
  11. use Trog::Auth();
  12. use Trog::Routes::HTML();
  13. use Trog::Log::Metrics();
  14. our %routes = (
  15. '/api/catalog' => {
  16. method => 'GET',
  17. callback => \&catalog,
  18. parameters => {},
  19. },
  20. '/api/webmanifest' => {
  21. method => 'GET',
  22. callback => \&webmanifest,
  23. parameters => {},
  24. },
  25. '/api/version' => {
  26. method => 'GET',
  27. callback => \&version,
  28. parameters => {},
  29. },
  30. '/api/auth_change_request/(.*)' => {
  31. method => 'GET',
  32. callback => \&process_auth_change_request,
  33. captures => ['token'],
  34. parameters => {
  35. token => sub { my $tok = shift; $tok =~ m/[a-f|0-9|-]+/; },
  36. },
  37. noindex => 1,
  38. robot_name => '/api/auth_change_request/*',
  39. },
  40. '/api/posts_of_form' => {
  41. method => 'GET',
  42. auth => 1,
  43. parameters => {
  44. # Same shape series.json demands of child_form.
  45. form => sub { $_[0] =~ m/^[A-Za-z0-9_-]+\.tx$/ },
  46. },
  47. callback => \&posts_of_form,
  48. noindex => 1,
  49. },
  50. '/api/requests_per' => {
  51. method => 'GET',
  52. auth => 1,
  53. parameters => {
  54. period => sub {
  55. grep {
  56. my $valid = $_;
  57. List::Util::any { $_ eq $valid } @_
  58. } qw{second minute hour day week month year};
  59. },
  60. num_periods => \&Scalar::Util::looks_like_number,
  61. before => \&Scalar::Util::looks_like_number,
  62. code => \&Scalar::Util::looks_like_number,
  63. },
  64. callback => \&requests_per,
  65. },
  66. );
  67. # Clone / redact for catalog
  68. my $cloned = clone( \%routes );
  69. foreach my $r ( keys(%$cloned) ) {
  70. delete $cloned->{$r}{callback};
  71. }
  72. my $enc = JSON::MaybeXS->new( utf8 => 1 );
  73. # Note to authors, don't forget to update this
  74. sub _version () {
  75. return '1.0';
  76. }
  77. # Special case of a non data-structure JSON return
  78. sub version ($query) {
  79. state $ret = [ 200, [ 'Content-type' => "application/json", ETag => 'version-' . _version() ], [ _version() ] ];
  80. return $ret;
  81. }
  82. sub catalog ($query) {
  83. return _render( 200, { ETag => 'catalog-' . _version() }, %$cloned );
  84. }
  85. sub webmanifest ($query) {
  86. state $headers = { ETag => 'manifest-' . _version() };
  87. my $theme_dir = Trog::Themes::get_dir();
  88. state %manifest = (
  89. "icons" => [
  90. { "src" => "$theme_dir/img/icon/favicon-32.png", "type" => "image/png", "sizes" => "32x32" },
  91. { "src" => "$theme_dir/img/icon/favicon-48.png", "type" => "image/png", "sizes" => "48x48" },
  92. { "src" => "$theme_dir/img/icon/favicon-167.png", "type" => "image/png", "sizes" => "167x167" },
  93. { "src" => "$theme_dir/img/icon/favicon-180.png", "type" => "image/png", "sizes" => "180x180" },
  94. { "src" => "$theme_dir/img/icon/favicon-192.png", "type" => "image/png", "sizes" => "192x192" },
  95. { "src" => "$theme_dir/img/icon/favicon-512.png", "type" => "image/png", "sizes" => "512x512" },
  96. ],
  97. );
  98. return _render( 200, $headers, %manifest );
  99. }
  100. sub process_auth_change_request ($query) {
  101. my $token = $query->{token};
  102. my $msg = Trog::Auth::process_change_request($token);
  103. return $query->{tpsgi}->forbidden($query) unless $msg;
  104. return _render(
  105. 200, undef,
  106. message => $msg,
  107. result => 'success',
  108. );
  109. }
  110. =head2 posts_of_form
  111. Implements GET /api/posts_of_form. Requires a login.
  112. The id and title of every post of the named type which the caller is allowed to
  113. see. This is what fills a relation field's dropdown in the post editor -- see
  114. www/scripts/post_relations.js and the x-tcms-relations tables in the post type
  115. sidecars.
  116. Only id and title: the caller is populating a picker, and a post's body is
  117. nobody's business here.
  118. =cut
  119. sub posts_of_form ($query) {
  120. my @posts = Trog::Routes::HTML::_post_helper( { form => $query->{form}, limit => 0 }, [], $query->{user_acls} );
  121. return _render(
  122. 200, undef,
  123. posts => [ map { { id => $_->{id}, title => $_->{title} } } @posts ],
  124. );
  125. }
  126. sub requests_per ($query) {
  127. my $code = Trog::Utils::coerce_array( $query->{code} );
  128. return _render(
  129. 200, undef,
  130. %{ Trog::Log::Metrics::requests_per( $query->{period}, $query->{num_periods}, $query->{before}, @$code ) }
  131. );
  132. }
  133. sub _render ( $code, $headers, %data ) {
  134. return Trog::Renderer->render(
  135. code => 200,
  136. data => \%data,
  137. template => 'bogus.tx',
  138. contenttype => 'application/json',
  139. headers => $headers,
  140. # TODO it may be appropriate to cache some of these methods.
  141. nocache => 1,
  142. );
  143. }
  144. 1;