DUMMY.pm 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. package Trog::Data::DUMMY;
  2. use strict;
  3. use warnings;
  4. no warnings 'experimental';
  5. use feature qw{signatures};
  6. =head1 QUERY FORMAT
  7. The $query_language and $query_help variables are presented to the user as to how to use the search box in the tCMS header.
  8. =cut
  9. our $query_language = 'Perl Regex in Quotemeta';
  10. our $query_help = 'https://perldoc.perl.org/functions/quotemeta.html';
  11. =head1 POST STRUCTURE
  12. Posts generally need to have the following:
  13. data: Brief description of content, or the content itself.
  14. content_type: What this content actually is. Used to filter into the appropriate pages.
  15. href: Primary link. This is the subject of a news post, or a link to the item itself. Can be local or remote.
  16. local_href: Backup link. Automatically created link to a static cache of the content.
  17. title: Title of the content. Used as link name for the 'href' attribute.
  18. user: User was banned for this post
  19. id: Internal identifier in datastore for the post.
  20. tags: array ref of appropriate tags.
  21. created: timestamp of creation of this version of the post
  22. version: revision # of this post.
  23. =cut
  24. my $example_posts = [
  25. {
  26. content_type => "text/html",
  27. data => "Here, caveman",
  28. href => '/',
  29. local_href => "/assets/today.html",
  30. title => 'Example Post',
  31. user => 'Nobody',
  32. id => 665,
  33. tags => ['news', 'public'],
  34. created => time(),
  35. version => 0,
  36. },
  37. {
  38. content_type => "text/html",
  39. data => "Is amazing",
  40. href => '/',
  41. local_href => "/assets/blog/Muh Blog.html",
  42. title => 'Muh Blog',
  43. user => 'Nobody',
  44. id => 666,
  45. tags => ['blog', 'public'],
  46. created => time(),
  47. version => 0,
  48. },
  49. {
  50. content_type => "text/html",
  51. data => "Vote for Nobody, nobody really cares!",
  52. href => '/',
  53. local_href => "/assets/about/Nobody.html",
  54. title => 'Nobody',
  55. user => 'Nobody',
  56. id => 669,
  57. tags => ['about', 'profile', 'public'],
  58. created => time(),
  59. version => 0,
  60. },
  61. {
  62. content_type => "image/gif",
  63. data => "Default avatar for new users",
  64. href => "/img/avatar/humm.gif",
  65. local_href => "/img/avatar/humm.gif",
  66. title => "humm.gif",
  67. user => 'Nobody',
  68. id => 420,
  69. tags => ['image', 'files', 'profile-image', 'public'],
  70. created => time(),
  71. version => 0,
  72. preview => '/img/avatar/humm.gif',
  73. },
  74. {
  75. content_type => "image/jpeg",
  76. data => "Test Pattern",
  77. href => "/img/sys/testpattern.jpg",
  78. local_href => "/img/sys/testpattern.jpg",
  79. title => "testpattern.jpg",
  80. user => 'Nobody',
  81. id => 90210,
  82. tags => ['image', 'files', 'public'],
  83. created => time(),
  84. version => 0,
  85. preview => '/img/sys/testpattern.jpg',
  86. },
  87. {
  88. content_type => "audio/mpeg",
  89. data => "Test recording for tCMS",
  90. href => "/assets/audio/test.mp3",
  91. local_href => "/assets/audio/test.mp3",
  92. title => "test.mp3",
  93. user => "Nobody",
  94. id => 111,
  95. tags => ["audio", "files", 'public'],
  96. created => time(),
  97. version => 0,
  98. preview => '/img/sys/testpattern.jpg',
  99. },
  100. {
  101. content_type => "video/ogg",
  102. data => "Test video for tCMS",
  103. href => "/assets/video/test.ogv",
  104. local_href => "/assets/video/test.ogv",
  105. title => "test.ogv",
  106. user => "Nobody",
  107. id => "222",
  108. tags => ["video", "files", 'public'],
  109. created => time(),
  110. version => 0,
  111. preview => '/img/sys/testpattern.jpg',
  112. },
  113. {
  114. content_type => 'text/plain',
  115. data => "Admin ACL",
  116. href => "/config",
  117. local_href => '/config',
  118. title => 'Administrative Posts',
  119. user => 'Nobody',
  120. id => "900",
  121. tags => ['series', 'private'],
  122. created => time(),
  123. version => 0,
  124. preview => '/img/sys/testpattern.jpg',
  125. },
  126. {
  127. content_type => "image/svg",
  128. data => "tCMS Logo",
  129. href => "/img/icon/tCMS.svg",
  130. local_href => "/img/icon/tCMS.svg",
  131. title => "tCMS.svg",
  132. user => 'Nobody',
  133. id => 90211,
  134. tags => ['image', 'files', 'admin'],
  135. created => time(),
  136. version => 0,
  137. preview => '/img/icon/tCMS.svg',
  138. },
  139. ];
  140. =head1 CONSTRUCTOR
  141. =head2 new(Config::Simple $config)
  142. Try not to do expensive things here.
  143. =cut
  144. sub new ($class, $config) {
  145. $config = $config->vars();
  146. $config->{lang} = $query_language;
  147. $config->{help} = $query_help;
  148. return bless($config,__PACKAGE__);
  149. }
  150. =head1 METHODS
  151. =head2 get(%request)
  152. Queries the data model in the way a "real" data model module ought to.
  153. 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).
  154. tags => ARRAYREF of tags, any one of which is required to give a result. If none are passed, no filtering is performed.
  155. 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.
  156. page => Offset multiplier for pagination.
  157. limit => Offset for pagination.
  158. like => Search query, as might be passed in the search bar.
  159. =cut
  160. # These have to be sorted as requested by the client
  161. sub get ($self, %request) {
  162. my @filtered = @$example_posts;
  163. # If an ID is passed, just get that
  164. @filtered = grep { $_->{id} eq $request{id} } @filtered if $request{id};
  165. # First, paginate
  166. my $offset = int($request{limit});
  167. $offset = @filtered < $offset ? @filtered : $offset;
  168. @filtered = splice(@filtered, ( int($request{page}) -1) * $offset, $offset) if $request{page} && $request{limit};
  169. # Next, handle the query, tags and ACLs
  170. @filtered = grep { my $tags = $_->{tags}; grep { my $t = $_; grep {$t eq $_ } @{$request{tags}} } @$tags } @filtered if @{$request{tags}};
  171. @filtered = grep { my $tags = $_->{tags}; grep { my $t = $_; grep {$t eq $_ } @{$request{acls}} } @$tags } @filtered unless grep { $_ eq 'admin' } @{$request{acls}};
  172. @filtered = grep { $_->{data} =~ m/\Q$request{like}\E/i } @filtered if $request{like};
  173. # Next, go ahead and build the "post type"
  174. @filtered = _add_post_type(@filtered);
  175. # Next, add the type of post this is
  176. @filtered = _add_media_type(@filtered);
  177. return \@filtered;
  178. }
  179. sub total_posts {
  180. return scalar(@$example_posts);
  181. }
  182. sub _add_post_type (@posts) {
  183. return map {
  184. my $post = $_;
  185. my $type = 'file';
  186. $type = 'blog' if grep { $_ eq 'blog' } @{$post->{tags}};
  187. $type = 'microblog' if grep { $_ eq 'news' } @{$post->{tags}};
  188. $type = 'profile' if grep { $_ eq 'profile' } @{$post->{tags}};
  189. $type = 'series' if grep { $_ eq 'series' } @{$post->{tags}};
  190. $post->{type} = $type;
  191. $post
  192. } @posts;
  193. }
  194. sub _add_media_type (@posts) {
  195. return map {
  196. my $post = $_;
  197. $post->{is_video} = 1 if $post->{content_type} =~ m/^video\//;
  198. $post->{is_audio} = 1 if $post->{content_type} =~ m/^audio\//;
  199. $post->{is_image} = 1 if $post->{content_type} =~ m/^image\//;
  200. $post
  201. } @posts;
  202. }
  203. sub add ($self, @posts) {
  204. return 1;
  205. }
  206. sub update($self, @posts) {
  207. return 1;
  208. }
  209. sub delete($self, @ids) {
  210. return 1;
  211. }
  212. 1;