Skip to content

Commit b3bb528

Browse files
authored
Merge pull request 5am-code#111 from 5am-code/feature/comments
Feature/comments
2 parents ff4794a + 63d288a commit b3bb528

12 files changed

+523
-0
lines changed

src/Endpoints/Comments.php

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
3+
namespace FiveamCode\LaravelNotionApi\Endpoints;
4+
5+
use FiveamCode\LaravelNotionApi\Entities\Collections\CommentCollection;
6+
use FiveamCode\LaravelNotionApi\Entities\Comment as CommentEntity;
7+
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
8+
use FiveamCode\LaravelNotionApi\Exceptions\NotionException;
9+
use FiveamCode\LaravelNotionApi\Notion;
10+
11+
/**
12+
* Class Comments.
13+
*/
14+
class Comments extends Endpoint
15+
{
16+
/**
17+
* @var ?string
18+
*/
19+
private ?string $discussionId = null;
20+
21+
/**
22+
* @var ?string
23+
*/
24+
private ?string $pageId = null;
25+
26+
/**
27+
* Block constructor.
28+
*
29+
* @param Notion $notion
30+
* @param string $blockId
31+
*
32+
* @throws HandlingException
33+
* @throws \FiveamCode\LaravelNotionApi\Exceptions\LaravelNotionAPIException
34+
*/
35+
public function __construct(Notion $notion)
36+
{
37+
parent::__construct($notion);
38+
}
39+
40+
/**
41+
* Retrieve a list of comments
42+
* url: https://api.notion.com/{version}/comments?block_id=* [get]
43+
* notion-api-docs: https://developers.notion.com/reference/retrieve-a-comment.
44+
*
45+
* @param string $blockId
46+
* @return CommentCollection
47+
*
48+
* @throws HandlingException
49+
* @throws NotionException
50+
*/
51+
public function ofBlock(string $blockId): CommentCollection
52+
{
53+
$response = $this->get(
54+
$this->url(Endpoint::COMMENTS."?block_id={$blockId}&{$this->buildPaginationQuery()}")
55+
);
56+
57+
return new CommentCollection($response->json());
58+
}
59+
60+
/**
61+
* @param string $discussionId
62+
* @return Comments
63+
*/
64+
public function onDiscussion(string $discussionId): self
65+
{
66+
if ($this->pageId !== null) {
67+
throw new HandlingException('You can only use `onDiscussion()` or `onPage()`.');
68+
}
69+
70+
$this->discussionId = $discussionId;
71+
72+
return $this;
73+
}
74+
75+
/**
76+
* @param string $pageId
77+
* @return Comments
78+
*/
79+
public function onPage(string $pageId): self
80+
{
81+
if ($this->discussionId !== null) {
82+
throw new HandlingException('You can only use `onDiscussion()` or `onPage()`.');
83+
}
84+
85+
$this->pageId = $pageId;
86+
87+
return $this;
88+
}
89+
90+
/**
91+
* Create a comment
92+
* url: https://api.notion.com/{version}/comments [post]
93+
* notion-api-docs: https://developers.notion.com/reference/create-a-comment.
94+
*
95+
* @param CommentEntity $comment
96+
* @return CommentEntity
97+
*
98+
* @throws HandlingException
99+
* @throws NotionException
100+
*/
101+
public function create($comment): CommentEntity
102+
{
103+
if ($this->discussionId === null && $this->pageId === null) {
104+
throw new HandlingException('You must use `onDiscussion()` or `onPage()`.');
105+
}
106+
107+
$body = $comment->getRawResponse();
108+
if ($this->discussionId !== null) {
109+
$body['discussion_id'] = $this->discussionId;
110+
} else {
111+
$body['parent'] = [
112+
'page_id' => $this->pageId,
113+
];
114+
}
115+
116+
$response = $this->post(
117+
$this->url(Endpoint::COMMENTS),
118+
$body
119+
);
120+
121+
return new CommentEntity($response->json());
122+
}
123+
}

src/Endpoints/Endpoint.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class Endpoint
1919
public const PAGES = 'pages';
2020
public const USERS = 'users';
2121
public const SEARCH = 'search';
22+
public const COMMENTS = 'comments';
2223

2324
/**
2425
* @var Notion
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace FiveamCode\LaravelNotionApi\Entities\Collections;
4+
5+
use FiveamCode\LaravelNotionApi\Entities\Comment;
6+
use Illuminate\Support\Collection;
7+
8+
/**
9+
* Class CommentCollection.
10+
*/
11+
class CommentCollection extends EntityCollection
12+
{
13+
/**
14+
* collects all comments from the raw results (from Notion).
15+
*/
16+
protected function collectChildren(): void
17+
{
18+
$this->collection = new Collection();
19+
foreach ($this->rawResults as $commentContent) {
20+
$this->collection->add(new Comment($commentContent));
21+
}
22+
}
23+
}

