DUMMY.pm 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 => 'admin',
  119. user => 'Nobody',
  120. id => "900",
  121. tags => ['acl'],
  122. created => time(),
  123. version => 0,
  124. preview => '/img/sys/testpattern.jpg',
  125. }
  126. ];
  127. =head1 CONSTRUCTOR
  128. =head2 new(Config::Simple $config)
  129. Try not to do expensive things here.
  130. =cut
  131. sub new ($class, $config) {
  132. $config = $config->vars();
  133. $config->{lang} = $query_language;
  134. $config->{help} = $query_help;
  135. return bless($config,__PACKAGE__);
  136. }
  137. =head1 METHODS
  138. =cut
  139. # These have to be sorted as requested by the client
  140. sub get ($self, %request) {
  141. my @filtered = @$example_posts;
  142. # If an ID is passed, just get that
  143. @filtered = grep { $_->{id} eq $request{id} } @filtered if $request{id};
  144. # First, paginate
  145. my $offset = int($request{limit});
  146. $offset = @filtered < $offset ? @filtered : $offset;
  147. @filtered = splice(@filtered, ( int($request{page}) -1) * $offset, $offset) if $request{page} && $request{limit};
  148. # Next, handle the query
  149. @filtered = grep { my $tags = $_->{tags}; grep { my $t = $_; grep {$t eq $_ } @{$request{tags}} } @$tags } @filtered if @{$request{tags}};
  150. @filtered = grep { $_->{data} =~ m/\Q$request{like}\E/i } @filtered if $request{like};
  151. # Next, go ahead and build the "post type"
  152. @filtered = _add_post_type(@filtered);
  153. # Next, add the type of post this is
  154. @filtered = _add_media_type(@filtered);
  155. return \@filtered;
  156. }
  157. sub total_posts {
  158. return scalar(@$example_posts);
  159. }
  160. sub _add_post_type (@posts) {
  161. return map {
  162. my $post = $_;
  163. my $type = 'file';
  164. $type = 'blog' if grep { $_ eq 'blog' } @{$post->{tags}};
  165. $type = 'microblog' if grep { $_ eq 'news' } @{$post->{tags}};
  166. $type = 'profile' if grep { $_ eq 'profile' } @{$post->{tags}};
  167. $post->{type} = $type;
  168. $post
  169. } @posts;
  170. }
  171. sub _add_media_type (@posts) {
  172. return map {
  173. my $post = $_;
  174. $post->{is_video} = 1 if $post->{content_type} =~ m/^video\//;
  175. $post->{is_audio} = 1 if $post->{content_type} =~ m/^audio\//;
  176. $post->{is_image} = 1 if $post->{content_type} =~ m/^image\//;
  177. $post
  178. } @posts;
  179. }
  180. sub add ($self, @posts) {
  181. return 1;
  182. }
  183. sub update($self, @posts) {
  184. return 1;
  185. }
  186. sub delete($self, @ids) {
  187. return 1;
  188. }
  189. 1;