Changeset 406 for veekun/trunk/lib/Vee/Controller/Forum/Post.pm
- Timestamp:
- 02/08/08 02:44:39 (2 years ago)
- Files:
-
- 1 modified
-
veekun/trunk/lib/Vee/Controller/Forum/Post.pm (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
-
veekun/trunk/lib/Vee/Controller/Forum/Post.pm
r327 r406 51 51 sub utils : LocalRegex('^(\d*)/(delete|undelete|nuke)') : Args(0) { 52 52 my ( $self, $c ) = @_; 53 my ($post id, $method) = @{ $c->req->captures };54 55 my $forum id = undef;53 my ($post_id, $method) = @{ $c->req->captures }; 54 55 my $forum_id = undef; 56 56 # This is the post to be deleted, as well as the entire result set of things to be modified 57 my $post = $c->model('DBIC::Posts')->find({ 'me.id' => $post id }, { prefetch => ['user', { 'thread' => 'forum' }], })58 or $c->vee_abort("There is no post with id of ", $post id, ". Perhaps another admin nuked it?");59 $c->vee_abort("The post with id of ", $post id, " has already been ", $method, "d.")57 my $post = $c->model('DBIC::Posts')->find({ 'me.id' => $post_id }, { prefetch => ['user', { 'thread' => 'forum' }], }) 58 or $c->vee_abort("There is no post with id of ", $post_id, ". Perhaps another admin nuked it?"); 59 $c->vee_abort("The post with id of ", $post_id, " has already been ", $method, "d.") 60 60 if (($post->flags =~ /deleted/ && $method eq 'delete') || ($post->flags !~ /deleted/ && $method eq 'undelete')); 61 61 $c->vee_abort("You don't have permission to ", $method, " this post.") 62 unless (($c->user->obj->id == $post->user id && $method ne 'nuke') || $c->can_i("post$method"));62 unless (($c->user->obj->id == $post->user_id && $method ne 'nuke') || $c->can_i("post$method")); 63 63 64 64 # Do everything in a transaction for rollback purposes … … 66 66 my ($thread, $forum, $user) = ($post->thread, $post->thread->forum, $post->user); 67 67 # If any of the post (or thread) counts are 0 or less, this will break. Of course, if that's the case, something is broken 68 return undef unless (($forum->post ct >= 0 || $forum->threadct >= 0 || $thread->postct >= 0 || $user->postct >= 0) || ($method eq 'undelete'));69 my $thread_postct = $c->model('DBIC::Posts')->count({ 'me.thread id' => $post->threadid, 'me.flags' => { '!=', 'deleted' } });68 return undef unless (($forum->post_count >= 0 || $forum->thread_count >= 0 || $thread->post_count >= 0 || $user->post_count >= 0) || ($method eq 'undelete')); 69 my $thread_postct = $c->model('DBIC::Posts')->count({ 'me.thread_id' => $post->thread_id, 'me.flags' => { '!=', 'deleted' } }); 70 70 # Unfortunately, this must be done here, since this needs to include deleted posts as well 71 my ($last postid, $lasttime) = ((map {($_->id)} $c->model('DBIC::Posts')->search({ 'thread.forumid' => $forum->id, 'me.threadid' => $post->threadid }, { prefetch => 'thread', columns => [qw/me.id me.threadid thread.forumid/], order_by => 'me.time DESC' })), undef);72 73 if ($method eq 'nuke') { $c->model('DBIC::Posts')->search({ 'me.id' => $post id})->delete; }71 my ($last_post_id, $last_post_time) = ((map {($_->id)} $c->model('DBIC::Posts')->search({ 'thread.forum_id' => $forum->id, 'me.thread_id' => $post->thread_id }, { prefetch => 'thread', columns => [qw/me.id me.thread_id thread.forum_id/], order_by => 'me.time DESC' })), undef); 72 73 if ($method eq 'nuke') { $c->model('DBIC::Posts')->search({ 'me.id' => $post_id })->delete; } 74 74 else { toggleflag( $post, 'deleted' ) or return undef; } 75 75 76 76 # If there's only one post left, delete the thread as well 77 if ($thread_postct > 1 && $last postid == $post->id) {78 ($last time, $lastpostid) = map {($_->time, $_->id)} $c->model('DBIC::Posts')->search({ 'thread.forumid' => $forum->id, 'me.threadid' => $post->threadid, 'me.flags' => { '!=', 'deleted' } }, { prefetch => 'thread', rows => 1, order_by => 'me.time DESC' });77 if ($thread_postct > 1 && $last_post_id == $post->id) { 78 ($last_post_time, $last_post_id) = map {($_->time, $_->id)} $c->model('DBIC::Posts')->search({ 'thread.forum_id' => $forum->id, 'me.thread_id' => $post->thread_id, 'me.flags' => { '!=', 'deleted' } }, { prefetch => 'thread', rows => 1, order_by => 'me.time DESC' }); 79 79 } elsif ($thread_postct <= 1) { 80 80 #try moving this elsewhere later, i hate it here 81 if ($method eq 'nuke') { $c->model('DBIC::Threads')->search({ 'me.id' => $post->thread id })->delete; }81 if ($method eq 'nuke') { $c->model('DBIC::Threads')->search({ 'me.id' => $post->thread_id })->delete; } 82 82 elsif (($method eq 'undelete' && $thread_postct == 0) || ($method eq 'delete' && $thread_postct <= 1)) { 83 83 toggleflag( $thread, 'deleted' ) or return undef; 84 84 } 85 ($last time, $lastpostid) = map {($_->time, $_->id)} $c->model('DBIC::Posts')->search({ 'thread.forumid' => $forum->id, 'thread.flags' => { '!=', 'deleted' }, 'me.flags' => { '!=', 'deleted' } }, { prefetch => 'thread', rows => 1, order_by => 'me.time DESC' });86 $forum id = $forum->id;87 $last postid = 0 unless $lastpostid;85 ($last_post_time, $last_post_id) = map {($_->time, $_->id)} $c->model('DBIC::Posts')->search({ 'thread.forum_id' => $forum->id, 'thread.flags' => { '!=', 'deleted' }, 'me.flags' => { '!=', 'deleted' } }, { prefetch => 'thread', rows => 1, order_by => 'me.time DESC' }); 86 $forum_id = $forum->id; 87 $last_post_id = 0 unless $last_post_id; 88 88 } 89 89 90 90 if (($method eq 'nuke' && $post->flags !~ /deleted/) || ($method eq 'delete')) { 91 $thread->post ct($thread->postct - 1);92 $forum->post ct($forum->postct - 1);93 $forum->thread ct($forum->threadct - 1) unless ($thread_postct > 1);94 $user->post ct($user->postct - 1);91 $thread->post_count($thread->post_count - 1); 92 $forum->post_count($forum->post_count - 1); 93 $forum->thread_count($forum->thread_count - 1) unless ($thread_postct > 1); 94 $user->post_count($user->post_count - 1); 95 95 } elsif ($method eq 'undelete') { 96 $thread->post ct($thread->postct + 1);97 $forum->post ct($forum->postct + 1);98 $forum->thread ct($forum->threadct + 1) unless ($thread_postct > 1);99 $user->post ct($user->postct + 1);100 } 101 # Only update last post and lasttime if this is actually the last post102 if ($last time && $post->id == $thread->lastpostid) {103 $thread->last postid($lastpostid);104 $thread->last time($lasttime);96 $thread->post_count($thread->post_count + 1); 97 $forum->post_count($forum->post_count + 1); 98 $forum->thread_count($forum->thread_count + 1) unless ($thread_postct > 1); 99 $user->post_count($user->post_count + 1); 100 } 101 # Only update last post and time if this is actually the last post 102 if ($last_post_time && $post->id == $thread->last_post_id) { 103 $thread->last_post_id($last_post_id); 104 $thread->last_post_time($last_post_time); 105 105 } 106 106 # Needs to be done here, in case the deleted post is the last post in a forum :( 107 $forum->last postid($lastpostid) if $post->id == $forum->lastpostid;107 $forum->last_post_id($last_post_id) if $post->id == $forum->last_post_id; 108 108 $thread->update if ($method ne 'nuke'); 109 109 $forum->update; … … 114 114 115 115 $c->error("This error message should never be displayed unless someone is screwing with things; sorry.") unless $action; 116 $c->res->redirect("/forum/" . (($forum id) ? "$forumid" : "post/$postid"));116 $c->res->redirect("/forum/" . (($forum_id) ? "$forum_id" : "post/$post_id")); 117 117 } 118 118 … … 125 125 sub edits : LocalRegex('^(\d*)/edits') { 126 126 my ($self, $c) = @_; 127 my ($s, $post id) = ($c->stash, $c->req->captures->[0]);127 my ($s, $post_id) = ($c->stash, $c->req->captures->[0]); 128 128 129 129 $c->vee_abort("You don't have permission to view post edits.") unless ($c->can_i('post_edits')); 130 130 131 my $post = $c->model('DBIC::Posts')->find({ 'me.id' => $post id}, { prefetch => [qw/user edits/, { thread => 'forum' }], order_by => 'edits.time DESC' });131 my $post = $c->model('DBIC::Posts')->find({ 'me.id' => $post_id}, { prefetch => [qw/user edits/, { thread => 'forum' }], order_by => 'edits.time DESC' }); 132 132 133 133 $s->{post} = $post; … … 143 143 144 144 my $reply_fields = { 145 message=> { type => 'textarea', rows => '10', cols => '100' },145 content => { type => 'textarea', rows => '10', cols => '100' }, 146 146 id => { type => 'hidden' }, 147 147 }; … … 149 149 sub edit : LocalRegex('^(\d*)/edit') : Args(0) { 150 150 my ($self, $c) = @_; 151 my ($s, $post id) = ($c->stash, $c->req->captures->[0]);152 153 my $post_rs = $c->model('DBIC::Posts')->search({ 'me.id' => $post id }, { prefetch => 'user' })->single;151 my ($s, $post_id) = ($c->stash, $c->req->captures->[0]); 152 153 my $post_rs = $c->model('DBIC::Posts')->search({ 'me.id' => $post_id }, { prefetch => 'user' })->single; 154 154 $c->vee_abort("You must be logged in to edit posts.") unless $c->user; 155 $c->vee_abort("You don't have permission to edit posts.") unless ($c->can_i('post_edit') || $c->user->obj->id == $post_rs->user id);155 $c->vee_abort("You don't have permission to edit posts.") unless ($c->can_i('post_edit') || $c->user->obj->id == $post_rs->user_id); 156 156 157 157 # Form submited stuff 158 158 if ('post' eq lc $c->req->params->{submit}) { 159 my ($parsed_message, @bbcode_errors) = Vee::BBCode::validate_bbcode( $c->req->params->{ message} );159 my ($parsed_message, @bbcode_errors) = Vee::BBCode::validate_bbcode( $c->req->params->{content} ); 160 160 if (@bbcode_errors) { 161 161 $c->vee_abort("Your post contains invalid bbcode. Please go back and fix it."); … … 164 164 165 165 # If post wasn't edited, don't do anything further; results in redirect only 166 unless ($parsed_message eq $post_rs-> message) {166 unless ($parsed_message eq $post_rs->content) { 167 167 my $post = $c->model('DBIC')->schema->txn_do( sub { 168 168 my $edit = $c->model('DBIC::Edits')->create({ 169 post id => $post_rs->id,170 user id => $c->user->obj->id,171 time => time,172 old message => $post_rs->message,169 post_id => $post_rs->id, 170 user_id => $c->user->obj->id, 171 time => time, 172 old_content => $post_rs->content, 173 173 }); 174 174 175 $post_rs->last editid( $edit->id );176 $post_rs-> message( $parsed_message);175 $post_rs->last_edit_id( $edit->id ); 176 $post_rs->content($parsed_message); 177 177 $post_rs->update; 178 178 … … 189 189 copy_params => 1, 190 190 ); 191 $form->force( id => $post_rs->thread id );192 $form->force( message => $c->req->params->{message} || $post_rs->message|| '' );191 $form->force( id => $post_rs->thread_id ); 192 $form->force( content => $c->req->params->{content} || $post_rs->content || '' ); 193 193 194 194 $s->{page_header} = "Edit Post";
