Collection.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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
  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')->orderBy('note.created', 'DESC')->addOrderBy('note.id', 'DESC');
  33. $actor_qb->select('actor')->from('App\Entity\Actor', 'actor')->orderBy('actor.created', 'DESC')->addOrderBy('actor.id', 'DESC');
  34. Event::handle('CollectionQueryAddJoins', [&$note_qb, &$actor_qb, $note_criteria, $actor_criteria]);
  35. $notes = [];
  36. $actors = [];
  37. if (!\is_null($note_criteria)) {
  38. $note_qb->addCriteria($note_criteria);
  39. $notes = $note_qb->getQuery()->execute();
  40. }
  41. if (!\is_null($actor_criteria)) {
  42. $actor_qb->addCriteria($actor_criteria);
  43. $actors = $actor_qb->getQuery()->execute();
  44. }
  45. // N.B.: Scope is only enforced at FeedController level
  46. return ['notes' => $notes ?? null, 'actors' => $actors ?? null];
  47. }
  48. public function onCollectionQueryAddJoins(QueryBuilder &$note_qb, QueryBuilder &$actor_qb): bool
  49. {
  50. $note_qb->leftJoin(ActorSubscription::class, 'subscription', Expr\Join::WITH, 'note.actor_id = subscription.subscribed_id')
  51. ->leftJoin(Actor::class, 'note_actor', Expr\Join::WITH, 'note.actor_id = note_actor.id');
  52. return Event::next;
  53. }
  54. /**
  55. * Convert $term to $note_expr and $actor_expr, search criteria. Handles searching for text
  56. * notes, for different types of actors and for the content of text notes
  57. */
  58. public function onCollectionQueryCreateExpression(ExpressionBuilder $eb, string $term, ?string $locale, ?Actor $actor, &$note_expr, &$actor_expr)
  59. {
  60. if (str_contains($term, ':')) {
  61. $term = explode(':', $term);
  62. if (Formatting::startsWith($term[0], 'note')) {
  63. switch ($term[0]) {
  64. case 'notes-all':
  65. $note_expr = $eb->neq('note.created', null);
  66. break;
  67. case 'note-local':
  68. $note_expr = $eb->eq('note.is_local', filter_var($term[1], \FILTER_VALIDATE_BOOLEAN));
  69. break;
  70. case 'note-types':
  71. case 'notes-include':
  72. case 'note-filter':
  73. if (\is_null($note_expr)) {
  74. $note_expr = [];
  75. }
  76. if (array_intersect(explode(',', $term[1]), ['text', 'words']) !== []) {
  77. $note_expr[] = $eb->neq('note.content', null);
  78. } else {
  79. $note_expr[] = $eb->eq('note.content', null);
  80. }
  81. break;
  82. case 'note-conversation':
  83. $note_expr = $eb->eq('note.conversation_id', (int) trim($term[1]));
  84. break;
  85. case 'note-from':
  86. case 'notes-from':
  87. $subscribed_expr = $eb->eq('subscription.subscriber_id', $actor->getId());
  88. $type_consts = [];
  89. if ($term[1] === 'subscribed') {
  90. $type_consts = null;
  91. }
  92. foreach (explode(',', $term[1]) as $from) {
  93. if (str_starts_with($from, 'subscribed-')) {
  94. [, $type] = explode('-', $from);
  95. if (\in_array($type, ['actor', 'actors'])) {
  96. $type_consts = null;
  97. } else {
  98. $type_consts[] = \constant(Actor::class . '::' . mb_strtoupper($type));
  99. }
  100. }
  101. }
  102. if (\is_null($type_consts)) {
  103. $note_expr = $subscribed_expr;
  104. } elseif (!empty($type_consts)) {
  105. $note_expr = $eb->andX($subscribed_expr, $eb->in('note_actor.type', $type_consts));
  106. }
  107. break;
  108. }
  109. } elseif (Formatting::startsWith($term, 'actor-')) {
  110. switch ($term[0]) {
  111. case 'actor-types':
  112. case 'actors-include':
  113. case 'actor-filter':
  114. case 'actor-local':
  115. if (\is_null($actor_expr)) {
  116. $actor_expr = [];
  117. }
  118. foreach (
  119. [
  120. Actor::PERSON => ['person', 'people'],
  121. Actor::GROUP => ['group', 'groups'],
  122. Actor::ORGANISATION => ['org', 'orgs', 'organization', 'organizations', 'organisation', 'organisations'],
  123. Actor::BOT => ['bot', 'bots'],
  124. ] as $type => $match) {
  125. if (array_intersect(explode(',', $term[1]), $match) !== []) {
  126. $actor_expr[] = $eb->eq('actor.type', $type);
  127. } else {
  128. $actor_expr[] = $eb->neq('actor.type', $type);
  129. }
  130. }
  131. break;
  132. }
  133. }
  134. } else {
  135. $note_expr = $eb->contains('note.content', $term);
  136. }
  137. return Event::next;
  138. }
  139. }