From 95e435cc19c0758e1d3ac60803f8fc76b65fae5c Mon Sep 17 00:00:00 2001 From: Gevorg Mansuryan Date: Tue, 16 May 2023 23:04:49 +0400 Subject: [PATCH 1/9] Add content scheduling to content #119 --- components/BaseContentController.php | 29 ++++++++++++++++++++++++++++ controllers/post/PostController.php | 1 + definitions/ContentDefinitions.php | 1 + tests/codeception/api/PostCest.php | 18 +++++++++++++++++ 4 files changed, 49 insertions(+) diff --git a/components/BaseContentController.php b/components/BaseContentController.php index f227264..758b488 100644 --- a/components/BaseContentController.php +++ b/components/BaseContentController.php @@ -21,6 +21,7 @@ use humhub\modules\topic\models\Topic; use humhub\modules\topic\permissions\AddTopic; use Yii; +use yii\base\DynamicModel; use yii\web\HttpException; use yii\web\UploadedFile; @@ -30,6 +31,7 @@ * * @package humhub\modules\rest\components */ + abstract class BaseContentController extends BaseController { /** @@ -396,6 +398,10 @@ protected function updateMetadata(ContentActiveRecord $activeRecord, array $data return false; } + if (!$this->updateScheduledAt($activeRecord, $data['metadata'])) { + return false; + } + return true; } @@ -482,4 +488,27 @@ protected function updateLockedComments(ContentActiveRecord $activeRecord, array $activeRecord->content->locked_comments = $data['locked_comments']; return $activeRecord->content->save(); } + + protected function updateScheduledAt(ContentActiveRecord $activeRecord, array $data): bool + { + if (!isset($data['scheduled_at'])) { + return true; + } + + $validator = DynamicModel::validateData([ + 'scheduled_at' => $data['scheduled_at'] + ], [ + ['scheduled_at', 'datetime', 'format' => 'php:Y-m-d H:i:s'] + ]); + + if (!$validator->validate()) { + $activeRecord->addError('scheduled_at', $validator->getFirstError('scheduled_at')); + + return false; + } + + $activeRecord->content->scheduled_at = $data['scheduled_at']; + + return $activeRecord->content->save(); + } } \ No newline at end of file diff --git a/controllers/post/PostController.php b/controllers/post/PostController.php index 5e2a2fd..918c488 100644 --- a/controllers/post/PostController.php +++ b/controllers/post/PostController.php @@ -7,6 +7,7 @@ namespace humhub\modules\rest\controllers\post; + use humhub\modules\content\components\ContentActiveRecord; use humhub\modules\post\models\Post; use humhub\modules\rest\components\BaseContentController; diff --git a/definitions/ContentDefinitions.php b/definitions/ContentDefinitions.php index 6de67e4..12bf307 100644 --- a/definitions/ContentDefinitions.php +++ b/definitions/ContentDefinitions.php @@ -49,6 +49,7 @@ public static function getContentMetadata(Content $content) 'created_at' => $content->created_at, 'updated_by' => $content->updatedBy ? UserDefinitions::getUserShort($content->updatedBy) : null, 'updated_at' => $content->updated_at, + 'scheduled_at' => $content->scheduled_at, 'url' => $content->getUrl(true), 'contentcontainer_id' => $content->contentcontainer_id, 'stream_channel' => $content->stream_channel diff --git a/tests/codeception/api/PostCest.php b/tests/codeception/api/PostCest.php index 1883c9b..4f274f0 100644 --- a/tests/codeception/api/PostCest.php +++ b/tests/codeception/api/PostCest.php @@ -57,6 +57,24 @@ public function testCreate(ApiTester $I) $I->seeSuccessResponseContainsJson($this->getRecordDefinition(15)); } + public function testCreateScheduled(ApiTester $I) + { + $I->wantTo('create a scheduled post'); + $I->amAdmin(); + + $I->sendPost('post/container/1', [ + 'data' => [ + 'message' => 'New created message from API test', + 'content' => [ + 'metadata' => [ + 'scheduled_at' => (new \Datetime('tomorrow'))->format('Y-m-d H:i:s'), + ] + ] + ] + ]); + $I->seeSuccessResponseContainsJson($this->getRecordDefinition(16)); + } + public function testUpdate(ApiTester $I) { $I->wantTo('update a post'); From 55d73fee258d10246482cfcd25b34f53b9996485 Mon Sep 17 00:00:00 2001 From: Gevorg Mansuryan Date: Tue, 16 May 2023 23:20:38 +0400 Subject: [PATCH 2/9] Add content scheduling to content --- tests/codeception/api/PostCest.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/tests/codeception/api/PostCest.php b/tests/codeception/api/PostCest.php index 4f274f0..7406171 100644 --- a/tests/codeception/api/PostCest.php +++ b/tests/codeception/api/PostCest.php @@ -6,6 +6,7 @@ use humhub\modules\rest\definitions\PostDefinitions; use rest\ApiTester; use tests\codeception\_support\HumHubApiTestCest; +use yii\helpers\ArrayHelper; class PostCest extends HumHubApiTestCest { @@ -62,17 +63,26 @@ public function testCreateScheduled(ApiTester $I) $I->wantTo('create a scheduled post'); $I->amAdmin(); + $scheduledDate = (new \Datetime('tomorrow'))->format('Y-m-d H:i:s'); + $I->sendPost('post/container/1', [ 'data' => [ 'message' => 'New created message from API test', 'content' => [ 'metadata' => [ - 'scheduled_at' => (new \Datetime('tomorrow'))->format('Y-m-d H:i:s'), + 'scheduled_at' => $scheduledDate, ] ] ] ]); - $I->seeSuccessResponseContainsJson($this->getRecordDefinition(16)); + + $I->seeSuccessResponseContainsJson(ArrayHelper::merge($this->getRecordDefinition(16), [ + 'content' => [ + 'metadata' => [ + 'scheduled_at' => $scheduledDate + ] + ] + ])); } public function testUpdate(ApiTester $I) From 89a23974d66020c835c3b5be0241e3f554706c0a Mon Sep 17 00:00:00 2001 From: Gevorg Mansuryan Date: Tue, 16 May 2023 23:39:01 +0400 Subject: [PATCH 3/9] Space: Membership Attributes --- components/BaseContentController.php | 4 ++-- docs/html/content.html | 6 +++--- docs/html/notification.html | 8 ++++---- docs/html/post.html | 12 ++++++------ docs/swagger/content.yaml | 4 ++++ 5 files changed, 19 insertions(+), 15 deletions(-) diff --git a/components/BaseContentController.php b/components/BaseContentController.php index 758b488..a0aee81 100644 --- a/components/BaseContentController.php +++ b/components/BaseContentController.php @@ -498,7 +498,7 @@ protected function updateScheduledAt(ContentActiveRecord $activeRecord, array $d $validator = DynamicModel::validateData([ 'scheduled_at' => $data['scheduled_at'] ], [ - ['scheduled_at', 'datetime', 'format' => 'php:Y-m-d H:i:s'] + ['scheduled_at', 'datetime', 'format' => 'php:.'] ]); if (!$validator->validate()) { @@ -511,4 +511,4 @@ protected function updateScheduledAt(ContentActiveRecord $activeRecord, array $d return $activeRecord->content->save(); } -} \ No newline at end of file +} diff --git a/docs/html/content.html b/docs/html/content.html index 166c65c..333ae47 100644 --- a/docs/html/content.html +++ b/docs/html/content.html @@ -396,11 +396,11 @@

Responses

Response samples

Content type
application/json
{
  • "total": 76,
  • "page": 1,
  • "pages": 8,
  • "links": {
    },
  • "results": [
    ]
}

Get content by id

path Parameters
id
required
integer

The id of the content

+

Response samples

Content type
application/json
{
  • "total": 76,
  • "page": 1,
  • "pages": 8,
  • "links": {
    },
  • "results": [
    ]
}

Get content by id

path Parameters
id
required
integer

The id of the content

Responses

Response samples

Content type
application/json
{
  • "metadata": {
    },
  • "comments": {
    },
  • "likes": {
    },
  • "files": {},
  • "topics": [
    ]
}

Deletes a content by id

path Parameters
id
required
integer

The id of content

+

Response samples

Content type
application/json
{
  • "metadata": {
    },
  • "comments": {
    },
  • "likes": {
    },
  • "files": {},
  • "topics": [
    ]
}

Deletes a content by id

path Parameters
id
required
integer

The id of content

Responses

Container

API to access and manage content containers.

@@ -410,7 +410,7 @@

Response samples

Content type
application/json
{
  • "total": 76,
  • "page": 1,
  • "pages": 8,
  • "links": {
    },
  • "results": [
    ]
}

Tags

Group API