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