move the --config option to be global, also add a command to unsubscribe a
[p5sagit/Promulger.git] / lib / Promulger / Web.pm
CommitLineData
cefad154 1package Promulger::Web;
2use Web::Simple 'Promulger::Web';
3use autodie;
4
5use Promulger::List;
6
7use Method::Signatures::Simple;
8
9has list_home => (
10 is => 'ro',
11 default => sub { 'etc/lists' },
12 lazy => 1,
13);
14
15sub dispatch_request {
16 my ($self) = shift;
17 sub (GET + /list/) {
18 redispatch_to '/index';
19 },
20 sub (GET + /list) {
21 redispatch_to '/index';
22 },
23 sub (GET + /) {
24 redispatch_to '/index';
25 },
26 sub (GET + /index) {
27 my ($self) = @_;
28 [ 200, [ 'Content-type', 'text/html' ], [ $self->render_index(@_) ] ]
29 },
30 sub (GET + /list/*/) {
31 my ($self, $list) = @_;
32 redispatch_to "/list/${list}";
33 },
34 sub (GET + /list/*) {
35 my ($self, $list) = @_;
36 [ 200, [ 'Content-type', 'text/html' ], [ $self->show_list($list) ] ]
37 },
38 sub (GET + /list/*/subscriber/*) {
39 my ($self, $list, $subscriber) = @_;
40 [
41 200,
42 [ 'Content-type', 'text/html' ],
43 [ $self->show_subscriber($list, $subscriber) ]
44 ]
45 },
7e9dc6a6 46 sub (POST + /list/*/subscriber/*/unsubscribe) {
cefad154 47 my ($self, $list, $subscriber) = @_;
48 [
49 200,
50 [ 'Content-type', 'text/html' ],
51 [ $self->unsubscribe($list, $subscriber) ]
52 ]
53 },
54 sub (POST + /list/*/subscribe + %email=) {
55 my ($self, $list, $email) = @_;
56 [
57 200,
58 [ 'Content-type', 'text/html' ],
59 [ $self->subscribe($list, $email) ],
60 ]
61 },
62 sub () {
63 [ 405, [ 'Content-type', 'text/plain' ], [ 'Method not allowed' ] ]
64 }
65}
66
67method render_index {
68 my ($self) = @_;
69 my @lists = Promulger::List->get_lists;
70 my $html = <<HTML;
71<p>Promulger</p>
72
73<p>Lists:</p>
74<ul>
75HTML
76 for my $list (@lists) {
77 $html .= qq{<li><a href="/list/${list}">${list}</a></li>};
78 }
79 $html .= "</ul>";
80 return $html;
81}
82
83method show_list($list_name) {
84 my $list = Promulger::List->resolve($list_name);
85 my $name = $list->listname;
86 my $active = $list->active;
87 my @subscribers = keys %{ $list->subscribers };
88 my $html = <<"HTML";
89<p>List: ${name}</p>
90<p>Active: ${active}</p>
91<form method="POST" action="/list/${list_name}/subscribe">
92<input type="text" name="email">
93</form>
94<p>Subscribers:</p>
95<ul>
96HTML
97 for my $sub (@subscribers) {
98 $html .= qq{<li><a href="/list/${name}/subscriber/${sub}">${sub}</a></li>};
99 }
100 $html .= "</ul>";
101 return $html;
102}
103
104method subscribe($list_name, $email) {
105 my $list = Promulger::List->resolve($list_name);
106 $list->subscribe($email);
107 return "<p>Subscribed ${email} to ${list_name}.</p>";
108}
109
110method unsubscribe($list_name, $email) {
111 my $list = Promulger::List->resolve($list_name);
112 $list->unsubscribe($email);
113 return "<p>Unsubscribed ${email} from ${list_name}.</p>";
114}
115
116method show_subscriber($list_name, $subscriber) {
117 my $html = <<"HTML";
118<p>Subscriber ${subscriber}</p>
7e9dc6a6 119<form method="POST" action="/list/${list_name}/subscriber/${subscriber}/unsubscribe">
120<input type="submit" value="Unsubscribe">
121</form>
cefad154 122HTML
123}
124
1251;