Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
pull/596/head
Maxence Lange 2019-06-25 14:15:23 -01:00
rodzic 9411cf6046
commit 6b8e1618ea
3 zmienionych plików z 73 dodań i 25 usunięć

Wyświetl plik

@ -475,7 +475,7 @@ class CoreRequestBuilder {
protected function limitToDBField( protected function limitToDBField(
IQueryBuilder &$qb, string $field, string $value, bool $cs = true, string $alias = '' IQueryBuilder &$qb, string $field, string $value, bool $cs = true, string $alias = ''
) { ) {
$expr = $this->exprLimitToDBField($qb, $field, $value, $cs, $alias); $expr = $this->exprLimitToDBField($qb, $field, $value, true, $cs, $alias);
$qb->andWhere($expr); $qb->andWhere($expr);
} }
@ -484,13 +484,15 @@ class CoreRequestBuilder {
* @param IQueryBuilder $qb * @param IQueryBuilder $qb
* @param string $field * @param string $field
* @param string $value * @param string $value
* @param bool $eq
* @param bool $cs * @param bool $cs
* @param string $alias * @param string $alias
* *
* @return string * @return string
*/ */
protected function exprLimitToDBField( protected function exprLimitToDBField(
IQueryBuilder &$qb, string $field, string $value, bool $cs = true, string $alias = '' IQueryBuilder &$qb, string $field, string $value, bool $eq = true, bool $cs = true,
string $alias = ''
): string { ): string {
$expr = $qb->expr(); $expr = $qb->expr();
@ -500,12 +502,20 @@ class CoreRequestBuilder {
} }
$field = $pf . $field; $field = $pf . $field;
if ($eq) {
$comp = 'eq';
} else {
$comp = 'neq';
}
if ($cs) { if ($cs) {
return $expr->eq($field, $qb->createNamedParameter($value)); return $expr->$comp($field, $qb->createNamedParameter($value));
} else { } else {
$func = $qb->func(); $func = $qb->func();
return $expr->eq($func->lower($field), $func->lower($qb->createNamedParameter($value))); return $expr->$comp(
$func->lower($field), $func->lower($qb->createNamedParameter($value))
);
} }
} }
@ -653,9 +663,12 @@ class CoreRequestBuilder {
/** /**
* @param IQueryBuilder $qb * @param IQueryBuilder $qb
* @param string $fieldActorId * @param string $fieldActorId
* @param Person $author
* @param string $alias * @param string $alias
*/ */
protected function leftJoinCacheActors(IQueryBuilder &$qb, string $fieldActorId, string $alias = '') { protected function leftJoinCacheActors(
IQueryBuilder &$qb, string $fieldActorId, Person $author = null, string $alias = ''
) {
if ($qb->getType() !== QueryBuilder::SELECT) { if ($qb->getType() !== QueryBuilder::SELECT) {
return; return;
} }
@ -681,11 +694,28 @@ class CoreRequestBuilder {
->selectAlias('ca.public_key', 'cacheactor_public_key') ->selectAlias('ca.public_key', 'cacheactor_public_key')
->selectAlias('ca.source', 'cacheactor_source') ->selectAlias('ca.source', 'cacheactor_source')
->selectAlias('ca.creation', 'cacheactor_creation') ->selectAlias('ca.creation', 'cacheactor_creation')
->selectAlias('ca.local', 'cacheactor_local') ->selectAlias('ca.local', 'cacheactor_local');
->leftJoin(
$this->defaultSelectAlias, CoreRequestBuilder::TABLE_CACHE_ACTORS, 'ca', $orX = $expr->orX();
$expr->eq($func->lower($pf . '.' . $fieldActorId), $func->lower('ca.id')) $orX->add($expr->eq($func->lower($pf . '.' . $fieldActorId), $func->lower('ca.id')));
); if ($author !== null) {
$andX = $expr->andX();
$andX->add(
$this->exprLimitToDBField($qb, 'attributed_to', $author->getId(), true, false, 's')
);
$andX->add(
$expr->eq(
$func->lower($this->defaultSelectAlias . '.attributed_to'),
$func->lower('ca.id')
)
);
$orX->add($andX);
}
$qb->leftJoin(
$this->defaultSelectAlias, CoreRequestBuilder::TABLE_CACHE_ACTORS, 'ca', $orX
);
} }

Wyświetl plik

@ -278,10 +278,11 @@ class StreamRequest extends StreamRequestBuilder {
public function getTimelineHome(Person $actor, int $since = 0, int $limit = 5): array { public function getTimelineHome(Person $actor, int $since = 0, int $limit = 5): array {
$qb = $this->getStreamSelectSql(); $qb = $this->getStreamSelectSql();
$this->joinFollowing($qb, $actor); $this->leftJoinFollowing($qb, $actor);
$this->limitToFollowing($qb, $actor);
$this->limitPaginate($qb, $since, $limit); $this->limitPaginate($qb, $since, $limit);
$this->leftJoinCacheActors($qb, 'object_id', 'f'); $this->leftJoinCacheActors($qb, 'object_id', $actor, 'f');
$this->leftJoinStreamAction($qb); $this->leftJoinStreamAction($qb);
$this->filterDuplicate($qb); $this->filterDuplicate($qb);
@ -646,7 +647,7 @@ class StreamRequest extends StreamRequestBuilder {
$pf = $this->defaultSelectAlias . '.'; $pf = $this->defaultSelectAlias . '.';
$on = $expr->andX(); $on = $expr->andX();
$on->add($this->exprLimitToDBField($qb, 'actor_id', $actor->getId(), false, 'fs')); $on->add($this->exprLimitToDBField($qb, 'actor_id', $actor->getId(), true, false, 'fs'));
$on->add($expr->eq($func->lower($pf . 'attributed_to'), $func->lower('fs.object_id'))); $on->add($expr->eq($func->lower($pf . 'attributed_to'), $func->lower('fs.object_id')));
$on->add($this->exprLimitToDBFieldInt($qb, 'accepted', 1, 'fs')); $on->add($this->exprLimitToDBFieldInt($qb, 'accepted', 1, 'fs'));

Wyświetl plik

@ -99,8 +99,7 @@ class StreamRequestBuilder extends CoreRequestBuilder {
's.object_id', 's.attributed_to', 's.in_reply_to', 's.source', 's.local', 's.object_id', 's.attributed_to', 's.in_reply_to', 's.source', 's.local',
's.instances', 's.creation', 's.hidden_on_timeline' 's.instances', 's.creation', 's.hidden_on_timeline'
) )
->from(self::TABLE_STREAMS, 's') ->from(self::TABLE_STREAMS, 's');
->groupBy('s.id');
$this->defaultSelectAlias = 's'; $this->defaultSelectAlias = 's';
@ -143,7 +142,7 @@ class StreamRequestBuilder extends CoreRequestBuilder {
protected function limitToViewer(IQueryBuilder $qb) { protected function limitToViewer(IQueryBuilder $qb) {
$actor = $this->viewer; $actor = $this->viewer;
$on = $this->exprJoinFollowing($qb, $actor, false); $on = $this->exprJoinFollowing($qb, $actor);
$on->add($this->exprLimitToRecipient($qb, ACore::CONTEXT_PUBLIC, false)); $on->add($this->exprLimitToRecipient($qb, ACore::CONTEXT_PUBLIC, false));
$on->add($this->exprLimitToRecipient($qb, $actor->getId(), true)); $on->add($this->exprLimitToRecipient($qb, $actor->getId(), true));
$qb->join($this->defaultSelectAlias, CoreRequestBuilder::TABLE_FOLLOWS, 'f', $on); $qb->join($this->defaultSelectAlias, CoreRequestBuilder::TABLE_FOLLOWS, 'f', $on);
@ -180,7 +179,9 @@ class StreamRequestBuilder extends CoreRequestBuilder {
$func->lower('attributed_to') $func->lower('attributed_to')
) )
); );
$follower->add($this->exprLimitToDBField($qb, 'actor_id', $actor->getId(), false, 'f')); $follower->add(
$this->exprLimitToDBField($qb, 'actor_id', $actor->getId(), true, false, 'f')
);
$filter->add($follower); $filter->add($follower);
$qb->andwhere($filter); $qb->andwhere($filter);
@ -191,33 +192,47 @@ class StreamRequestBuilder extends CoreRequestBuilder {
* @param IQueryBuilder $qb * @param IQueryBuilder $qb
* @param Person $actor * @param Person $actor
*/ */
protected function joinFollowing(IQueryBuilder $qb, Person $actor) { protected function leftJoinFollowing(IQueryBuilder $qb, Person $actor) {
if ($qb->getType() !== QueryBuilder::SELECT) { if ($qb->getType() !== QueryBuilder::SELECT) {
return; return;
} }
$on = $this->exprJoinFollowing($qb, $actor); $on = $this->exprJoinFollowing($qb, $actor);
$qb->selectAlias('f.object_id', 'following_actor_id'); $qb->selectAlias('f.object_id', 'following_actor_id');
$qb->join($this->defaultSelectAlias, CoreRequestBuilder::TABLE_FOLLOWS, 'f', $on); $qb->leftJoin($this->defaultSelectAlias, CoreRequestBuilder::TABLE_FOLLOWS, 'f', $on);
}
/**
* @param IQueryBuilder $qb
* @param Person $actor
*/
protected function limitToFollowing(IQueryBuilder $qb, Person $actor) {
$expr = $qb->expr();
$andX = $expr->andX();
$andX->add($this->exprLimitToDBField($qb, 'attributed_to', $actor->getId(), true, false));
$andX->add($this->exprLimitToDBField($qb, 'cc', '[]', false));
$orX = $expr->orX();
$orX->add($andX);
$orX->add($expr->isNotNull('f.object_id'));
$qb->andWhere($orX);
} }
/** /**
* @param IQueryBuilder $qb * @param IQueryBuilder $qb
* @param Person $actor * @param Person $actor
* @param bool $followers
* *
* @return ICompositeExpression * @return ICompositeExpression
*/ */
protected function exprJoinFollowing(IQueryBuilder $qb, Person $actor, bool $followers = true) { protected function exprJoinFollowing(IQueryBuilder $qb, Person $actor) {
$expr = $qb->expr(); $expr = $qb->expr();
$func = $qb->func(); $func = $qb->func();
$pf = $this->defaultSelectAlias . '.'; $pf = $this->defaultSelectAlias . '.';
$on = $expr->orX(); $on = $expr->orX();
if ($followers) {
$on->add($this->exprLimitToRecipient($qb, $actor->getFollowers(), false));
}
// list of possible recipient as a follower (to, to_array, cc, ...) // list of possible recipient as a follower (to, to_array, cc, ...)
$recipientFields = $expr->orX(); $recipientFields = $expr->orX();
@ -229,7 +244,9 @@ class StreamRequestBuilder extends CoreRequestBuilder {
// all possible follow, but linked by followers (actor_id) and accepted follow // all possible follow, but linked by followers (actor_id) and accepted follow
$crossFollows = $expr->andX(); $crossFollows = $expr->andX();
$crossFollows->add($recipientFields); $crossFollows->add($recipientFields);
$crossFollows->add($this->exprLimitToDBField($qb, 'actor_id', $actor->getId(), false, 'f')); $crossFollows->add(
$this->exprLimitToDBField($qb, 'actor_id', $actor->getId(), true, false, 'f')
);
$crossFollows->add($this->exprLimitToDBFieldInt($qb, 'accepted', 1, 'f')); $crossFollows->add($this->exprLimitToDBFieldInt($qb, 'accepted', 1, 'f'));
$on->add($crossFollows); $on->add($crossFollows);