DataModule.pm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. package Trog::DataModule;
  2. use strict;
  3. use warnings;
  4. use UUID::Tiny;
  5. use List::Util;
  6. use File::Copy;
  7. use Mojo::File;
  8. use Plack::MIME;
  9. use Path::Tiny();
  10. no warnings 'experimental';
  11. use feature qw{signatures};
  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. =head1 POST STRUCTURE
  15. Posts generally need to have the following:
  16. data: Brief description of content, or the content itself.
  17. content_type: What this content actually is. Used to filter into the appropriate pages.
  18. href: Primary link. This is the subject of a news post, or a link to the item itself. Can be local or remote.
  19. local_href: Backup link. Automatically created link to a static cache of the content.
  20. title: Title of the content. Used as link name for the 'href' attribute.
  21. user: User was banned for this post
  22. id: Internal identifier in datastore for the post.
  23. tags: array ref of appropriate tags.
  24. created: timestamp of creation of this version of the post
  25. version: revision # of this post.
  26. =head1 CONSTRUCTOR
  27. =head2 new(Config::Simple $config)
  28. Try not to do expensive things here.
  29. =cut
  30. sub new ( $class, $config ) {
  31. $config = $config->vars();
  32. return bless( $config, $class );
  33. }
  34. #It is required that subclasses implement this
  35. sub lang ($self) { ... }
  36. sub help ($self) { ... }
  37. sub read ( $self, $query = {} ) { ... }
  38. sub write ($self) { ... }
  39. sub count ($self) { ... }
  40. sub tags ($self) { ... }
  41. =head1 METHODS
  42. =head2 get(%request)
  43. Queries the data model. Should return the following:
  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. version => if id is passed, return the provided post version rather than the most recent one
  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. author => filter by post author
  52. If it is more efficient to filter within your data storage engine, you probably should override this method.
  53. As implemented, this takes the data as a given and filters in post.
  54. =cut
  55. sub get ( $self, %request ) {
  56. my $posts = $self->read( \%request );
  57. return @$posts if $request{raw};
  58. my @filtered = $self->filter( \%request, @$posts );
  59. @filtered = $self->_fixup(@filtered);
  60. @filtered = $self->paginate( \%request, @filtered );
  61. return @filtered;
  62. }
  63. sub _fixup ( $self, @filtered ) {
  64. # urlencode spaces in filenames
  65. @filtered = map {
  66. my $subj = $_;
  67. foreach my $param (qw{href preview video_href audio_href local_href wallpaper}) {
  68. next unless exists $subj->{$param};
  69. $subj->{$param} =~ s/ /%20/g;
  70. }
  71. #XXX Add dynamic routing data for posts which don't have them (/posts/$id) and (/users/$user)
  72. my $is_user_page = List::Util::any { $_ eq 'about' } @{ $subj->{tags} };
  73. if ( !exists $subj->{local_href} ) {
  74. $subj->{local_href} = "/posts/$subj->{id}";
  75. $subj->{local_href} = "/users/$subj->{user}" if $is_user_page;
  76. }
  77. if ( !exists $subj->{callback} ) {
  78. $subj->{callback} = "Trog::Routes::HTML::posts";
  79. $subj->{callback} = "Trog::Routes::HTML::users" if $is_user_page;
  80. }
  81. $subj->{method} = 'GET' unless exists( $subj->{method} );
  82. $subj
  83. } @filtered;
  84. return @filtered;
  85. }
  86. sub filter ( $self, $query, @filtered ) {
  87. $query->{acls} //= [];
  88. $query->{tags} //= [];
  89. $query->{exclude_tags} //= [];
  90. # If an ID is passed, just get that (and all it's prior versions)
  91. if ( $query->{id} ) {
  92. @filtered = grep { $_->{id} eq $query->{id} } @filtered;
  93. @filtered = _dedup_versions( $query->{version}, @filtered );
  94. return @filtered;
  95. }
  96. # XXX aclname and id are essentially serving the same purpose, should unify
  97. if ( $query->{aclname} ) {
  98. @filtered = grep { ( $_->{aclname} || '' ) eq $query->{aclname} } @filtered;
  99. @filtered = _dedup_versions( $query->{version}, @filtered );
  100. return @filtered;
  101. }
  102. @filtered = _dedup_versions( undef, @filtered );
  103. #Filter out posts which are too old
  104. #Coerce older into numeric
  105. if ($query->{older}) {
  106. $query->{older} =~ s/[^0-9]//g;
  107. @filtered = grep { $_->{created} < $query->{older} } @filtered;
  108. }
  109. if ($query->{newer}) {
  110. $query->{newer} =~ s/[^0-9]//g;
  111. @filtered = grep { $_->{created} > $query->{newer} } @filtered;
  112. }
  113. # Filter posts not matching the passed tag(s), if any
  114. @filtered = grep {
  115. my $tags = $_->{tags};
  116. grep {
  117. my $t = $_;
  118. grep { $t eq $_ } @{ $query->{tags} }
  119. } @$tags
  120. } @filtered if @{ $query->{tags} };
  121. # Filter posts *matching* the passed exclude_tag(s), if any
  122. @filtered = grep {
  123. my $tags = $_->{tags};
  124. !grep {
  125. my $t = $_;
  126. grep { $t eq $_ } @{ $query->{exclude_tags} }
  127. } @$tags
  128. } @filtered if @{ $query->{exclude_tags} };
  129. # Filter posts without the proper ACLs
  130. @filtered = grep {
  131. my $tags = $_->{tags};
  132. grep {
  133. my $t = $_;
  134. grep { $t eq $_ } @{ $query->{acls} }
  135. } @$tags
  136. } @filtered unless grep { $_ eq 'admin' } @{ $query->{acls} };
  137. @filtered = grep { $_->{title} =~ m/\Q$query->{like}\E/i || $_->{data} =~ m/\Q$query->{like}\E/i } @filtered if $query->{like};
  138. @filtered = grep { $_->{user} eq $query->{author} } @filtered if $query->{author};
  139. return @filtered;
  140. }
  141. sub paginate ( $self, $query, @filtered ) {
  142. my $offset = int( $query->{limit} // 25 );
  143. $offset = @filtered < $offset ? @filtered : $offset;
  144. @filtered = splice( @filtered, ( int( $query->{page} ) - 1 ) * $offset, $offset ) if $query->{page} && $query->{limit};
  145. return @filtered;
  146. }
  147. sub _dedup_versions ( $version = -1, @posts ) {
  148. #ASSUMPTION made here - if we pass version this is direct ID query
  149. if ( defined $version ) {
  150. my $version_max = List::Util::max( map { $_->{version} } @posts );
  151. return map {
  152. $_->{version_max} //= $version_max;
  153. $_
  154. } grep { $_->{version} eq $version } @posts;
  155. }
  156. my @uniqids = List::Util::uniq( map { $_->{id} } @posts );
  157. my %posts_deduped;
  158. for my $id (@uniqids) {
  159. my @ofid = sort { $b->{version} <=> $a->{version} } grep { $_->{id} eq $id } @posts;
  160. my $version_max = List::Util::max( map { $_->{version} } @ofid );
  161. $posts_deduped{$id} = $ofid[0];
  162. $posts_deduped{$id}{version_max} = $version_max;
  163. # Show orig creation date, and original author.
  164. # XXX this doesn't show the mtime correctly for whatever reason, so I'm omitting it from the interface
  165. $posts_deduped{$id}{modified} = $ofid[0]{created};
  166. $posts_deduped{$id}{created} = $ofid[-1]{created};
  167. $posts_deduped{$id}{author} = $ofid[-1]{author};
  168. }
  169. my @deduped = @posts_deduped{@uniqids};
  170. return @deduped;
  171. }
  172. =head2 count() = INT $num
  173. Returns the total number of posts.
  174. Used to determine paginator parameters.
  175. =cut
  176. =head2 add(@posts) = BOOL $failed_or_not
  177. Add the provided posts to the datastore.
  178. If any post already exists with the same id, a new post with a version higher than it will be added.
  179. Passes an array of new posts to add to the data store module's write() function.
  180. You probably won't want to override this.
  181. =cut
  182. sub add ( $self, @posts ) {
  183. my @to_write;
  184. foreach my $post (@posts) {
  185. $post->{id} //= UUID::Tiny::create_uuid_as_string( UUID::Tiny::UUID_V1, UUID::Tiny::UUID_NS_DNS );
  186. $post->{aliases} //= [];
  187. $post->{aliases} = [ $post->{aliases} ] unless ref $post->{aliases} eq 'ARRAY';
  188. if ( $post->{aclname} ) {
  189. # Then this is a series
  190. $post->{local_href} //= "/$post->{aclname}";
  191. push( @{ $post->{aliases} }, "/posts/$post->{id}", "/series/$post->{id}" );
  192. }
  193. $post->{callback} //= 'Trog::Routes::HTML::posts';
  194. # If this is a user creation post, add in the /user/ route
  195. if ( $post->{callback} eq 'Trog::Routes::HTML::users' ) {
  196. $post->{local_href} = "/users/$post->{user}";
  197. }
  198. $post->{local_href} //= "/posts/$post->{id}";
  199. $post->{method} //= 'GET';
  200. $post->{created} = time();
  201. my @existing_posts = $self->get( id => $post->{id} );
  202. if (@existing_posts) {
  203. my $existing_post = $existing_posts[0];
  204. $post->{version} = $existing_post->{version};
  205. $post->{version}++;
  206. }
  207. $post->{version} //= 0;
  208. $post = _process($post);
  209. push @to_write, $post;
  210. }
  211. $self->write( \@to_write );
  212. #hup the parent to refresh the routing table IFF we aren't in an interactive session, such as migrate.pl
  213. if ( !$ENV{NOHUP} ) {
  214. my $parent = getppid;
  215. kill 'HUP', $parent;
  216. }
  217. # Gorilla cache invalidation
  218. Path::Tiny::path('www/statics')->remove_tree;
  219. return 0;
  220. }
  221. #XXX this level of post-processing seems gross, but may be unavoidable
  222. # Not actually a subprocess, kek
  223. sub _process ($post) {
  224. $post->{href} = _handle_upload( $post->{file}, $post->{id} ) if $post->{file};
  225. $post->{preview} = _handle_upload( $post->{preview_file}, $post->{id} ) if $post->{preview_file};
  226. $post->{wallpaper} = _handle_upload( $post->{wallpaper_file}, $post->{id} ) if $post->{wallpaper_file};
  227. $post->{preview} = $post->{href} if $post->{app} && $post->{app} eq 'image';
  228. delete $post->{app};
  229. delete $post->{file};
  230. delete $post->{preview_file};
  231. delete $post->{wallpaper_file};
  232. delete $post->{scheme};
  233. delete $post->{route};
  234. delete $post->{domain};
  235. # Handle acls/tags
  236. $post->{tags} //= [];
  237. $post->{acls} //= [];
  238. @{ $post->{tags} } = grep {
  239. my $subj = $_;
  240. !grep { $_ eq $subj } qw{public private unlisted}
  241. } @{ $post->{tags} };
  242. push( @{ $post->{tags} }, @{ $post->{acls} } ) if $post->{visibility} eq 'private';
  243. delete $post->{acls};
  244. push( @{ $post->{tags} }, $post->{visibility} );
  245. # Add the 'series' tag if we are in a series, restrict to relevant acl
  246. if ( $post->{series} ) {
  247. push( @{ $post->{tags} }, 'series' );
  248. push( @{ $post->{tags} }, $post->{series} );
  249. }
  250. #Filter adding the same acl twice
  251. @{ $post->{tags} } = List::Util::uniq( @{ $post->{tags} } );
  252. @{ $post->{aliases} } = List::Util::uniq( @{ $post->{aliases} } );
  253. # Handle multimedia content types
  254. if ( $post->{href} ) {
  255. my $mf = Mojo::File->new("www/$post->{href}");
  256. my $ext = '.' . $mf->extname();
  257. $post->{content_type} = Plack::MIME->mime_type($ext) if $ext;
  258. }
  259. if ( $post->{video_href} ) {
  260. my $mf = Mojo::File->new("www/$post->{video_href}");
  261. my $ext = '.' . $mf->extname();
  262. $post->{video_content_type} = Plack::MIME->mime_type($ext) if $ext;
  263. }
  264. if ( $post->{audio_href} ) {
  265. my $mf = Mojo::File->new("www/$post->{audio_href}");
  266. my $ext = '.' . $mf->extname();
  267. $post->{audio_content_type} = Plack::MIME->mime_type($ext) if $ext;
  268. }
  269. $post->{content_type} ||= 'text/html';
  270. $post->{is_video} = 1 if $post->{content_type} =~ m/^video\//;
  271. $post->{is_audio} = 1 if $post->{content_type} =~ m/^audio\//;
  272. $post->{is_image} = 1 if $post->{content_type} =~ m/^image\//;
  273. $post->{is_profile} = 1 if grep { $_ eq 'about' } @{ $post->{tags} };
  274. return $post;
  275. }
  276. sub _handle_upload ( $file, $uuid ) {
  277. my $f = $file->{tempname};
  278. my $newname = "$uuid.$file->{filename}";
  279. File::Copy::move( $f, "www/assets/$newname" );
  280. return "/assets/$newname";
  281. }
  282. =head2 delete(@posts)
  283. Delete the following posts.
  284. Will remove all versions of said post.
  285. You should override this, it is a stub here.
  286. =cut
  287. sub delete ($self) { die 'stub' }
  288. =head2 routes() = HASH
  289. Returns the routes to each post.
  290. You should override this for performance reasons, as it's just a wrapper around get() by defualt.
  291. =cut
  292. sub routes ($self) {
  293. my %routes = map { $_->{local_href} => { method => $_->{method}, callback => \&{ $_->{callback} } } } ( $self->get( limit => 0, acls => ['admin'] ) );
  294. return %routes;
  295. }
  296. =head2 aliases() = HASH
  297. Returns the aliases for each post, indexed by aliases.
  298. You should override this for performance reasons, as it's just a wrapper around get() by defualt.
  299. =cut
  300. sub aliases ($self) {
  301. my @posts = $self->get( limit => 0, acls => ['admin'] );
  302. my %aliases;
  303. foreach my $post (@posts) {
  304. @aliases{ @{ $post->{aliases} } } = $post->{local_href};
  305. }
  306. return %aliases;
  307. }
  308. 1;