| 1 | use utf8; | ||
| 2 | package Rapi::Blog::DB::Result::Post; | ||
| 3 | |||
| 4 | # Created by DBIx::Class::Schema::Loader | ||
| 5 | # DO NOT MODIFY THE FIRST PART OF THIS FILE | ||
| 6 | |||
| 7 | use strict; | ||
| 8 | use warnings; | ||
| 9 | |||
| 10 | use Moose; | ||
| 11 | use MooseX::NonMoose; | ||
| 12 | use MooseX::MarkAsMethods autoclean => 1; | ||
| 13 | extends 'DBIx::Class::Core'; | ||
| 14 | __PACKAGE__->load_components("InflateColumn::DateTime"); | ||
| 15 | __PACKAGE__->table("post"); | ||
| 16 | __PACKAGE__->add_columns( | ||
| 17 | "id", | ||
| 18 | { data_type => "integer", is_auto_increment => 1, is_nullable => 0 }, | ||
| 19 | "name", | ||
| 20 | { data_type => "varchar", is_nullable => 0, size => 255 }, | ||
| 21 | "title", | ||
| 22 | { | ||
| 23 | data_type => "varchar", | ||
| 24 | default_value => \"null", | ||
| 25 | is_nullable => 1, | ||
| 26 | size => 255, | ||
| 27 | }, | ||
| 28 | "image", | ||
| 29 | { | ||
| 30 | data_type => "varchar", | ||
| 31 | default_value => \"null", | ||
| 32 | is_nullable => 1, | ||
| 33 | size => 255, | ||
| 34 | }, | ||
| 35 | "ts", | ||
| 36 | { data_type => "datetime", is_nullable => 0 }, | ||
| 37 | "create_ts", | ||
| 38 | { data_type => "datetime", is_nullable => 0 }, | ||
| 39 | "update_ts", | ||
| 40 | { data_type => "datetime", is_nullable => 0 }, | ||
| 41 | "author_id", | ||
| 42 | { data_type => "integer", is_foreign_key => 1, is_nullable => 0 }, | ||
| 43 | "creator_id", | ||
| 44 | { data_type => "integer", is_foreign_key => 1, is_nullable => 0 }, | ||
| 45 | "updater_id", | ||
| 46 | { data_type => "integer", is_foreign_key => 1, is_nullable => 0 }, | ||
| 47 | "section_id", | ||
| 48 | { | ||
| 49 | data_type => "integer", | ||
| 50 | default_value => \"null", | ||
| 51 | is_foreign_key => 1, | ||
| 52 | is_nullable => 1, | ||
| 53 | }, | ||
| 54 | "published", | ||
| 55 | { data_type => "boolean", default_value => 0, is_nullable => 0 }, | ||
| 56 | "publish_ts", | ||
| 57 | { data_type => "datetime", default_value => \"null", is_nullable => 1 }, | ||
| 58 | "size", | ||
| 59 | { data_type => "integer", default_value => \"null", is_nullable => 1 }, | ||
| 60 | "tag_names", | ||
| 61 | { data_type => "text", default_value => \"null", is_nullable => 1 }, | ||
| 62 | "custom_summary", | ||
| 63 | { data_type => "text", default_value => \"null", is_nullable => 1 }, | ||
| 64 | "summary", | ||
| 65 | { data_type => "text", default_value => \"null", is_nullable => 1 }, | ||
| 66 | "body", | ||
| 67 | { data_type => "text", default_value => "", is_nullable => 1 }, | ||
| 68 | ); | ||
| 69 | __PACKAGE__->set_primary_key("id"); | ||
| 70 | __PACKAGE__->add_unique_constraint("name_unique", ["name"]); | ||
| 71 | __PACKAGE__->belongs_to( | ||
| 72 | "author", | ||
| 73 | "Rapi::Blog::DB::Result::User", | ||
| 74 | { id => "author_id" }, | ||
| 75 | { is_deferrable => 0, on_delete => "RESTRICT", on_update => "CASCADE" }, | ||
| 76 | ); | ||
| 77 | __PACKAGE__->has_many( | ||
| 78 | "comments", | ||
| 79 | "Rapi::Blog::DB::Result::Comment", | ||
| 80 | { "foreign.post_id" => "self.id" }, | ||
| 81 | { cascade_copy => 0, cascade_delete => 0 }, | ||
| 82 | ); | ||
| 83 | __PACKAGE__->belongs_to( | ||
| 84 | "creator", | ||
| 85 | "Rapi::Blog::DB::Result::User", | ||
| 86 | { id => "creator_id" }, | ||
| 87 | { is_deferrable => 0, on_delete => "RESTRICT", on_update => "CASCADE" }, | ||
| 88 | ); | ||
| 89 | __PACKAGE__->has_many( | ||
| 90 | "hits", | ||
| 91 | "Rapi::Blog::DB::Result::Hit", | ||
| 92 | { "foreign.post_id" => "self.id" }, | ||
| 93 | { cascade_copy => 0, cascade_delete => 0 }, | ||
| 94 | ); | ||
| 95 | __PACKAGE__->has_many( | ||
| 96 | "post_categories", | ||
| 97 | "Rapi::Blog::DB::Result::PostCategory", | ||
| 98 | { "foreign.post_id" => "self.id" }, | ||
| 99 | { cascade_copy => 0, cascade_delete => 0 }, | ||
| 100 | ); | ||
| 101 | __PACKAGE__->has_many( | ||
| 102 | "post_tags", | ||
| 103 | "Rapi::Blog::DB::Result::PostTag", | ||
| 104 | { "foreign.post_id" => "self.id" }, | ||
| 105 | { cascade_copy => 0, cascade_delete => 0 }, | ||
| 106 | ); | ||
| 107 | __PACKAGE__->belongs_to( | ||
| 108 | "section", | ||
| 109 | "Rapi::Blog::DB::Result::Section", | ||
| 110 | { id => "section_id" }, | ||
| 111 | { | ||
| 112 | is_deferrable => 0, | ||
| 113 | join_type => "LEFT", | ||
| 114 | on_delete => "SET DEFAULT", | ||
| 115 | on_update => "CASCADE", | ||
| 116 | }, | ||
| 117 | ); | ||
| 118 | __PACKAGE__->has_many( | ||
| 119 | "trk_section_posts", | ||
| 120 | "Rapi::Blog::DB::Result::TrkSectionPost", | ||
| 121 | { "foreign.post_id" => "self.id" }, | ||
| 122 | { cascade_copy => 0, cascade_delete => 0 }, | ||
| 123 | ); | ||
| 124 | __PACKAGE__->belongs_to( | ||
| 125 | "updater", | ||
| 126 | "Rapi::Blog::DB::Result::User", | ||
| 127 | { id => "updater_id" }, | ||
| 128 | { is_deferrable => 0, on_delete => "RESTRICT", on_update => "CASCADE" }, | ||
| 129 | ); | ||
| 130 | |||
| 131 | |||
| 132 | # Created by DBIx::Class::Schema::Loader v0.07045 @ 2017-10-04 18:42:02 | ||
| 133 | # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:EgoroG32J0nSECxuTHx0yQ | ||
| 134 | |||
| 135 | __PACKAGE__->load_components('+RapidApp::DBIC::Component::TableSpec'); | ||
| 136 | __PACKAGE__->TableSpec_m2m( categories => "post_categories", 'category_name'); | ||
| 137 | __PACKAGE__->apply_TableSpec; | ||
| 138 | |||
| 139 | __PACKAGE__->load_components('+Rapi::Blog::DB::Component::SafeResult'); | ||
| 140 | |||
| 141 | __PACKAGE__->has_many( | ||
| 142 | "direct_comments", | ||
| 143 | "Rapi::Blog::DB::Result::Comment", | ||
| 144 | { "foreign.post_id" => "self.id" }, | ||
| 145 | { cascade_copy => 0, cascade_delete => 0, where => { parent_id => undef } }, | ||
| 146 | ); | ||
| 147 | |||
| 148 | __PACKAGE__->many_to_many( 'tags', 'post_tags', 'tag_name' ); | ||
| 149 | |||
| 150 | use RapidApp::Util ':all'; | ||
| 151 | use Rapi::Blog::Util; | ||
| 152 | use HTML::Strip; | ||
| 153 | |||
| 154 | sub schema { (shift)->result_source->schema } | ||
| 155 | # This relies on us having been loaded via RapidApp::Util::Role::ModelDBIC | ||
| 156 | sub parent_app_class { (shift)->schema->_ra_catalyst_origin_model->app_class } | ||
| 157 | sub Access { (shift)->parent_app_class->template_controller->Access } | ||
| 158 | |||
| 159 | sub public_url_path { | ||
| 160 | my $self = shift; | ||
| 161 | return undef unless $self->Access->default_view_path; | ||
| 162 | $self->{_public_url_path} //= do { | ||
| 163 | my $pfx = ''; | ||
| 164 | if(my $c = RapidApp->active_request_context) { $pfx = $c->mount_url || ''; } | ||
| 165 | my $path = join('',$pfx,'/',$self->Access->default_view_path); | ||
| 166 | $path =~ s/\/?$/\//; # make sure there is a trailing '/'; | ||
| 167 | $path | ||
| 168 | } | ||
| 169 | } | ||
| 170 | |||
| 171 | sub public_url { | ||
| 172 | my $self = shift; | ||
| 173 | my $path = $self->public_url_path or return undef; | ||
| 174 | return join('',$path,$self->name) | ||
| 175 | } | ||
| 176 | |||
| 177 | sub preview_url_path { | ||
| 178 | my $self = shift; | ||
| 179 | return undef unless $self->Access->preview_path; | ||
| 180 | $self->{_preview_url_path} //= do { | ||
| 181 | my $pfx = ''; | ||
| 182 | if(my $c = RapidApp->active_request_context) { $pfx = $c->mount_url || ''; } | ||
| 183 | my $path = join('',$pfx,'/',$self->Access->preview_path); | ||
| 184 | $path =~ s/\/?$/\//; # make sure there is a trailing '/'; | ||
| 185 | $path | ||
| 186 | } | ||
| 187 | } | ||
| 188 | |||
| 189 | sub preview_url { | ||
| 190 | my $self = shift; | ||
| 191 | my $path = $self->preview_url_path or return undef; | ||
| 192 | return join('',$path,$self->name) | ||
| 193 | } | ||
| 194 | |||
| 195 | sub open_url_path { | ||
| 196 | my $self = shift; | ||
| 197 | my $mode = shift; | ||
| 198 | my $app = $self->parent_app_class; | ||
| 199 | my $pfx = ''; | ||
| 200 | if(my $c = RapidApp->active_request_context) { $pfx = $c->mount_url || ''; } | ||
| 201 | if($mode) { | ||
| 202 | $mode = lc($mode); | ||
| 203 | die "open_url_path(): bad argument '$mode' -- must be undef, 'direct' or 'navable'" | ||
| 204 | unless ($mode eq 'direct' or $mode eq 'navable'); | ||
| 205 | return join('',$pfx,'/rapidapp/module/',$mode,$self->getRestPath) | ||
| 206 | } | ||
| 207 | else { | ||
| 208 | my $ns = $app->module_root_namespace; | ||
| 209 | return join('',$pfx,'/',$ns,'/#!',$self->getRestPath) | ||
| 210 | } | ||
| 211 | } | ||
| 212 | |||
| 213 | |||
| 214 | sub insert { | ||
| 215 | my $self = shift; | ||
| 216 | my $columns = shift; | ||
| 217 | |||
| 218 | if(my $User = Rapi::Blog::Util->get_User) { | ||
| 219 | die usererr "Insert Post: PERMISSION DENIED" if ($User->id && !$User->can_post); | ||
| 220 | } | ||
| 221 | |||
| 222 | $self->set_inflated_columns($columns) if $columns; | ||
| 223 | |||
| 224 | $self->_sanitize_body; | ||
| 225 | |||
| 226 | $self->_set_column_defaults('insert'); | ||
| 227 | |||
| 228 | $self->next::method; | ||
| 229 | |||
| 230 | $self->_update_tags; | ||
| 231 | |||
| 232 | return $self; | ||
| 233 | } | ||
| 234 | |||
| 235 | sub update { | ||
| 236 | my $self = shift; | ||
| 237 | my $columns = shift; | ||
| 238 | $self->set_inflated_columns($columns) if $columns; | ||
| 239 | |||
| 240 | my $uid = Rapi::Blog::Util->get_uid; | ||
| 241 | die usererr "Update Post: PERMISSION DENIED" if ($uid && !$self->can_modify); | ||
| 242 | |||
| 243 | $self->updater_id( $uid ); | ||
| 244 | |||
| 245 | $self->_sanitize_body if ($self->is_column_changed('body')); | ||
| 246 | |||
| 247 | $self->_set_column_defaults('update'); | ||
| 248 | |||
| 249 | $self->_update_tags if ($self->is_column_changed('body')); | ||
| 250 | |||
| 251 | $self->next::method; | ||
| 252 | } | ||
| 253 | |||
| 254 | sub _sanitize_body { | ||
| 255 | my $self = shift; | ||
| 256 | $self->body( Rapi::Blog::Util->sanitize_input( $self->body ) ) | ||
| 257 | } | ||
| 258 | |||
| 259 | sub delete { | ||
| 260 | my $self = shift; | ||
| 261 | |||
| 262 | my $uid = Rapi::Blog::Util->get_uid; | ||
| 263 | die usererr "Delete Post: PERMISSION DENIED" if ($uid && !$self->can_delete); | ||
| 264 | |||
| 265 | $self->next::method(@_) | ||
| 266 | } | ||
| 267 | |||
| 268 | sub image_url { | ||
| 269 | my $self = shift; | ||
| 270 | $self->{_image_url} //= $self->image | ||
| 271 | ? join('/','_ra-rel-mnt_','simplecas','fetch_content',$self->image) | ||
| 272 | : undef | ||
| 273 | } | ||
| 274 | |||
| 275 | sub tag_names_list { | ||
| 276 | my $self = shift; | ||
| 277 | split(/\s+/,$self->tag_names) | ||
| 278 | } | ||
| 279 | |||
| 280 | sub num_categories { | ||
| 281 | my $self = shift; | ||
| 282 | $self->post_categories->count | ||
| 283 | } | ||
| 284 | |||
| 285 | sub category_list { | ||
| 286 | my $self = shift; | ||
| 287 | sort $self | ||
| 288 | ->post_categories | ||
| 289 | ->get_column('category_name') | ||
| 290 | ->all | ||
| 291 | } | ||
| 292 | |||
| 293 | sub section_name { | ||
| 294 | my $self = shift; | ||
| 295 | $self->section ? $self->section->get_column('name') : undef | ||
| 296 | } | ||
| 297 | |||
| 298 | sub section_name_or { | ||
| 299 | my ($self, $default) = @_; | ||
| 300 | $self->section_name || $default | ||
| 301 | } | ||
| 302 | |||
| 303 | sub _set_column_defaults { | ||
| 304 | my $self = shift; | ||
| 305 | my $for = shift || ''; | ||
| 306 | |||
| 307 | # default title: | ||
| 308 | $self->title($self->name) unless $self->title; | ||
| 309 | |||
| 310 | if ($for eq 'insert' || $self->is_column_changed('body') || $self->is_column_changed('custom_summary')) { | ||
| 311 | $self->size( length $self->body ); | ||
| 312 | $self->summary( | ||
| 313 | $self->custom_summary ? $self->custom_summary : $self->_generate_auto_summary | ||
| 314 | ); | ||
| 315 | $self->tag_names(join(' ',$self->_extract_normalized_hashtags)); | ||
| 316 | } | ||
| 317 | |||
| 318 | my $uid = Rapi::Blog::Util->get_uid; | ||
| 319 | my $now_ts = Rapi::Blog::Util->now_ts; | ||
| 320 | |||
| 321 | if ($self->published) { | ||
| 322 | $self->publish_ts($now_ts) unless $self->publish_ts; | ||
| 323 | } | ||
| 324 | else { | ||
| 325 | $self->publish_ts(undef) if $self->publish_ts; | ||
| 326 | } | ||
| 327 | |||
| 328 | $self->update_ts($now_ts); | ||
| 329 | $self->updater_id( $uid ); | ||
| 330 | $self->author_id( $uid ) unless $self->author_id; | ||
| 331 | |||
| 332 | if($for eq 'insert') { | ||
| 333 | $self->create_ts($now_ts); | ||
| 334 | $self->creator_id( $uid ); | ||
| 335 | } | ||
| 336 | |||
| 337 | if ($for eq 'insert' || $self->is_column_changed('section_id')) { | ||
| 338 | $self->_apply_track_sections | ||
| 339 | } | ||
| 340 | |||
| 341 | } | ||
| 342 | |||
| 343 | sub _extract_normalized_hashtags { | ||
| 344 | my $self = shift; | ||
| 345 | # normalized list of keywords, lowercased and _ converted to - | ||
| 346 | uniq(map { $_ =~ s/\_/\-/g; lc($_) } $self->_extract_hashtags); | ||
| 347 | } | ||
| 348 | |||
| 349 | sub _update_tags { | ||
| 350 | my $self = shift; | ||
| 351 | my @kw = $self->_extract_normalized_hashtags; | ||
| 352 | $self->set_tags([ map {{ name => $_ }} @kw ]); | ||
| 353 | } | ||
| 354 | |||
| 355 | |||
| 356 | sub _parse_social_entities { | ||
| 357 | my $self = shift; | ||
| 358 | my $body = $self->body or return (); | ||
| 359 | |||
| 360 | my @ents = | ||
| 361 | grep { ! $self->_should_ignore_hashtag($_) } | ||
| 362 | $body =~ /(?:^|\s)([#@][-\w]{1,64})\b/g; | ||
| 363 | |||
| 364 | return uniq(@ents) | ||
| 365 | } | ||
| 366 | |||
| 367 | |||
| 368 | sub _should_ignore_hashtag { | ||
| 369 | my ($self, $string) = @_; | ||
| 370 | |||
| 371 | return 1 if ($string =~ /^#\d+$/); # Exclude hashtags that are only numbers | ||
| 372 | |||
| 373 | my $len = length($string); | ||
| 374 | if($len == 4 || $len == 7) { | ||
| 375 | # ignore all strings that look like hex numbers that are 3 or 6 characters long | ||
| 376 | return 1 if ($string =~ /^#[0-9A-Fa-f]+$/); | ||
| 377 | } | ||
| 378 | |||
| 379 | return 0 | ||
| 380 | } | ||
| 381 | |||
| 382 | |||
| 383 | sub _extract_hashtags { | ||
| 384 | my $self = shift; | ||
| 385 | map { $_ =~ s/^#//; $_ } grep { $_ =~ /^#/ } $self->_parse_social_entities | ||
| 386 | } | ||
| 387 | |||
| 388 | |||
| 389 | sub _generate_auto_summary { | ||
| 390 | my $self = shift; | ||
| 391 | |||
| 392 | my $num_words = 70; | ||
| 393 | |||
| 394 | my $body = $self->body; | ||
| 395 | |||
| 396 | # Convert markdown links to plain text (labels) (provided by @deven) | ||
| 397 | $body =~ s/(!?)\[(.*?)\]\((.*?)\)/$1 ? "" : $2/ge; | ||
| 398 | |||
| 399 | # Convert ![], [] and () to <> so they will look like tags and get stripped in the next step | ||
| 400 | $body =~ s/\!?[\[\(]/\</g; | ||
| 401 | $body =~ s/[\]\)]/\>/g; | ||
| 402 | |||
| 403 | # Strip HTML markup from body | ||
| 404 | my $text = HTML::Strip->new->parse( $body ); | ||
| 405 | |||
| 406 | my $i = 0; | ||
| 407 | my $buf = ''; | ||
| 408 | for my $line (split(/\r?\n/,$text)) { | ||
| 409 | for my $word (split(/\s+/,$line)) { | ||
| 410 | next if ($word =~ /^\W+$/); | ||
| 411 | $buf .= "$word "; | ||
| 412 | return $buf if (++$i >= $num_words); | ||
| 413 | } | ||
| 414 | } | ||
| 415 | |||
| 416 | return $buf | ||
| 417 | } | ||
| 418 | |||
| 419 | |||
| 420 | sub _apply_track_sections { | ||
| 421 | my $self = shift; | ||
| 422 | |||
| 423 | $self->trk_section_posts->delete_all; | ||
| 424 | return unless ($self->section); | ||
| 425 | |||
| 426 | my @ids = $self->section->all_section_ids; | ||
| 427 | my ($id, $depth) = ($self->get_column('id'), 0); | ||
| 428 | |||
| 429 | $self->trk_section_posts->populate([ | ||
| 430 | map {{ | ||
| 431 | section_id => $_, | ||
| 432 | post_id => $id, | ||
| 433 | depth => $depth++ | ||
| 434 | }} @ids | ||
| 435 | ]) | ||
| 436 | } | ||
| 437 | |||
| 438 | |||
| 439 | |||
| 440 | sub record_hit { | ||
| 441 | my $self = shift; | ||
| 442 | |||
| 443 | my @args = ({ post_id => $self->id, ts => Rapi::Blog::Util->now_ts }); | ||
| 444 | if(my $c = RapidApp->active_request_context) { | ||
| 445 | push @args, $c->request; | ||
| 446 | } | ||
| 447 | |||
| 448 | # This is a rare example where we do want to allow an insert from a template | ||
| 449 | local $Rapi::Blog::DB::Component::SafeResult::ALLOW = 1; | ||
| 450 | |||
| 451 | $self->hits->create_from_request(@args); | ||
| 452 | |||
| 453 | return ""; | ||
| 454 | } | ||
| 455 | |||
| 456 | sub can_delete { | ||
| 457 | my $self = shift; | ||
| 458 | my $User = Rapi::Blog::Util->get_User or return 0; | ||
| 459 | $User->admin or ($User->author && $self->author_id == $User->id) | ||
| 460 | } | ||
| 461 | |||
| 462 | sub can_modify { | ||
| 463 | my $self = shift; | ||
| 464 | my $User = Rapi::Blog::Util->get_User or return 0; | ||
| 465 | $User->admin or ($User->author && $self->author_id == $User->id) | ||
| 466 | } | ||
| 467 | |||
| 468 | sub can_change_author { | ||
| 469 | my $self = shift; | ||
| 470 | my $User = Rapi::Blog::Util->get_User or return 0; | ||
| 471 | $User->admin | ||
| 472 | } | ||
| 473 | |||
| 474 | |||
| 475 | # You can replace this text with custom code or comments, and it will be preserved on regeneration | ||
| 476 | __PACKAGE__->meta->make_immutable; | ||
| 477 | 1; | ||
| 478 | |||
| 479 | |||
| 480 | __END__ |