1 | package Bookmarks::Controller; |
---|
2 | |
---|
3 | use Moose; |
---|
4 | |
---|
5 | use Encode; |
---|
6 | use HTTP::Date qw{time2str str2time}; |
---|
7 | use JSON; |
---|
8 | use Bookmarks; |
---|
9 | use URI; |
---|
10 | use Template; |
---|
11 | |
---|
12 | has dbname => ( |
---|
13 | is => 'ro', |
---|
14 | required => 1, |
---|
15 | ); |
---|
16 | has bookmarks => ( |
---|
17 | is => 'ro', |
---|
18 | handles => [qw{get_bookmark}], |
---|
19 | builder => '_build_bookmarks', |
---|
20 | lazy => 1, |
---|
21 | ); |
---|
22 | has base_uri => ( |
---|
23 | is => 'ro', |
---|
24 | builder => '_build_base_uri', |
---|
25 | lazy => 1, |
---|
26 | ); |
---|
27 | has request => ( |
---|
28 | is => 'ro', |
---|
29 | required => 1, |
---|
30 | ); |
---|
31 | |
---|
32 | sub _build_bookmarks { |
---|
33 | my $self = shift; |
---|
34 | return Bookmarks->new({ |
---|
35 | dbname => $self->dbname, |
---|
36 | base_uri => $self->base_uri, |
---|
37 | }); |
---|
38 | } |
---|
39 | |
---|
40 | sub _build_base_uri { |
---|
41 | my $self = shift; |
---|
42 | my $url = $self->request->base; |
---|
43 | |
---|
44 | $url .= '/' unless $url =~ m{/$}; |
---|
45 | return URI->new($url); |
---|
46 | } |
---|
47 | |
---|
48 | sub find_or_new { |
---|
49 | my $self = shift; |
---|
50 | |
---|
51 | my $bookmark = $self->bookmarks->get_bookmark({ uri => $self->request->param('uri') }); |
---|
52 | if ($bookmark) { |
---|
53 | # redirect to the view of the existing bookmark |
---|
54 | return [301, [Location => $bookmark->bookmark_uri], []]; |
---|
55 | } else { |
---|
56 | # bookmark was not found; show the form to create a new bookmark |
---|
57 | my $template = Template->new; |
---|
58 | $template->process( |
---|
59 | 'bookmark.tt', |
---|
60 | { |
---|
61 | bookmark => { |
---|
62 | uri => $self->request->param('uri'), |
---|
63 | title => $self->request->param('title') || '', |
---|
64 | }, |
---|
65 | }, |
---|
66 | \my $output, |
---|
67 | ); |
---|
68 | return [404, ['Content-Type' => 'text/html; charset=UTF-8'], [$output]]; |
---|
69 | } |
---|
70 | } |
---|
71 | |
---|
72 | sub list { |
---|
73 | my $self = shift; |
---|
74 | |
---|
75 | # list all the bookmarks |
---|
76 | my $mtime = $self->bookmarks->get_last_modified_time; |
---|
77 | |
---|
78 | my $format = $self->request->param('format') || 'html'; |
---|
79 | |
---|
80 | my @tags = grep { $_ ne '' } $self->request->param('tag'); |
---|
81 | my $query = $self->request->param('q'); |
---|
82 | my $limit = $self->request->param('limit'); |
---|
83 | my $offset = $self->request->param('offset'); |
---|
84 | |
---|
85 | my $list = $self->bookmarks->get_bookmarks({ |
---|
86 | query => $query, |
---|
87 | tags => \@tags, |
---|
88 | limit => $limit, |
---|
89 | offset => $offset, |
---|
90 | }); |
---|
91 | |
---|
92 | my $as_format = "as_$format"; |
---|
93 | if (!$list->meta->has_method($as_format)) { |
---|
94 | return [406, ['Content-Type' => 'text/plain; charset=UTF-8'], [qq{"$format" is not a supported format}]]; |
---|
95 | } |
---|
96 | return $list->$as_format; |
---|
97 | } |
---|
98 | |
---|
99 | sub feed { |
---|
100 | my $self = shift; |
---|
101 | |
---|
102 | my $query = $self->request->param('q'); |
---|
103 | my @tags = grep { $_ ne '' } $self->request->param('tag'); |
---|
104 | |
---|
105 | # construct a feed from the most recent 12 bookmarks |
---|
106 | my $list = $self->bookmarks->get_bookmarks({ query => $query, tags => \@tags, limit => 12 }); |
---|
107 | return $list->as_atom; |
---|
108 | } |
---|
109 | |
---|
110 | # returns 1 if there is an If-Modified-Since header and it is newer than the given $mtime |
---|
111 | # returns 0 if there is an If-Modified-Since header but the $mtime is newer |
---|
112 | # returns undef if there is no If-Modified-Since header |
---|
113 | sub _request_is_newer_than { |
---|
114 | my $self = shift; |
---|
115 | my $mtime = shift; |
---|
116 | |
---|
117 | # check If-Modified-Since header to return cache response |
---|
118 | if ($self->request->env->{HTTP_IF_MODIFIED_SINCE}) { |
---|
119 | my $cache_time = str2time($self->request->env->{HTTP_IF_MODIFIED_SINCE}); |
---|
120 | return $mtime <= $cache_time ? 1 : 0; |
---|
121 | } else { |
---|
122 | return; |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | sub view { |
---|
127 | my ($self, $id) = @_; |
---|
128 | |
---|
129 | my $format = $self->request->param('format') || 'html'; |
---|
130 | |
---|
131 | my $bookmark = $self->get_bookmark({ id => $id }); |
---|
132 | if ($bookmark) { |
---|
133 | return [304, [], []] if $self->_request_is_newer_than($bookmark->mtime); |
---|
134 | |
---|
135 | my $last_modified = time2str($bookmark->mtime); |
---|
136 | |
---|
137 | if ($format eq 'json') { |
---|
138 | my $json = decode_utf8(JSON->new->utf8->convert_blessed->encode($bookmark)); |
---|
139 | return [200, ['Content-Type' => 'application/json; charset=UTF-8', 'Last-Modified' => $last_modified], [$json]]; |
---|
140 | } else { |
---|
141 | # display the bookmark form for this bookmark |
---|
142 | my $template = Template->new; |
---|
143 | $template->process( |
---|
144 | 'bookmark.tt', |
---|
145 | { bookmark => $bookmark }, |
---|
146 | \my $output, |
---|
147 | ); |
---|
148 | return [200, ['Content-Type' => 'text/html; charset=UTF-8', 'Last-Modified' => $last_modified], [$output]]; |
---|
149 | } |
---|
150 | } else { |
---|
151 | return [404, ['Content-Type' => 'text/plain; charset=UTF-8'], ["Bookmark $id not found"]]; |
---|
152 | } |
---|
153 | } |
---|
154 | |
---|
155 | sub view_field { |
---|
156 | my ($self, $id, $field) = @_; |
---|
157 | |
---|
158 | my $bookmark = $self->get_bookmark({ id => $id }); |
---|
159 | if ($bookmark) { |
---|
160 | return [304, [], []] if $self->_request_is_newer_than($bookmark->mtime); |
---|
161 | |
---|
162 | # check whether the requested field is part of the bookmark |
---|
163 | if (!$bookmark->meta->has_attribute($field)) { |
---|
164 | return [404, ['Content-Type' => 'text/plain; charset=UTF-8'], [qq{"$field" is not a valid bookmark data field}]]; |
---|
165 | } |
---|
166 | |
---|
167 | # respond with just the requested field as plain text |
---|
168 | my $value = $bookmark->$field; |
---|
169 | my $last_modified = time2str($bookmark->mtime); |
---|
170 | return [200, ['Content-Type' => 'text/plain; charset=UTF-8', 'Last-Modified' => $last_modified], [ref $value eq 'ARRAY' ? join(' ', @{ $value }) : $value]]; |
---|
171 | } else { |
---|
172 | return [404, ['Content-Type' => 'text/plain; charset=UTF-8'], ["Bookmark $id not found"]]; |
---|
173 | } |
---|
174 | } |
---|
175 | |
---|
176 | sub create_and_redirect { |
---|
177 | my $self = shift; |
---|
178 | |
---|
179 | my $uri = $self->request->param('uri'); |
---|
180 | my $title = $self->request->param('title'); |
---|
181 | my @tags = split ' ', $self->request->param('tags'); |
---|
182 | |
---|
183 | my $bookmark = $self->bookmarks->add({ |
---|
184 | uri => $uri, |
---|
185 | title => $title, |
---|
186 | tags => \@tags, |
---|
187 | }); |
---|
188 | |
---|
189 | #TODO: not RESTful; the proper RESTful response would be a 201 |
---|
190 | return [303, ['Location' => $bookmark->bookmark_uri->canonical], []]; |
---|
191 | } |
---|
192 | |
---|
193 | sub update_and_redirect { |
---|
194 | my $self = shift; |
---|
195 | my $id = shift; |
---|
196 | |
---|
197 | my $bookmark = $self->bookmarks->get_bookmark({ id => $id }); |
---|
198 | if ($bookmark) { |
---|
199 | # update the URI, title, and tags |
---|
200 | $bookmark->uri($self->request->param('uri')); |
---|
201 | $bookmark->title($self->request->param('title')); |
---|
202 | $bookmark->tags([ split ' ', $self->request->param('tags') || '' ]); |
---|
203 | |
---|
204 | # write to the database |
---|
205 | $bookmark->update; |
---|
206 | |
---|
207 | #TODO: not RESTful; proper response would be a 200 |
---|
208 | return [303, ['Location' => $bookmark->bookmark_uri->canonical], []]; |
---|
209 | } else { |
---|
210 | return [404, ['Content-Type' => 'text/plain; charset=UTF-8'], ["Bookmark $id not found"]]; |
---|
211 | } |
---|
212 | } |
---|
213 | |
---|
214 | 1; |
---|