select('note')->from('App\Entity\Note', 'note'); $actor_qb->select('actor')->from('App\Entity\Actor', 'actor'); Event::handle('CollectionQueryAddJoins', [&$note_qb, &$actor_qb, $note_criteria, $actor_criteria]); // Handle ordering $note_order_by = !empty($note_order_by) ? $note_order_by : ['note.created' => 'DESC', 'note.id' => 'DESC']; $actor_order_by = !empty($actor_order_by) ? $actor_order_by : ['actor.created' => 'DESC', 'actor.id' => 'DESC']; foreach ($note_order_by as $field => $order) { $note_qb->addOrderBy($field, $order); } foreach ($actor_order_by as $field => $order) { $actor_qb->addOrderBy($field, $order); } $notes = []; $actors = []; if (!\is_null($note_criteria)) { $note_qb->addCriteria($note_criteria); $notes = $note_qb->getQuery()->execute(); } if (!\is_null($actor_criteria)) { $actor_qb->addCriteria($actor_criteria); $actors = $actor_qb->getQuery()->execute(); } // N.B.: Scope is only enforced at FeedController level return ['notes' => $notes ?? null, 'actors' => $actors ?? null]; } public function onCollectionQueryAddJoins(QueryBuilder &$note_qb, QueryBuilder &$actor_qb): bool { $note_aliases = $note_qb->getAllAliases(); if (!\in_array('subscription', $note_aliases)) { $note_qb->leftJoin(ActorSubscription::class, 'subscription', Expr\Join::WITH, 'note.actor_id = subscription.subscribed_id'); } if (!\in_array('note_actor', $note_aliases)) { $note_qb->leftJoin(Actor::class, 'note_actor', Expr\Join::WITH, 'note.actor_id = note_actor.id'); } return Event::next; } /** * Convert $term to $note_expr and $actor_expr, search criteria. Handles searching for text * notes, for different types of actors and for the content of text notes */ public function onCollectionQueryCreateExpression(ExpressionBuilder $eb, string $term, ?string $locale, ?Actor $actor, &$note_expr, &$actor_expr) { if (str_contains($term, ':')) { $term = explode(':', $term); if (Formatting::startsWith($term[0], 'note')) { switch ($term[0]) { case 'notes-all': $note_expr = $eb->neq('note.created', null); break; case 'note-local': $note_expr = $eb->eq('note.is_local', filter_var($term[1], \FILTER_VALIDATE_BOOLEAN)); break; case 'note-types': case 'notes-include': case 'note-filter': if (\is_null($note_expr)) { $note_expr = []; } if (array_intersect(explode(',', $term[1]), ['text', 'words']) !== []) { $note_expr[] = $eb->neq('note.content', null); } else { $note_expr[] = $eb->eq('note.content', null); } break; case 'note-conversation': $note_expr = $eb->eq('note.conversation_id', (int) trim($term[1])); break; case 'note-from': case 'notes-from': $subscribed_expr = $eb->eq('subscription.subscriber_id', $actor->getId()); $type_consts = []; if ($term[1] === 'subscribed') { $type_consts = null; } foreach (explode(',', $term[1]) as $from) { if (str_starts_with($from, 'subscribed-')) { [, $type] = explode('-', $from); if (\in_array($type, ['actor', 'actors'])) { $type_consts = null; } else { $type_consts[] = \constant(Actor::class . '::' . mb_strtoupper($type === 'organisation' ? 'group' : $type)); } } } if (\is_null($type_consts)) { $note_expr = $subscribed_expr; } elseif (!empty($type_consts)) { $note_expr = $eb->andX($subscribed_expr, $eb->in('note_actor.type', $type_consts)); } break; } } elseif (Formatting::startsWith($term, 'actor-')) { switch ($term[0]) { case 'actor-types': case 'actors-include': case 'actor-filter': case 'actor-local': if (\is_null($actor_expr)) { $actor_expr = []; } foreach ( [ Actor::PERSON => ['person', 'people'], Actor::GROUP => ['group', 'groups', 'org', 'orgs', 'organisation', 'organisations', 'organization', 'organizations'], Actor::BOT => ['bot', 'bots'], ] as $type => $match) { if (array_intersect(explode(',', $term[1]), $match) !== []) { $actor_expr[] = $eb->eq('actor.type', $type); } else { $actor_expr[] = $eb->neq('actor.type', $type); } } break; } } } else { $note_expr = $eb->contains('note.content', $term); } return Event::next; } }