Skip to content

Commit 5b3245e

Browse files
committed
UserChannel support.
1 parent 9fa1d18 commit 5b3245e

File tree

15 files changed

+515
-11
lines changed

15 files changed

+515
-11
lines changed

Database/etc/schema.sql

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ CREATE TABLE message (
3333
author_id int not null references person(id),
3434
parent_id int references message(id),
3535
title text ,
36-
content text not null,
36+
content text ,
3737
url text ,
3838
created_at timestamptz not null default current_timestamp
3939
);
@@ -75,3 +75,12 @@ create TABLE topic_channel_message (
7575
is_stickied boolean not null default false,
7676
created_at timestamptz not null default current_timestamp
7777
);
78+
79+
create TABLE user_channel_message (
80+
id serial PRIMARY KEY,
81+
author_id int not null references person(id),
82+
message_id int not null references message(id),
83+
is_archived boolean not null default false,
84+
is_stickied boolean not null default false,
85+
created_at timestamptz not null default current_timestamp
86+
);

Database/lib/OpenNewsWire/DB/Result/Message.pm

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ __PACKAGE__->table("message");
6464
=head2 content
6565
6666
data_type: 'text'
67-
is_nullable: 0
67+
is_nullable: 1
6868
6969
=head2 url
7070
@@ -94,7 +94,7 @@ __PACKAGE__->add_columns(
9494
"title",
9595
{ data_type => "text", is_nullable => 1 },
9696
"content",
97-
{ data_type => "text", is_nullable => 0 },
97+
{ data_type => "text", is_nullable => 1 },
9898
"url",
9999
{ data_type => "text", is_nullable => 1 },
100100
"created_at",
@@ -199,9 +199,24 @@ __PACKAGE__->has_many(
199199
{ cascade_copy => 0, cascade_delete => 0 },
200200
);
201201

202+
=head2 user_channel_messages
203+
204+
Type: has_many
205+
206+
Related object: L<OpenNewsWire::DB::Result::UserChannelMessage>
207+
208+
=cut
209+
210+
__PACKAGE__->has_many(
211+
"user_channel_messages",
212+
"OpenNewsWire::DB::Result::UserChannelMessage",
213+
{ "foreign.message_id" => "self.id" },
214+
{ cascade_copy => 0, cascade_delete => 0 },
215+
);
216+
202217

203-
# Created by DBIx::Class::Schema::Loader v0.07049 @ 2021-09-08 14:30:18
204-
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:UqCL3m0DEMgcjDA73SZk2w
218+
# Created by DBIx::Class::Schema::Loader v0.07049 @ 2021-09-10 04:43:57
219+
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:lKrkAKr/ER66sdet2buqcg
205220

206221
sub time_ago {
207222
my ( $self ) = @_;

Database/lib/OpenNewsWire/DB/Result/Person.pm

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,24 @@ __PACKAGE__->has_many(
208208
{ cascade_copy => 0, cascade_delete => 0 },
209209
);
210210

211+
=head2 user_channel_messages
211212
212-
# Created by DBIx::Class::Schema::Loader v0.07049 @ 2021-09-08 14:30:18
213-
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:irY/GBQ/r8VIJXqXiJpL2Q
213+
Type: has_many
214+
215+
Related object: L<OpenNewsWire::DB::Result::UserChannelMessage>
216+
217+
=cut
218+
219+
__PACKAGE__->has_many(
220+
"user_channel_messages",
221+
"OpenNewsWire::DB::Result::UserChannelMessage",
222+
{ "foreign.author_id" => "self.id" },
223+
{ cascade_copy => 0, cascade_delete => 0 },
224+
);
225+
226+
227+
# Created by DBIx::Class::Schema::Loader v0.07049 @ 2021-09-10 04:43:57
228+
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:KCmYIsQsxa9tL8u5tCntEQ
214229

215230
sub setting {
216231
my ( $self, $setting, $value ) = @_;
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
use utf8;
2+
package OpenNewsWire::DB::Result::UserChannelMessage;
3+
4+
# Created by DBIx::Class::Schema::Loader
5+
# DO NOT MODIFY THE FIRST PART OF THIS FILE
6+
7+
=head1 NAME
8+
9+
OpenNewsWire::DB::Result::UserChannelMessage
10+
11+
=cut
12+
13+
use strict;
14+
use warnings;
15+
16+
use base 'DBIx::Class::Core';
17+
18+
=head1 COMPONENTS LOADED
19+
20+
=over 4
21+
22+
=item * L<DBIx::Class::InflateColumn::DateTime>
23+
24+
=item * L<DBIx::Class::InflateColumn::Serializer>
25+
26+
=back
27+
28+
=cut
29+
30+
__PACKAGE__->load_components("InflateColumn::DateTime", "InflateColumn::Serializer");
31+
32+
=head1 TABLE: C<user_channel_message>
33+
34+
=cut
35+
36+
__PACKAGE__->table("user_channel_message");
37+
38+
=head1 ACCESSORS
39+
40+
=head2 id
41+
42+
data_type: 'integer'
43+
is_auto_increment: 1
44+
is_nullable: 0
45+
sequence: 'user_channel_message_id_seq'
46+
47+
=head2 author_id
48+
49+
data_type: 'integer'
50+
is_foreign_key: 1
51+
is_nullable: 0
52+
53+
=head2 message_id
54+
55+
data_type: 'integer'
56+
is_foreign_key: 1
57+
is_nullable: 0
58+
59+
=head2 is_archived
60+
61+
data_type: 'boolean'
62+
default_value: false
63+
is_nullable: 0
64+
65+
=head2 is_stickied
66+
67+
data_type: 'boolean'
68+
default_value: false
69+
is_nullable: 0
70+
71+
=head2 created_at
72+
73+
data_type: 'timestamp with time zone'
74+
default_value: current_timestamp
75+
is_nullable: 0
76+
77+
=cut
78+
79+
__PACKAGE__->add_columns(
80+
"id",
81+
{
82+
data_type => "integer",
83+
is_auto_increment => 1,
84+
is_nullable => 0,
85+
sequence => "user_channel_message_id_seq",
86+
},
87+
"author_id",
88+
{ data_type => "integer", is_foreign_key => 1, is_nullable => 0 },
89+
"message_id",
90+
{ data_type => "integer", is_foreign_key => 1, is_nullable => 0 },
91+
"is_archived",
92+
{ data_type => "boolean", default_value => \"false", is_nullable => 0 },
93+
"is_stickied",
94+
{ data_type => "boolean", default_value => \"false", is_nullable => 0 },
95+
"created_at",
96+
{
97+
data_type => "timestamp with time zone",
98+
default_value => \"current_timestamp",
99+
is_nullable => 0,
100+
},
101+
);
102+
103+
=head1 PRIMARY KEY
104+
105+
=over 4
106+
107+
=item * L</id>
108+
109+
=back
110+
111+
=cut
112+
113+
__PACKAGE__->set_primary_key("id");
114+
115+
=head1 RELATIONS
116+
117+
=head2 author
118+
119+
Type: belongs_to
120+
121+
Related object: L<OpenNewsWire::DB::Result::Person>
122+
123+
=cut
124+
125+
__PACKAGE__->belongs_to(
126+
"author",
127+
"OpenNewsWire::DB::Result::Person",
128+
{ id => "author_id" },
129+
{ is_deferrable => 0, on_delete => "NO ACTION", on_update => "NO ACTION" },
130+
);
131+
132+
=head2 message
133+
134+
Type: belongs_to
135+
136+
Related object: L<OpenNewsWire::DB::Result::Message>
137+
138+
=cut
139+
140+
__PACKAGE__->belongs_to(
141+
"message",
142+
"OpenNewsWire::DB::Result::Message",
143+
{ id => "message_id" },
144+
{ is_deferrable => 0, on_delete => "NO ACTION", on_update => "NO ACTION" },
145+
);
146+
147+
148+
# Created by DBIx::Class::Schema::Loader v0.07049 @ 2021-09-10 04:43:57
149+
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:EgmPWfUdt5Udv66wWF1Ihw
150+
151+
152+
# You can replace this text with custom code or comments, and it will be preserved on regeneration
153+
1;

Web/lib/OpenNewsWire/Web/Controller/Create.pm

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ sub create_message :Chained('base') PathPart('message') Args(0) Method('POST') {
5858
my $msg_id = $c->req->params->{message_id};
5959
my $msg_slug = $c->req->params->{message_slug};
6060
my $chan_name = $c->req->params->{channel_name};
61+
my $self_post = 0;
6162

6263
$c->stash(
6364
form_to => $target,
@@ -83,10 +84,24 @@ sub create_message :Chained('base') PathPart('message') Args(0) Method('POST') {
8384
channel_id => $channel_id,
8485
});
8586
# 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+
( $msg_id, $msg_slug, $chan_name ) = ( $message->id, $message->slug, $name );
88+
} elsif ( $mode eq 'u' ) {
89+
# Self-Post
90+
if ( $name eq $c->stash->{user}->name ) {
91+
$c->model('DB')->resultset('UserChannelMessage')->create({
92+
author_id => $c->stash->{user}->id,
93+
message_id => $message->id,
94+
});
95+
# Set the variables so that the user is redirected to the new message post..
96+
( $msg_id, $msg_slug, $self_post ) = ( $message->id, $message->slug, 1 );
97+
}
8798
}
8899
});
89100

101+
if ( $self_post ) {
102+
$c->res->redirect( $c->uri_for_action( '/userchannel/get_user_message', [], $c->stash->{user}->name, $msg_id, $msg_slug ) );
103+
}
104+
90105
if ( $msg_id && $msg_slug && $chan_name ) {
91106
$c->res->redirect( $c->uri_for_action( '/topicchannel/get_topic_message', [], $chan_name, $msg_id, $msg_slug ) );
92107
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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;

Web/root/topic/_show_topic_message_comment.tx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<div class="card my-4">
22
<div class="card-body">
33
<div class="">
4-
<a href="[% $c.uri_for_action( '/userchannel/get_user', [], $comment.message.author.name ) %]">u/[% $comment.author.name %]</a>
4+
<a href="[% $c.uri_for_action( '/userchannel/get_user', [ $comment.author.name ] ) %]">u/[% $comment.author.name %]</a>
55
<small><span title="[% $comment.message.created %]">[% $comment.message.time_ago %]</span></small>
66
</div>
77
<div class="pt-4">

Web/root/topic/show_topic.tx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@
2020
%% }
2121
<br />
2222
<a href="#" class="card-link">Comments ([% $message.message.comment_count %])</a>
23-
<a class="card-link" href="[% $c.uri_for_action( '/userchannel/get_user', [], $message.author.name ) %]">u/[% $message.author.name %]</a>
23+
<a class="card-link" href="[% $c.uri_for_action( '/userchannel/get_user', [ $message.author.name ] ) %]">u/[% $message.author.name %]</a>
2424
<small><span title="[% $message.message.created_at.strftime('%Y %T') %]">[% $message.message.time_ago %]</span></small>
2525
</div>
2626
</div>
2727
%% }
28-
2928
%% }

0 commit comments

Comments
 (0)