1
0
mirror of https://github.com/flarum/core.git synced 2025-08-11 19:04:29 +02:00

refactor: JSON:API (#3971)

* refactor: json:api refactor iteration 1
* chore: delete dead code
* fix: regressions
* chore: move additions/changes to package
* feat: AccessTokenResource
* feat: allow dependency injection in resources
* feat: `ApiResource` extender
* feat: improve
* feat: refactor tags extension
* feat: refactor flags extension
* fix: regressions
* fix: drop bc layer
* feat: refactor suspend extension
* feat: refactor subscriptions extension
* feat: refactor approval extension
* feat: refactor sticky extension
* feat: refactor nicknames extension
* feat: refactor mentions extension
* feat: refactor lock extension
* feat: refactor likes extension
* chore: merge conflicts
* feat: refactor extension-manager extension
* feat: context current endpoint helpers
* chore: minor
* feat: cleaner sortmap implementation
* chore: drop old package
* chore: not needed (auto scoping)
* fix: actor only fields
* refactor: simplify index endpoint
* feat: eager loading
* test: adapt
* test: phpstan
* test: adapt
* fix: typing
* fix: approving content
* tet: adapt frontend tests
* chore: typings
* chore: review
* fix: breaking change
This commit is contained in:
Sami Mazouz
2024-06-21 09:36:32 +01:00
committed by GitHub
parent 10514709f1
commit a8777c6198
296 changed files with 7148 additions and 8860 deletions

View File

@@ -93,11 +93,12 @@ class GroupMentionsTest extends TestCase
'authenticatedAs' => 1,
'json' => [
'data' => [
'type' => 'posts',
'attributes' => [
'content' => '@"InvalidGroup"#g99',
],
'relationships' => [
'discussion' => ['data' => ['id' => 2]],
'discussion' => ['data' => ['type' => 'discussions', 'id' => 2]],
]
],
],
@@ -168,11 +169,12 @@ class GroupMentionsTest extends TestCase
'authenticatedAs' => 1,
'json' => [
'data' => [
'type' => 'posts',
'attributes' => [
'content' => '@"Mods"#g4',
],
'relationships' => [
'discussion' => ['data' => ['id' => 2]],
'discussion' => ['data' => ['type' => 'discussions', 'id' => 2]],
]
]
]
@@ -200,11 +202,12 @@ class GroupMentionsTest extends TestCase
'authenticatedAs' => 1,
'json' => [
'data' => [
'type' => 'posts',
'attributes' => [
'content' => '@"Admins"#g1 @"Mods"#g4',
],
'relationships' => [
'discussion' => ['data' => ['id' => 2]],
'discussion' => ['data' => ['type' => 'discussions', 'id' => 2]],
]
]
]
@@ -234,11 +237,12 @@ class GroupMentionsTest extends TestCase
'authenticatedAs' => 1,
'json' => [
'data' => [
'type' => 'posts',
'attributes' => [
'content' => '@"Members"#g3 @"Guests"#g2',
],
'relationships' => [
'discussion' => ['data' => ['id' => 2]],
'discussion' => ['data' => ['type' => 'discussions', 'id' => 2]],
]
]
]
@@ -290,11 +294,12 @@ class GroupMentionsTest extends TestCase
'authenticatedAs' => 3,
'json' => [
'data' => [
'type' => 'posts',
'attributes' => [
'content' => '@"Mods"#g4',
],
'relationships' => [
'discussion' => ['data' => ['id' => 2]],
'discussion' => ['data' => ['type' => 'discussions', 'id' => 2]],
],
],
],
@@ -321,11 +326,12 @@ class GroupMentionsTest extends TestCase
'authenticatedAs' => 4,
'json' => [
'data' => [
'type' => 'posts',
'attributes' => [
'content' => '@"Mods"#g4',
],
'relationships' => [
'discussion' => ['data' => ['id' => 2]],
'discussion' => ['data' => ['type' => 'discussions', 'id' => 2]],
],
],
],
@@ -352,11 +358,12 @@ class GroupMentionsTest extends TestCase
'authenticatedAs' => 4,
'json' => [
'data' => [
'type' => 'posts',
'attributes' => [
'content' => '@"Ninjas"#g10',
],
'relationships' => [
'discussion' => ['data' => ['id' => 2]],
'discussion' => ['data' => ['type' => 'discussions', 'id' => 2]],
],
],
],
@@ -383,6 +390,7 @@ class GroupMentionsTest extends TestCase
'authenticatedAs' => 1,
'json' => [
'data' => [
'type' => 'posts',
'attributes' => [
'content' => 'New content with @"Mods"#g4 mention',
],

View File

@@ -11,7 +11,7 @@ namespace Flarum\Mentions\Tests\integration\api\discussions;
use Carbon\Carbon;
use Flarum\Discussion\Discussion;
use Flarum\Mentions\Api\LoadMentionedByRelationship;
use Flarum\Mentions\Api\PostResourceFields;
use Flarum\Post\Post;
use Flarum\Testing\integration\RetrievesAuthorizedUsers;
use Flarum\Testing\integration\TestCase;
@@ -170,7 +170,7 @@ class ListPostsTest extends TestCase
$mentionedBy = $data['relationships']['mentionedBy']['data'];
// Only displays a limited amount of mentioned by posts
$this->assertCount(LoadMentionedByRelationship::$maxMentionedBy, $mentionedBy);
$this->assertCount(PostResourceFields::$maxMentionedBy, $mentionedBy);
// Of the limited amount of mentioned by posts, they must be visible to the actor
$this->assertEquals([102, 104, 105, 106], Arr::pluck($mentionedBy, 'id'));
}
@@ -190,14 +190,14 @@ class ListPostsTest extends TestCase
])
);
$data = json_decode($response->getBody()->getContents(), true)['data'];
$data = json_decode($body = $response->getBody()->getContents(), true)['data'] ?? [];
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals(200, $response->getStatusCode(), $body);
$mentionedBy = $data[0]['relationships']['mentionedBy']['data'];
// Only displays a limited amount of mentioned by posts
$this->assertCount(LoadMentionedByRelationship::$maxMentionedBy, $mentionedBy);
$this->assertCount(PostResourceFields::$maxMentionedBy, $mentionedBy);
// Of the limited amount of mentioned by posts, they must be visible to the actor
$this->assertEquals([102, 104, 105, 106], Arr::pluck($mentionedBy, 'id'));
}
@@ -206,7 +206,7 @@ class ListPostsTest extends TestCase
* @dataProvider mentionedByIncludeProvider
* @test
*/
public function mentioned_by_relation_returns_limited_results_and_shows_only_visible_posts_in_show_discussion_endpoint(string $include)
public function mentioned_by_relation_returns_limited_results_and_shows_only_visible_posts_in_show_discussion_endpoint(?string $include)
{
$this->prepareMentionedByData();
@@ -219,15 +219,18 @@ class ListPostsTest extends TestCase
])
);
$included = json_decode($response->getBody()->getContents(), true)['included'];
$included = json_decode($body = $response->getBody()->getContents(), true)['included'] ?? [];
$this->assertEquals(200, $response->getStatusCode(), $body);
$mentionedBy = collect($included)
->where('type', 'posts')
->where('id', 101)
->first()['relationships']['mentionedBy']['data'];
->first()['relationships']['mentionedBy']['data'] ?? null;
$this->assertNotNull($mentionedBy, 'Mentioned by relation not included');
// Only displays a limited amount of mentioned by posts
$this->assertCount(LoadMentionedByRelationship::$maxMentionedBy, $mentionedBy);
$this->assertCount(PostResourceFields::$maxMentionedBy, $mentionedBy);
// Of the limited amount of mentioned by posts, they must be visible to the actor
$this->assertEquals([102, 104, 105, 106], Arr::pluck($mentionedBy, 'id'));
}
@@ -237,7 +240,7 @@ class ListPostsTest extends TestCase
return [
['posts,posts.mentionedBy'],
['posts.mentionedBy'],
[''],
[null],
];
}
@@ -253,10 +256,54 @@ class ListPostsTest extends TestCase
])
);
$data = json_decode($response->getBody()->getContents(), true)['data'];
$data = json_decode($body = $response->getBody()->getContents(), true)['data'] ?? [];
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals(200, $response->getStatusCode(), $body);
$this->assertEquals(0, $data['attributes']['mentionedByCount']);
}
/** @test */
public function mentioned_by_count_works_on_show_endpoint()
{
$this->prepareMentionedByData();
// List posts endpoint
$response = $this->send(
$this->request('GET', '/api/posts/101', [
'authenticatedAs' => 1,
])
);
$data = json_decode($body = $response->getBody()->getContents(), true)['data'] ?? [];
$this->assertEquals(200, $response->getStatusCode(), $body);
$this->assertEquals(10, $data['attributes']['mentionedByCount']);
}
/** @test */
public function mentioned_by_count_works_on_list_endpoint()
{
$this->prepareMentionedByData();
// List posts endpoint
$response = $this->send(
$this->request('GET', '/api/posts', [
'authenticatedAs' => 1,
])->withQueryParams([
'filter' => ['discussion' => 100],
])
);
$data = json_decode($body = $response->getBody()->getContents(), true)['data'] ?? [];
$this->assertEquals(200, $response->getStatusCode(), $body);
$post101 = collect($data)->where('id', 101)->first();
$post112 = collect($data)->where('id', 112)->first();
$this->assertEquals(10, $post101['attributes']['mentionedByCount']);
$this->assertEquals(0, $post112['attributes']['mentionedByCount']);
}
}

