1
0
mirror of https://github.com/flarum/core.git synced 2025-08-12 19:34:18 +02:00

Fix starting discussion without tags

This commit is contained in:
SychO9
2020-12-25 18:12:27 +01:00
committed by Alexander Skvortsov
parent 5291c969a4
commit c2e5c1008f

View File

@@ -57,39 +57,25 @@ class SaveTagsToDatabase
$discussion = $event->discussion; $discussion = $event->discussion;
$actor = $event->actor; $actor = $event->actor;
// TODO: clean up, prevent discussion from being created without tags $newTagIds = [];
if (isset($event->data['relationships']['tags']['data'])) { $newTags = [];
if ($discussion->exists) {
$actor->assertCan('tag', $discussion);
}
$primaryCount = 0;
$secondaryCount = 0;
if (isset($event->data['relationships']['tags']['data'])) {
$linkage = (array) $event->data['relationships']['tags']['data']; $linkage = (array) $event->data['relationships']['tags']['data'];
$newTagIds = [];
foreach ($linkage as $link) { foreach ($linkage as $link) {
$newTagIds[] = (int) $link['id']; $newTagIds[] = (int) $link['id'];
} }
$newTags = Tag::whereIn('id', $newTagIds)->get(); $newTags = Tag::whereIn('id', $newTagIds)->get();
$primaryCount = 0;
$secondaryCount = 0;
foreach ($newTags as $tag) {
if ($actor->cannot('startDiscussion', $tag)) {
throw new PermissionDeniedException;
} }
if ($tag->position !== null && $tag->parent_id === null) { if ($discussion->exists && isset($event->data['relationships']['tags']['data'])) {
$primaryCount++; $actor->assertCan('tag', $discussion);
} else {
$secondaryCount++;
}
}
$this->validateTagCount('primary', $primaryCount);
$this->validateTagCount('secondary', $secondaryCount);
if ($discussion->exists) {
$oldTags = $discussion->tags()->get(); $oldTags = $discussion->tags()->get();
$oldTagIds = $oldTags->pluck('id')->all(); $oldTagIds = $oldTags->pluck('id')->all();
@@ -108,11 +94,25 @@ class SaveTagsToDatabase
); );
} }
if (! $discussion->exists || isset($event->data['relationships']['tags']['data'])) {
foreach ($newTags as $tag) {
if (! $discussion->exists && $actor->cannot('startDiscussion', $tag)) {
throw new PermissionDeniedException;
}
if ($tag->position !== null && $tag->parent_id === null) {
$primaryCount++;
} else {
$secondaryCount++;
}
}
$this->validateTagCount('primary', $primaryCount);
$this->validateTagCount('secondary', $secondaryCount);
$discussion->afterSave(function ($discussion) use ($newTagIds) { $discussion->afterSave(function ($discussion) use ($newTagIds) {
$discussion->tags()->sync($newTagIds); $discussion->tags()->sync($newTagIds);
}); });
} elseif (! $discussion->exists && ! $actor->hasPermission('startDiscussion')) {
throw new PermissionDeniedException;
} }
} }