From 523acba2aaf55a3d4afaebcad9f2556d00fc637d Mon Sep 17 00:00:00 2001
From: Daniel Klabbers <daniel@klabbers.email>
Date: Wed, 16 May 2018 09:36:04 +0200
Subject: [PATCH] new api tests uncovered more issues, fixed tokens and
 discussion posts

---
 .../Controller/ShowDiscussionController.php   |  4 ++--
 .../src/Api/Controller/TokenController.php    |  2 +-
 framework/core/src/Http/AccessToken.php       | 21 ++++++++++++-------
 3 files changed, 16 insertions(+), 11 deletions(-)

diff --git a/framework/core/src/Api/Controller/ShowDiscussionController.php b/framework/core/src/Api/Controller/ShowDiscussionController.php
index ba1c43684..33fc20477 100644
--- a/framework/core/src/Api/Controller/ShowDiscussionController.php
+++ b/framework/core/src/Api/Controller/ShowDiscussionController.php
@@ -118,7 +118,7 @@ class ShowDiscussionController extends AbstractShowController
      */
     private function loadPostIds(Discussion $discussion, User $actor)
     {
-        return $discussion->posts()->whereVisibleTo($actor)->orderBy('time')->pluck('id')->all();
+        return $discussion->posts()->whereVisibleTo($actor)->orderBy('created_at')->pluck('id')->all();
     }
 
     /**
@@ -172,7 +172,7 @@ class ShowDiscussionController extends AbstractShowController
     {
         $query = $discussion->posts()->whereVisibleTo($actor);
 
-        $query->orderBy('time')->skip($offset)->take($limit)->with($include);
+        $query->orderBy('created_at')->skip($offset)->take($limit)->with($include);
 
         $posts = $query->get()->all();
 
diff --git a/framework/core/src/Api/Controller/TokenController.php b/framework/core/src/Api/Controller/TokenController.php
index e0f6f6464..dd854c98b 100644
--- a/framework/core/src/Api/Controller/TokenController.php
+++ b/framework/core/src/Api/Controller/TokenController.php
@@ -70,7 +70,7 @@ class TokenController implements ControllerInterface
         $token->save();
 
         return new JsonResponse([
-            'token' => $token->id,
+            'token' => $token->token,
             'userId' => $user->id
         ]);
     }
diff --git a/framework/core/src/Http/AccessToken.php b/framework/core/src/Http/AccessToken.php
index 7b181bef6..cd0c6aea4 100644
--- a/framework/core/src/Http/AccessToken.php
+++ b/framework/core/src/Http/AccessToken.php
@@ -13,12 +13,13 @@ namespace Flarum\Http;
 
 use Carbon\Carbon;
 use Flarum\Database\AbstractModel;
+use Flarum\User\User;
 
 /**
- * @property string $id
+ * @property string $token
  * @property int $user_id
- * @property int $last_activity
- * @property int $lifetime
+ * @property int $last_activity_at
+ * @property int $lifetime_seconds
  * @property \Flarum\User\User|null $user
  */
 class AccessToken extends AbstractModel
@@ -35,6 +36,10 @@ class AccessToken extends AbstractModel
      */
     public $incrementing = false;
 
+    protected $primaryKey = 'token';
+
+    protected $dates = ['last_activity_at'];
+
     /**
      * Generate an access token for the specified user.
      *
@@ -46,17 +51,17 @@ class AccessToken extends AbstractModel
     {
         $token = new static;
 
-        $token->id = str_random(40);
+        $token->token = str_random(40);
         $token->user_id = $userId;
-        $token->last_activity = Carbon::now();
-        $token->lifetime = $lifetime;
+        $token->last_activity_at = Carbon::now();
+        $token->lifetime_seconds = $lifetime;
 
         return $token;
     }
 
     public function touch()
     {
-        $this->last_activity = Carbon::now();
+        $this->last_activity_at = Carbon::now();
 
         return $this->save();
     }
@@ -68,6 +73,6 @@ class AccessToken extends AbstractModel
      */
     public function user()
     {
-        return $this->belongsTo('Flarum\User\User');
+        return $this->belongsTo(User::class);
     }
 }