Skip to content

Commit

Permalink
Merge pull request #121 from humhub/enh/content-scheduling
Browse files Browse the repository at this point in the history
Add content scheduling to content
  • Loading branch information
luke- committed May 23, 2023
2 parents a40e1a7 + caffce8 commit 7aff3cb
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 15 deletions.
31 changes: 30 additions & 1 deletion components/BaseContentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -30,6 +31,7 @@
*
* @package humhub\modules\rest\components
*/

abstract class BaseContentController extends BaseController
{
/**
Expand Down Expand Up @@ -396,6 +398,10 @@ protected function updateMetadata(ContentActiveRecord $activeRecord, array $data
return false;
}

if (!$this->updateScheduledAt($activeRecord, $data['metadata'])) {
return false;
}

return true;
}

Expand Down Expand Up @@ -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->getStateService()->schedule($data['scheduled_at']);

return $activeRecord->content->save();
}
}
3 changes: 2 additions & 1 deletion components/auth/JwtAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use yii\filters\auth\HttpBearerAuth;
use Exception;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use humhub\modules\rest\models\JwtAuthForm;
use humhub\modules\user\models\User;

Expand All @@ -23,7 +24,7 @@ public function authenticate($user, $request, $response)
$token = $matches[1];

try {
$validData = JWT::decode($token, JwtAuthForm::getInstance()->jwtKey, ['HS512']);
$validData = JWT::decode($token, new Key(JwtAuthForm::getInstance()->jwtKey, 'HS512'));
if (
!empty($validData->uid) &&
($identity = User::find()->active()->andWhere(['user.id' => $validData->uid])->one()) &&
Expand Down
1 change: 1 addition & 0 deletions controllers/post/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions definitions/ContentDefinitions.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,16 @@ public static function getContentMetadata(Content $content)
'object_model' => $content->object_model,
'object_id' => $content->object_id,
'visibility' => (int) $content->visibility,
'state' => (int) $content->state,
'archived' => (bool) $content->archived,
'hidden' => (bool) $content->hidden,
'pinned' => (bool) $content->pinned,
'locked_comments' => (bool) $content->locked_comments,
'created_by' => $content->createdBy ? UserDefinitions::getUserShort($content->createdBy) : null,
'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
Expand Down
6 changes: 3 additions & 3 deletions docs/html/content.html

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions docs/html/notification.html

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions docs/html/post.html

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions docs/swagger/content.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ definitions:
readOnly: true
example: 12
visibility:
type: integer
format: int64
example: 1
state:
type: integer
format: int64
minimum: 0
Expand All @@ -240,12 +244,18 @@ definitions:
archived:
type: boolean
example: true
hidden:
type: boolean
example: true
pinned:
type: boolean
example: false
locked_comments:
type: boolean
example: false
scheduled_at:
type: string
example: "2023-09-14 05:15:00"
created_by:
readOnly: true
allOf:
Expand Down
28 changes: 28 additions & 0 deletions tests/codeception/api/PostCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -57,6 +58,33 @@ public function testCreate(ApiTester $I)
$I->seeSuccessResponseContainsJson($this->getRecordDefinition(15));
}

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' => $scheduledDate,
]
]
]
]);

$I->seeSuccessResponseContainsJson(ArrayHelper::merge($this->getRecordDefinition(16), [
'content' => [
'metadata' => [
'scheduled_at' => $scheduledDate
]
]
]));
}

public function testUpdate(ApiTester $I)
{
$I->wantTo('update a post');
Expand Down

0 comments on commit 7aff3cb

Please sign in to comment.