Skip to content

Commit

Permalink
Merge pull request #977 from nextcloud/fix/noid/dm-on-unknown-recipient
Browse files Browse the repository at this point in the history
fix mention based on content
  • Loading branch information
ArtificialOwl authored Aug 22, 2020
2 parents 2194c11 + 0da568f commit f876c25
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 10 deletions.
4 changes: 3 additions & 1 deletion lib/Model/ActivityPub/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,9 @@ public function getToAll(): array {
* @return Item
*/
public function addToArray(string $to): Item {
$this->toArray[] = $to;
if (!in_array($to, $this->toArray)) {
$this->toArray[] = $to;
}

return $this;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Model/ActivityPub/Object/Note.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function fillHashtags() {
if (substr($hashtag, 0, 1) === '#') {
$hashtag = substr($hashtag, 1);
}
$hashtags[] = $hashtag;
$hashtags[] = trim($hashtag);
}

$this->setHashtags($hashtags);
Expand Down
11 changes: 10 additions & 1 deletion lib/Model/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ public function getActor(): Person {
* @return Post
*/
public function addTo(string $to): Post {
if ($to !== '') {
$to = trim($to);
if ($to !== '' && !in_array($to, $this->to)) {
$this->to[] = $to;
}

Expand Down Expand Up @@ -175,6 +176,14 @@ public function setHashtags(array $hashtags): Post {
return $this;
}

public function addHashtag(string $hashtag): Post {
$hashtag = trim($hashtag);
if ($hashtag !== '' && !in_array($hashtag, $this->hashtags)) {
$this->hashtags[] = $hashtag;
}

return $this;
}

/**
* @return string[]
Expand Down
29 changes: 24 additions & 5 deletions lib/Service/PostService.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@


use daita\MySmallPhpTools\Exceptions\MalformedArrayException;
use daita\MySmallPhpTools\Exceptions\RequestContentException;
use daita\MySmallPhpTools\Exceptions\RequestNetworkException;
use daita\MySmallPhpTools\Exceptions\RequestResultNotJsonException;
use daita\MySmallPhpTools\Exceptions\RequestResultSizeException;
use daita\MySmallPhpTools\Exceptions\RequestServerException;
use Exception;
use OCA\Social\AP;
use OCA\Social\Exceptions\CacheContentMimeTypeException;
use OCA\Social\Exceptions\InvalidOriginException;
use OCA\Social\Exceptions\InvalidResourceException;
use OCA\Social\Exceptions\ItemUnknownException;
use OCA\Social\Exceptions\RedundancyLimitException;
use daita\MySmallPhpTools\Exceptions\RequestContentException;
use daita\MySmallPhpTools\Exceptions\RequestNetworkException;
use daita\MySmallPhpTools\Exceptions\RequestResultNotJsonException;
use daita\MySmallPhpTools\Exceptions\RequestResultSizeException;
use daita\MySmallPhpTools\Exceptions\RequestServerException;
use OCA\Social\Exceptions\SocialAppConfigException;
use OCA\Social\Exceptions\StreamNotFoundException;
use OCA\Social\Exceptions\UnauthorizedFediverseException;
Expand Down Expand Up @@ -119,6 +119,8 @@ public function __construct(
* @throws UnauthorizedFediverseException
*/
public function createPost(Post $post, &$token = ''): ACore {
$this->fixRecipientAndHashtags($post);

$note = new Note();
$actor = $post->getActor();
$this->streamService->assignItem($note, $actor, $post->getType());
Expand Down Expand Up @@ -192,5 +194,22 @@ private function generateDocumentFromAttachment(Note $note, string $attachment):
}


/**
* @param Post $post
*/
public function fixRecipientAndHashtags(Post $post) {
preg_match_all('/(?!\b)@([^\s]+)/', $post->getContent(), $matchesTo);
preg_match_all('/(?!\b)#([^\s]+)/', $post->getContent(), $matchesHash);

foreach ($matchesTo[1] as $to) {
$post->addTo($to);
}

foreach ($matchesHash[1] as $hash) {
$post->addHashtag($hash);
}

}

}

4 changes: 2 additions & 2 deletions lib/Service/StreamService.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public function addRecipient(Stream $stream, string $type, string $account) {
}

try {
$actor = $this->cacheActorService->getFromAccount($account);
$actor = $this->cacheActorService->getFromAccount($account, true);
} catch (Exception $e) {
return;
}
Expand All @@ -253,7 +253,7 @@ public function addRecipient(Stream $stream, string $type, string $account) {
if ($type === Stream::TYPE_DIRECT) {
$instancePath->setPriority(InstancePath::PRIORITY_HIGH);
$stream->addToArray($actor->getId());
$stream->setFilterDuplicate(true);
$stream->setFilterDuplicate(true); // TODO: really needed ?
} else {
$stream->addCc($actor->getId());
}
Expand Down

0 comments on commit f876c25

Please sign in to comment.