Messages from user

This commit is contained in:
Difrex(Denis Zheleztsov) 2014-06-24 11:31:20 +04:00
parent d592353384
commit 568a2a53ff
3 changed files with 70 additions and 5 deletions

View File

@ -153,6 +153,35 @@ sub select_subg {
} }
# Select user messages
sub select_user {
my ( $self, $user ) = @_;
my $dbh = $self->{_dbh};
my $q
= "select from_user, to_user, subg, time, echo, post, hash from messages where from_user='$user' order by time desc";
my $sth = $dbh->prepare($q);
$sth->execute();
my @posts;
while ( my @hash = $sth->fetchrow_array() ) {
my ( $from, $to, $subg, $time, $echo, $post, $h ) = @hash;
my $data = {
from => "$from",
to => "$to",
subg => "$subg",
time => $time,
echo => "$echo",
post => "$post",
hash => $h,
};
push( @posts, $data );
}
return @posts;
}
sub from_me { sub from_me {
my ( $self, $config ) = @_; my ( $self, $config ) = @_;
my $dbh = $self->{_dbh}; my $dbh = $self->{_dbh};

View File

@ -151,6 +151,29 @@ sub index {
return $render; return $render;
} }
# Messages from user
sub user {
my ( $self, $user ) = @_;
my $db = $self->{_db};
my $t = $self->{_template};
# Render header
my $render
= $t->head("ii :: Сообщения пользователя $user");
my @post = $db->select_user($user);
my $count = 0;
while ( $count < @post ) {
# Render post
$render .= $t->post( @post[$count] );
$count++;
}
$render .= $t->foot();
}
# Render new message form # Render new message form
sub send_new { sub send_new {
my ( $self, $echo ) = @_; my ( $self, $echo ) = @_;

View File

@ -1,3 +1,4 @@
#!/usr/bin/perl
# Copyright © 2014 Difrex <difrex.punk@gmail.com> # Copyright © 2014 Difrex <difrex.punk@gmail.com>
# This work is free. You can redistribute it and/or modify it under the # This work is free. You can redistribute it and/or modify it under the
# terms of the Do What The Fuck You Want To Public License, Version 2, # terms of the Do What The Fuck You Want To Public License, Version 2,
@ -154,10 +155,22 @@ my $push = sub {
return [ 302, [ 'Location' => "/e?echo=$echo" ], [], ]; return [ 302, [ 'Location' => "/e?echo=$echo" ], [], ];
}; };
# Messages from user
my $user = sub {
my $env = shift;
my $req = Plack::Request->new($env);
my $user = $req->param('user');
my $mes_from = $render->user($user);
return [ 200, [ 'Content-type' => 'text/html' ], [$mes_from], ];
};
builder { builder {
mount '/' => $root; mount '/' => $root;
mount '/e' => $echo; mount '/e' => $echo;
mount '/s' => $thread; mount '/s' => $thread;
mount '/u' => $user;
mount '/me' => $me; mount '/me' => $me;
mount '/tree' => $tree; mount '/tree' => $tree;
mount '/get/' => $get; mount '/get/' => $get;