Collection.php 7.0 KB

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