src/Entities/Comment.php

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php
2+
3+
namespace FiveamCode\LaravelNotionApi\Entities;
4+
5+
use FiveamCode\LaravelNotionApi\Entities\PropertyItems\RichText;
6+
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
7+
use FiveamCode\LaravelNotionApi\Traits\HasParent;
8+
use FiveamCode\LaravelNotionApi\Traits\HasTimestamps;
9+
use Illuminate\Support\Arr;
10+
11+
/**
12+
* Class Comment.
13+
*/
14+
class Comment extends Entity
15+
{
16+
use HasTimestamps, HasParent;
17+
18+
/**
19+
* @var string
20+
*/
21+
private string $discussionId;
22+
23+
/**
24+
* @var RichText
25+
*/
26+
private RichText $richText;
27+
28+
public function __construct(?array $rawResponse = null)
29+
{
30+
if ($rawResponse !== null) {
31+
$this->setResponseData($rawResponse);
32+
}
33+
}
34+
35+
public static function fromText($content): Comment
36+
{
37+
$commentEntity = new Comment();
38+
39+
if (is_string($content)) {
40+
$richText = new RichText();
41+
$richText->setPlainText($content);
42+
$commentEntity->richText = $richText;
43+
} else {
44+
$commentEntity->richText = $content;
45+
}
46+
47+
//!INFO: Currently only plain_text is transfered into rawContent
48+
//TODO: Later the RichText has to return it's raw structure into 'content'
49+
$commentEntity->responseData = [
50+
'rich_text' => [
51+
[
52+
'type' => 'text',
53+
'text' => [
54+
'content' => $commentEntity->getText(),
55+
],
56+
],
57+
],
58+
];
59+
60+
return $commentEntity;
61+
}
62+
63+
/**
64+
* @param array $responseData
65+
*
66+
* @throws HandlingException
67+
* @throws \FiveamCode\LaravelNotionApi\Exceptions\NotionException
68+
*/
69+
protected function setResponseData(array $responseData): void
70+
{
71+
parent::setResponseData($responseData);
72+
if ($responseData['object'] !== 'comment') {
73+
throw HandlingException::instance('invalid json-array: the given object is not a comment');
74+
}
75+
$this->fillFromRaw();
76+
}
77+
78+
private function fillFromRaw(): void
79+
{
80+
parent::fillEssentials();
81+
$this->fillRichText();
82+
$this->fillDiscussionId();
83+
}
84+
85+
private function fillDiscussionId(): void
86+
{
87+
if (Arr::exists($this->responseData, 'discussion_id') && $this->responseData['discussion_id'] !== null) {
88+
$this->discussionId = $this->responseData['discussion_id'];
89+
}
90+
}
91+
92+
private function fillRichText(): void
93+
{
94+
if (Arr::exists($this->responseData, 'rich_text') && $this->responseData['rich_text'] !== null) {
95+
$this->richText = new RichText($this->responseData['rich_text']);
96+
}
97+
}
98+
99+
/**
100+
* @return string
101+
*/
102+
public function getDiscussionId(): string
103+
{
104+
return $this->discussionId;
105+
}
106+
107+
/**
108+
* @return RichText
109+
*/
110+
public function getRichText(): RichText
111+
{
112+
return $this->richText;
113+
}
114+
115+
/**
116+
* @return string
117+
*/
118+
public function getText(): string
119+
{
120+
return $this->getRichText()->getPlainText();
121+
}
122+
}

src/Notion.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace FiveamCode\LaravelNotionApi;
44

55
use FiveamCode\LaravelNotionApi\Endpoints\Block;
6+
use FiveamCode\LaravelNotionApi\Endpoints\Comments;
67
use FiveamCode\LaravelNotionApi\Endpoints\Database;
78
use FiveamCode\LaravelNotionApi\Endpoints\Databases;
89
use FiveamCode\LaravelNotionApi\Endpoints\Endpoint;
@@ -184,6 +185,17 @@ public function search(?string $searchText = ''): Search
184185
return new Search($this, $searchText);
185186
}
186187

188+
/**
189+
* @return Comments
190+
*
191+
* @throws Exceptions\LaravelNotionAPIException
192+
* @throws HandlingException
193+
*/
194+
public function comments(): Comments
195+
{
196+
return new Comments($this);
197+
}
198+
187199
/**
188200
* @return string
189201
*/

0 commit comments

Comments
 (0)