1 |
use strict; |
2 |
use warnings; |
3 |
|
4 |
package Grep::Model::Item; |
5 |
use Jifty::DBI::Schema; |
6 |
|
7 |
use Grep::Model::Feed; |
8 |
|
9 |
use Grep::Record schema { |
10 |
|
11 |
column in_feed => |
12 |
refers_to Grep::Model::Feed by 'id', |
13 |
is mandatory; |
14 |
|
15 |
column title => |
16 |
type is 'text', |
17 |
is mandatory; |
18 |
|
19 |
column link => |
20 |
type is 'text', |
21 |
is mandatory; |
22 |
|
23 |
column content => |
24 |
type is 'text', |
25 |
is mandatory; |
26 |
|
27 |
column summary => |
28 |
type is 'text'; |
29 |
|
30 |
column category => |
31 |
type is 'text'; |
32 |
|
33 |
column author => |
34 |
type is 'text'; |
35 |
|
36 |
column created_on => |
37 |
type is 'timestamp', |
38 |
default is defer { DateTime->now }, |
39 |
filters are qw( Jifty::Filter::DateTime Jifty::DBI::Filter::DateTime ); |
40 |
|
41 |
column last_update => |
42 |
type is 'timestamp', |
43 |
default is defer { DateTime->now }, |
44 |
filters are qw( Jifty::Filter::DateTime Jifty::DBI::Filter::DateTime ); |
45 |
|
46 |
}; |
47 |
|
48 |
sub since { '0.0.1' } |
49 |
|
50 |
# Your model-specific methods go here. |
51 |
|
52 |
sub current_user_can { |
53 |
my $self = shift; |
54 |
my $what = shift; |
55 |
|
56 |
my $uid = Jifty->web->current_user->id; |
57 |
|
58 |
# superuser |
59 |
return 1 if ( defined $uid && $uid == 0 ); |
60 |
|
61 |
return 1 if ( $uid && $what eq 'create' ); |
62 |
|
63 |
return 1 if ( $uid && ! $self->id ); |
64 |
|
65 |
my $owner_id = $self->in_feed->owner->id; |
66 |
|
67 |
# Jifty->log->debug("current_user[$uid]_can($what) on Item ", $self->id || '???', " owner ", $owner_id || '???'); |
68 |
|
69 |
return 1 if ( $uid && $owner_id && $uid == $owner_id ); |
70 |
|
71 |
# system user |
72 |
return 1 if ( defined( $uid ) && $uid == 0 ); |
73 |
|
74 |
warn "$uid can't $what"; |
75 |
} |
76 |
|
77 |
sub _set { |
78 |
my $self = shift; |
79 |
my ($val, $msg) = $self->SUPER::_set(@_); |
80 |
|
81 |
$self->SUPER::_set(column => 'last_update', value => defer { DateTime->now }); |
82 |
|
83 |
return ($val, $msg); |
84 |
} |
85 |
|
86 |
=head2 link_current |
87 |
|
88 |
my $item = Grep::Model::Item->link_currnet( 'http://www.example.com' ) |
89 |
my $item = $item->link_current( 'http://www.example.com' ); |
90 |
|
91 |
=cut |
92 |
|
93 |
my $update_every_hr = 24; # s |
94 |
|
95 |
sub link_current { |
96 |
my $self = shift; |
97 |
my $uri = shift || die "no uri?"; |
98 |
|
99 |
my $item = $self; |
100 |
if ( ! ref($self) || ! $self->link || ( $self->link && $self->link ne $uri ) ) { |
101 |
$item = Grep::Model::Item->new; |
102 |
#warn "load_by_cols link => $uri"; |
103 |
$item->load_by_cols( link => $uri ); |
104 |
return $item if $item->id; |
105 |
return 0; |
106 |
} |
107 |
|
108 |
return 0 unless ( $item ); |
109 |
|
110 |
die "item is not Grep::Model::Item, but ", ref( $item ) unless ref( $item ) eq 'Grep::Model::Item'; |
111 |
|
112 |
my $last_update = $item->last_update || $item->created_on; # fallback |
113 |
return 0 unless defined( $last_update ) && $last_update; |
114 |
|
115 |
my $newer_than = DateTime->now->subtract( hours => $update_every_hr ); |
116 |
#warn "$last_update > $newer_than"; |
117 |
return 0 if ( $last_update - $newer_than )->is_negative; |
118 |
return $item; |
119 |
} |
120 |
|
121 |
1; |
122 |
|