Added special:(dis)liked support to post searching

This commit is contained in:
Marcin Kurczewski 2014-10-03 14:39:27 +02:00
parent c021c9a1c0
commit 5db571ab7f
5 changed files with 46 additions and 5 deletions

2
TODO
View file

@ -12,8 +12,6 @@ everything related to posts:
- comment_count: 3..5 - comment_count: 3..5
- file_size:3K..5M - file_size:3K..5M
- image_size:huge/large/medium/small - image_size:huge/large/medium/small
- special:liked
- special:disliked
- special:fav - special:fav
- search order - search order
- order:comment_count - order:comment_count

View file

@ -112,7 +112,8 @@ class PostDao extends AbstractDao implements ICrudDao
elseif ($requirement->getType() === \Szurubooru\SearchServices\Filters\PostFilter::REQUIREMENT_FAVORITE) elseif ($requirement->getType() === \Szurubooru\SearchServices\Filters\PostFilter::REQUIREMENT_FAVORITE)
{ {
$query->innerJoin('favorites _fav ON _fav.postId = posts.id INNER JOIN users favorite ON favorite.id = _fav.userId'); $query->innerJoin('favorites _fav ON _fav.postId = posts.id');
$query->innerJoin('users favoritedBy ON favoritedBy.id = _fav.userId');
} }
elseif ($requirement->getType() === \Szurubooru\SearchServices\Filters\PostFilter::REQUIREMENT_UPLOADER) elseif ($requirement->getType() === \Szurubooru\SearchServices\Filters\PostFilter::REQUIREMENT_UPLOADER)
@ -120,6 +121,23 @@ class PostDao extends AbstractDao implements ICrudDao
$query->innerJoin('users uploader ON uploader.id = userId'); $query->innerJoin('users uploader ON uploader.id = userId');
} }
elseif ($requirement->getType() === \Szurubooru\SearchServices\Filters\PostFilter::REQUIREMENT_USER_SCORE)
{
$values = $requirement->getValue()->getValues();
$userName = $values[0];
$score = $values[1];
$sql = 'EXISTS (
SELECT 1 FROM postScores
INNER JOIN users ON postScores.userId = users.id
WHERE postScores.postId = posts.id
AND LOWER(users.name) = LOWER(?)
AND postScores.score = ?)';
if ($requirement->isnegated())
$sql = 'NOT ' . $sql;
$query->where($sql, $userName, $score);
return;
}
parent::decorateQueryFromRequirement($query, $requirement); parent::decorateQueryFromRequirement($query, $requirement);
} }

View file

@ -19,8 +19,9 @@ class PostFilter extends BasicFilter implements IFilter
const REQUIREMENT_SCORE = 'score'; const REQUIREMENT_SCORE = 'score';
const REQUIREMENT_UPLOADER = 'uploader.name'; const REQUIREMENT_UPLOADER = 'uploader.name';
const REQUIREMENT_SAFETY = 'safety'; const REQUIREMENT_SAFETY = 'safety';
const REQUIREMENT_FAVORITE = 'favorite.name'; const REQUIREMENT_FAVORITE = 'favoritedBy.name';
const REQUIREMENT_TYPE = 'contentType'; const REQUIREMENT_TYPE = 'contentType';
const REQUIREMENT_USER_SCORE = 'userScore';
public function __construct() public function __construct()
{ {

View file

@ -81,7 +81,7 @@ abstract class AbstractSearchParser
protected function addRequirementFromToken($filter, $token, $type, $flags, $valueDecorator = null) protected function addRequirementFromToken($filter, $token, $type, $flags, $valueDecorator = null)
{ {
$requirement = new \Szurubooru\SearchServices\Requirements\Requirement(); $requirement = new \Szurubooru\SearchServices\Requirements\Requirement();
$requirement->setType($type, $flags); $requirement->setType($type);
$requirement->setValue($this->createRequirementValue($token->getValue(), $flags, $valueDecorator)); $requirement->setValue($this->createRequirementValue($token->getValue(), $flags, $valueDecorator));
$requirement->setNegated($token->isNegated()); $requirement->setNegated($token->isNegated());
$filter->addRequirement($requirement); $filter->addRequirement($requirement);

View file

@ -3,6 +3,13 @@ namespace Szurubooru\SearchServices\Parsers;
class PostSearchParser extends AbstractSearchParser class PostSearchParser extends AbstractSearchParser
{ {
private $authService;
public function __construct(\Szurubooru\Services\AuthService $authService)
{
$this->authService = $authService;
}
protected function createFilter() protected function createFilter()
{ {
return new \Szurubooru\SearchServices\Filters\PostFilter; return new \Szurubooru\SearchServices\Filters\PostFilter;
@ -49,6 +56,12 @@ class PostSearchParser extends AbstractSearchParser
elseif ($token->getKey() === 'type') elseif ($token->getKey() === 'type')
$this->addTypeRequirement($filter, $token); $this->addTypeRequirement($filter, $token);
elseif ($token->getKey() === 'special' and $token->getValue() === 'liked' and $this->authService->isLoggedIn())
$this->addUserScoreRequirement($filter, $this->authService->getLoggedInUser()->getName(), 1, $token->isNegated());
elseif ($token->getKey() === 'special' and $token->getValue() === 'disliked' and $this->authService->isLoggedIn())
$this->addUserScoreRequirement($filter, $this->authService->getLoggedInUser()->getName(), -1, $token->isNegated());
else else
throw new \BadMethodCallException('Not supported'); throw new \BadMethodCallException('Not supported');
} }
@ -194,6 +207,17 @@ class PostSearchParser extends AbstractSearchParser
}); });
} }
private function addUserScoreRequirement($filter, $userName, $score, $isNegated)
{
$tokenValue = new \Szurubooru\SearchServices\Requirements\RequirementCompositeValue();
$tokenValue->setValues([$userName, $score]);
$requirement = new \Szurubooru\SearchServices\Requirements\Requirement();
$requirement->setType(\Szurubooru\SearchServices\Filters\PostFilter::REQUIREMENT_USER_SCORE);
$requirement->setValue($tokenValue);
$requirement->setNegated($isNegated);
$filter->addRequirement($requirement);
}
private function dateToTime($value) private function dateToTime($value)
{ {
$value = strtolower(trim($value)); $value = strtolower(trim($value));