Feed.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. declare(strict_types = 1);
  3. // {{{ License
  4. // This file is part of GNU social - https://www.gnu.org/software/social
  5. //
  6. // GNU social is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // GNU social is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  18. // }}}
  19. namespace Component\Feed;
  20. use App\Core\DB\DB;
  21. use App\Core\Event;
  22. use App\Core\Modules\Component;
  23. use App\Entity\Actor;
  24. use App\Entity\Subscription;
  25. use App\Util\Formatting;
  26. use Component\Search\Util\Parser;
  27. use Doctrine\Common\Collections\ExpressionBuilder;
  28. use Doctrine\DBAL\Query\QueryBuilder as DBALQueryBuilder;
  29. use Doctrine\ORM\Query\Expr;
  30. use Doctrine\ORM\QueryBuilder;
  31. use Somnambulist\Components\CTEBuilder\RecursiveExpression;
  32. class Feed extends Component
  33. {
  34. /**
  35. * Perform a high level query on notes or actors
  36. *
  37. * Supports a variety of query terms and is used both in feeds and
  38. * in search. Uses query builders to allow for extension
  39. */
  40. public static function query(string $query, int $page, ?string $language = null, ?Actor $actor = null): array
  41. {
  42. $note_criteria = null;
  43. $actor_criteria = null;
  44. if (!empty($query = trim($query))) {
  45. [$note_criteria, $actor_criteria] = Parser::parse($query, $language, $actor);
  46. }
  47. // $note_qb = DB::createQueryBuilder();
  48. // $note_qb->select('note')->from('App\Entity\Note', 'note')->orderBy('note.created', 'DESC')->orderBy('note.id', 'DESC');
  49. $note_qb = new DBALQueryBuilder(DB::getConnection());
  50. $note_qb->select('note')->from('App\Entity\Note', 'note')->orderBy('note.created', 'DESC')->orderBy('note.id', 'DESC')
  51. ->where('note.reply_to is null');
  52. $recursive = new RecursiveExpression('notes', new DBALQueryBuilder(DB::getConnection()));
  53. $recursive->withInitialSelect($note_qb);
  54. $recursive->query()->select('note_replies')->from('App\Entity\Note', 'note_replies')
  55. ->where('note_replies.reply_to = note.id')->orderBy('note.created', 'DESC')->orderBy('note.id', 'DESC');
  56. dd($recursive->getInlineSQL());
  57. $actor_qb = DB::createQueryBui1lder();
  58. $actor_qb->select('actor')->from('App\Entity\Actor', 'actor')->orderBy('actor.created', 'DESC')->orderBy('actor.id', 'DESC');
  59. Event::handle('SearchQueryAddJoins', [&$note_qb, &$actor_qb, $note_criteria, $actor_criteria]);
  60. $notes = [];
  61. $actors = [];
  62. if (!\is_null($note_criteria)) {
  63. $note_qb->addCriteria($note_criteria);
  64. }
  65. $notes = $note_qb->getQuery()->execute();
  66. if (!\is_null($actor_criteria)) {
  67. $actor_qb->addCriteria($actor_criteria);
  68. }
  69. $actors = $actor_qb->getQuery()->execute();
  70. // TODO: Enforce scoping on the notes before returning
  71. return ['notes' => $notes ?? null, 'actors' => $actors ?? null];
  72. }
  73. public function onSearchQueryAddJoins(QueryBuilder &$note_qb, QueryBuilder &$actor_qb)
  74. {
  75. $note_qb->leftJoin(Subscription::class, 'subscription', Expr\Join::WITH, 'note.actor_id = subscription.subscribed');
  76. return Event::next;
  77. }
  78. /**
  79. * Convert $term to $note_expr and $actor_expr, search criteria. Handles searching for text
  80. * notes, for different types of actors and for the content of text notes
  81. */
  82. public function onSearchCreateExpression(ExpressionBuilder $eb, string $term, ?string $language, ?Actor $actor, &$note_expr, &$actor_expr)
  83. {
  84. if (str_contains($term, ':')) {
  85. $term = explode(':', $term);
  86. if (Formatting::startsWith($term[0], 'note-')) {
  87. switch ($term[0]) {
  88. case 'note-local':
  89. $note_expr = $eb->eq('note.is_local', filter_var($term[1], \FILTER_VALIDATE_BOOLEAN));
  90. break;
  91. case 'note-types':
  92. case 'notes-include':
  93. case 'note-filter':
  94. if (\is_null($note_expr)) {
  95. $note_expr = [];
  96. }
  97. if (array_intersect(explode(',', $term[1]), ['text', 'words']) !== []) {
  98. $note_expr[] = $eb->neq('note.content', null);
  99. } else {
  100. $note_expr[] = $eb->eq('note.content', null);
  101. }
  102. break;
  103. case 'note-conversation':
  104. $note_expr = $eb->eq('note.conversation_id', (int) trim($term[1]));
  105. break;
  106. case 'note-from':
  107. if ($term[1] === 'subscribed') {
  108. $note_expr = $eb->eq('subscription.subscriber', $actor->getId());
  109. }
  110. break;
  111. }
  112. } elseif (Formatting::startsWith($term, 'actor-')) {
  113. switch ($term[0]) {
  114. case 'actor-types':
  115. case 'actors-include':
  116. case 'actor-filter':
  117. case 'actor-local':
  118. if (\is_null($actor_expr)) {
  119. $actor_expr = [];
  120. }
  121. foreach (
  122. [
  123. Actor::PERSON => ['person', 'people'],
  124. Actor::GROUP => ['group', 'groups'],
  125. Actor::ORGANIZATION => ['org', 'orgs', 'organization', 'organizations', 'organisation', 'organisations'],
  126. Actor::BUSINESS => ['business', 'businesses'],
  127. Actor::BOT => ['bot', 'bots'],
  128. ] as $type => $match) {
  129. if (array_intersect(explode(',', $term[1]), $match) !== []) {
  130. $actor_expr[] = $eb->eq('actor.type', $type);
  131. } else {
  132. $actor_expr[] = $eb->neq('actor.type', $type);
  133. }
  134. }
  135. break;
  136. }
  137. }
  138. } else {
  139. $note_expr = $eb->contains('note.content', $term);
  140. }
  141. return Event::next;
  142. }
  143. }