When you like a repost, put likes on the entire hierarchy of repost posts recursively (#287)

This commit is contained in:
Maxim Leshchenko 2021-11-13 22:28:17 +02:00 committed by GitHub
parent 33f027b84b
commit 5c9d3c085e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 73 additions and 7 deletions

View file

@ -3,12 +3,34 @@ namespace openvk\Web\Models\Entities;
use Chandler\Database\DatabaseConnection as DB;
use openvk\Web\Models\Repositories\Clubs;
use openvk\Web\Models\RowModel;
use openvk\Web\Models\Entities\Notifications\LikeNotification;
class Post extends Postable
{
protected $tableName = "posts";
protected $upperNodeReferenceColumnName = "wall";
private function setLikeRecursively(bool $liked, User $user, int $depth): void
{
$searchData = [
"origin" => $user->getId(),
"model" => static::class,
"target" => $this->getRecord()->id,
];
if((sizeof(DB::i()->getContext()->table("likes")->where($searchData)) > 0) !== $liked) {
if($this->getOwner(false)->getId() !== $user->getId() && !($this->getOwner() instanceof Club))
(new LikeNotification($this->getOwner(false), $this, $user))->emit();
parent::setLike($liked, $user);
}
if($depth < ovkGetQuirk("wall.repost-liking-recursion-limit"))
foreach($this->getChildren() as $attachment)
if($attachment instanceof Post)
$attachment->setLikeRecursively($liked, $user, $depth + 1);
}
/**
* May return fake owner (group), if flags are [1, (*)]
*
@ -122,6 +144,25 @@ class Post extends Postable
$this->stateChanges("content", $content);
}
function toggleLike(User $user): bool
{
$liked = parent::toggleLike($user);
if($this->getOwner(false)->getId() !== $user->getId() && !($this->getOwner() instanceof Club))
(new LikeNotification($this->getOwner(false), $this, $user))->emit();
foreach($this->getChildren() as $attachment)
if($attachment instanceof Post)
$attachment->setLikeRecursively($liked, $user, 2);
return $liked;
}
function setLike(bool $liked, User $user): void
{
$this->setLikeRecursively($liked, $user, 1);
}
function deletePost(): void
{
$this->setDeleted(1);

View file

@ -96,17 +96,35 @@ abstract class Postable extends Attachable
yield (new Users)->get($like->origin);
}
function toggleLike(User $user): void
function toggleLike(User $user): bool
{
$searchData = [
"origin" => $user->getId(),
"model" => static::class,
"target" => $this->getRecord()->id,
];
if(sizeof(DB::i()->getContext()->table("likes")->where($searchData)) > 0)
if(sizeof(DB::i()->getContext()->table("likes")->where($searchData)) > 0) {
DB::i()->getContext()->table("likes")->where($searchData)->delete();
else
return false;
}
DB::i()->getContext()->table("likes")->insert($searchData);
return true;
}
function setLike(bool $liked, User $user): void
{
$searchData = [
"origin" => $user->getId(),
"model" => static::class,
"target" => $this->getRecord()->id,
];
if($liked)
DB::i()->getContext()->table("likes")->insert($searchData);
else
DB::i()->getContext()->table("likes")->where($searchData)->delete();
}
function hasLikeFrom(User $user): bool

View file

@ -1,7 +1,7 @@
<?php declare(strict_types=1);
namespace openvk\Web\Presenters;
use openvk\Web\Models\Entities\{Post, Photo, Video, Club, User};
use openvk\Web\Models\Entities\Notifications\{LikeNotification, RepostNotification, WallPostNotification};
use openvk\Web\Models\Entities\Notifications\{RepostNotification, WallPostNotification};
use openvk\Web\Models\Repositories\{Posts, Users, Clubs, Albums};
use Chandler\Database\DatabaseConnection;
use Nette\InvalidStateException as ISE;
@ -278,9 +278,6 @@ final class WallPresenter extends OpenVKPresenter
if(!is_null($this->user)) {
$post->toggleLike($this->user->identity);
if($post->getOwner(false)->getId() !== $this->user->identity->getId() && !($post->getOwner() instanceof Club))
(new LikeNotification($post->getOwner(false), $post, $this->user->identity))->emit();
}
$this->redirect(

View file

@ -34,3 +34,13 @@ blobs.erase-upon-deletion: 0
# + Set this option to 1 if you want to use quirky VK Mobile behaviour (yes graffiti in comments)
# + Set this option to 0 if you want to use VK Desktop behaviour (no graffiti in comments)
comments.allow-graffiti: 0
# Maximum recursion depth of likes on reposts
# Restriction on the recursion depth of repost likes.
# For example, if the value is 5, likes will be set for a maximum of 5 posts, even if there are more posts in the repost hierarchy.
# Needed to protect against attacks
#
# Possible values:
# + Set this option to -1 if you want to disable the limit
# + Set this option to any non-negative number to be this limit
wall.repost-liking-recursion-limit: 10