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