DUMMY.pm 6.4 KB

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