|
| 1 | +package OpenNewsWire::Web::Controller::Create; |
| 2 | +use Moose; |
| 3 | +use namespace::autoclean; |
| 4 | + |
| 5 | +BEGIN { extends 'Catalyst::Controller' } |
| 6 | + |
| 7 | +sub base :Chained('/base') PathPart('create') CaptureArgs(0) { |
| 8 | + my ( $self, $c ) = @_; |
| 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 | + |
| 17 | +sub show_create_topic :Chained('base') PathPart('topic') Args(0) Method('GET') { |
| 18 | + my ( $self, $c ) = @_; |
| 19 | + |
| 20 | + $c->stash->{template} = 'create/topic.tx'; |
| 21 | +} |
| 22 | + |
| 23 | +sub create_topic :Chained('base') PathPart('topic') Args(0) Method('POST') { |
| 24 | + my ( $self, $c ) = @_; |
| 25 | + |
| 26 | + my $name = $c->req->params->{topic_name}; |
| 27 | + |
| 28 | + # TODO: Validate name |
| 29 | + # * Make sure it follows the rules (what are the rules?) |
| 30 | + # * Doesn't already exist) |
| 31 | + |
| 32 | + $c->stash->{user}->create_related( 'topic_channels', { |
| 33 | + name => $name, |
| 34 | + }); |
| 35 | + |
| 36 | + $c->res->redirect( $c->uri_for_action( '/topicchannel/get_topic', [], $name ) ); |
| 37 | +} |
| 38 | + |
| 39 | +sub show_create_message :Chained('base') PathPart('message') Args(0) Method('GET') { |
| 40 | + my ( $self, $c ) = @_; |
| 41 | + |
| 42 | + if ( $c->req->params->{topic} ) { |
| 43 | + $c->stash->{form_to} = 't/' . $c->req->params->{topic}; |
| 44 | + } |
| 45 | + $c->stash->{template} = 'create/message.tx'; |
| 46 | +} |
| 47 | + |
| 48 | +sub create_message :Chained('base') PathPart('message') Args(0) Method('POST') { |
| 49 | + my ( $self, $c ) = @_; |
| 50 | + |
| 51 | + $c->stash->{template} = 'create/message.tx'; |
| 52 | + |
| 53 | + my $target = $c->req->params->{to}; |
| 54 | + my $title = $c->req->params->{title}; |
| 55 | + my $url = $c->req->params->{url}; |
| 56 | + my $content = $c->req->params->{message}; |
| 57 | + my $parent = $c->req->params->{reply_to}; |
| 58 | + my $msg_id = $c->req->params->{message_id}; |
| 59 | + my $msg_slug = $c->req->params->{message_slug}; |
| 60 | + my $chan_name = $c->req->params->{channel_name}; |
| 61 | + |
| 62 | + $c->stash( |
| 63 | + form_to => $target, |
| 64 | + form_title => $title, |
| 65 | + form_message => $content, |
| 66 | + ); |
| 67 | + |
| 68 | + $c->model('DB')->schema->txn_do( sub { |
| 69 | + my $message = $c->stash->{user}->create_related( 'messages', { |
| 70 | + ( $url ? ( url => $url ) : ( ) ), |
| 71 | + ( $title ? ( title => $title ) : ( ) ), |
| 72 | + ( $content ? ( content => $content ) : ( ) ), |
| 73 | + ( $parent ? ( parent_id => $parent ) : ( ) ), |
| 74 | + }); |
| 75 | + |
| 76 | + my ( $mode, $name ) = split( /\//, $target, 2 ); |
| 77 | + |
| 78 | + if ( $mode eq 't' ) { |
| 79 | + my $channel_id = $c->model('DB')->resultset('TopicChannel')->search({ name => $name })->first->id; |
| 80 | + $c->model('DB')->resultset('TopicChannelMessage')->create({ |
| 81 | + author_id => $c->stash->{user}->id, |
| 82 | + message_id => $message->id, |
| 83 | + channel_id => $channel_id, |
| 84 | + }); |
| 85 | + # Set the variables so that the user is redirected to the new message post.. |
| 86 | + ( $msg_id, $msg_slug, $chan_name ) = ( $message->id, $title, $name ); |
| 87 | + } |
| 88 | + }); |
| 89 | + |
| 90 | + if ( $msg_id && $msg_slug && $chan_name ) { |
| 91 | + $c->res->redirect( $c->uri_for_action( '/topicchannel/get_topic_message', [], $chan_name, $msg_id, $msg_slug ) ); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +__PACKAGE__->meta->make_immutable; |
0 commit comments