diff --git a/src/Api/Controller/CreatePostController.php b/src/Api/Controller/CreatePostController.php index b4c881137..4cdae617e 100644 --- a/src/Api/Controller/CreatePostController.php +++ b/src/Api/Controller/CreatePostController.php @@ -84,7 +84,7 @@ class CreatePostController extends AbstractCreateController } $discussion = $post->discussion; - $discussion->posts = $discussion->posts()->whereVisibleTo($actor)->orderBy('time')->pluck('id'); + $discussion->posts = $discussion->posts()->whereVisibleTo($actor)->orderBy('created_at')->pluck('id'); return $post; } diff --git a/src/Api/Serializer/BasicPostSerializer.php b/src/Api/Serializer/BasicPostSerializer.php index 103bd8a06..fcd689136 100644 --- a/src/Api/Serializer/BasicPostSerializer.php +++ b/src/Api/Serializer/BasicPostSerializer.php @@ -39,7 +39,7 @@ class BasicPostSerializer extends AbstractSerializer $attributes = [ 'id' => (int) $post->id, 'number' => (int) $post->number, - 'time' => $this->formatDate($post->time), + 'time' => $this->formatDate($post->created_at), 'contentType' => $post->type ]; diff --git a/src/Api/Serializer/CurrentUserSerializer.php b/src/Api/Serializer/CurrentUserSerializer.php index 87dd1490c..d4d6bdcbf 100644 --- a/src/Api/Serializer/CurrentUserSerializer.php +++ b/src/Api/Serializer/CurrentUserSerializer.php @@ -14,16 +14,17 @@ namespace Flarum\Api\Serializer; class CurrentUserSerializer extends UserSerializer { /** - * {@inheritdoc} + * @param \Flarum\User\User $user + * @return array */ protected function getDefaultAttributes($user) { $attributes = parent::getDefaultAttributes($user); $attributes += [ - 'isActivated' => (bool) $user->is_activated, + 'isActivated' => (bool) $user->is_email_confirmed, 'email' => $user->email, - 'readTime' => $this->formatDate($user->read_time), + 'readTime' => $this->formatDate($user->read_notifications_at), 'unreadNotificationsCount' => (int) $user->getUnreadNotificationsCount(), 'newNotificationsCount' => (int) $user->getNewNotificationsCount(), 'preferences' => (array) $user->preferences diff --git a/src/Api/Serializer/DiscussionSerializer.php b/src/Api/Serializer/DiscussionSerializer.php index 393faaf5d..e99d5522b 100644 --- a/src/Api/Serializer/DiscussionSerializer.php +++ b/src/Api/Serializer/DiscussionSerializer.php @@ -57,8 +57,8 @@ class DiscussionSerializer extends BasicDiscussionSerializer if ($state = $discussion->state) { $attributes += [ - 'readTime' => $this->formatDate($state->read_time), - 'readNumber' => (int) $state->read_number + 'readTime' => $this->formatDate($state->last_read_at), + 'readNumber' => (int) $state->last_read_post_number ]; } diff --git a/src/Api/Serializer/NotificationSerializer.php b/src/Api/Serializer/NotificationSerializer.php index 55b809079..03a8d898a 100644 --- a/src/Api/Serializer/NotificationSerializer.php +++ b/src/Api/Serializer/NotificationSerializer.php @@ -47,12 +47,13 @@ class NotificationSerializer extends AbstractSerializer 'id' => (int) $notification->id, 'contentType' => $notification->type, 'content' => $notification->data, - 'time' => $this->formatDate($notification->time), - 'isRead' => (bool) $notification->is_read + 'time' => $this->formatDate($notification->created_at), + 'isRead' => (bool) $notification->read_at ]; } /** + * @param Notification $notification * @return \Tobscure\JsonApi\Relationship */ protected function user($notification) @@ -61,6 +62,7 @@ class NotificationSerializer extends AbstractSerializer } /** + * @param Notification $notification * @return \Tobscure\JsonApi\Relationship */ protected function sender($notification) @@ -69,6 +71,7 @@ class NotificationSerializer extends AbstractSerializer } /** + * @param Notification $notification * @return \Tobscure\JsonApi\Relationship */ protected function subject($notification) diff --git a/src/Api/Serializer/PostSerializer.php b/src/Api/Serializer/PostSerializer.php index ee807374b..871826d3a 100644 --- a/src/Api/Serializer/PostSerializer.php +++ b/src/Api/Serializer/PostSerializer.php @@ -55,13 +55,13 @@ class PostSerializer extends BasicPostSerializer $attributes['content'] = $post->content; } - if ($post->edit_time) { - $attributes['editTime'] = $this->formatDate($post->edit_time); + if ($post->edited_at) { + $attributes['editTime'] = $this->formatDate($post->edited_at); } - if ($post->hide_time) { + if ($post->hidden_at) { $attributes['isHidden'] = true; - $attributes['hideTime'] = $this->formatDate($post->hide_time); + $attributes['hideTime'] = $this->formatDate($post->hidden_at); } $attributes += [ diff --git a/src/Api/Serializer/UserSerializer.php b/src/Api/Serializer/UserSerializer.php index 8a521f6cb..106c39d7a 100644 --- a/src/Api/Serializer/UserSerializer.php +++ b/src/Api/Serializer/UserSerializer.php @@ -29,7 +29,8 @@ class UserSerializer extends BasicUserSerializer } /** - * {@inheritdoc} + * @param \Flarum\User\User $user + * @return array */ protected function getDefaultAttributes($user) { @@ -40,22 +41,22 @@ class UserSerializer extends BasicUserSerializer $canEdit = $gate->allows('edit', $user); $attributes += [ - 'joinTime' => $this->formatDate($user->join_time), - 'discussionsCount' => (int) $user->discussions_count, - 'commentsCount' => (int) $user->comments_count, + 'joinTime' => $this->formatDate($user->joined_at), + 'discussionsCount' => (int) $user->discussion_count, + 'commentsCount' => (int) $user->comment_count, 'canEdit' => $canEdit, 'canDelete' => $gate->allows('delete', $user), ]; if ($user->getPreference('discloseOnline')) { $attributes += [ - 'lastSeenTime' => $this->formatDate($user->last_seen_time) + 'lastSeenTime' => $this->formatDate($user->last_seen_at) ]; } if ($canEdit || $this->actor->id === $user->id) { $attributes += [ - 'isActivated' => (bool) $user->is_activated, + 'isActivated' => (bool) $user->is_email_confirmed, 'email' => $user->email ]; } diff --git a/src/Discussion/Discussion.php b/src/Discussion/Discussion.php index de44636a1..e48825d09 100644 --- a/src/Discussion/Discussion.php +++ b/src/Discussion/Discussion.php @@ -176,7 +176,7 @@ class Discussion extends AbstractModel public function hide(User $actor = null) { if (! $this->hidden_at) { - $this->hidden_at = time(); + $this->hidden_at = Carbon::now(); $this->hidden_user_id = $actor ? $actor->id : null; $this->raise(new Hidden($this)); diff --git a/src/Discussion/DiscussionPolicy.php b/src/Discussion/DiscussionPolicy.php index 33c521838..73f7fbc0d 100644 --- a/src/Discussion/DiscussionPolicy.php +++ b/src/Discussion/DiscussionPolicy.php @@ -92,7 +92,7 @@ class DiscussionPolicy extends AbstractPolicy // user, or the current user has permission to view hidden discussions. if (! $actor->hasPermission('discussion.hide')) { $query->where(function ($query) use ($actor) { - $query->whereNull('discussions.hide_time') + $query->whereNull('discussions.hidden_at') ->orWhere('user_id', $actor->id) ->orWhere(function ($query) use ($actor) { $this->events->fire( diff --git a/src/Discussion/UserState.php b/src/Discussion/UserState.php index 6de1c998e..4840b00b7 100644 --- a/src/Discussion/UserState.php +++ b/src/Discussion/UserState.php @@ -11,6 +11,7 @@ namespace Flarum\Discussion; +use Carbon\Carbon; use Flarum\Database\AbstractModel; use Flarum\Discussion\Event\UserRead; use Flarum\Foundation\EventGeneratorTrait; @@ -56,7 +57,7 @@ class UserState extends AbstractModel { if ($number > $this->last_read_at) { $this->last_read_at = $number; - $this->last_read_at = time(); + $this->last_read_at = Carbon::now(); $this->raise(new UserRead($this)); } diff --git a/src/Http/AccessToken.php b/src/Http/AccessToken.php index dad24ae3b..7b181bef6 100644 --- a/src/Http/AccessToken.php +++ b/src/Http/AccessToken.php @@ -11,6 +11,7 @@ namespace Flarum\Http; +use Carbon\Carbon; use Flarum\Database\AbstractModel; /** @@ -47,7 +48,7 @@ class AccessToken extends AbstractModel $token->id = str_random(40); $token->user_id = $userId; - $token->last_activity = time(); + $token->last_activity = Carbon::now(); $token->lifetime = $lifetime; return $token; @@ -55,7 +56,7 @@ class AccessToken extends AbstractModel public function touch() { - $this->last_activity = time(); + $this->last_activity = Carbon::now(); return $this->save(); } diff --git a/src/Http/CookieFactory.php b/src/Http/CookieFactory.php index 0ddb3de4c..f8ebd70e5 100644 --- a/src/Http/CookieFactory.php +++ b/src/Http/CookieFactory.php @@ -11,6 +11,7 @@ namespace Flarum\Http; +use Carbon\Carbon; use Dflydev\FigCookies\SetCookie; use Flarum\Foundation\Application; @@ -79,7 +80,7 @@ class CookieFactory if ($maxAge) { $cookie = $cookie ->withMaxAge($maxAge) - ->withExpires(time() + $maxAge); + ->withExpires(Carbon::now()->timestamp + $maxAge); } if ($this->domain != null) { diff --git a/src/Http/Middleware/CollectGarbage.php b/src/Http/Middleware/CollectGarbage.php index f4882c9fb..0075fe0ea 100644 --- a/src/Http/Middleware/CollectGarbage.php +++ b/src/Http/Middleware/CollectGarbage.php @@ -11,6 +11,7 @@ namespace Flarum\Http\Middleware; +use Carbon\Carbon; use Flarum\Http\AccessToken; use Flarum\User\AuthToken; use Flarum\User\EmailToken; @@ -54,9 +55,11 @@ class CollectGarbage implements MiddlewareInterface return; } - AccessToken::whereRaw('last_activity <= ? - lifetime', [time()])->delete(); + $time = Carbon::now()->timestamp; - $earliestToKeep = date('Y-m-d H:i:s', time() - 24 * 60 * 60); + AccessToken::whereRaw('last_activity <= ? - lifetime', [$time])->delete(); + + $earliestToKeep = date('Y-m-d H:i:s', $time - 24 * 60 * 60); EmailToken::where('created_at', '<=', $earliestToKeep)->delete(); PasswordToken::where('created_at', '<=', $earliestToKeep)->delete(); diff --git a/src/Notification/Notification.php b/src/Notification/Notification.php index cb906dea2..6a87ad692 100644 --- a/src/Notification/Notification.php +++ b/src/Notification/Notification.php @@ -11,6 +11,7 @@ namespace Flarum\Notification; +use Carbon\Carbon; use Flarum\Database\AbstractModel; use Flarum\User\User; @@ -70,7 +71,7 @@ class Notification extends AbstractModel */ public function read() { - $this->read_at = time(); + $this->read_at = Carbon::now(); } /** diff --git a/src/Notification/NotificationRepository.php b/src/Notification/NotificationRepository.php index 4b0d2b54a..57ed3b02a 100644 --- a/src/Notification/NotificationRepository.php +++ b/src/Notification/NotificationRepository.php @@ -11,6 +11,7 @@ namespace Flarum\Notification; +use Carbon\Carbon; use Flarum\User\User; class NotificationRepository @@ -53,6 +54,6 @@ class NotificationRepository */ public function markAllAsRead(User $user) { - Notification::where('user_id', $user->id)->update(['read_at' => time()]); + Notification::where('user_id', $user->id)->update(['read_at' => Carbon::now()]); } } diff --git a/src/Post/Command/PostReplyHandler.php b/src/Post/Command/PostReplyHandler.php index a3e066c50..816448da5 100644 --- a/src/Post/Command/PostReplyHandler.php +++ b/src/Post/Command/PostReplyHandler.php @@ -11,7 +11,7 @@ namespace Flarum\Post\Command; -use DateTime; +use Carbon\Carbon; use Flarum\Discussion\DiscussionRepository; use Flarum\Foundation\DispatchEventsTrait; use Flarum\Notification\NotificationSyncer; @@ -77,7 +77,7 @@ class PostReplyHandler // If this is the first post in the discussion, it's technically not a // "reply", so we won't check for that permission. - if ($discussion->number_index > 0) { + if ($discussion->post_number_index > 0) { $this->assertCan($actor, 'reply', $discussion); } @@ -92,7 +92,7 @@ class PostReplyHandler ); if ($actor->isAdmin() && ($time = array_get($command->data, 'attributes.time'))) { - $post->time = new DateTime($time); + $post->created_at = new Carbon($time); } $this->events->dispatch( diff --git a/src/Post/CommentPost.php b/src/Post/CommentPost.php index ae171ad23..4c0a58cb4 100644 --- a/src/Post/CommentPost.php +++ b/src/Post/CommentPost.php @@ -11,6 +11,7 @@ namespace Flarum\Post; +use Carbon\Carbon; use Flarum\Formatter\Formatter; use Flarum\Post\Event\Hidden; use Flarum\Post\Event\Posted; @@ -51,7 +52,7 @@ class CommentPost extends Post { $post = new static; - $post->created_at = time(); + $post->created_at = Carbon::now(); $post->discussion_id = $discussionId; $post->user_id = $userId; $post->type = static::$type; @@ -77,7 +78,7 @@ class CommentPost extends Post if ($this->content !== $content) { $this->content = $content; - $this->edited_at = time(); + $this->edited_at = Carbon::now(); $this->edited_user_id = $actor->id; $this->raise(new Revised($this)); @@ -95,7 +96,7 @@ class CommentPost extends Post public function hide(User $actor = null) { if (! $this->hidden_at) { - $this->hidden_at = time(); + $this->hidden_at = Carbon::now(); $this->hidden_user_id = $actor ? $actor->id : null; $this->raise(new Hidden($this)); diff --git a/src/Post/DiscussionRenamedPost.php b/src/Post/DiscussionRenamedPost.php index 21243d902..3feb96e5d 100644 --- a/src/Post/DiscussionRenamedPost.php +++ b/src/Post/DiscussionRenamedPost.php @@ -11,6 +11,8 @@ namespace Flarum\Post; +use Carbon\Carbon; + /** * A post which indicates that a discussion's title was changed. * @@ -64,7 +66,7 @@ class DiscussionRenamedPost extends AbstractEventPost implements MergeableInterf $post = new static; $post->content = static::buildContent($oldTitle, $newTitle); - $post->time = time(); + $post->created_at = Carbon::now(); $post->discussion_id = $discussionId; $post->user_id = $userId; diff --git a/src/User/AuthToken.php b/src/User/AuthToken.php index 516dfa3c0..d13fd4e3e 100644 --- a/src/User/AuthToken.php +++ b/src/User/AuthToken.php @@ -57,7 +57,7 @@ class AuthToken extends AbstractModel $token->token = str_random(40); $token->payload = $payload; - $token->created_at = time(); + $token->created_at = Carbon::now(); return $token; } diff --git a/src/User/EmailToken.php b/src/User/EmailToken.php index 0d25df3e7..5e73c7f53 100644 --- a/src/User/EmailToken.php +++ b/src/User/EmailToken.php @@ -11,6 +11,7 @@ namespace Flarum\User; +use Carbon\Carbon; use DateTime; use Flarum\Database\AbstractModel; use Flarum\User\Exception\InvalidConfirmationTokenException; @@ -55,7 +56,7 @@ class EmailToken extends AbstractModel $token->token = str_random(40); $token->user_id = $userId; $token->email = $email; - $token->created_at = time(); + $token->created_at = Carbon::now(); return $token; } @@ -80,9 +81,10 @@ class EmailToken extends AbstractModel */ public function scopeValidOrFail($query, $id) { + /** @var EmailToken $token */ $token = $query->find($id); - if (! $token || $token->created_at < new DateTime('-1 day')) { + if (! $token || $token->created_at->diffInDays() >= 1) { throw new InvalidConfirmationTokenException; } diff --git a/src/User/PasswordToken.php b/src/User/PasswordToken.php index 0597012ce..cda750209 100644 --- a/src/User/PasswordToken.php +++ b/src/User/PasswordToken.php @@ -11,6 +11,7 @@ namespace Flarum\User; +use Carbon\Carbon; use Flarum\Database\AbstractModel; /** @@ -54,7 +55,7 @@ class PasswordToken extends AbstractModel $token->token = str_random(40); $token->user_id = $userId; - $token->created_at = time(); + $token->created_at = Carbon::now(); return $token; } diff --git a/src/User/User.php b/src/User/User.php index 53057990e..446d5d107 100644 --- a/src/User/User.php +++ b/src/User/User.php @@ -11,6 +11,7 @@ namespace Flarum\User; +use Carbon\Carbon; use DomainException; use Flarum\Database\AbstractModel; use Flarum\Database\ScopeVisibilityTrait; @@ -166,7 +167,7 @@ class User extends AbstractModel $user->username = $username; $user->email = $email; $user->password = $password; - $user->joined_at = time(); + $user->joined_at = Carbon::now(); $user->raise(new Registered($user)); @@ -270,7 +271,7 @@ class User extends AbstractModel */ public function markAllAsRead() { - $this->marked_all_as_read_at = time(); + $this->marked_all_as_read_at = Carbon::now(); return $this; } @@ -282,7 +283,7 @@ class User extends AbstractModel */ public function markNotificationsAsRead() { - $this->read_notifications_at = time(); + $this->read_notifications_at = Carbon::now(); return $this; } @@ -469,7 +470,7 @@ class User extends AbstractModel public function getNewNotificationsCount() { return $this->getUnreadNotifications()->filter(function ($notification) { - return $notification->time > $this->read_notifications_at ?: 0; + return $notification->created_at > $this->read_notifications_at ?: 0; })->count(); } @@ -568,7 +569,7 @@ class User extends AbstractModel */ public function updateLastSeen() { - $this->last_seen_at = time(); + $this->last_seen_at = Carbon::now(); return $this; }