1
0
mirror of https://github.com/flarum/core.git synced 2025-08-02 14:37:49 +02:00

fixed more attributes to match beta 8

This commit is contained in:
Daniel Klabbers
2018-05-14 13:49:52 +02:00
parent a9501ceae0
commit 3e3e1cbde5
22 changed files with 65 additions and 45 deletions

View File

@@ -84,7 +84,7 @@ class CreatePostController extends AbstractCreateController
} }
$discussion = $post->discussion; $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; return $post;
} }

View File

@@ -39,7 +39,7 @@ class BasicPostSerializer extends AbstractSerializer
$attributes = [ $attributes = [
'id' => (int) $post->id, 'id' => (int) $post->id,
'number' => (int) $post->number, 'number' => (int) $post->number,
'time' => $this->formatDate($post->time), 'time' => $this->formatDate($post->created_at),
'contentType' => $post->type 'contentType' => $post->type
]; ];

View File

@@ -14,16 +14,17 @@ namespace Flarum\Api\Serializer;
class CurrentUserSerializer extends UserSerializer class CurrentUserSerializer extends UserSerializer
{ {
/** /**
* {@inheritdoc} * @param \Flarum\User\User $user
* @return array
*/ */
protected function getDefaultAttributes($user) protected function getDefaultAttributes($user)
{ {
$attributes = parent::getDefaultAttributes($user); $attributes = parent::getDefaultAttributes($user);
$attributes += [ $attributes += [
'isActivated' => (bool) $user->is_activated, 'isActivated' => (bool) $user->is_email_confirmed,
'email' => $user->email, 'email' => $user->email,
'readTime' => $this->formatDate($user->read_time), 'readTime' => $this->formatDate($user->read_notifications_at),
'unreadNotificationsCount' => (int) $user->getUnreadNotificationsCount(), 'unreadNotificationsCount' => (int) $user->getUnreadNotificationsCount(),
'newNotificationsCount' => (int) $user->getNewNotificationsCount(), 'newNotificationsCount' => (int) $user->getNewNotificationsCount(),
'preferences' => (array) $user->preferences 'preferences' => (array) $user->preferences

View File

@@ -57,8 +57,8 @@ class DiscussionSerializer extends BasicDiscussionSerializer
if ($state = $discussion->state) { if ($state = $discussion->state) {
$attributes += [ $attributes += [
'readTime' => $this->formatDate($state->read_time), 'readTime' => $this->formatDate($state->last_read_at),
'readNumber' => (int) $state->read_number 'readNumber' => (int) $state->last_read_post_number
]; ];
} }

View File

@@ -47,12 +47,13 @@ class NotificationSerializer extends AbstractSerializer
'id' => (int) $notification->id, 'id' => (int) $notification->id,
'contentType' => $notification->type, 'contentType' => $notification->type,
'content' => $notification->data, 'content' => $notification->data,
'time' => $this->formatDate($notification->time), 'time' => $this->formatDate($notification->created_at),
'isRead' => (bool) $notification->is_read 'isRead' => (bool) $notification->read_at
]; ];
} }
/** /**
* @param Notification $notification
* @return \Tobscure\JsonApi\Relationship * @return \Tobscure\JsonApi\Relationship
*/ */
protected function user($notification) protected function user($notification)
@@ -61,6 +62,7 @@ class NotificationSerializer extends AbstractSerializer
} }
/** /**
* @param Notification $notification
* @return \Tobscure\JsonApi\Relationship * @return \Tobscure\JsonApi\Relationship
*/ */
protected function sender($notification) protected function sender($notification)
@@ -69,6 +71,7 @@ class NotificationSerializer extends AbstractSerializer
} }
/** /**
* @param Notification $notification
* @return \Tobscure\JsonApi\Relationship * @return \Tobscure\JsonApi\Relationship
*/ */
protected function subject($notification) protected function subject($notification)

View File

