diff --git a/II/DB.pm b/II/DB.pm index b060d42..b96e6fe 100644 --- a/II/DB.pm +++ b/II/DB.pm @@ -24,13 +24,13 @@ sub check_hash { my ( $self, $hash, $echo ) = @_; my $dbh = $self->{_dbh}; - my $q = "select hash from echo where hash='$hash' and echo='$echo'"; + my $q = "select hash from echo where hash='$hash' and echo='$echo'"; my $sth = $dbh->prepare($q); $sth->execute(); while ( my @h = $sth->fetchrow_array() ) { my ($base_hash) = @h; - if ($hash eq $base_hash) { + if ( $hash eq $base_hash ) { return 1; } else { @@ -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 { my ( $self, $config ) = @_; my $dbh = $self->{_dbh}; diff --git a/II/Render.pm b/II/Render.pm index 1808e9d..d970cdf 100644 --- a/II/Render.pm +++ b/II/Render.pm @@ -74,7 +74,7 @@ sub echo_mes { $render .= $t->echo($echo); my $count = 0; - if ($view eq 'thread') { + if ( $view eq 'thread' ) { while ( $count < @post ) { # Render post @@ -151,6 +151,29 @@ sub index { 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 sub send_new { my ( $self, $echo ) = @_; diff --git a/iiplc.app b/iiplc.app index b14b622..8120e06 100644 --- a/iiplc.app +++ b/iiplc.app @@ -1,3 +1,4 @@ +#!/usr/bin/perl # Copyright © 2014 Difrex # 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, @@ -7,7 +8,7 @@ # the extent permitted by applicable law. You can redistribute it # and/or modify it under the terms of the Do What The Fuck You Want # To Public License, Version 2, as published by Sam Hocevar. See -# http://www.wtfpl.net/ for more details. +# http://www.wtfpl.net/ for more details. use strict; use warnings; @@ -40,7 +41,7 @@ my $echo = sub { my $echo = $req->param('echo'); my $view = $req->param('view'); - my $echo_messages = $render->echo_mes($echo, $view); + my $echo_messages = $render->echo_mes( $echo, $view ); return [ 200, [ 'Content-type' => 'text/html' ], ["$echo_messages"], ]; }; @@ -154,10 +155,22 @@ my $push = sub { 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 { mount '/' => $root; mount '/e' => $echo; mount '/s' => $thread; + mount '/u' => $user; mount '/me' => $me; mount '/tree' => $tree; mount '/get/' => $get;