Favourite.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 Plugin\Favourite;
  20. use App\Core\DB\DB;
  21. use App\Core\Event;
  22. use App\Core\Modules\NoteHandlerPlugin;
  23. use App\Core\Router\RouteLoader;
  24. use App\Core\Router\Router;
  25. use App\Entity\Activity;
  26. use App\Entity\Actor;
  27. use App\Entity\Feed;
  28. use App\Entity\LocalUser;
  29. use App\Entity\Note;
  30. use App\Util\Common;
  31. use App\Util\Exception\InvalidFormException;
  32. use App\Util\Exception\NoSuchNoteException;
  33. use App\Util\Exception\RedirectException;
  34. use App\Util\Nickname;
  35. use Plugin\Favourite\Entity\Favourite as FavouriteEntity;
  36. use Symfony\Component\HttpFoundation\Request;
  37. use function App\Core\I18n\_m;
  38. class Favourite extends NoteHandlerPlugin
  39. {
  40. public static function favourNote(int $note_id, int $actor_id, string $source = 'web'): ?Activity
  41. {
  42. $opts = ['note_id' => $note_id, 'actor_id' => $actor_id];
  43. $note_already_favoured = DB::find('favourite', $opts);
  44. if (is_null($note_already_favoured)) {
  45. DB::persist(FavouriteEntity::create($opts));
  46. $act = Activity::create([
  47. 'actor_id' => $actor_id,
  48. 'verb' => 'favourite',
  49. 'object_type' => 'note',
  50. 'object_id' => $note_id,
  51. 'source' => $source,
  52. ]);
  53. DB::persist($act);
  54. Event::handle('NewNotification', [$actor = Actor::getById($actor_id), $act, [], "{$actor->getNickname()} favoured note {$note_id}"]);
  55. }
  56. return $act ?? null;
  57. }
  58. public static function unfavourNote(int $note_id, int $actor_id, string $source = 'web'): ?Activity
  59. {
  60. $note_already_favoured = DB::find('favourite', ['note_id' => $note_id, 'actor_id' => $actor_id]);
  61. if (!is_null($note_already_favoured)) {
  62. DB::remove($note_already_favoured);
  63. $favourite_activity = DB::findBy('activity', ['verb' => 'favourite', 'object_type' => 'note', 'object_id' => $note_id], order_by: ['created' => 'DESC'])[0];
  64. $act = Activity::create([
  65. 'actor_id' => $actor_id,
  66. 'verb' => 'undo', // 'undo_favourite',
  67. 'object_type' => 'activity', // 'note',
  68. 'object_id' => $favourite_activity->getId(), // $note_id,
  69. 'source' => $source,
  70. ]);
  71. DB::persist($act);
  72. Event::handle('NewNotification', [$actor = Actor::getById($actor_id), $act, [], "{$actor->getNickname()} unfavoured note {$note_id}"]);
  73. }
  74. return $act ?? null;
  75. }
  76. /**
  77. * HTML rendering event that adds the favourite form as a note
  78. * action, if a user is logged in
  79. *
  80. * @param Request $request
  81. * @param Note $note
  82. * @param array $actions
  83. * @return bool Event hook
  84. */
  85. public function onAddNoteActions(Request $request, Note $note, array &$actions): bool
  86. {
  87. if (is_null($user = Common::user())) {
  88. return Event::next;
  89. }
  90. // If note is favourite, "is_favourite" is 1
  91. $opts = ['note_id' => $note->getId(), 'actor_id' => $user->getId()];
  92. $is_favourite = DB::find('favourite', $opts) !== null;
  93. // Generating URL for favourite action route
  94. $args = ['id' => $note->getId()];
  95. $type = Router::ABSOLUTE_PATH;
  96. $favourite_action_url = $is_favourite
  97. ? Router::url('favourite_remove', $args, $type)
  98. : Router::url('favourite_add', $args, $type);
  99. $query_string = $request->getQueryString();
  100. // Concatenating get parameter to redirect the user to where he came from
  101. $favourite_action_url .= !is_null($query_string) ? '?from=' . mb_substr($query_string, 2) : '';
  102. $extra_classes = $is_favourite ? 'note-actions-set' : 'note-actions-unset';
  103. $favourite_action = [
  104. 'url' => $favourite_action_url,
  105. 'title' => $is_favourite ? 'Remove this note from favourites' : 'Favourite this note!',
  106. 'classes' => "button-container favourite-button-container {$extra_classes}",
  107. 'id' => 'favourite-button-container-' . $note->getId(),
  108. ];
  109. $actions[] = $favourite_action;
  110. return Event::next;
  111. }
  112. public function onAppendCardNote(array $vars, array &$result)
  113. {
  114. // if note is the original, append on end "user favoured this"
  115. $actor = $vars['actor'];
  116. $note = $vars['note'];
  117. return Event::next;
  118. }
  119. public function onAddRoute(RouteLoader $r): bool
  120. {
  121. // Add/remove note to/from favourites
  122. $r->connect(id: 'favourite_add', uri_path: '/object/note/{id<\d+>}/favour', target: [Controller\Favourite::class, 'favouriteAddNote']);
  123. $r->connect(id: 'favourite_remove', uri_path: '/object/note/{id<\d+>}/unfavour', target: [Controller\Favourite::class, 'favouriteRemoveNote']);
  124. // View all favourites by actor id
  125. $r->connect(id: 'favourites_view_by_actor_id', uri_path: '/actor/{id<\d+>}/favourites', target: [Controller\Favourite::class, 'favouritesViewByActorId']);
  126. $r->connect(id: 'favourites_reverse_view_by_actor_id', uri_path: '/actor/{id<\d+>}/reverse_favourites', target: [Controller\Favourite::class, 'favouritesReverseViewByActorId']);
  127. // View all favourites by nickname
  128. $r->connect(id: 'favourites_view_by_nickname', uri_path: '/@{nickname<' . Nickname::DISPLAY_FMT . '>}/favourites', target: [Controller\Favourite::class, 'favouritesByActorNickname']);
  129. $r->connect(id: 'favourites_reverse_view_by_nickname', uri_path: '/@{nickname<' . Nickname::DISPLAY_FMT . '>}/reverse_favourites', target: [Controller\Favourite::class, 'reverseFavouritesByActorNickname']);
  130. return Event::next;
  131. }
  132. public function onCreateDefaultFeeds(int $actor_id, LocalUser $user, int &$ordering)
  133. {
  134. DB::persist(Feed::create([
  135. 'actor_id' => $actor_id,
  136. 'url' => Router::url($route = 'favourites_view_by_nickname', ['nickname' => $user->getNickname()]),
  137. 'route' => $route,
  138. 'title' => _m('Favourites'),
  139. 'ordering' => $ordering++,
  140. ]));
  141. DB::persist(Feed::create([
  142. 'actor_id' => $actor_id,
  143. 'url' => Router::url($route = 'favourites_reverse_view_by_nickname', ['nickname' => $user->getNickname()]),
  144. 'route' => $route,
  145. 'title' => _m('Reverse favourites'),
  146. 'ordering' => $ordering++,
  147. ]));
  148. return Event::next;
  149. }
  150. // ActivityPub
  151. private function activitypub_handler(Actor $actor, \ActivityPhp\Type\AbstractObject $type_activity, mixed $type_object, ?\Plugin\ActivityPub\Entity\ActivitypubActivity &$ap_act): bool
  152. {
  153. if (!in_array($type_activity->get('type'), ['Like', 'Undo'])) {
  154. return Event::next;
  155. }
  156. if ($type_activity->get('type') === 'Like') { // Favourite
  157. if ($type_object instanceof \ActivityPhp\Type\AbstractObject) {
  158. if ($type_object->get('type') === 'Note') {
  159. $note_id = \Plugin\ActivityPub\Util\Model\Note::fromJson($type_object)->getId();
  160. } else {
  161. return Event::next;
  162. }
  163. } else if ($type_object instanceof Note) {
  164. $note_id = $type_object->getId();
  165. } else {
  166. return Event::next;
  167. }
  168. } else { // Undo Favourite
  169. if ($type_object instanceof \ActivityPhp\Type\AbstractObject) {
  170. $ap_prev_favourite_act = \Plugin\ActivityPub\Util\Model\Activity::fromJson($type_object);
  171. $prev_favourite_act = $ap_prev_favourite_act->getActivity();
  172. if ($prev_favourite_act->getVerb() === 'favourite' && $prev_favourite_act->getObjectType() === 'note') {
  173. $note_id = $prev_favourite_act->getObjectId();
  174. } else {
  175. return Event::next;
  176. }
  177. } else if ($type_object instanceof Activity) {
  178. if ($type_object->getVerb() === 'favourite' && $type_object->getObjectType() === 'note') {
  179. $note_id = $type_object->getObjectId();
  180. } else {
  181. return Event::next;
  182. }
  183. } else {
  184. return Event::next;
  185. }
  186. }
  187. if ($type_activity->get('type') === 'Like') {
  188. $act = self::favourNote($note_id, $actor->getId(), source: 'ActivityPub');
  189. } else {
  190. $act = self::unfavourNote($note_id, $actor->getId(), source: 'ActivityPub');
  191. }
  192. // Store ActivityPub Activity
  193. $ap_act = \Plugin\ActivityPub\Entity\ActivitypubActivity::create([
  194. 'activity_id' => $act->getId(),
  195. 'activity_uri' => $type_activity->get('id'),
  196. 'created' => new \DateTime($type_activity->get('published') ?? 'now'),
  197. 'modified' => new \DateTime(),
  198. ]);
  199. DB::persist($ap_act);
  200. return Event::stop;
  201. }
  202. public function onNewActivityPubActivity(Actor $actor, \ActivityPhp\Type\AbstractObject $type_activity, \ActivityPhp\Type\AbstractObject $type_object, ?\Plugin\ActivityPub\Entity\ActivitypubActivity &$ap_act): bool
  203. {
  204. return $this->activitypub_handler($actor, $type_activity, $type_object, $ap_act);
  205. }
  206. public function onNewActivityPubActivityWithObject(Actor $actor, \ActivityPhp\Type\AbstractObject $type_activity, mixed $type_object, ?\Plugin\ActivityPub\Entity\ActivitypubActivity &$ap_act): bool
  207. {
  208. return $this->activitypub_handler($actor, $type_activity, $type_object, $ap_act);
  209. }
  210. public function onGSVerbToActivityStreamsTwoActivityType(string $verb, ?string &$gs_verb_to_activity_stream_two_verb): bool
  211. {
  212. if ($verb === 'favourite') {
  213. $gs_verb_to_activity_stream_two_verb = 'Like';
  214. return Event::stop;
  215. }
  216. return Event::next;
  217. }
  218. }