source: bookmarks/trunk/BookmarkController.pm @ 61

Last change on this file since 61 was 61, checked in by peter, 11 years ago
  • wrap the creation of the Bookmarks object into the BookmarkController
  • bookmark is a lazy-loaded read-only attribute with a builder in BookmarkController
  • made dbname and request required attributes of BookmarkController
  • updated the api notes
File size: 12.3 KB
RevLine 
[59]1package BookmarkController;
2use Moose;
[5]3
4use Encode;
[57]5use HTTP::Date qw{time2isoz time2iso time2str str2time};
[5]6use JSON;
7use Bookmarks;
[7]8use URI;
[59]9use Template;
[7]10
[61]11has dbname => (
12    is => 'ro',
13    required => 1,
14);
[59]15has bookmarks => (
[61]16    is => 'ro',
[59]17    handles => [qw{get_bookmark}],
[61]18    builder => '_build_bookmarks',
19    lazy => 1,
[59]20);
21has base_uri => (
22    is => 'ro',
23    builder => '_build_base_uri',
24    lazy => 1,
25);
26has request => (
27    is => 'ro',
[61]28    required => 1,
[59]29);
30
[61]31sub _build_bookmarks {
32    my $self = shift;
33    return Bookmarks->new({
34        dbname   => $self->dbname,
35        base_uri => $self->base_uri,
36    });
37}
38
[59]39sub _build_base_uri {
[5]40    my $self = shift;
[59]41    my $url = $self->request->base;
[41]42
[53]43    $url .= '/' unless $url =~ m{/$};
[59]44    return URI->new($url);
[5]45}
46
[38]47sub _get_list_links {
48    my $self = shift;
[52]49    my ($self_type, $query) = @_;
[38]50    my @links = (
51        {
52            text => 'JSON',
53            type => 'application/json',
54            query => {
[52]55                %$query,
[38]56                format => 'json',
57            },
58        },
59        {
60            text => 'XBEL',
61            type => 'application/xml',
62            query => {
[52]63                %$query,
[38]64                format => 'xbel',
65            },
66        },
67        {
68            text => 'Atom',
69            type => 'application/atom+xml',
70            path => 'feed',
71            query => {
[52]72                %$query,
[38]73            },
74        },
75        {
[49]76            text => 'CSV',
77            type => 'text/csv',
78            query => {
[52]79                %$query,
[49]80                format => 'csv',
81            },
82        },
83        {
[38]84            text => 'URI List',
85            type => 'text/uri-list',
86            query => {
[52]87                %$query,
[40]88                format => 'text',
[38]89            },
[49]90        },
[50]91        {
92            text => 'HTML',
93            type => 'text/html',
94            query => {
[52]95                %$query,
[50]96            },
97        },
[38]98    );
99
100    for my $link (@links) {
101        $link->{rel}  = $link->{type} eq $self_type ? 'self' : 'alternate';
[59]102        $link->{href} = URI->new_abs($link->{path} || '', $self->base_uri);
[38]103        $link->{href}->query_form($link->{query});
104    }
105
106    return @links;
107}
108
[59]109sub find_or_new {
[5]110    my $self = shift;
111
[59]112    my $bookmark = $self->bookmarks->get_bookmark({ uri => $self->request->param('uri') });
113    if ($bookmark) {
114        # redirect to the view of the existing bookmark
115        return [301, [Location => $bookmark->bookmark_uri], []];
116    } else {
117        # bookmark was not found; show the form to create a new bookmark
118        my $template = Template->new;
119        $template->process(
120            'bookmark.tt',
121            {
122                uri   => $self->request->param('uri'),
123                title => $self->request->param('title') || '',
124            },
125            \my $output,
126        );
127        return [404, ['Content-Type' => 'text/html; charset=UTF-8'], [$output]];
[5]128    }
[59]129}
[5]130
[59]131sub list {
132    my $self = shift;
133
[5]134    # list all the bookmarks
[59]135    my $mtime = $self->bookmarks->get_last_modified_time;
[57]136
[59]137    my $format = $self->request->param('format') || 'html';
[35]138
[59]139    my @tags = grep { $_ ne '' } $self->request->param('tag');
140    my $query = $self->request->param('q');
141    my $limit = $self->request->param('limit');
142    my $offset = $self->request->param('offset');
143    my @resources = $self->bookmarks->get_bookmarks({
[52]144        query  => $query,
[20]145        tag    => \@tags,
[13]146        limit  => $limit,
147        offset => $offset,
148    });
[59]149    my @all_tags = $self->bookmarks->get_tags({ selected => $tags[0] });
150    my @cotags = $self->bookmarks->get_cotags({
[52]151        query  => $query,
152        tag    => \@tags,
153    });
[31]154   
[52]155    my $title = 'Bookmarks' . (@tags ? " tagged as " . join(' & ', @tags) : '') . ($query ? " matching '$query'" : '');
[5]156
157    if ($format eq 'json') {
[59]158        my $json = decode_utf8(
[25]159            JSON->new->utf8->convert_blessed->encode({
[7]160                bookmarks => \@resources,
[5]161            })
162        );
[59]163        return [200, ['Content-Type' => 'application/json; charset=UTF-8'], [$json]];
[19]164    } elsif ($format eq 'xbel') {
165        require XML::XBEL;
166        #TODO: conditional support; if XML::XBEL is not present, return a 5xx response
167
168        my $xbel = XML::XBEL->new;
169
170        $xbel->new_document({
[31]171            title => $title,
[19]172        });
173
174        for my $bookmark (@resources) {
[36]175            my $cdatetime = time2isoz $bookmark->ctime;
176            my $mdatetime = time2isoz $bookmark->mtime;
[19]177            # make the timestamps W3C-correct
178            s/ /T/ foreach ($cdatetime, $mdatetime);
179
180            $xbel->add_bookmark({
[36]181                href     => $bookmark->uri,
182                title    => $bookmark->title,
183                desc     => 'Tags: ' . join(', ', @{ $bookmark->tags }),
[19]184                added    => $cdatetime,
185                #XXX: are we sure that modified is the mtime of the bookmark or the resource?
186                modified => $mdatetime,
187            });
188        }
189
[59]190        return [200, ['Content-Type' => 'application/xml; charset=UTF-8'], [$xbel->toString]];
[36]191    } elsif ($format eq 'text') {
[59]192        my $text = join '', 
[36]193            map {
194                sprintf "# %s\n# Tags: %s\n%s\n",
195                $_->title,
196                join(', ', @{ $_->tags }), 
197                $_->uri
198            } @resources;
[59]199        return [200, ['Content-Type' => 'text/uri-list; charset=UTF-8'], [$text]];
[49]200    } elsif ($format eq 'csv') {
[51]201        require Text::CSV::Encoded;
[59]202        my $csv = Text::CSV::Encoded->new({ encoding_out => 'utf8' });
[49]203        my $text = qq{id,uri,title,tags,ctime,mtime\n};
204        for my $bookmark (@resources) {
205            my $success = $csv->combine(
206                $bookmark->id,
207                $bookmark->uri,
208                $bookmark->title,
209                join(' ', @{ $bookmark->tags }),
210                $bookmark->ctime,
211                $bookmark->mtime,
212            );
213            $text .= $csv->string . "\n" if $success;
214        }
215
216        # include the local timestamp in the attchment filename
217        my $dt = time2iso;
218        $dt =~ s/[^\d]//g;
219
[59]220        my $filename = sprintf(
221            'bookmarks-%s-%s.csv',
222            join('_', @tags),
223            $dt,
[49]224        );
[59]225
226        return [200, ['Content-Type' => 'text/csv; charset=UTF-8', 'Content-Disposition' => sprintf('attachement; filename="%s"', $filename)], [$text]];
[5]227    } else {
[59]228        my $template = Template->new;
[5]229
[59]230        $template->process(
[5]231            'list.tt',
232            {
[59]233                base_url     => $self->base_uri,
[31]234                title        => $title,
[52]235                query        => $query,
[35]236                selected_tag => $tags[0],
[20]237                search_tags  => \@tags,
[52]238                links        => [ $self->_get_list_links('text/html', { q => $query, tag => \@tags }) ],
[20]239                all_tags     => \@all_tags,
[5]240                cotags       => \@cotags,
241                resources    => \@resources,
242            },
[59]243            \my $output,
[5]244        );
[59]245        return [200, ['Content-Type' => 'text/html; charset=UTF-8'], [$output]];
[5]246    }
247}
248
[9]249sub feed {
250    my $self = shift;
251
[59]252    my $query = $self->request->param('q');
253    my @tags = grep { $_ ne '' } $self->request->param('tag');
[31]254    my $title = 'Bookmarks' . (@tags ? " tagged as " . join(' & ', @tags) : '');
255
[33]256    require XML::Atom;
257    $XML::Atom::DefaultVersion = "1.0";
258
[9]259    require XML::Atom::Feed;
260    require XML::Atom::Entry;
261    require XML::Atom::Link;
[39]262    require XML::Atom::Category;
[9]263
264    my $feed = XML::Atom::Feed->new;
[31]265    $feed->title($title);
[32]266
[59]267    my $feed_uri = URI->new_abs('feed', $self->base_uri);
[32]268    $feed_uri->query_form(tag => \@tags);
269    $feed->id($feed_uri->canonical);
270
[52]271    for my $link ($self->_get_list_links('application/atom+xml', { q => $query, tag => \@tags })) {
[38]272        my $atom_link = XML::Atom::Link->new;
273        $atom_link->type($link->{type});
274        $atom_link->rel($link->{rel});
275        $atom_link->href($link->{href}->canonical);
276        $feed->add_link($atom_link);
277    }
[32]278
[9]279    # construct a feed from the most recent 12 bookmarks
[59]280    for my $bookmark ($self->bookmarks->get_bookmarks({ query => $query, tag => \@tags, limit => 12 })) {
[9]281        my $entry = XML::Atom::Entry->new;
[31]282        $entry->id($bookmark->bookmark_uri->canonical);
283        $entry->title($bookmark->title);
[39]284       
[9]285        my $link = XML::Atom::Link->new;
[31]286        $link->href($bookmark->uri);
[9]287        $entry->add_link($link);
[39]288       
[31]289        $entry->summary('Tags: ' . join(', ', @{ $bookmark->tags }));
[39]290
291        my $cdatetime = time2isoz $bookmark->ctime;
[33]292        my $mdatetime = time2isoz $bookmark->mtime;
293        # make the timestamp W3C-correct
[39]294        s/ /T/ foreach ($cdatetime, $mdatetime);
295        $entry->published($cdatetime);
[33]296        $entry->updated($mdatetime);
[39]297       
298        for my $tag (@{ $bookmark->tags }) {
299            my $category = XML::Atom::Category->new;
300            $category->term($tag);
301            $entry->add_category($category);
302        }
303
[9]304        $feed->add_entry($entry);
305    }
306
[59]307    return [200, ['Content-Type' => 'application/atom+xml; charset=UTF-8'], [$feed->as_xml]];
[9]308}
309
[5]310sub view {
[59]311    my ($self, $id) = @_;
[5]312
[59]313    my $format = $self->request->param('format') || 'html';
314
315    my $bookmark = $self->get_bookmark({ id => $id });
[5]316    if ($bookmark) {
[57]317        # check If-Modified-Since header to return cache response
[59]318        if ($self->request->env->{HTTP_IF_MODIFIED_SINCE}) {
319            my $cache_time = str2time($self->request->env->{HTTP_IF_MODIFIED_SINCE});
[57]320            if ($bookmark->mtime <= $cache_time) {
[59]321                return [304, [], []];
[57]322            }
323        }
[59]324        my $last_modified = time2str($bookmark->mtime);
[57]325       
[30]326        if ($format eq 'json') {
[59]327            my $json = decode_utf8(JSON->new->utf8->convert_blessed->encode($bookmark));
328            return [200, ['Content-Type' => 'application/json; charset=UTF-8', 'Last-Modified' => $last_modified], [$json]];
[5]329        } else {
[30]330            # display the bookmark form for this bookmark
331            $bookmark->{exists} = 1;
332            $bookmark->{created} = "Created " . localtime($bookmark->ctime);
333            $bookmark->{created} .= '; Updated ' . localtime($bookmark->mtime) unless $bookmark->ctime == $bookmark->mtime;
[59]334            my $template = Template->new;
335            $template->process(
[30]336                'bookmark.tt',
337                $bookmark,
[59]338                \my $output,
[30]339            );
[59]340            return [200, ['Content-Type' => 'text/html; charset=UTF-8', 'Last-Modified' => $last_modified], [$output]];
[30]341        }
342    } else {
[59]343        return [404, ['Content-Type' => 'text/plain; charset=UTF-8'], ["Boomark $id not found"]];
[30]344    }
345}
346
347sub view_field {
[59]348    my ($self, $id, $field) = @_;
[30]349
[59]350    my $bookmark = $self->bookmarks->get_bookmark({ id => $id });
[30]351    if ($bookmark) {
352        # respond with just the requested field as plain text
353        my $value = eval { $bookmark->$field };
354        if ($@) {
355            if ($@ =~ /Can't locate object method/) {
[59]356                return [404, ['Content-Type' => 'text/plain; charset=UTF-8'], [qq{"$field" is not a valid bookmark data field}]];
[7]357            } else {
[30]358                die $@;
[7]359            }
[5]360        }
[59]361        return [200, ['Content-Type' => 'text/plain; charset=UTF-8'], [ref $value eq 'ARRAY' ? join(' ', @{ $value }) : $value]];
[5]362    } else {
[59]363        return [404, ['Content-Type' => 'text/plain; charset=UTF-8'], ["Boomark $id not found"]];
[5]364    }
365}
366
[45]367sub create {
[5]368    my $self = shift;
[59]369
370    my $uri   = $self->request->param('uri');
371    my $title = $self->request->param('title');
372    my @tags  = split ' ', $self->request->param('tags');
373
374    my $bookmark = $self->bookmarks->add({
[5]375        uri   => $uri,
376        title => $title,
377        tags  => \@tags,
378    });
379
[59]380    #TODO: not RESTful; the proper RESTful response would be a 201
381    return [303, ['Location' => $bookmark->bookmark_uri->canonical], []];
[5]382}
383
[45]384sub edit {
385    my $self = shift;
[59]386    my $id = shift;
[45]387
[59]388    my $bookmark = $self->bookmarks->get_bookmark({ id => $id });
[45]389    if ($bookmark) {
390        # update the URI, title, and tags
[59]391        $bookmark->uri($self->request->param('uri'));
392        $bookmark->title($self->request->param('title'));
393        $bookmark->tags([ split ' ', $self->request->param('tags') || '' ]);
[45]394
[46]395        # write to the database
[59]396        $self->bookmarks->update($bookmark);
[46]397
[59]398        #TODO: not RESTful; proper response would be a 200
399        return [303, ['Location' => $bookmark->bookmark_uri->canonical], []];
[45]400    } else {
[59]401        return [404, ['Content-Type' => 'text/plain; charset=UTF-8'], ["Boomark $id not found"]];
[45]402    }
403}
404
[5]4051;
Note: See TracBrowser for help on using the repository browser.