diff --git a/components/FreeNetwork/FreeNetwork.php b/components/FreeNetwork/FreeNetwork.php index 5831006c77..7dc6f72002 100644 --- a/components/FreeNetwork/FreeNetwork.php +++ b/components/FreeNetwork/FreeNetwork.php @@ -22,6 +22,7 @@ declare(strict_types = 1); namespace Component\FreeNetwork; use App\Core\Event; +use App\Entity\Activity; use function App\Core\I18n\_m; use App\Core\Log; use App\Core\Modules\Component; @@ -48,6 +49,9 @@ use Plugin\ActivityPub\Util\Response\TypeResponse; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use XML_XRD_Element_Link; +use function count; +use function in_array; +use const PREG_SET_ORDER; /** * Implements WebFinger (RFC7033) for GNU social, as well as Link-based Resource Descriptor Discovery based on RFC6415, @@ -110,7 +114,7 @@ class FreeNetwork extends Component $profile = null; if (Discovery::isAcct($resource)) { $parts = explode('@', mb_substr(urldecode($resource), 5)); // 5 is strlen of 'acct:' - if (\count($parts) == 2) { + if (count($parts) == 2) { [$nick, $domain] = $parts; if ($domain !== $_ENV['SOCIAL_DOMAIN']) { throw new ServerException(_m('Remote profiles not supported via WebFinger yet.')); @@ -135,9 +139,9 @@ class FreeNetwork extends Component $renick = '/\/@(' . Nickname::DISPLAY_FMT . ')\/?/m'; // actor_view_id $reuri = '/\/actor\/(\d+)\/?/m'; - if (preg_match_all($renick, $str, $matches, \PREG_SET_ORDER, 0) === 1) { + if (preg_match_all($renick, $str, $matches, PREG_SET_ORDER, 0) === 1) { $profile = LocalUser::getWithPK(['nickname' => $matches[0][1]])->getActor(); - } elseif (preg_match_all($reuri, $str, $matches, \PREG_SET_ORDER, 0) === 1) { + } elseif (preg_match_all($reuri, $str, $matches, PREG_SET_ORDER, 0) === 1) { $profile = Actor::getById((int) $matches[0][1]); } } @@ -230,7 +234,7 @@ class FreeNetwork extends Component */ public function onControllerResponseInFormat(string $route, array $accept_header, array $vars, ?TypeResponse &$response = null): bool { - if (!\in_array($route, ['freenetwork_hostmeta', 'freenetwork_hostmeta_format', 'freenetwork_webfinger', 'freenetwork_webfinger_format', 'freenetwork_ownerxrd'])) { + if (!in_array($route, ['freenetwork_hostmeta', 'freenetwork_hostmeta_format', 'freenetwork_webfinger', 'freenetwork_webfinger_format', 'freenetwork_ownerxrd'])) { return Event::next; } @@ -244,7 +248,7 @@ class FreeNetwork extends Component * -- RFC 7033 (WebFinger) * http://tools.ietf.org/html/rfc7033 */ - $mimeType = \count($mimeType) !== 0 ? array_pop($mimeType) : $vars['default_mimetype']; + $mimeType = count($mimeType) !== 0 ? array_pop($mimeType) : $vars['default_mimetype']; $headers = []; @@ -261,6 +265,12 @@ class FreeNetwork extends Component return Event::stop; } + public static function notify(Actor $sender, Activity $activity, Actor $target, ?string $reason = null): bool + { + // TODO: implement + return false; + } + public function onPluginVersion(array &$versions): bool { $versions[] = [ diff --git a/src/Entity/Notification.php b/components/Notification/Entity/Notification.php similarity index 71% rename from src/Entity/Notification.php rename to components/Notification/Entity/Notification.php index 4cd8a05bd1..2c3afc399a 100644 --- a/src/Entity/Notification.php +++ b/components/Notification/Entity/Notification.php @@ -17,9 +17,11 @@ // along with GNU social. If not, see . // }}} -namespace App\Entity; +namespace Component\Notification\Entity; +use App\Core\DB\DB; use App\Core\Entity; +use App\Entity\Actor; use DateTimeInterface; /** @@ -41,7 +43,7 @@ class Notification extends Entity // {{{ Autocode // @codeCoverageIgnoreStart private int $activity_id; - private int $actor_id; + private int $target_id; private ?string $reason; private \DateTimeInterface $created; private \DateTimeInterface $modified; @@ -57,15 +59,15 @@ class Notification extends Entity return $this->activity_id; } - public function setActorId(int $actor_id): self + public function setTargetId(int $target_id): self { - $this->actor_id = $actor_id; + $this->target_id = $target_id; return $this; } - public function getActorId(): int + public function getTargetId(): int { - return $this->actor_id; + return $this->target_id; } public function setReason(?string $reason): self @@ -104,6 +106,38 @@ class Notification extends Entity // @codeCoverageIgnoreEnd // }}} Autocode + /** + * @return Actor + */ + public function getTarget(): Actor + { + return Actor::getById($this->getActorId()); + } + + /** + * Pull the complete list of known activity context notifications for this activity. + * + * @return array of integer actor ids (also group profiles) + */ + public static function getNotificationTargetIdsByActivity(int|Activity $activity_id): array + { + $notifications = DB::findBy('notification', ['activity_id' => is_int($activity_id) ? $activity_id : $activity_id->getId()]); + $targets = []; + foreach ($notifications as $notification) { + $targets[] = $notification->getTargetId(); + } + return $targets; + } + + /** + * @param int|Activity $activity_id + * @return array + */ + public function getNotificationTargetsByActivity(int|Activity $activity_id): array + { + return DB::findBy('actor', ['id' => $this->getNotificationTargetIdsByActivity($activity_id)]); + } + public static function schemaDef(): array { return [ @@ -111,15 +145,15 @@ class Notification extends Entity 'description' => 'Activity notification for actors (that are not a mention and not result of a subscription)', 'fields' => [ 'activity_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Activity.id', 'multiplicity' => 'one to one', 'not null' => true, 'description' => 'activity_id to give attention'], - 'actor_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Actor.id', 'multiplicity' => 'one to one', 'not null' => true, 'description' => 'actor_id for feed receiver'], + 'target_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Actor.id', 'multiplicity' => 'one to one', 'not null' => true, 'description' => 'actor_id for feed receiver'], 'reason' => ['type' => 'varchar', 'length' => 191, 'description' => 'Optional reason why this was brought to the attention of actor_id'], 'created' => ['type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was created'], 'modified' => ['type' => 'timestamp', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'], ], - 'primary key' => ['activity_id', 'actor_id'], + 'primary key' => ['activity_id', 'target_id'], 'indexes' => [ 'attention_activity_id_idx' => ['activity_id'], - 'attention_actor_id_idx' => ['actor_id'], + 'attention_target_id_idx' => ['target_id'], ], ]; } diff --git a/src/Entity/UserNotificationPrefs.php b/components/Notification/Entity/UserNotificationPrefs.php similarity index 99% rename from src/Entity/UserNotificationPrefs.php rename to components/Notification/Entity/UserNotificationPrefs.php index b132e42f17..bfaf773026 100644 --- a/src/Entity/UserNotificationPrefs.php +++ b/components/Notification/Entity/UserNotificationPrefs.php @@ -17,7 +17,7 @@ // along with GNU social. If not, see . // }}} -namespace App\Entity; +namespace Component\Notification\Entity; use App\Core\Entity; use DateTimeInterface; diff --git a/components/Notification/Notification.php b/components/Notification/Notification.php new file mode 100644 index 0000000000..def7940d11 --- /dev/null +++ b/components/Notification/Notification.php @@ -0,0 +1,68 @@ +. +// }}} + +namespace Component\Notification; + +use App\Core\DB\DB; +use App\Core\Event; +use App\Core\Log; +use App\Core\Modules\Component; +use App\Entity\Activity; +use App\Entity\Actor; +use Component\FreeNetwork\FreeNetwork; +use DateTime; +use DateTimeInterface; + +class Notification extends Component +{ + /** + * 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 + { + $targets = $activity->getNotificationTargets($ids_already_known); + foreach ($targets as $target) { + $this->notify($sender, $activity, $target, $reason); + } + + return Event::next; + } + + public function notify(Actor $sender, Activity $activity, Actor $target, ?string $reason = null): bool + { + 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."); + return false; + } + } + + if ($target->getIsLocal()) { + // TODO: use https://symfony.com/doc/current/notifier.html + } else { + return FreeNetwork::notify($sender, $activity, $target, $reason); + } + return true; + } +} diff --git a/components/Posting/Posting.php b/components/Posting/Posting.php index c330fb5820..cc9522d6f4 100644 --- a/components/Posting/Posting.php +++ b/components/Posting/Posting.php @@ -28,6 +28,8 @@ use App\Core\DB\DB; use App\Core\Event; use App\Core\Form; use App\Core\GSFile; +use App\Core\Router\Router; +use App\Entity\Activity; use function App\Core\I18n\_m; use App\Core\Modules\Component; use App\Core\Security; @@ -152,7 +154,8 @@ class Posting extends Component public static function storeLocalNote(Actor $actor, string $content, string $content_type, string $language, array $attachments = [], $processed_attachments = []) { $rendered = null; - Event::handle('RenderNoteContent', [$content, $content_type, &$rendered, $actor, $language]); + $mentions = []; + Event::handle('RenderNoteContent', [$content, $content_type, &$rendered, &$mentions, $actor, $language]); $note = Note::create([ 'actor_id' => $actor->getId(), 'content' => $content, @@ -177,6 +180,7 @@ class Posting extends Component DB::persist($note); // Need file and note ids for the next step + $note->setUrl(Router::url('note_view', ['id' => $note->getId()], Router::ABSOLUTE_URL)); Event::handle('ProcessNoteContent', [$note, $content, $content_type]); if ($processed_attachments !== []) { @@ -188,17 +192,36 @@ class Posting extends Component } } + $act = Activity::create([ + 'actor_id' => $actor->getId(), + 'verb' => 'create', + 'object_type' => 'note', + 'object_id' => $note->getId(), + 'is_local' => true, + 'source' => 'web', + ]); + DB::persist($act); + DB::flush(); + $mentioned = []; + foreach ($mentions as $mention) { + foreach ($mention['mentioned'] as $m) { + $mentioned[] = $m->getId(); + } + } + + Event::handle('NewNotification', [$actor, $act, ['object' => $mentioned], "{$actor->getNickname()} created note {$note->getUrl()}", ]); + return $note; } - public function onRenderNoteContent(string $content, string $content_type, ?string &$rendered, Actor $author, string $language, ?Note $reply_to = null) + public function onRenderNoteContent(string $content, string $content_type, ?string &$rendered, array &$mentions, Actor $author, string $language) { switch ($content_type) { case 'text/plain': $rendered = Formatting::renderPlainText($content, $language); - $rendered = Formatting::linkifyMentions($rendered, $author, $language, $reply_to); + [$rendered, $mentions] = Formatting::linkifyMentions($rendered, $author, $language); return Event::stop; case 'text/html': // TODO: It has to linkify and stuff as well diff --git a/src/Core/Entity.php b/src/Core/Entity.php index ef8eee3d84..b380982516 100644 --- a/src/Core/Entity.php +++ b/src/Core/Entity.php @@ -134,4 +134,13 @@ abstract class Entity return null; } } + + /** + * + * @return array of Actors + */ + public function getNotificationTargets(array $ids_already_known = []): array + { + return []; + } } diff --git a/src/Entity/Activity.php b/src/Entity/Activity.php new file mode 100644 index 0000000000..9848cc7f80 --- /dev/null +++ b/src/Entity/Activity.php @@ -0,0 +1,226 @@ +. + +// }}} + +namespace App\Entity; + +use App\Core\DB\DB; +use App\Core\Entity; +use App\Core\Event; +use App\Core\Log; +use App\Entity\Actor; +use Component\Notification\Entity\Notification; +use DateTimeInterface; +use Exception; + +/** + * Entity for all activities we know about + * + * @category DB + * @package GNUsocial + * + * @author Hugo Sales + * @author Diogo Peralta Cordeiro <@diogo.site> + * @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org + * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later + */ +class Activity extends Entity +{ + // {{{ Autocode + // @codeCoverageIgnoreStart + private int $id; + private int $actor_id; + private string $verb; + private string $object_type; + private int $object_id; + private bool $is_local; + private ?string $source; + private \DateTimeInterface $created; + + public function setId(int $id): self + { + $this->id = $id; + return $this; + } + + public function getId(): int + { + return $this->id; + } + + public function setActorId(int $actor_id): self + { + $this->actor_id = $actor_id; + return $this; + } + + public function getActorId(): int + { + return $this->actor_id; + } + + public function setVerb(string $verb): self + { + $this->verb = $verb; + return $this; + } + + public function getVerb(): string + { + return $this->verb; + } + + public function setObjectType(string $object_type): self + { + $this->object_type = $object_type; + return $this; + } + + public function getObjectType(): string + { + return $this->object_type; + } + + public function setObjectId(int $object_id): self + { + $this->object_id = $object_id; + return $this; + } + + public function getObjectId(): int + { + return $this->object_id; + } + + public function setIsLocal(bool $is_local): self + { + $this->is_local = $is_local; + return $this; + } + + public function getIsLocal(): bool + { + return $this->is_local; + } + + public function setSource(?string $source): self + { + $this->source = $source; + return $this; + } + + public function getSource(): ?string + { + return $this->source; + } + + public function setCreated(DateTimeInterface $created): self + { + $this->created = $created; + return $this; + } + + public function getCreated(): DateTimeInterface + { + return $this->created; + } + + // @codeCoverageIgnoreEnd + // }}} Autocode + + public function getActor(): Actor + { + return Actor::getById($this->getActorId()); + } + + public function getObject(): mixed + { + return DB::findBy($this->getObjectType(), ['id' => $this->getObjectId()]); + } + + /** + * @return array + */ + public function getNotificationTargetIdsFromActorTags(): array + { + $actors = []; + $self_tags = $this->getActor()->getSelfTags(); + foreach ($self_tags as $circle) { + // Get subscriptions + array_push($actors, ...$circle->getSubscribedActors()); + } + return $actors; + } + + /** + * + * @return array of Actors + */ + public function getNotificationTargets(array $ids_already_known = []): array + { + $target_ids = []; + + // Actor Circles + if (array_key_exists('actor_circle', $ids_already_known)) { + array_push($target_ids, ...$ids_already_known['actor_circle']); + } else { + array_push($target_ids, ...$this->getNotificationTargetIdsFromActorTags()); + } + + // Notifications + if (array_key_exists('notification_activity', $ids_already_known)) { + array_push($target_ids, ...$ids_already_known['notification_activity']); + } else { + array_push($target_ids, ...Notification::getNotificationTargetIdsByActivity($this->getId())); + } + + // Object's targets + if (array_key_exists('object', $ids_already_known)) { + array_push($target_ids, ...$ids_already_known['object']); + } else { + array_push($target_ids, ...$this->getObject()->getNotificationTargets($ids_already_known)); + } + + // Additional actors that should know about this + if (array_key_exists('additional', $ids_already_known)) { + array_push($target_ids, ...$ids_already_known['additional']); + } + + return DB::findBy('actor', ['id' => array_unique($target_ids)]); + } + + public static function schemaDef(): array + { + return [ + 'name' => 'activity', + 'fields' => [ + 'id' => ['type' => 'serial', 'not null' => true], + 'actor_id' => ['type' => 'int', 'not null' => true, 'description' => 'foreign key to actor table'], + 'verb' => ['type' => 'varchar', 'length' => 32, 'not null' => true, 'description' => 'internal activity verb, influenced by activity pub verbs'], + 'object_type' => ['type' => 'varchar', 'length' => 32, 'not null' => true, 'description' => 'the name of the table this object refers to'], + 'object_id' => ['type' => 'int', 'not null' => true, 'description' => 'id in the referenced table'], + 'is_local' => ['type' => 'bool', 'not null' => true, 'description' => 'whether this was a locally generated or an imported activity'], + 'source' => ['type' => 'varchar', 'length' => 32, 'description' => 'the source of this activity'], + 'created' => ['type' => 'datetime', 'not null' => true, 'description' => 'date this record was created', 'default' => 'CURRENT_TIMESTAMP'], + ], + 'primary key' => ['id'], + ]; + } +} diff --git a/src/Entity/Actor.php b/src/Entity/Actor.php index dd85b1d3b4..94922a8505 100644 --- a/src/Entity/Actor.php +++ b/src/Entity/Actor.php @@ -240,6 +240,12 @@ class Actor extends Entity } } + public function isGroup() + { + // TODO: implement + return false; + } + public function getAvatarUrl(string $size = 'full') { return Avatar::getAvatarUrl($this->getId(), $size); diff --git a/src/Entity/ActorCircle.php b/src/Entity/ActorCircle.php index 3258288be7..f0403d641c 100644 --- a/src/Entity/ActorCircle.php +++ b/src/Entity/ActorCircle.php @@ -139,11 +139,11 @@ class ActorCircle extends Entity "circle-{$this->getId()}", fn() => DB::dql( <<< EOQ - SELECT actor - FROM App\Entity\Actor actor - JOIN App\Entity\ActorCircleSubscription subscription - WITH actor.id = subscription.actor_id - ORDER BY subscription.created DESC, actor.id DESC + SELECT a + FROM App\Entity\Actor a + JOIN App\Entity\ActorCircleSubscription s + WITH a.id = s.actor_id + ORDER BY s.created DESC, a.id DESC EOQ, options: ['offset' => $offset, diff --git a/src/Entity/Note.php b/src/Entity/Note.php index cf611fff43..10574e28e2 100644 --- a/src/Entity/Note.php +++ b/src/Entity/Note.php @@ -26,8 +26,10 @@ namespace App\Entity; use App\Core\Cache; use App\Core\DB\DB; use App\Core\Entity; +use App\Core\Event; use App\Core\VisibilityScope; use Component\Avatar\Avatar; +use Component\Notification\Entity\Notification; use DateTimeInterface; /** @@ -298,6 +300,19 @@ class Note extends Entity )); } + public function getNotificationTargets(array $ids_already_known = []): array + { + $mentions = []; + Event::handle('RenderNoteContent', [$this->getContent(), $this->getContentType(), &$rendered, &$mentions, $this->getActor(), Language::getFromId($this->getLanguageId())->getLocale()]); + $mentioned = []; + foreach ($mentions as $mention) { + foreach ($mention['mentioned'] as $m) { + $mentioned[] = $m; + } + } + return $mentioned; + } + public static function schemaDef(): array { return [ diff --git a/src/Util/Formatting.php b/src/Util/Formatting.php index fb2b8d29ee..543e83e537 100644 --- a/src/Util/Formatting.php +++ b/src/Util/Formatting.php @@ -135,7 +135,7 @@ abstract class Formatting /** * If $haystack ends with $needle, remove it from the end */ - public static function removeSuffix(string $haystack, string $needle) + public static function removeSuffix(string $haystack, string $needle): string { return self::endsWith($haystack, $needle) && !empty($needle) ? mb_substr($haystack, 0, -mb_strlen($needle)) : $haystack; } @@ -197,7 +197,7 @@ abstract class Formatting } /** - * Convert a user supplied string to array and return whether the conversion was successfull + * Convert a user supplied string to array and return whether the conversion was successful */ public static function toArray(string $input, &$output, string $split_type = self::SPLIT_BY_COMMA): bool { @@ -267,7 +267,7 @@ abstract class Formatting $str = mb_convert_case($str, \MB_CASE_LOWER, 'UTF-8'); return mb_substr($str, 0, $length); } - $str = transliterator_transliterate('Any-Latin;' // any charset to latin compatible + $str = transliterator_transliterate('Any-Latin;' // any charset to latin compatible . 'NFD;' // decompose . '[:Nonspacing Mark:] Remove;' // remove nonspacing marks (accents etc.) . 'NFC;' // composite again @@ -287,27 +287,13 @@ abstract class Formatting * such. Should not be used directly; rather, call common_linkify_mentions(). * * @param Actor $actor the Actor that is sending the current text - * @param Note $parent the Note this text is in reply to, if any */ - public static function findMentions(string $text, Actor $actor, ?Note $parent = null): array + public static function findMentions(string $text, Actor $actor): array { $mentions = []; if (Event::handle('StartFindMentions', [$actor, $text, &$mentions])) { - // Get the context of the original notice, if any - $origMentions = []; - // Does it have a parent notice for context? - if ($parent instanceof Note) { - foreach ($parent->getAttentionProfiles() as $repliedTo) { - if (!$repliedTo->isPerson()) { - continue; - } - $origMentions[$repliedTo->getId()] = $repliedTo; - } - } - $matches = self::findMentionsRaw($text, '@'); - //dd($matches); foreach ($matches as $match) { try { $nickname = Nickname::normalize($match[0], check_already_used: false); @@ -316,28 +302,7 @@ abstract class Formatting continue; } - // primarily mention the profiles mentioned in the parent - $mention_found_in_origMentions = false; - foreach ($origMentions as $origMentionsId => $origMention) { - if ($origMention->getNickname() == $nickname) { - $mention_found_in_origMentions = $origMention; - // don't mention same twice! the parent might have mentioned - // two users with same nickname on different instances - unset($origMentions[$origMentionsId]); - break; - } - } - - // Try to get a profile for this nickname. - // Start with parents mentions, then go to parents sender context - if ($mention_found_in_origMentions) { - $mentioned = $mention_found_in_origMentions; - } elseif ($parent instanceof Note && $parent->getActorNickname() === $nickname) { - $mentioned = $parent->getActor(); - } else { - // sets to null if no match - $mentioned = $actor->findRelativeActor($nickname); - } + $mentioned = $actor->findRelativeActor($nickname); if ($mentioned instanceof Actor) { $url = $mentioned->getUri(); // prefer the URI as URL, if it is one. @@ -411,7 +376,6 @@ abstract class Formatting Event::handle('EndFindMentions', [$actor, $text, &$mentions]); } - //dd($text,$mentions); return $mentions; } @@ -425,14 +389,6 @@ abstract class Formatting */ private static function findMentionsRaw(string $text, string $preMention = '@'): array { - $tmatches = []; - preg_match_all( - '/^T (' . Nickname::DISPLAY_FMT . ') /', - $text, - $tmatches, - \PREG_OFFSET_CAPTURE, - ); - $atmatches = []; // the regexp's "(?!\@)" makes sure it doesn't matches the single "@remote" in "@remote@server.com" preg_match_all( @@ -441,9 +397,8 @@ abstract class Formatting $atmatches, \PREG_OFFSET_CAPTURE, ); - //dd('/' . Nickname::BEFORE_MENTIONS . preg_quote($preMention, '/') . '(' . Nickname::DISPLAY_FMT . ')\b(?!\@)/', $atmatches); - return array_merge($tmatches[1], $atmatches[1]); + return $atmatches[1]; } /** @@ -454,13 +409,12 @@ abstract class Formatting * * @param string $text partially-rendered HTML * @param Actor $author the Actor that is composing the current notice - * @param Note $parent the Note this is sent in reply to, if any * * @return string partially-rendered HTML */ - public static function linkifyMentions(string $text, Actor $author, string $language, ?Note $parent = null): string + public static function linkifyMentions(string $text, Actor $author, string $language): array { - $mentions = self::findMentions($text, $author, $parent); + $mentions = self::findMentions($text, $author); // We need to go through in reverse order by position, // so our positions stay valid despite our fudging with the @@ -480,7 +434,7 @@ abstract class Formatting $text = substr_replace($text, $linkText, $position, $mention['length']); } - return $text; + return [$text, $mentions]; } public static function linkifyMentionArray(array $mention)