DUMMY.pm 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. package Trog::Data::DUMMY;
  2. use strict;
  3. use warnings;
  4. no warnings 'experimental';
  5. use feature qw{signatures};
  6. use Carp qw{confess};
  7. use JSON::MaybeXS;
  8. use File::Slurper;
  9. use File::Copy;
  10. use Mojo::File;
  11. =head1 WARNING
  12. Do not use this as a production data model. It is *not* safe to race conditions, and is only here for testing.
  13. =head1 QUERY FORMAT
  14. The $query_language and $query_help variables are presented to the user as to how to use the search box in the tCMS header.
  15. =cut
  16. our $datastore = 'data/DUMMY.json';
  17. our $query_language = 'Perl Regex in Quotemeta';
  18. our $query_help = 'https://perldoc.perl.org/functions/quotemeta.html';
  19. =head1 POST STRUCTURE
  20. Posts generally need to have the following:
  21. data: Brief description of content, or the content itself.
  22. content_type: What this content actually is. Used to filter into the appropriate pages.
  23. href: Primary link. This is the subject of a news post, or a link to the item itself. Can be local or remote.
  24. local_href: Backup link. Automatically created link to a static cache of the content.
  25. title: Title of the content. Used as link name for the 'href' attribute.
  26. user: User was banned for this post
  27. id: Internal identifier in datastore for the post.
  28. tags: array ref of appropriate tags.
  29. created: timestamp of creation of this version of the post
  30. version: revision # of this post.
  31. =cut
  32. =head1 CONSTRUCTOR
  33. =head2 new(Config::Simple $config)
  34. Try not to do expensive things here.
  35. =cut
  36. sub new ($class, $config) {
  37. $config = $config->vars();
  38. $config->{lang} = $query_language;
  39. $config->{help} = $query_help;
  40. return bless($config,__PACKAGE__);
  41. }
  42. =head1 METHODS
  43. =head2 get(%request)
  44. Queries the data model in the way a "real" data model module ought to.
  45. id => Filter down to just the post by ID. May be subsequently filtered by ACL, resulting in a 404 (which is good, as it does not disclose info).
  46. tags => ARRAYREF of tags, any one of which is required to give a result. If none are passed, no filtering is performed.
  47. acls => ARRAYREF of acl tags, any one of which is required to give result. Filter applies after tags. 'admin' ACL being present skips this filter.
  48. page => Offset multiplier for pagination.
  49. limit => Offset for pagination.
  50. like => Search query, as might be passed in the search bar.
  51. =cut
  52. sub _read {
  53. confess "Can't find datastore!" unless -f $datastore;
  54. my $slurped = File::Slurper::read_text($datastore);
  55. return JSON::MaybeXS::decode_json($slurped);
  56. }
  57. sub _write($data) {
  58. open(my $fh, '>', $datastore) or confess;
  59. print $fh JSON::MaybeXS::encode_json($data);
  60. close $fh;
  61. }
  62. # These have to be sorted as requested by the client
  63. sub get ($self, %request) {
  64. my $example_posts = _read();
  65. $request{acls} //= [];
  66. $request{tags} //=[];
  67. my @filtered = @$example_posts;
  68. # If an ID is passed, just get that
  69. @filtered = grep { $_->{id} eq $request{id} } @filtered if $request{id};
  70. # Next, handle the query, tags and ACLs
  71. @filtered = grep { my $tags = $_->{tags}; grep { my $t = $_; grep {$t eq $_ } @{$request{tags}} } @$tags } @filtered if @{$request{tags}};
  72. @filtered = grep { my $tags = $_->{tags}; grep { my $t = $_; grep {$t eq $_ } @{$request{acls}} } @$tags } @filtered unless grep { $_ eq 'admin' } @{$request{acls}};
  73. @filtered = grep { $_->{data} =~ m/\Q$request{like}\E/i } @filtered if $request{like};
  74. # Finally, paginate
  75. my $offset = int($request{limit} // 25);
  76. $offset = @filtered < $offset ? @filtered : $offset;
  77. my $pages = int(scalar(@filtered) / ($offset || 1) );
  78. @filtered = splice(@filtered, ( int($request{page}) -1) * $offset, $offset) if $request{page} && $request{limit};
  79. # Next, go ahead and build the "post type"
  80. @filtered = _add_post_type(@filtered);
  81. # Next, add the type of post this is
  82. @filtered = _add_media_type(@filtered);
  83. # Finally, add visibility
  84. @filtered = _add_visibility(@filtered);
  85. return ($pages,\@filtered);
  86. }
  87. sub total_posts {
  88. my $example_posts = _read();
  89. return scalar(@$example_posts);
  90. }
  91. sub _add_post_type (@posts) {
  92. return map {
  93. my $post = $_;
  94. my $type = 'file';
  95. $type = 'blog' if grep { $_ eq 'blog' } @{$post->{tags}};
  96. $type = 'microblog' if grep { $_ eq 'news' } @{$post->{tags}};
  97. $type = 'profile' if grep { $_ eq 'profile' } @{$post->{tags}};
  98. $type = 'series' if grep { $_ eq 'series' } @{$post->{tags}};
  99. $post->{type} = $type;
  100. $post
  101. } @posts;
  102. }
  103. sub _add_media_type (@posts) {
  104. return map {
  105. my $post = $_;
  106. $post->{content_type} //= '';
  107. $post->{is_video} = 1 if $post->{content_type} =~ m/^video\//;
  108. $post->{is_audio} = 1 if $post->{content_type} =~ m/^audio\//;
  109. $post->{is_image} = 1 if $post->{content_type} =~ m/^image\//;
  110. $post
  111. } @posts;
  112. }
  113. sub _add_visibility (@posts) {
  114. return map {
  115. my $post = $_;
  116. my @visibilities = grep { my $tag = $_; grep { $_ eq $tag } qw{private unlisted public} } @{$post->{tags}};
  117. $post->{visibility} = $visibilities[0];
  118. $post
  119. } @posts;
  120. }
  121. sub add ($self, @posts) {
  122. require UUID::Tiny;
  123. my $example_posts = _read();
  124. foreach my $post (@posts) {
  125. $post->{id} //= UUID::Tiny::create_uuid_as_string(UUID::Tiny::UUID_V1, UUID::Tiny::UUID_NS_DNS);
  126. my (undef, $existing_posts) = $self->get( id => $post->{id} );
  127. if (@$existing_posts) {
  128. my $existing_post = $existing_posts->[0];
  129. $post->{version} = $existing_post->{version};
  130. $post->{version}++;
  131. }
  132. $post->{version} //= 0;
  133. #TODO set local_href and content type correctly
  134. $post = _process($post);
  135. push @$example_posts, $post;
  136. }
  137. _write($example_posts);
  138. return 0;
  139. }
  140. # Not actually a subprocess, kek
  141. sub _process ($post) {
  142. $post->{href} = _handle_upload($post->{file}, $post->{id}) if $post->{file};
  143. $post->{preview} = _handle_upload($post->{preview_file}, $post->{id}) if $post->{preview_file};
  144. $post->{preview} = $post->{href} if $post->{app} eq 'image';
  145. delete $post->{app};
  146. delete $post->{file};
  147. delete $post->{preview_file};
  148. delete $post->{route};
  149. delete $post->{domain};
  150. $post->{tags} //= [];
  151. push(@{$post->{tags}}, delete $post->{acls}) if $post->{visibility} eq 'private';
  152. push(@{$post->{tags}}, delete $post->{visibility});
  153. if ($post->{href}) {
  154. my $mf = Mojo::File->new("www/$post->{href}");
  155. my $ext = '.'.$mf->extname();
  156. $post->{content_type} = Plack::MIME->mime_type($ext) if $ext;
  157. }
  158. if ($post->{video_href}) {
  159. my $mf = Mojo::File->new("www/$post->{video_href}");
  160. my $ext = '.'.$mf->extname();
  161. $post->{video_content_type} = Plack::MIME->mime_type($ext) if $ext;
  162. }
  163. if ($post->{audio_href}) {
  164. my $mf = Mojo::File->new("www/$post->{audio_href}");
  165. my $ext = '.'.$mf->extname();
  166. $post->{audio_content_type} = Plack::MIME->mime_type($ext) if $ext;
  167. }
  168. return $post;
  169. }
  170. sub _handle_upload ($file, $uuid) {
  171. my $f = $file->{tempname};
  172. my $newname = "$uuid.$file->{filename}";
  173. File::Copy::move($f, "www/assets/$newname");
  174. return "/assets/$newname";
  175. }
  176. sub delete($self, @posts) {
  177. my $example_posts = _read();
  178. foreach my $update (@posts) {
  179. @$example_posts = grep { $_->{id} ne $update->{id} } @$example_posts;
  180. }
  181. _write($example_posts);
  182. return 0;
  183. }
  184. 1;