basic (non-AJAX) chat implemented
[chaz/chatty] / lib / Chatty / Controller / Chat.pm
1 package Chatty::Controller::Chat;
2 use Moose;
3 use namespace::autoclean;
4
5 BEGIN { extends 'Catalyst::Controller' }
6
7 #__PACKAGE__->config(namespace => 'room');
8
9 use Chatty::Form::RoomCreate;
10 use Chatty::Form::MessageCreate;
11
12 has 'roomcreate_form' => (
13 isa => 'Chatty::Form::RoomCreate',
14 is => 'rw',
15 lazy => 1,
16 default => sub { Chatty::Form::RoomCreate->new }
17 );
18
19 has 'messagecreate_form' => (
20 isa => 'Chatty::Form::MessageCreate',
21 is => 'rw',
22 lazy => 1,
23 default => sub { Chatty::Form::MessageCreate->new }
24 );
25
26 =head1 NAME
27
28 Chatty::Controller::Chat - Catalyst Controller
29
30 =head1 DESCRIPTION
31
32 Catalyst Controller.
33
34 =head1 METHODS
35
36 =head2 room
37
38 Base action for chat rooms. Sets up the model.
39
40 =cut
41
42 sub room :Chained(/) :CaptureArgs(0) {
43 my ($self, $c) = @_;
44 $c->detach('/access_denied') if !$c->user_exists;
45 }
46
47 =head2 list
48
49 List the current list of chat rooms.
50
51 =cut
52
53 sub list :Chained(room) :Args(0) {
54 my ($self, $c) = @_;
55
56 $c->stash(rooms => [$c->model('DB::Room')->all]);
57
58 my $form = Chatty::Form::RoomCreate->new(action =>
59 $c->uri_for_action('/chat/create'));
60 $c->stash(form => $form);
61 }
62
63 =head2 create
64
65 Create a new chat room.
66
67 =cut
68
69 sub create :Chained(room) :Args(0) {
70 my ($self, $c) = @_;
71
72 $c->stash(form => $self->roomcreate_form);
73
74 my $new_room = $c->model('DB::Room')->new_result({});
75 $self->roomcreate_form->process(
76 item => $new_room,
77 params => $c->req->params
78 );
79
80 if (!$self->roomcreate_form->is_valid) {
81 if ($c->req->method eq 'POST') {
82 $c->stash->{error} = "The form has a validation error. Try again...";
83 }
84 return;
85 }
86
87 $c->flash->{message} = "Your new room was created!";
88 $c->res->redirect($c->uri_for_action('/chat/view', $new_room->id));
89 }
90
91 =head2 view
92
93 View a chat room.
94
95 =cut
96
97 sub view :Chained(room) :PathPart('') :Args(1) {
98 my ($self, $c, $room) = @_;
99
100 $c->stash(room => $c->model('DB::Room')->find($room));
101 $c->detach('/missing') if !$c->stash->{room};
102
103 $c->stash(messages => [$c->model('DB::Message')->search(room => $room)]);
104
105 $c->stash(form => $self->messagecreate_form);
106
107 my $new_message = $c->model('DB::Message')->new_result({
108 author => $c->user->obj->id,
109 room => $c->stash->{room}->id
110 });
111 $self->messagecreate_form->process(
112 item => $new_message,
113 params => $c->req->params
114 );
115
116 if (!$self->messagecreate_form->is_valid) {
117 if ($c->req->method eq 'POST') {
118 $c->stash->{error} = "The form has a validation error. Try again...";
119 }
120 return;
121 }
122
123 $c->res->redirect($c->uri_for_action('/chat/view', $c->stash->{room}->id));
124 }
125
126 =head1 AUTHOR
127
128 Charles McGarvey
129
130 =head1 LICENSE
131
132 This library is free software. You can redistribute it and/or modify
133 it under the same terms as Perl itself.
134
135 =cut
136
137 __PACKAGE__->meta->make_immutable;
138
139 1;
This page took 0.04495 seconds and 5 git commands to generate.