Collection.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. declare(strict_types = 1);
  3. namespace Component\Collection;
  4. use App\Core\DB;
  5. use App\Core\Event;
  6. use App\Core\Modules\Component;
  7. use App\Entity\Actor;
  8. use App\Entity\Note;
  9. use App\Util\Formatting;
  10. use Component\Collection\Util\Parser;
  11. use Component\Subscription\Entity\ActorSubscription;
  12. use Doctrine\Common\Collections\ExpressionBuilder;
  13. use Doctrine\ORM\Query\Expr;
  14. use Doctrine\ORM\QueryBuilder;
  15. use EventResult;
  16. class Collection extends Component
  17. {
  18. /**
  19. * Perform a high level query on notes or actors
  20. *
  21. * Supports a variety of query terms and is used both in feeds and
  22. * in search. Uses query builders to allow for extension
  23. *
  24. * @param array<string, OrderByType> $note_order_by
  25. * @param array<string, OrderByType> $actor_order_by
  26. *
  27. * @return array{notes: null|Note[], actors: null|Actor[]}
  28. */
  29. public static function query(string $query, int $page, ?string $locale = null, ?Actor $actor = null, array $note_order_by = [], array $actor_order_by = []): array
  30. {
  31. $note_criteria = null;
  32. $actor_criteria = null;
  33. if (!empty($query = trim($query))) {
  34. [$note_criteria, $actor_criteria] = Parser::parse($query, $locale, $actor);
  35. }
  36. $note_qb = DB::createQueryBuilder();
  37. $actor_qb = DB::createQueryBuilder();
  38. // TODO consider selecting note related stuff, to avoid separate queries (though they're cached, so maybe it's okay)
  39. $note_qb->select('note')->from('App\Entity\Note', 'note');
  40. $actor_qb->select('actor')->from('App\Entity\Actor', 'actor');
  41. Event::handle('CollectionQueryAddJoins', [&$note_qb, &$actor_qb, $note_criteria, $actor_criteria]);
  42. // Handle ordering
  43. $note_order_by = !empty($note_order_by) ? $note_order_by : ['note.created' => 'DESC', 'note.id' => 'DESC'];
  44. $actor_order_by = !empty($actor_order_by) ? $actor_order_by : ['actor.created' => 'DESC', 'actor.id' => 'DESC'];
  45. foreach ($note_order_by as $field => $order) {
  46. $note_qb->addOrderBy($field, $order);
  47. }
  48. foreach ($actor_order_by as $field => $order) {
  49. $actor_qb->addOrderBy($field, $order);
  50. }
  51. $notes = [];
  52. $actors = [];
  53. if (!\is_null($note_criteria)) {
  54. $note_qb->addCriteria($note_criteria);
  55. $notes = $note_qb->getQuery()->execute();
  56. }
  57. if (!\is_null($actor_criteria)) {
  58. $actor_qb->addCriteria($actor_criteria);
  59. $actors = $actor_qb->getQuery()->execute();
  60. }
  61. // N.B.: Scope is only enforced at FeedController level
  62. return ['notes' => $notes ?? null, 'actors' => $actors ?? null];
  63. }
  64. public function onCollectionQueryAddJoins(QueryBuilder &$note_qb, QueryBuilder &$actor_qb): EventResult
  65. {
  66. $note_aliases = $note_qb->getAllAliases();
  67. if (!\in_array('subscription', $note_aliases)) {
  68. $note_qb->leftJoin(ActorSubscription::class, 'subscription', Expr\Join::WITH, 'note.actor_id = subscription.subscribed_id');
  69. }
  70. if (!\in_array('note_actor', $note_aliases)) {
  71. $note_qb->leftJoin(Actor::class, 'note_actor', Expr\Join::WITH, 'note.actor_id = note_actor.id');
  72. }
  73. return Event::next;
  74. }
  75. /**
  76. * Convert $term to $note_expr and $actor_expr, search criteria. Handles searching for text
  77. * notes, for different types of actors and for the content of text notes
  78. *
  79. * @param mixed $note_expr
  80. * @param mixed $actor_expr
  81. */
  82. public function onCollectionQueryCreateExpression(ExpressionBuilder $eb, string $term, ?string $locale, ?Actor $actor, &$note_expr, &$actor_expr): EventResult
  83. {
  84. if (str_contains($term, ':')) {
  85. $term = explode(':', $term);
  86. if (Formatting::startsWith($term[0], 'note')) {
  87. switch ($term[0]) {
  88. case 'notes-all':
  89. $note_expr = $eb->neq('note.created', null);
  90. break;
  91. case 'note-local':
  92. $note_expr = $eb->eq('note.is_local', filter_var($term[1], \FILTER_VALIDATE_BOOLEAN));
  93. break;
  94. case 'note-types':
  95. case 'notes-include':
  96. case 'note-filter':
  97. if (\is_null($note_expr)) {
  98. $note_expr = [];
  99. }
  100. if (array_intersect(explode(',', $term[1]), ['text', 'words']) !== []) {
  101. $note_expr[] = $eb->neq('note.content', null);
  102. } else {
  103. $note_expr[] = $eb->eq('note.content', null);
  104. }
  105. break;
  106. case 'note-conversation':
  107. $note_expr = $eb->eq('note.conversation_id', (int) trim($term[1]));
  108. break;
  109. case 'note-from':
  110. case 'notes-from':
  111. $subscribed_expr = $eb->eq('subscription.subscriber_id', $actor->getId());
  112. $type_consts = [];
  113. if ($term[1] === 'subscribed') {
  114. $type_consts = null;
  115. }
  116. foreach (explode(',', $term[1]) as $from) {
  117. if (str_starts_with($from, 'subscribed-')) {
  118. [, $type] = explode('-', $from);
  119. if (\in_array($type, ['actor', 'actors'])) {
  120. $type_consts = null;
  121. } else {
  122. $type_consts[] = \constant(Actor::class . '::' . mb_strtoupper($type === 'organisation' ? 'group' : $type));
  123. }
  124. }
  125. }
  126. if (\is_null($type_consts)) {
  127. $note_expr = $subscribed_expr;
  128. } elseif (!empty($type_consts)) {
  129. $note_expr = $eb->andX($subscribed_expr, $eb->in('note_actor.type', $type_consts));
  130. }
  131. break;
  132. }
  133. } elseif (Formatting::startsWith($term, 'actor-')) {
  134. switch ($term[0]) {
  135. case 'actor-types':
  136. case 'actors-include':
  137. case 'actor-filter':
  138. case 'actor-local':
  139. if (\is_null($actor_expr)) {
  140. $actor_expr = [];
  141. }
  142. foreach (
  143. [
  144. Actor::PERSON => ['person', 'people'],
  145. Actor::GROUP => ['group', 'groups', 'org', 'orgs', 'organisation', 'organisations', 'organization', 'organizations'],
  146. Actor::BOT => ['bot', 'bots'],
  147. ] as $type => $match) {
  148. if (array_intersect(explode(',', $term[1]), $match) !== []) {
  149. $actor_expr[] = $eb->eq('actor.type', $type);
  150. } else {
  151. $actor_expr[] = $eb->neq('actor.type', $type);
  152. }
  153. }
  154. break;
  155. }
  156. }
  157. } else {
  158. $note_expr = $eb->contains('note.content', $term);
  159. }
  160. return Event::next;
  161. }
  162. }