@@ -55,13 +55,13 @@ class PostSerializer extends BasicPostSerializer
$attributes['content'] = $post->content; $attributes['content'] = $post->content;
} }
if ($post->edit_time) { if ($post->edited_at) {
$attributes['editTime'] = $this->formatDate($post->edit_time); $attributes['editTime'] = $this->formatDate($post->edited_at);
} }
if ($post->hide_time) { if ($post->hidden_at) {
$attributes['isHidden'] = true; $attributes['isHidden'] = true;
$attributes['hideTime'] = $this->formatDate($post->hide_time); $attributes['hideTime'] = $this->formatDate($post->hidden_at);
} }
$attributes += [ $attributes += [

View File

@@ -29,7 +29,8 @@ class UserSerializer extends BasicUserSerializer
} }
/** /**
* {@inheritdoc} * @param \Flarum\User\User $user
* @return array
*/ */
protected function getDefaultAttributes($user) protected function getDefaultAttributes($user)
{ {
@@ -40,22 +41,22 @@ class UserSerializer extends BasicUserSerializer
$canEdit = $gate->allows('edit', $user); $canEdit = $gate->allows('edit', $user);
$attributes += [ $attributes += [
'joinTime' => $this->formatDate($user->join_time), 'joinTime' => $this->formatDate($user->joined_at),
'discussionsCount' => (int) $user->discussions_count, 'discussionsCount' => (int) $user->discussion_count,
'commentsCount' => (int) $user->comments_count, 'commentsCount' => (int) $user->comment_count,
'canEdit' => $canEdit, 'canEdit' => $canEdit,
'canDelete' => $gate->allows('delete', $user), 'canDelete' => $gate->allows('delete', $user),
]; ];
if ($user->getPreference('discloseOnline')) { if ($user->getPreference('discloseOnline')) {
$attributes += [ $attributes += [
'lastSeenTime' => $this->formatDate($user->last_seen_time) 'lastSeenTime' => $this->formatDate($user->last_seen_at)
]; ];
} }
if ($canEdit || $this->actor->id === $user->id) { if ($canEdit || $this->actor->id === $user->id) {
$attributes += [ $attributes += [
'isActivated' => (bool) $user->is_activated, 'isActivated' => (bool) $user->is_email_confirmed,
'email' => $user->email 'email' => $user->email
]; ];
} }

View File

@@ -176,7 +176,7 @@ class Discussion extends AbstractModel
public function hide(User $actor = null) public function hide(User $actor = null)
{ {
if (! $this->hidden_at) { if (! $this->hidden_at) {
$this->hidden_at = time(); $this->hidden_at = Carbon::now();
$this->hidden_user_id = $actor ? $actor->id : null; $this->hidden_user_id = $actor ? $actor->id : null;
$this->raise(new Hidden($this)); $this->raise(new Hidden($this));

View File

@@ -92,7 +92,7 @@ class DiscussionPolicy extends AbstractPolicy
// user, or the current user has permission to view hidden discussions. // user, or the current user has permission to view hidden discussions.
if (! $actor->hasPermission('discussion.hide')) { if (! $actor->hasPermission('discussion.hide')) {
$query->where(function ($query) use ($actor) { $query->where(function ($query) use ($actor) {
$query->whereNull('discussions.hide_time') $query->whereNull('discussions.hidden_at')
->orWhere('user_id', $actor->id) ->orWhere('user_id', $actor->id)
->orWhere(function ($query) use ($actor) { ->orWhere(function ($query) use ($actor) {
$this->events->fire( $this->events->fire(

View File

@@ -11,6 +11,7 @@
namespace Flarum\Discussion; namespace Flarum\Discussion;
use Carbon\Carbon;
use Flarum\Database\AbstractModel; use Flarum\Database\AbstractModel;
use Flarum\Discussion\Event\UserRead; use Flarum\Discussion\Event\UserRead;
use Flarum\Foundation\EventGeneratorTrait; use Flarum\Foundation\EventGeneratorTrait;
@@ -56,7 +57,7 @@ class UserState extends AbstractModel
{ {
if ($number > $this->last_read_at) { if ($number > $this->last_read_at) {
$this->last_read_at = $number; $this->last_read_at = $number;
$this->last_read_at = time(); $this->last_read_at = Carbon::now();
$this->raise(new UserRead($this)); $this->raise(new UserRead($this));
} }

View File

@@ -11,6 +11,7 @@
namespace Flarum\Http; namespace Flarum\Http;
use Carbon\Carbon;
use Flarum\Database\AbstractModel; use Flarum\Database\AbstractModel;
/** /**
@@ -47,7 +48,7 @@ class AccessToken extends AbstractModel
$token->id = str_random(40); $token->id = str_random(40);
$token->user_id = $userId; $token->user_id = $userId;
$token->last_activity = time(); $token->last_activity = Carbon::now();
$token->lifetime = $lifetime; $token->lifetime = $lifetime;
return $token; return $token;
@@ -55,7 +56,7 @@ class AccessToken extends AbstractModel
public function touch() public function touch()
{ {
$this->last_activity = time(); $this->last_activity = Carbon::now();
return $this->save(); return $this->save();
} }

View File

@@ -11,6 +11,7 @@
namespace Flarum\Http; namespace Flarum\Http;
use Carbon\Carbon;
use Dflydev\FigCookies\SetCookie; use Dflydev\FigCookies\SetCookie;
use Flarum\Foundation\Application; use Flarum\Foundation\Application;
@@ -79,7 +80,7 @@ class CookieFactory
if ($maxAge) { if ($maxAge) {
$cookie = $cookie $cookie = $cookie
->withMaxAge($maxAge) ->withMaxAge($maxAge)
->withExpires(time() + $maxAge); ->withExpires(Carbon::now()->timestamp + $maxAge);
} }
if ($this->domain != null) { if ($this->domain != null) {

View File

@@ -11,6 +11,7 @@
namespace Flarum\Http\Middleware; namespace Flarum\Http\Middleware;
use Carbon\Carbon;
use Flarum\Http\AccessToken; use Flarum\Http\AccessToken;
use Flarum\User\AuthToken; use Flarum\User\AuthToken;
use Flarum\User\EmailToken; use Flarum\User\EmailToken;
@@ -54,9 +55,11 @@ class CollectGarbage implements MiddlewareInterface
return; 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(); EmailToken::where('created_at', '<=', $earliestToKeep)->delete();
PasswordToken::where('created_at', '<=', $earliestToKeep)->delete(); PasswordToken::where('created_at', '<=', $earliestToKeep)->delete();

View File

@@ -11,6 +11,7 @@
namespace Flarum\Notification; namespace Flarum\Notification;
use Carbon\Carbon;
use Flarum\Database\AbstractModel; use Flarum\Database\AbstractModel;
use Flarum\User\User; use Flarum\User\User;
@@ -70,7 +71,7 @@ class Notification extends AbstractModel
*/ */
public function read() public function read()
{ {
$this->read_at = time(); $this->read_at = Carbon::now();
} }
/** /**

View File

@@ -11,6 +11,7 @@
namespace Flarum\Notification; namespace Flarum\Notification;
use Carbon\Carbon;
use Flarum\User\User; use Flarum\User\User;
class NotificationRepository class NotificationRepository
@@ -53,6 +54,6 @@ class NotificationRepository
*/ */
public function markAllAsRead(User $user) 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()]);
} }
} }

View File

@@ -11,7 +11,7 @@
namespace Flarum\Post\Command; namespace Flarum\Post\Command;
use DateTime; use Carbon\Carbon;
use Flarum\Discussion\DiscussionRepository; use Flarum\Discussion\DiscussionRepository;
use Flarum\Foundation\DispatchEventsTrait; use Flarum\Foundation\DispatchEventsTrait;
use Flarum\Notification\NotificationSyncer; use Flarum\Notification\NotificationSyncer;
@@ -77,7 +77,7 @@ class PostReplyHandler
// If this is the first post in the discussion, it's technically not a // If this is the first post in the discussion, it's technically not a
// "reply", so we won't check for that permission. // "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); $this->assertCan($actor, 'reply', $discussion);
} }
@@ -92,7 +92,7 @@ class PostReplyHandler
); );
if ($actor->isAdmin() && ($time = array_get($command->data, 'attributes.time'))) { if ($actor->isAdmin() && ($time = array_get($command->data, 'attributes.time'))) {
$post->time = new DateTime($time); $post->created_at = new Carbon($time);
} }
$this->events->dispatch( $this->events->dispatch(

View File

@@ -11,6 +11,7 @@
namespace Flarum\Post; namespace Flarum\Post;
use Carbon\Carbon;
use Flarum\Formatter\Formatter; use Flarum\Formatter\Formatter;
use Flarum\Post\Event\Hidden; use Flarum\Post\Event\Hidden;
use Flarum\Post\Event\Posted; use Flarum\Post\Event\Posted;
@@ -51,7 +52,7 @@ class CommentPost extends Post
{ {
$post = new static; $post = new static;
$post->created_at = time(); $post->created_at = Carbon::now();
$post->discussion_id = $discussionId; $post->discussion_id = $discussionId;
$post->user_id = $userId; $post->user_id = $userId;
$post->type = static::$type; $post->type = static::$type;
@@ -77,7 +78,7 @@ class CommentPost extends Post
if ($this->content !== $content) { if ($this->content !== $content) {
$this->content = $content; $this->content = $content;
$this->edited_at = time(); $this->edited_at = Carbon::now();
$this->edited_user_id = $actor->id; $this->edited_user_id = $actor->id;
$this->raise(new Revised($this)); $this->raise(new Revised($this));
@@ -95,7 +96,7 @@ class CommentPost extends Post
public function hide(User $actor = null) public function hide(User $actor = null)
{ {
if (! $this->hidden_at) { if (! $this->hidden_at) {
$this->hidden_at = time(); $this->hidden_at = Carbon::now();
$this->hidden_user_id = $actor ? $actor->id : null; $this->hidden_user_id = $actor ? $actor->id : null;
$this->raise(new Hidden($this)); $this->raise(new Hidden($this));

View File

@@ -11,6 +11,8 @@
namespace Flarum\Post; namespace Flarum\Post;
use Carbon\Carbon;
/** /**
* A post which indicates that a discussion's title was changed. * 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 = new static;
$post->content = static::buildContent($oldTitle, $newTitle); $post->content = static::buildContent($oldTitle, $newTitle);
$post->time = time(); $post->created_at = Carbon::now();
$post->discussion_id = $discussionId; $post->discussion_id = $discussionId;
$post->user_id = $userId; $post->user_id = $userId;

View File

@@ -57,7 +57,7 @@ class AuthToken extends AbstractModel
$token->token = str_random(40); $token->token = str_random(40);
$token->payload = $payload; $token->payload = $payload;
$token->created_at = time(); $token->created_at = Carbon::now();
return $token; return $token;
} }

View File

@@ -11,6 +11,7 @@
namespace Flarum\User; namespace Flarum\User;
use Carbon\Carbon;
use DateTime; use DateTime;
use Flarum\Database\AbstractModel; use Flarum\Database\AbstractModel;
use Flarum\User\Exception\InvalidConfirmationTokenException; use Flarum\User\Exception\InvalidConfirmationTokenException;
@@ -55,7 +56,7 @@ class EmailToken extends AbstractModel
$token->token = str_random(40); $token->token = str_random(40);
$token->user_id = $userId; $token->user_id = $userId;
$token->email = $email; $token->email = $email;
$token->created_at = time(); $token->created_at = Carbon::now();
return $token; return $token;
} }
@@ -80,9 +81,10 @@ class EmailToken extends AbstractModel
*/ */
public function scopeValidOrFail($query, $id) public function scopeValidOrFail($query, $id)
{ {
/** @var EmailToken $token */
$token = $query->find($id); $token = $query->find($id);
if (! $token || $token->created_at < new DateTime('-1 day')) { if (! $token || $token->created_at->diffInDays() >= 1) {
throw new InvalidConfirmationTokenException; throw new InvalidConfirmationTokenException;
} }

View File

@@ -11,6 +11,7 @@
namespace Flarum\User; namespace Flarum\User;
use Carbon\Carbon;
use Flarum\Database\AbstractModel; use Flarum\Database\AbstractModel;
/** /**
@@ -54,7 +55,7 @@ class PasswordToken extends AbstractModel
$token->token = str_random(40); $token->token = str_random(40);
$token->user_id = $userId; $token->user_id = $userId;
$token->created_at = time(); $token->created_at = Carbon::now();
return $token; return $token;
} }

View File

@@ -11,6 +11,7 @@
namespace Flarum\User; namespace Flarum\User;
use Carbon\Carbon;
use DomainException; use DomainException;
use Flarum\Database\AbstractModel; use Flarum\Database\AbstractModel;
use Flarum\Database\ScopeVisibilityTrait; use Flarum\Database\ScopeVisibilityTrait;
@@ -166,7 +167,7 @@ class User extends AbstractModel
$user->username = $username; $user->username = $username;
$user->email = $email; $user->email = $email;
$user->password = $password; $user->password = $password;
$user->joined_at = time(); $user->joined_at = Carbon::now();
$user->raise(new Registered($user)); $user->raise(new Registered($user));
@@ -270,7 +271,7 @@ class User extends AbstractModel
*/ */
public function markAllAsRead() public function markAllAsRead()
{ {
$this->marked_all_as_read_at = time(); $this->marked_all_as_read_at = Carbon::now();
return $this; return $this;
} }
@@ -282,7 +283,7 @@ class User extends AbstractModel
*/ */
public function markNotificationsAsRead() public function markNotificationsAsRead()
{ {
$this->read_notifications_at = time(); $this->read_notifications_at = Carbon::now();
return $this; return $this;
} }
@@ -469,7 +470,7 @@ class User extends AbstractModel
public function getNewNotificationsCount() public function getNewNotificationsCount()
{ {
return $this->getUnreadNotifications()->filter(function ($notification) { return $this->getUnreadNotifications()->filter(function ($notification) {
return $notification->time > $this->read_notifications_at ?: 0; return $notification->created_at > $this->read_notifications_at ?: 0;
})->count(); })->count();
} }
@@ -568,7 +569,7 @@ class User extends AbstractModel
*/ */
public function updateLastSeen() public function updateLastSeen()
{ {
$this->last_seen_at = time(); $this->last_seen_at = Carbon::now();
return $this; return $this;
} }