DataModule.pm 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. package Trog::DataModule;
  2. use strict;
  3. use warnings;
  4. use List::Util;
  5. use File::Copy;
  6. use Mojo::File;
  7. no warnings 'experimental';
  8. use feature qw{signatures};
  9. =head1 QUERY FORMAT
  10. The $query_language and $query_help variables are presented to the user as to how to use the search box in the tCMS header.
  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. =head1 CONSTRUCTOR
  24. =head2 new(Config::Simple $config)
  25. Try not to do expensive things here.
  26. =cut
  27. sub new ($class, $config) {
  28. $config = $config->vars();
  29. return bless($config, $class);
  30. }
  31. #It is required that subclasses implement this
  32. sub lang ($self) { ... }
  33. sub help ($self) { ... }
  34. sub read ($self,$query={}) { ... }
  35. sub write ($self) { ... }
  36. sub count ($self) { ... }
  37. =head1 METHODS
  38. =head2 get(%request)
  39. Queries the data model. Should return the following:
  40. 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).
  41. version => if id is passed, return the provided post version rather than the most recent one
  42. tags => ARRAYREF of tags, any one of which is required to give a result. If none are passed, no filtering is performed.
  43. 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.
  44. page => Offset multiplier for pagination.
  45. limit => Offset for pagination.
  46. like => Search query, as might be passed in the search bar.
  47. author => filter by post author
  48. If it is more efficient to filter within your data storage engine, you probably should override this method.
  49. As implemented, this takes the data as a given and filters in post.
  50. =cut
  51. sub get ($self, %request) {
  52. my $posts = $self->read(\%request);
  53. my @filtered = $self->filter(\%request, @$posts);
  54. @filtered = $self->_fixup(@filtered);
  55. @filtered = $self->paginate(\%request,@filtered);
  56. return @filtered;
  57. }
  58. sub _fixup ($self, @filtered) {
  59. @filtered = _add_post_type(@filtered);
  60. # Next, add the type of post this is
  61. @filtered = _add_media_type(@filtered);
  62. # Finally, add visibility
  63. @filtered = _add_visibility(@filtered);
  64. return @filtered;
  65. }
  66. sub filter ($self, $query, @filtered) {
  67. my %request = %$query; #XXX update varnames instead
  68. $request{acls} //= [];
  69. $request{tags} //=[];
  70. # If an ID is passed, just get that (and all it's prior versions)
  71. if ($request{id}) {
  72. @filtered = grep { $_->{id} eq $request{id} } @filtered if $request{id};
  73. @filtered = _dedup_versions($request{version}, @filtered);
  74. return @filtered;
  75. }
  76. @filtered = _dedup_versions(undef, @filtered);
  77. #Filter out posts which are too old
  78. @filtered = grep { $_->{created} < $request{older} } @filtered if $request{older};
  79. #XXX Heal bad data -- probably not needed
  80. @filtered = map { my $t = $_->{tags}; @$t = grep { defined $_ } @$t; $_ } @filtered;
  81. # Next, handle the query, tags and ACLs
  82. @filtered = grep { my $tags = $_->{tags}; grep { my $t = $_; grep {$t eq $_ } @{$request{tags}} } @$tags } @filtered if @{$request{tags}};
  83. @filtered = grep { my $tags = $_->{tags}; grep { my $t = $_; grep {$t eq $_ } @{$request{acls}} } @$tags } @filtered unless grep { $_ eq 'admin' } @{$request{acls}};
  84. @filtered = grep { $_->{title} =~ m/\Q$request{like}\E/i || $_->{data} =~ m/\Q$request{like}\E/i } @filtered if $request{like};
  85. @filtered = grep { $_->{user} eq $request{author} } @filtered if $request{author};
  86. return @filtered;
  87. }
  88. sub paginate ($self, $query, @filtered) {
  89. my %request = %$query; #XXX change varnames
  90. my $offset = int($request{limit} // 25);
  91. $offset = @filtered < $offset ? @filtered : $offset;
  92. @filtered = splice(@filtered, ( int($request{page}) -1) * $offset, $offset) if $request{page} && $request{limit};
  93. return @filtered;
  94. }
  95. sub _dedup_versions ($version=-1, @posts) {
  96. #ASSUMPTION made here - if we pass version this is direct ID query
  97. if (defined $version) {
  98. my $version_max = List::Util::max(map { $_->{version} } @posts);
  99. return map {
  100. $_->{version_max} //= $version_max;
  101. $_
  102. } grep { $_->{version} eq $version } @posts;
  103. }
  104. my @uniqids = List::Util::uniq(map { $_->{id} } @posts);
  105. my %posts_deduped;
  106. for my $id (@uniqids) {
  107. my @ofid = sort { $b->{version} cmp $a->{version} } grep { $_->{id} eq $id } @posts;
  108. my $version_max = List::Util::max(map { $_->{version } } @ofid);
  109. $posts_deduped{$id} = $ofid[0];
  110. $posts_deduped{$id}{version_max} = $version_max;
  111. }
  112. my @deduped = @posts_deduped{@uniqids};
  113. return @deduped;
  114. }
  115. #XXX this probably should be re-factored to be baked into the data from the get-go
  116. sub _add_post_type (@posts) {
  117. return map {
  118. my $post = $_;
  119. my $type = 'file';
  120. $type = 'blog' if grep { $_ eq 'blog' } @{$post->{tags}};
  121. $type = 'microblog' if grep { $_ eq 'news' } @{$post->{tags}};
  122. $type = 'profile' if grep { $_ eq 'about' } @{$post->{tags}};
  123. $type = 'series' if grep { $_ eq 'series' } @{$post->{tags}};
  124. $post->{type} = $type;
  125. $post
  126. } @posts;
  127. }
  128. sub _add_media_type (@posts) {
  129. return map {
  130. my $post = $_;
  131. $post->{content_type} //= '';
  132. $post->{is_video} = 1 if $post->{content_type} =~ m/^video\//;
  133. $post->{is_audio} = 1 if $post->{content_type} =~ m/^audio\//;
  134. $post->{is_image} = 1 if $post->{content_type} =~ m/^image\//;
  135. $post->{is_profile} = 1 if grep {$_ eq 'about' } @{$post->{tags}};
  136. $post
  137. } @posts;
  138. }
  139. sub _add_visibility (@posts) {
  140. return map {
  141. my $post = $_;
  142. my @visibilities = grep { my $tag = $_; grep { $_ eq $tag } qw{private unlisted public} } @{$post->{tags}};
  143. $post->{visibility} = $visibilities[0];
  144. $post
  145. } @posts;
  146. }
  147. =head2 count() = INT $num
  148. Returns the total number of posts.
  149. Used to determine paginator parameters.
  150. =cut
  151. =head2 add(@posts) = BOOL $failed_or_not
  152. Add the provided posts to the datastore.
  153. If any post already exists with the same id, a new post with a version higher than it will be added.
  154. Passes an array of new posts to add to the data store module's write() function.
  155. You probably won't want to override this.
  156. =cut
  157. sub add ($self, @posts) {
  158. require UUID::Tiny;
  159. my @to_write;
  160. foreach my $post (@posts) {
  161. $post->{id} //= UUID::Tiny::create_uuid_as_string(UUID::Tiny::UUID_V1, UUID::Tiny::UUID_NS_DNS);
  162. $post->{created} = time();
  163. my @existing_posts = $self->get( id => $post->{id} );
  164. if (@existing_posts) {
  165. my $existing_post = $existing_posts[0];
  166. $post->{version} = $existing_post->{version};
  167. $post->{version}++;
  168. }
  169. $post->{version} //= 0;
  170. $post = _process($post);
  171. push @to_write, $post;
  172. }
  173. $self->write(\@to_write);
  174. return 0;
  175. }
  176. #XXX this level of post-processing seems gross, but may be unavoidable
  177. # Not actually a subprocess, kek
  178. sub _process ($post) {
  179. $post->{href} = _handle_upload($post->{file}, $post->{id}) if $post->{file};
  180. $post->{preview} = _handle_upload($post->{preview_file}, $post->{id}) if $post->{preview_file};
  181. $post->{wallpaper} = _handle_upload($post->{wallpaper_file}, $post->{id}) if $post->{wallpaper_file};
  182. $post->{preview} = $post->{href} if $post->{app} eq 'image';
  183. delete $post->{app};
  184. delete $post->{file};
  185. delete $post->{preview_file};
  186. delete $post->{route};
  187. delete $post->{domain};
  188. # Handle acls/tags
  189. $post->{tags} //= [];
  190. @{$post->{tags}} = grep { my $subj = $_; !grep { $_ eq $subj} qw{public private unlisted} } @{$post->{tags}};
  191. push(@{$post->{tags}}, delete $post->{acls}) if $post->{visibility} eq 'private';
  192. push(@{$post->{tags}}, delete $post->{visibility});
  193. #Filter adding the same acl twice
  194. @{$post->{tags}} = List::Util::uniq(@{$post->{tags}});
  195. # Handle multimedia content types
  196. if ($post->{href}) {
  197. my $mf = Mojo::File->new("www/$post->{href}");
  198. my $ext = '.'.$mf->extname();
  199. $post->{content_type} = Plack::MIME->mime_type($ext) if $ext;
  200. }
  201. if ($post->{video_href}) {
  202. my $mf = Mojo::File->new("www/$post->{video_href}");
  203. my $ext = '.'.$mf->extname();
  204. $post->{video_content_type} = Plack::MIME->mime_type($ext) if $ext;
  205. }
  206. if ($post->{audio_href}) {
  207. my $mf = Mojo::File->new("www/$post->{audio_href}");
  208. my $ext = '.'.$mf->extname();
  209. $post->{audio_content_type} = Plack::MIME->mime_type($ext) if $ext;
  210. }
  211. return $post;
  212. }
  213. sub _handle_upload ($file, $uuid) {
  214. my $f = $file->{tempname};
  215. my $newname = "$uuid.$file->{filename}";
  216. File::Copy::move($f, "www/assets/$newname");
  217. return "/assets/$newname";
  218. }
  219. =head2 delete(@posts)
  220. Delete the following posts.
  221. Will remove all versions of said post.
  222. You should override this, it is a stub here.
  223. =cut
  224. sub delete ($self) { die 'stub' }
  225. 1;