summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorMaxence Lange <maxence@artificial-owl.com>2019-01-02 22:31:25 -0100
committerMaxence Lange <maxence@artificial-owl.com>2019-02-05 15:10:24 -0100
commit01ed889984f710371e65132110fabda875a6f2ad (patch)
tree82fe5d7cc764395aa03aa6c35708706fe16f779f /lib
parentf5da0c0e7e9bb4bb23f390ca770036bd3f061818 (diff)
adding hashtags on post creation
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/Command/NoteCreate.php6
-rw-r--r--lib/Controller/LocalController.php1
-rw-r--r--lib/Model/Post.php22
-rw-r--r--lib/Service/NoteService.php38
-rw-r--r--lib/Service/PostService.php1
5 files changed, 63 insertions, 5 deletions
diff --git a/lib/Command/NoteCreate.php b/lib/Command/NoteCreate.php
index 1d16de9d..96867c54 100644
--- a/lib/Command/NoteCreate.php
+++ b/lib/Command/NoteCreate.php
@@ -108,6 +108,10 @@ class NoteCreate extends Base {
'type', 'y', InputOption::VALUE_OPTIONAL,
'type: public (default), followers, unlisted, direct'
)
+ ->addOption(
+ 'hashtag', 'g', InputOption::VALUE_OPTIONAL,
+ 'hashtag, without the leading #'
+ )
->addArgument('userid', InputArgument::REQUIRED, 'userId of the author')
->addArgument('content', InputArgument::REQUIRED, 'content of the post')
->setDescription('Create a new note');
@@ -125,6 +129,7 @@ class NoteCreate extends Base {
$userId = $input->getArgument('userid');
$content = $input->getArgument('content');
$to = $input->getOption('to');
+ $hashtag = $input->getOption('hashtag');
$replyTo = $input->getOption('replyTo');
$type = $input->getOption('type');
@@ -134,6 +139,7 @@ class NoteCreate extends Base {
$post->setType(($type === null) ? '' : $type);
$post->setReplyTo(($replyTo === null) ? '' : $replyTo);
$post->addTo(($to === null) ? '' : $to);
+ $post->setHashtags(($hashtag === null) ? [] : [$hashtag]);
$token = $this->postService->createPost($post, $activity);
diff --git a/lib/Controller/LocalController.php b/lib/Controller/LocalController.php
index a65752b0..9b435490 100644
--- a/lib/Controller/LocalController.php
+++ b/lib/Controller/LocalController.php
@@ -147,6 +147,7 @@ class LocalController extends Controller {
$post->setTo($this->getArray('to', $data, []));
$post->addTo($this->get('to', $data, ''));
$post->setType($this->get('type', $data, Note::TYPE_PUBLIC));
+ $post->setHashtags($this->getArray('hashtags', $data, []));
/** @var ACore $activity */
$token = $this->postService->createPost($post, $activity);
diff --git a/lib/Model/Post.php b/lib/Model/Post.php
index 7ce1c6bd..03d6aeb4 100644
--- a/lib/Model/Post.php
+++ b/lib/Model/Post.php
@@ -61,6 +61,9 @@ class Post implements JsonSerializable {
/** @var string */
private $type = '';
+ /** @var array */
+ private $hashtags = [];
+
/**
* Post constructor.
@@ -149,6 +152,25 @@ class Post implements JsonSerializable {
/**
+ * @return array
+ */
+ public function getHashtags(): array {
+ return $this->hashtags;
+ }
+
+ /**
+ * @param array $hashtags
+ *
+ * @return Post
+ */
+ public function setHashtags(array $hashtags): Post {
+ $this->hashtags = $hashtags;
+
+ return $this;
+ }
+
+
+ /**
* @return string
*/
public function getContent(): string {
diff --git a/lib/Service/NoteService.php b/lib/Service/NoteService.php
index 8e444523..af3860f5 100644
--- a/lib/Service/NoteService.php
+++ b/lib/Service/NoteService.php
@@ -212,7 +212,8 @@ class NoteService {
$note->addTag(
[
'type' => 'Mention',
- 'href' => $actor->getId()
+ 'href' => $actor->getId(),
+ 'name' => '@' . $account
]
);
@@ -222,14 +223,30 @@ class NoteService {
/**
* @param Note $note
+ * @param string $hashtag
+ */
+ public function addHashtag(Note $note, string $hashtag) {
+ try {
+ $note->addTag(
+ [
+ 'type' => 'Hashtag',
+ 'href' => $this->configService->getCloudAddress() . '/tag/' . strtolower(
+ $hashtag
+ ),
+ 'name' => '#' . $hashtag
+ ]
+ );
+ } catch (SocialAppConfigException $e) {
+ }
+ }
+
+
+ /**
+ * @param Note $note
* @param string $type
* @param array $accounts
*/
public function addRecipients(Note $note, string $type, array $accounts) {
- if ($accounts === []) {
- return;
- }
-
foreach ($accounts as $account) {
$this->addRecipient($note, $type, $account);
}
@@ -238,6 +255,17 @@ class NoteService {
/**
* @param Note $note
+ * @param array $hashtags
+ */
+ public function addHashtags(Note $note, array $hashtags) {
+ foreach ($hashtags as $hashtag) {
+ $this->addHashtag($note, $hashtag);
+ }
+ }
+
+
+ /**
+ * @param Note $note
* @param string $replyTo
*
* @throws InvalidOriginException
diff --git a/lib/Service/PostService.php b/lib/Service/PostService.php
index 89c0f603..817ddc40 100644
--- a/lib/Service/PostService.php
+++ b/lib/Service/PostService.php
@@ -104,6 +104,7 @@ class PostService {
$this->noteService->replyTo($note, $post->getReplyTo());
$this->noteService->addRecipients($note, $post->getType(), $post->getTo());
+ $this->noteService->addHashtags($note, $post->getHashtags());
$result = $this->activityService->createActivity($post->getActor(), $note, $activity);
$this->accountService->cacheLocalActorDetailCount($post->getActor());