]> Dogcows Code - chaz/chatty/blob - lib/Chatty/Controller/Chat.pm
add comet support for real-time chatting
[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
11 has 'roomcreate_form' => (
12 isa => 'Chatty::Form::RoomCreate',
13 is => 'rw',
14 lazy => 1,
15 default => sub { Chatty::Form::RoomCreate->new }
16 );
17
18 =head1 NAME
19
20 Chatty::Controller::Chat - Catalyst Controller
21
22 =head1 DESCRIPTION
23
24 Catalyst Controller.
25
26 =head1 METHODS
27
28 =head2 room
29
30 Base action for chat rooms. Sets up the model.
31
32 =cut
33
34 sub room :Chained(/) :CaptureArgs(0) {
35 my ($self, $c) = @_;
36 $c->detach('/access_denied') if !$c->user_exists;
37 }
38
39 =head2 list
40
41 List the current list of chat rooms.
42
43 =cut
44
45 sub list :Chained(room) :Args(0) {
46 my ($self, $c) = @_;
47
48 $c->stash(rooms => [$c->model('DB::Room')->all]);
49
50 my $form = Chatty::Form::RoomCreate->new(action =>
51 $c->uri_for_action('/chat/create'));
52 $c->stash(form => $form);
53 }
54
55 =head2 create
56
57 Create a new chat room.
58
59 =cut
60
61 sub create :Chained(room) :Args(0) {
62 my ($self, $c) = @_;
63
64 $c->stash(form => $self->roomcreate_form);
65
66 my $new_room = $c->model('DB::Room')->new_result({});
67 $self->roomcreate_form->process(
68 item => $new_room,
69 params => $c->req->params
70 );
71
72 if (!$self->roomcreate_form->is_valid) {
73 if ($c->req->method eq 'POST') {
74 $c->stash->{error} = "The form has a validation error. Try again...";
75 }
76 return;
77 }
78
79 $c->flash->{message} = "Your new room was created!";
80 $c->res->redirect($c->uri_for_action('/chat/view', $new_room->id));
81 }
82
83 =head2 view
84
85 View a chat room.
86
87 =cut
88
89 sub view :Chained(room) :PathPart('') :Args(1) {
90 my ($self, $c, $room) = @_;
91
92 $c->stash(room => $c->model('DB::Room')->find($room));
93 $c->detach('/missing') if !$c->stash->{room};
94
95 my $name = $c->user->obj->username;
96
97 my $msg = $c->req->param('msg');
98 if ($msg) {
99 $c->model('Meteor')->addMessage($room, "$name: $msg");
100 $c->stash->{json} = \1;
101 $c->forward('View::JSON');
102 return;
103 }
104
105 $c->model('Meteor')->addMessage($room, "** $name has entered **");
106 }
107
108 =head1 AUTHOR
109
110 Charles McGarvey
111
112 =head1 LICENSE
113
114 This library is free software. You can redistribute it and/or modify
115 it under the same terms as Perl itself.
116
117 =cut
118
119 __PACKAGE__->meta->make_immutable;
120
121 1;
This page took 0.034086 seconds and 4 git commands to generate.