Skip to content

Commit 48c8a53

Browse files
authored
Add property crosspostedEntries to API (#1705)
1 parent 7ac0a31 commit 48c8a53

19 files changed

Lines changed: 107 additions & 65 deletions

src/Controller/Api/BaseApi.php

Lines changed: 24 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -220,46 +220,30 @@ public function serializePaginated(array $serializedItems, PagerfantaInterface $
220220
public function serializeContentInterface(ContentInterface $content, bool $forceVisible = false): mixed
221221
{
222222
$toReturn = null;
223-
$className = $this->entityManager->getClassMetadata(\get_class($content))->rootEntityName;
224-
switch ($className) {
225-
case Entry::class:
226-
/**
227-
* @var Entry $content
228-
*/
229-
$dto = $this->entryFactory->createResponseDto($content, $this->tagLinkRepository->getTagsOfEntry($content));
230-
$dto->visibility = $forceVisible ? VisibilityInterface::VISIBILITY_VISIBLE : $dto->visibility;
231-
$toReturn = $dto->jsonSerialize();
232-
$toReturn['itemType'] = 'entry';
233-
break;
234-
case EntryComment::class:
235-
/**
236-
* @var EntryComment $content
237-
*/
238-
$dto = $this->entryCommentFactory->createResponseDto($content, $this->tagLinkRepository->getTagsOfEntryComment($content));
239-
$dto->visibility = $forceVisible ? VisibilityInterface::VISIBILITY_VISIBLE : $dto->visibility;
240-
$toReturn = $dto->jsonSerialize();
241-
$toReturn['itemType'] = 'entry_comment';
242-
break;
243-
case Post::class:
244-
/**
245-
* @var Post $content
246-
*/
247-
$dto = $this->postFactory->createResponseDto($content, $this->tagLinkRepository->getTagsOfPost($content));
248-
$dto->visibility = $forceVisible ? VisibilityInterface::VISIBILITY_VISIBLE : $dto->visibility;
249-
$toReturn = $dto->jsonSerialize();
250-
$toReturn['itemType'] = 'post';
251-
break;
252-
case PostComment::class:
253-
/**
254-
* @var PostComment $content
255-
*/
256-
$dto = $this->postCommentFactory->createResponseDto($content, $this->tagLinkRepository->getTagsOfPostComment($content));
257-
$dto->visibility = $forceVisible ? VisibilityInterface::VISIBILITY_VISIBLE : $dto->visibility;
258-
$toReturn = $dto->jsonSerialize();
259-
$toReturn['itemType'] = 'post_comment';
260-
break;
261-
default:
262-
throw new \LogicException('Invalid contentInterface classname "'.$className.'"');
223+
if ($content instanceof Entry) {
224+
$cross = $this->entryRepository->findCross($content);
225+
$crossDtos = array_map(fn ($entry) => $this->entryFactory->createResponseDto($entry, []), $cross);
226+
$dto = $this->entryFactory->createResponseDto($content, $this->tagLinkRepository->getTagsOfEntry($content), $crossDtos);
227+
$dto->visibility = $forceVisible ? VisibilityInterface::VISIBILITY_VISIBLE : $dto->visibility;
228+
$toReturn = $dto->jsonSerialize();
229+
$toReturn['itemType'] = 'entry';
230+
} elseif ($content instanceof EntryComment) {
231+
$dto = $this->entryCommentFactory->createResponseDto($content, $this->tagLinkRepository->getTagsOfEntryComment($content));
232+
$dto->visibility = $forceVisible ? VisibilityInterface::VISIBILITY_VISIBLE : $dto->visibility;
233+
$toReturn = $dto->jsonSerialize();
234+
$toReturn['itemType'] = 'entry_comment';
235+
} elseif ($content instanceof Post) {
236+
$dto = $this->postFactory->createResponseDto($content, $this->tagLinkRepository->getTagsOfPost($content));
237+
$dto->visibility = $forceVisible ? VisibilityInterface::VISIBILITY_VISIBLE : $dto->visibility;
238+
$toReturn = $dto->jsonSerialize();
239+
$toReturn['itemType'] = 'post';
240+
} elseif ($content instanceof PostComment) {
241+
$dto = $this->postCommentFactory->createResponseDto($content, $this->tagLinkRepository->getTagsOfPostComment($content));
242+
$dto->visibility = $forceVisible ? VisibilityInterface::VISIBILITY_VISIBLE : $dto->visibility;
243+
$toReturn = $dto->jsonSerialize();
244+
$toReturn['itemType'] = 'post_comment';
245+
} else {
246+
throw new \LogicException('Invalid contentInterface classname "'.$this->entityManager->getClassMetadata(\get_class($content))->rootEntityName.'"');
263247
}
264248

265249
if ($forceVisible) {

src/Controller/Api/Entry/Admin/EntriesChangeMagazineApi.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function __invoke(
8686
$manager->changeMagazine($entry, $target);
8787

8888
return new JsonResponse(
89-
$this->serializeEntry($factory->createDto($entry), $this->tagLinkRepository->getTagsOfEntry($entry)),
89+
$this->serializeEntry($factory->createDto($entry), $this->tagLinkRepository->getTagsOfEntry($entry), $this->entryRepository->findCross($entry)),
9090
headers: $headers
9191
);
9292
}

src/Controller/Api/Entry/DomainEntriesRetrieveApi.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public function __invoke(
152152
try {
153153
\assert($value instanceof Entry);
154154
$this->handlePrivateContent($value);
155-
array_push($dtos, $this->serializeEntry($factory->createDto($value), $this->tagLinkRepository->getTagsOfEntry($value)));
155+
$dtos[] = $this->serializeEntry($factory->createDto($value), $this->tagLinkRepository->getTagsOfEntry($value), $this->entryRepository->findCross($value));
156156
} catch (\Exception $e) {
157157
continue;
158158
}

src/Controller/Api/Entry/EntriesBaseApi.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,16 @@ public function setCommentsFactory(EntryCommentFactory $commentsFactory): void
3636

3737
/**
3838
* Serialize a single entry to JSON.
39+
*
40+
* @param Entry[]|null $crosspostedEntries
3941
*/
40-
protected function serializeEntry(EntryDto|Entry $dto, array $tags): EntryResponseDto
42+
protected function serializeEntry(EntryDto|Entry $dto, array $tags, ?array $crosspostedEntries = null): EntryResponseDto
4143
{
42-
$response = $this->entryFactory->createResponseDto($dto, $tags);
44+
$crosspostedEntryDtos = null;
45+
if (null !== $crosspostedEntries) {
46+
$crosspostedEntryDtos = array_map(fn (Entry $item) => $this->entryFactory->createResponseDto($item, []), $crosspostedEntries);
47+
}
48+
$response = $this->entryFactory->createResponseDto($dto, $tags, $crosspostedEntryDtos);
4349

4450
if ($this->isGranted('ROLE_OAUTH2_ENTRY:VOTE')) {
4551
$response->isFavourited = $dto instanceof EntryDto ? $dto->isFavourited : $dto->isFavored($this->getUserOrThrow());

src/Controller/Api/Entry/EntriesFavouriteApi.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function __invoke(
6969
$manager->toggle($this->getUserOrThrow(), $entry);
7070

7171
return new JsonResponse(
72-
$this->serializeEntry($factory->createDto($entry), $this->tagLinkRepository->getTagsOfEntry($entry)),
72+
$this->serializeEntry($factory->createDto($entry), $this->tagLinkRepository->getTagsOfEntry($entry), $this->entryRepository->findCross($entry)),
7373
headers: $headers
7474
);
7575
}

src/Controller/Api/Entry/EntriesRetrieveApi.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function __invoke(
8484
$dto = $factory->createDto($entry);
8585

8686
return new JsonResponse(
87-
$this->serializeEntry($dto, $this->tagLinkRepository->getTagsOfEntry($entry)),
87+
$this->serializeEntry($dto, $this->tagLinkRepository->getTagsOfEntry($entry), $this->entryRepository->findCross($entry)),
8888
headers: $headers
8989
);
9090
}
@@ -207,7 +207,7 @@ public function collection(
207207
try {
208208
\assert($value instanceof Entry);
209209
$this->handlePrivateContent($value);
210-
array_push($dtos, $this->serializeEntry($factory->createDto($value), $this->tagLinkRepository->getTagsOfEntry($value)));
210+
$dtos[] = $this->serializeEntry($factory->createDto($value), $this->tagLinkRepository->getTagsOfEntry($value), $this->entryRepository->findCross($value));
211211
} catch (AccessDeniedException $e) {
212212
continue;
213213
}
@@ -322,7 +322,7 @@ public function subscribed(
322322
try {
323323
\assert($value instanceof Entry);
324324
$this->handlePrivateContent($value);
325-
array_push($dtos, $this->serializeEntry($factory->createDto($value), $this->tagLinkRepository->getTagsOfEntry($value)));
325+
$dtos[] = $this->serializeEntry($factory->createDto($value), $this->tagLinkRepository->getTagsOfEntry($value), $this->entryRepository->findCross($value));
326326
} catch (\Exception $e) {
327327
continue;
328328
}
@@ -437,7 +437,7 @@ public function moderated(
437437
try {
438438
\assert($value instanceof Entry);
439439
$this->handlePrivateContent($value);
440-
array_push($dtos, $this->serializeEntry($factory->createDto($value), $this->tagLinkRepository->getTagsOfEntry($value)));
440+
$dtos[] = $this->serializeEntry($factory->createDto($value), $this->tagLinkRepository->getTagsOfEntry($value), $this->entryRepository->findCross($value));
441441
} catch (\Exception $e) {
442442
continue;
443443
}
@@ -552,7 +552,7 @@ public function favourited(
552552
try {
553553
\assert($value instanceof Entry);
554554
$this->handlePrivateContent($value);
555-
array_push($dtos, $this->serializeEntry($factory->createDto($value), $this->tagLinkRepository->getTagsOfEntry($value)));
555+
$dtos[] = $this->serializeEntry($factory->createDto($value), $this->tagLinkRepository->getTagsOfEntry($value), $this->entryRepository->findCross($value));
556556
} catch (\Exception $e) {
557557
continue;
558558
}

src/Controller/Api/Entry/EntriesUpdateApi.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public function __invoke(
9696
$entry = $manager->edit($entry, $dto, $this->getUserOrThrow());
9797

9898
return new JsonResponse(
99-
$this->serializeEntry($entry, $this->tagLinkRepository->getTagsOfEntry($entry)),
99+
$this->serializeEntry($entry, $this->tagLinkRepository->getTagsOfEntry($entry), $this->entryRepository->findCross($entry)),
100100
headers: $headers
101101
);
102102
}

src/Controller/Api/Entry/EntriesVoteApi.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public function __invoke(
9797
$manager->vote($choice, $entry, $this->getUserOrThrow(), rateLimit: false);
9898

9999
return new JsonResponse(
100-
$this->serializeEntry($factory->createDto($entry), $this->tagLinkRepository->getTagsOfEntry($entry)),
100+
$this->serializeEntry($factory->createDto($entry), $this->tagLinkRepository->getTagsOfEntry($entry), $this->entryRepository->findCross($entry)),
101101
headers: $headers
102102
);
103103
}

src/Controller/Api/Entry/MagazineEntriesRetrieveApi.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public function __invoke(
153153
foreach ($entries->getCurrentPageResults() as $value) {
154154
try {
155155
\assert($value instanceof Entry);
156-
array_push($dtos, $this->serializeEntry($factory->createDto($value), $this->tagLinkRepository->getTagsOfEntry($value)));
156+
$dtos[] = $this->serializeEntry($factory->createDto($value), $this->tagLinkRepository->getTagsOfEntry($value), $this->entryRepository->findCross($value));
157157
} catch (\Exception $e) {
158158
continue;
159159
}

src/Controller/Api/Entry/MagazineEntryCreateApi.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public function article(
101101
]);
102102

103103
return new JsonResponse(
104-
$this->serializeEntry($manager->createDto($entry), $this->tagLinkRepository->getTagsOfEntry($entry)),
104+
$this->serializeEntry($manager->createDto($entry), $this->tagLinkRepository->getTagsOfEntry($entry), $this->entryRepository->findCross($entry)),
105105
status: 201,
106106
headers: $headers
107107
);
@@ -179,7 +179,7 @@ public function link(
179179
]);
180180

181181
return new JsonResponse(
182-
$this->serializeEntry($manager->createDto($entry), $this->tagLinkRepository->getTagsOfEntry($entry)),
182+
$this->serializeEntry($manager->createDto($entry), $this->tagLinkRepository->getTagsOfEntry($entry), $this->entryRepository->findCross($entry)),
183183
status: 201,
184184
headers: $headers
185185
);
@@ -252,7 +252,7 @@ public function video(
252252
]);
253253

254254
return new JsonResponse(
255-
$this->serializeEntry($manager->createDto($entry), $this->tagLinkRepository->getTagsOfEntry($entry)),
255+
$this->serializeEntry($manager->createDto($entry), $this->tagLinkRepository->getTagsOfEntry($entry), $this->entryRepository->findCross($entry)),
256256
status: 201,
257257
headers: $headers
258258
);
@@ -363,7 +363,7 @@ public function uploadImage(
363363
$entry = $manager->create($dto, $this->getUserOrThrow());
364364

365365
return new JsonResponse(
366-
$this->serializeEntry($manager->createDto($entry), $this->tagLinkRepository->getTagsOfEntry($entry)),
366+
$this->serializeEntry($manager->createDto($entry), $this->tagLinkRepository->getTagsOfEntry($entry), $this->entryRepository->findCross($entry)),
367367
status: 201,
368368
headers: $headers
369369
);

0 commit comments

Comments
 (0)