View File

@@ -83,11 +83,12 @@ class PostMentionsTest extends TestCase
'authenticatedAs' => 1,
'json' => [
'data' => [
'type' => 'posts',
'attributes' => [
'content' => '@potato#4',
],
'relationships' => [
'discussion' => ['data' => ['id' => 2]],
'discussion' => ['data' => ['type' => 'discussions', 'id' => 2]],
],
],
],
@@ -114,11 +115,12 @@ class PostMentionsTest extends TestCase
'authenticatedAs' => 1,
'json' => [
'data' => [
'type' => 'posts',
'attributes' => [
'content' => '@"POTATO$"#p4',
],
'relationships' => [
'discussion' => ['data' => ['id' => 2]],
'discussion' => ['data' => ['type' => 'discussions', 'id' => 2]],
],
],
],
@@ -145,11 +147,12 @@ class PostMentionsTest extends TestCase
'authenticatedAs' => 1,
'json' => [
'data' => [
'type' => 'posts',
'attributes' => [
'content' => '@"potato"#p50',
],
'relationships' => [
'discussion' => ['data' => ['id' => 2]],
'discussion' => ['data' => ['type' => 'discussions', 'id' => 2]],
],
],
],
@@ -176,11 +179,12 @@ class PostMentionsTest extends TestCase
'authenticatedAs' => 1,
'json' => [
'data' => [
'type' => 'posts',
'attributes' => [
'content' => '@“POTATO$”#p4',
],
'relationships' => [
'discussion' => ['data' => ['id' => 2]],
'discussion' => ['data' => ['type' => 'discussions', 'id' => 2]],
],
],
],
@@ -207,11 +211,12 @@ class PostMentionsTest extends TestCase
'authenticatedAs' => 1,
'json' => [
'data' => [
'type' => 'posts',
'attributes' => [
'content' => '@"franzofflarum"#p215',
],
'relationships' => [
'discussion' => ['data' => ['id' => 2]],
'discussion' => ['data' => ['type' => 'discussions', 'id' => 2]],
],
],
],
@@ -238,11 +243,12 @@ class PostMentionsTest extends TestCase
'authenticatedAs' => 1,
'json' => [
'data' => [
'type' => 'posts',
'attributes' => [
'content' => '@"TOBY$"#p5 @"flarum"#2015 @"franzofflarum"#220 @"POTATO$"#3 @"POTATO$"#p4',
],
'relationships' => [
'discussion' => ['data' => ['id' => 2]],
'discussion' => ['data' => ['type' => 'discussions', 'id' => 2]],
],
],
],
@@ -385,11 +391,12 @@ class PostMentionsTest extends TestCase
'authenticatedAs' => 1,
'json' => [
'data' => [
'type' => 'posts',
'attributes' => [
'content' => '@"Bad "#p6 User"#p9',
],
'relationships' => [
'discussion' => ['data' => ['id' => 2]],
'discussion' => ['data' => ['type' => 'discussions', 'id' => 2]],
],
],
],
@@ -437,11 +444,12 @@ class PostMentionsTest extends TestCase
'authenticatedAs' => 1,
'json' => [
'data' => [
'type' => 'posts',
'attributes' => [
'content' => '@"Bad _ User"#p9',
],
'relationships' => [
'discussion' => ['data' => ['id' => 2]],
'discussion' => ['data' => ['type' => 'discussions', 'id' => 2]],
],
],
],
@@ -468,6 +476,7 @@ class PostMentionsTest extends TestCase
'authenticatedAs' => 1,
'json' => [
'data' => [
'type' => 'posts',
'attributes' => [
'content' => '@"Bad _ User"#p9',
],
@@ -496,6 +505,7 @@ class PostMentionsTest extends TestCase
'authenticatedAs' => 1,
'json' => [
'data' => [
'type' => 'posts',
'attributes' => [
'content' => '@"Bad _ User"#p9',
],
@@ -524,6 +534,7 @@ class PostMentionsTest extends TestCase
'authenticatedAs' => 1,
'json' => [
'data' => [
'type' => 'posts',
'attributes' => [
'content' => '@"acme"#p11',
],

View File

@@ -72,11 +72,12 @@ class TagMentionsTest extends TestCase
'authenticatedAs' => 1,
'json' => [
'data' => [
'type' => 'posts',
'attributes' => [
'content' => '#flarum',
],
'relationships' => [
'discussion' => ['data' => ['id' => 2]],
'discussion' => ['data' => ['type' => 'discussions', 'id' => 2]],
],
],
],
@@ -100,11 +101,12 @@ class TagMentionsTest extends TestCase
'authenticatedAs' => 1,
'json' => [
'data' => [
'type' => 'posts',
'attributes' => [
'content' => '#戦い',
],
'relationships' => [
'discussion' => ['data' => ['id' => 2]],
'discussion' => ['data' => ['type' => 'discussions', 'id' => 2]],
],
],
],
@@ -129,11 +131,12 @@ class TagMentionsTest extends TestCase
'authenticatedAs' => 1,
'json' => [
'data' => [
'type' => 'posts',
'attributes' => [
'content' => '#franzofflarum',
],
'relationships' => [
'discussion' => ['data' => ['id' => 2]],
'discussion' => ['data' => ['type' => 'discussions', 'id' => 2]],
],
],
],
@@ -159,11 +162,12 @@ class TagMentionsTest extends TestCase
'authenticatedAs' => 1,
'json' => [
'data' => [
'type' => 'posts',
'attributes' => [
'content' => '#test',
],
'relationships' => [
'discussion' => ['data' => ['id' => 2]],
'discussion' => ['data' => ['type' => 'discussions', 'id' => 2]],
],
],
],
@@ -187,11 +191,12 @@ class TagMentionsTest extends TestCase
'authenticatedAs' => 3,
'json' => [
'data' => [
'type' => 'posts',
'attributes' => [
'content' => '#dev',
],
'relationships' => [
'discussion' => ['data' => ['id' => 2]],
'discussion' => ['data' => ['type' => 'discussions', 'id' => 2]],
],
],
],
@@ -215,11 +220,12 @@ class TagMentionsTest extends TestCase
'authenticatedAs' => 1,
'json' => [
'data' => [
'type' => 'posts',
'attributes' => [
'content' => '#dev',
],
'relationships' => [
'discussion' => ['data' => ['id' => 2]],
'discussion' => ['data' => ['type' => 'discussions', 'id' => 2]],
],
],
],
@@ -243,11 +249,12 @@ class TagMentionsTest extends TestCase
'authenticatedAs' => 1,
'json' => [
'data' => [
'type' => 'posts',
'attributes' => [
'content' => '#test #flarum #support #laravel #franzofflarum',
],
'relationships' => [
'discussion' => ['data' => ['id' => 2]],
'discussion' => ['data' => ['type' => 'discussions', 'id' => 2]],
],
],
],
@@ -369,6 +376,7 @@ class TagMentionsTest extends TestCase
'authenticatedAs' => 1,
'json' => [
'data' => [
'type' => 'posts',
'attributes' => [
'content' => '#laravel',
],

View File

@@ -74,11 +74,12 @@ class UserMentionsTest extends TestCase
'authenticatedAs' => 1,
'json' => [
'data' => [
'type' => 'posts',
'attributes' => [
'content' => '@potato',
],
'relationships' => [
'discussion' => ['data' => ['id' => 2]],
'discussion' => ['data' => ['type' => 'discussions', 'id' => 2]],
],
],
],
@@ -107,11 +108,12 @@ class UserMentionsTest extends TestCase
'authenticatedAs' => 1,
'json' => [
'data' => [
'type' => 'posts',
'attributes' => [
'content' => '@potato',
],
'relationships' => [
'discussion' => ['data' => ['id' => 2]],
'discussion' => ['data' => ['type' => 'discussions', 'id' => 2]],
],
],
],
@@ -138,6 +140,7 @@ class UserMentionsTest extends TestCase
'authenticatedAs' => 1,
'json' => [
'data' => [
'type' => 'posts',
'attributes' => [
'content' => '@"POTATO$"#3',
],
@@ -169,6 +172,7 @@ class UserMentionsTest extends TestCase
'authenticatedAs' => 1,
'json' => [
'data' => [
'type' => 'posts',
'attributes' => [
'content' => '@“POTATO$”#3',
],
@@ -200,6 +204,7 @@ class UserMentionsTest extends TestCase
'authenticatedAs' => 1,
'json' => [
'data' => [
'type' => 'posts',
'attributes' => [
'content' => '@"franzofflarum"#82',
],
@@ -231,6 +236,7 @@ class UserMentionsTest extends TestCase
'authenticatedAs' => 1,
'json' => [
'data' => [
'type' => 'posts',
'attributes' => [
'content' => '@"TOBY$"#4 @"POTATO$"#p4 @"franzofflarum"#82 @"POTATO$"#3',
],
@@ -284,6 +290,7 @@ class UserMentionsTest extends TestCase
'authenticatedAs' => 1,
'json' => [
'data' => [
'type' => 'posts',
'attributes' => [
'content' => '@"potato_"#3',
],
@@ -314,6 +321,7 @@ class UserMentionsTest extends TestCase
'authenticatedAs' => 1,
'json' => [
'data' => [
'type' => 'posts',
'attributes' => [
'content' => '@"potato_"#3',
],
@@ -369,6 +377,7 @@ class UserMentionsTest extends TestCase
'authenticatedAs' => 1,
'json' => [
'data' => [
'type' => 'posts',
'attributes' => [
'content' => '@"Bad "#p6 User"#5',
],
@@ -421,6 +430,7 @@ class UserMentionsTest extends TestCase
'authenticatedAs' => 1,
'json' => [
'data' => [
'type' => 'posts',
'attributes' => [
'content' => '@"Bad _ User"#5',
],
@@ -452,6 +462,7 @@ class UserMentionsTest extends TestCase
'authenticatedAs' => 1,
'json' => [
'data' => [
'type' => 'posts',
'attributes' => [
'content' => '@"Bad _ User"#5',
],
@@ -480,6 +491,7 @@ class UserMentionsTest extends TestCase
'authenticatedAs' => 1,
'json' => [
'data' => [
'type' => 'posts',
'attributes' => [
'content' => '@"Bad _ User"#5',
],