| 
									
										
										
										
											2021-11-27 04:12:44 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-23 13:18:50 +00:00
										 |  |  | declare(strict_types = 1); | 
					
						
							| 
									
										
										
										
											2021-11-27 04:12:44 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | // {{{ License
 | 
					
						
							|  |  |  | // This file is part of GNU social - https://www.gnu.org/software/social
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // GNU social is free software: you can redistribute it and/or modify
 | 
					
						
							|  |  |  | // it under the terms of the GNU Affero General Public License as published by
 | 
					
						
							|  |  |  | // the Free Software Foundation, either version 3 of the License, or
 | 
					
						
							|  |  |  | // (at your option) any later version.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // GNU social is distributed in the hope that it will be useful,
 | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
					
						
							|  |  |  | // GNU Affero General Public License for more details.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // You should have received a copy of the GNU Affero General Public License
 | 
					
						
							|  |  |  | // along with GNU social.  If not, see <http://www.gnu.org/licenses/>.
 | 
					
						
							|  |  |  | // }}}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Component\Notification; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-23 13:18:50 +00:00
										 |  |  | use App\Core\DB\DB; | 
					
						
							| 
									
										
										
										
											2021-11-27 04:12:44 +00:00
										 |  |  | use App\Core\Event; | 
					
						
							| 
									
										
										
										
											2021-12-23 13:18:50 +00:00
										 |  |  | use function App\Core\I18n\_m; | 
					
						
							| 
									
										
										
										
											2021-11-27 04:12:44 +00:00
										 |  |  | use App\Core\Log; | 
					
						
							|  |  |  | use App\Core\Modules\Component; | 
					
						
							| 
									
										
										
										
											2021-12-23 13:18:50 +00:00
										 |  |  | use App\Core\Router\RouteLoader; | 
					
						
							|  |  |  | use App\Core\Router\Router; | 
					
						
							| 
									
										
										
										
											2021-11-27 04:12:44 +00:00
										 |  |  | use App\Entity\Activity; | 
					
						
							|  |  |  | use App\Entity\Actor; | 
					
						
							| 
									
										
										
										
											2021-12-23 13:18:50 +00:00
										 |  |  | use App\Entity\LocalUser; | 
					
						
							| 
									
										
										
										
											2021-11-27 04:12:44 +00:00
										 |  |  | use Component\FreeNetwork\FreeNetwork; | 
					
						
							| 
									
										
										
										
											2021-12-23 13:18:50 +00:00
										 |  |  | use Component\Notification\Controller\Feed; | 
					
						
							| 
									
										
										
										
											2021-11-27 04:12:44 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class Notification extends Component | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-12-23 13:18:50 +00:00
										 |  |  |     public function onAddRoute(RouteLoader $m): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $m->connect('feed_notifications', '/feed/notifications', [Feed::class, 'notifications']); | 
					
						
							|  |  |  |         return Event::next; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function onCreateDefaultFeeds(int $actor_id, LocalUser $user, int &$ordering) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         DB::persist(\App\Entity\Feed::create([ | 
					
						
							|  |  |  |             'actor_id' => $actor_id, | 
					
						
							| 
									
										
										
										
											2021-12-29 13:27:04 +00:00
										 |  |  |             'url'      => Router::url($route = 'feed_notifications'), | 
					
						
							| 
									
										
										
										
											2021-12-23 13:18:50 +00:00
										 |  |  |             'route'    => $route, | 
					
						
							|  |  |  |             'title'    => _m('Notifications'), | 
					
						
							|  |  |  |             'ordering' => $ordering++, | 
					
						
							|  |  |  |         ])); | 
					
						
							|  |  |  |         return Event::next; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-27 04:12:44 +00:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Enqueues a notification for an Actor (user or group) which means | 
					
						
							|  |  |  |      * it shows up in their home feed and such. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function onNewNotification(Actor $sender, Activity $activity, array $ids_already_known = [], ?string $reason = null): bool | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-12-25 17:46:45 +00:00
										 |  |  |         $targets = $activity->getNotificationTargets(ids_already_known: $ids_already_known, sender_id: $sender->getId()); | 
					
						
							| 
									
										
										
										
											2021-11-29 23:58:27 +00:00
										 |  |  |         $this->notify($sender, $activity, $targets, $reason); | 
					
						
							| 
									
										
										
										
											2021-11-27 04:12:44 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return Event::next; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-04 19:52:14 +00:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Bring given Activity to Targets's attention | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-11-29 23:58:27 +00:00
										 |  |  |     public function notify(Actor $sender, Activity $activity, array $targets, ?string $reason = null): bool | 
					
						
							| 
									
										
										
										
											2021-11-27 04:12:44 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-11-29 23:58:27 +00:00
										 |  |  |         $remote_targets = []; | 
					
						
							|  |  |  |         foreach ($targets as $target) { | 
					
						
							|  |  |  |             if ($target->getIsLocal()) { | 
					
						
							|  |  |  |                 if ($target->isGroup()) { | 
					
						
							|  |  |  |                     // FIXME: Make sure we check (for both local and remote) users are in the groups they send to!
 | 
					
						
							|  |  |  |                 } else { | 
					
						
							|  |  |  |                     if ($target->hasBlocked($activity->getActor())) { | 
					
						
							|  |  |  |                         Log::info("Not saving reply to actor {$target->getId()} from sender {$sender->getId()} because of a block."); | 
					
						
							|  |  |  |                         continue; | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2021-12-28 18:45:18 +00:00
										 |  |  |                 if (Event::handle('NewNotificationShould', [$activity, $target]) === Event::next) { | 
					
						
							|  |  |  |                     // TODO: use https://symfony.com/doc/current/notifier.html
 | 
					
						
							|  |  |  |                     DB::persist(Entity\Notification::create([ | 
					
						
							|  |  |  |                         'activity_id' => $activity->getId(), | 
					
						
							|  |  |  |                         'target_id'   => $target->getId(), | 
					
						
							|  |  |  |                         'reason'      => $reason, | 
					
						
							|  |  |  |                     ])); | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2021-11-29 23:58:27 +00:00
										 |  |  |             } else { | 
					
						
							| 
									
										
										
										
											2021-12-25 17:46:45 +00:00
										 |  |  |                 // We have no authority nor responsibility of notifying remote actors of a remote actor's doing
 | 
					
						
							|  |  |  |                 if ($sender->getIsLocal()) { | 
					
						
							|  |  |  |                     $remote_targets[] = $target; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2021-11-27 04:12:44 +00:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-29 23:58:27 +00:00
										 |  |  |         FreeNetwork::notify($sender, $activity, $remote_targets, $reason); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return Event::next; | 
					
						
							| 
									
										
										
										
											2021-11-27 04:12:44 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | } |