|
| 1 | +package OpenNewsWire::Web::Controller::UserChannel; |
| 2 | +use Moose; |
| 3 | +use namespace::autoclean; |
| 4 | + |
| 5 | +BEGIN { extends 'Catalyst::Controller' } |
| 6 | + |
| 7 | +sub base :Chained('/base') PathPart('u') CaptureArgs(1) { |
| 8 | + my ( $self, $c, $profile_name ) = @_; |
| 9 | + |
| 10 | + # Is there a valid user logged in? - If not, send them to the login page. |
| 11 | + if ( ! $c->stash->{user} ) { |
| 12 | + $c->res->redirect( $c->uri_for_action('/get_login') ); |
| 13 | + $c->detach; |
| 14 | + } |
| 15 | + |
| 16 | + my $user = $c->model('DB')->resultset('Person')->search({ name => $profile_name })->first; |
| 17 | + |
| 18 | + $c->stash->{user_profile} = $user; |
| 19 | + # TODO: Throw an error when there is no user. |
| 20 | +} |
| 21 | + |
| 22 | +sub get_user :Chained('base') PathPart('') Args(0) Method('GET') { |
| 23 | + my ( $self, $c ) = @_; |
| 24 | + $c->stash->{template} = 'user/show_user.tx'; |
| 25 | + |
| 26 | + push @{$c->stash->{messages}}, |
| 27 | + $c->model('DB')->resultset('UserChannelMessage')->search({author_id => $c->stash->{user_profile}->id})->all; |
| 28 | +} |
| 29 | + |
| 30 | +sub get_user_submissions :Chained('base') PathPart('submissions') Args(0) Method('GET') { |
| 31 | + my ( $self, $c, ) = @_; |
| 32 | + $c->stash->{template} = 'user/show_user_submissions.tx'; |
| 33 | + |
| 34 | + push @{$c->stash->{messages}}, |
| 35 | + $c->model('DB')->resultset('TopicChannelMessage')->search({author_id => $c->stash->{user_profile}->id})->all; |
| 36 | +} |
| 37 | + |
| 38 | +sub get_user_comments :Chained('base') PathPart('comments) Args(0) Method('GET') { |
| 39 | + my ( $self, $c, ) = @_; |
| 40 | + $c->stash->{template} = 'user/show_user_comments.tx'; |
| 41 | + |
| 42 | + push @{$c->stash->{messages}}, |
| 43 | + $c->model('DB')->resultset('Message')->search({ |
| 44 | + author_id => $c->stash->{user_profile}->id, |
| 45 | + parent_id => { '!=' => [ 0, undef ] }, |
| 46 | + })->all; |
| 47 | +} |
| 48 | +
|
| 49 | +sub get_user_message :Chained('base') PathPart('') Args(2) Method('GET') { |
| 50 | + my ( $self, $c, $message_id, $message_slug ) = @_; |
| 51 | + $c->stash->{template} = 'user/show_user_message.tx'; |
| 52 | + |
| 53 | + my $message = $c->model('DB')->resultset('Message')->find( $message_id ); |
| 54 | +
|
| 55 | + # TODO Check for errors: |
| 56 | + # 1. user_profile.id != message.author_id |
| 57 | + # 2. ! user |
| 58 | + # 3. ! message |
| 59 | +
|
| 60 | + $c->stash->{message} = $message; |
| 61 | + $c->stash->{comments} = $self->_get_message_children( $c, $message->id ); |
| 62 | +} |
| 63 | +
|
| 64 | +sub _get_message_children { |
| 65 | + my ( $self, $c, $parent_id ) = @_; |
| 66 | +
|
| 67 | + my @results; |
| 68 | +
|
| 69 | + my $search_rs = $c->model('DB')->resultset('Message')->search({ parent_id => $parent_id } ); |
| 70 | + while ( my $message = $search_rs->next ) { |
| 71 | + push @results, { |
| 72 | + message => { |
| 73 | + content => $message->content, |
| 74 | + id => $message->id, |
| 75 | + created => $message->created_at->strftime( '%F %T' ), |
| 76 | + time_ago => $message->time_ago, |
| 77 | + }, |
| 78 | + author => { |
| 79 | + name => $message->author->name, |
| 80 | + id => $message->author->id, |
| 81 | + }, |
| 82 | + children => $self->_get_message_children( $c, $message->id ), |
| 83 | + }; |
| 84 | + } |
| 85 | +
|
| 86 | + return [ @results ]; |
| 87 | +} |
| 88 | +
|
| 89 | +__PACKAGE__->meta->make_immutable; |
0 commit comments