Add post deletion

Co-authored-by: vovocka <22993408+veselcraft@users.noreply.github.com>
This commit is contained in:
Jill Stingray 2020-06-18 14:22:32 +03:00
parent 026b9895b5
commit 20ba764f22
4 changed files with 25 additions and 6 deletions

View file

@ -72,6 +72,16 @@ class Post extends Postable
return $this->getRecord()->owner; return $this->getRecord()->owner;
} }
function canBeDeletedBy(User $user): bool
{
if($this->getTargetWall() < 0)
$cDel = (new Clubs)->get(abs($this->getTargetWall()))->canBeModifiedBy($user);
else
$cDel = $this->getTargetWall() === $user->getId();
return $this->getOwnerPost() === $user->getId() || $cDel;
}
function setContent(string $content): void function setContent(string $content): void
{ {
if(ctype_space($content)) if(ctype_space($content))

View file

@ -295,9 +295,15 @@ final class WallPresenter extends OpenVKPresenter
if(!$post) if(!$post)
$this->notFound(); $this->notFound();
$user = $this->user->id; $user = $this->user->id;
$wallOwner = ($wall > 0 ? (new Users)->get($wall) : (new Clubs)->get($wall * -1))
?? $this->flashFail("err", "Не удалось удалить пост", "Такого пользователя не существует.");
if($wall < 0) $canBeDeletedByOtherUser = $wallOwner->canBeModifiedBy($this->user->identity);
else $canBeDeletedByOtherUser = false;
if(!is_null($user)) { if(!is_null($user)) {
if($post->getOwnerPost() == $user || $post->getTargetWall() == $user) { if($post->getOwnerPost() == $user || $post->getTargetWall() == $user || $canBeDeletedByOtherUser) {
$post->unwire(); $post->unwire();
$post->delete(false); $post->delete(false);
} }
@ -305,7 +311,7 @@ final class WallPresenter extends OpenVKPresenter
$this->flashFail("err", "Не удалось удалить пост", "Вы не вошли в аккаунт."); $this->flashFail("err", "Не удалось удалить пост", "Вы не вошли в аккаунт.");
} }
$this->redirect($_SERVER["HTTP_REFERER"], static::REDIRECT_TEMPORARY); $this->redirect($wall < 0 ? "/club".($wall*-1) : "/id".$wall, static::REDIRECT_TEMPORARY);
exit; exit;
} }
} }

View file

@ -18,8 +18,11 @@
</div> </div>
<div style="float: left; min-height: 100px; width: 32%;"> <div style="float: left; min-height: 100px; width: 32%;">
<h4>Действия</h4> <h4>Действия</h4>
{if $post->getOwnerPost() == $thisUser->getId() || $post->getTargetWall() == $thisUser->getId()} {if isset($thisUser)}
<a class="profile_link" style="display:block;width:96%;" href="/wall{$post->getPrettyId()}/delete">Удалить</a> {var canDelete = $post->canBeDeletedBy($thisUser)}
{/if}
<a n:if="$canDelete ?? false" class="profile_link" style="display:block;width:96%;" href="/wall{$post->getPrettyId()}/delete">Удалить</a>
{/if} {/if}
<a class="profile_link" style="display:block;width:96%;" href="/report.pl/{$post->getId()}?type=post">Пожаловаться</a> <a class="profile_link" style="display:block;width:96%;" href="/report.pl/{$post->getId()}?type=post">Пожаловаться</a>
</div> </div>

View file

@ -61,7 +61,7 @@
</div> </div>
</div> </div>
<div n:if="isset($thisUser) &&! ($compact ?? false)" class="post-menu"> <div n:if="isset($thisUser) &&! ($compact ?? false)" class="post-menu">
{if ($post->getOwnerPost() == $thisUser->getId() || $post->getTargetWall() == $thisUser->getId()) && !($forceNoDeleteLink ?? false)} {if $post->canBeDeletedBy($thisUser) && !($forceNoDeleteLink ?? false)}
<a href="/wall{$post->getPrettyId()}/delete">{_"delete"}</a>&nbsp;|&nbsp; <a href="/wall{$post->getPrettyId()}/delete">{_"delete"}</a>&nbsp;|&nbsp;
{/if} {/if}