mirror of
https://github.com/flarum/core.git
synced 2025-08-10 10:24:46 +02:00
Fix starting discussion without tags
This commit is contained in:
committed by
Alexander Skvortsov
parent
5291c969a4
commit
c2e5c1008f
@@ -57,25 +57,46 @@ 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;
|
|
||||||
|
if ($discussion->exists && isset($event->data['relationships']['tags']['data'])) {
|
||||||
|
$actor->assertCan('tag', $discussion);
|
||||||
|
|
||||||
|
$oldTags = $discussion->tags()->get();
|
||||||
|
$oldTagIds = $oldTags->pluck('id')->all();
|
||||||
|
|
||||||
|
if ($oldTagIds == $newTagIds) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
foreach ($newTags as $tag) {
|
foreach ($newTags as $tag) {
|
||||||
if ($actor->cannot('startDiscussion', $tag)) {
|
if (! in_array($tag->id, $oldTagIds) && $actor->cannot('addToDiscussion', $tag)) {
|
||||||
|
throw new PermissionDeniedException;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$discussion->raise(
|
||||||
|
new DiscussionWasTagged($discussion, $actor, $oldTags->all())
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! $discussion->exists || isset($event->data['relationships']['tags']['data'])) {
|
||||||
|
foreach ($newTags as $tag) {
|
||||||
|
if (! $discussion->exists && $actor->cannot('startDiscussion', $tag)) {
|
||||||
throw new PermissionDeniedException;
|
throw new PermissionDeniedException;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -89,30 +110,9 @@ class SaveTagsToDatabase
|
|||||||
$this->validateTagCount('primary', $primaryCount);
|
$this->validateTagCount('primary', $primaryCount);
|
||||||
$this->validateTagCount('secondary', $secondaryCount);
|
$this->validateTagCount('secondary', $secondaryCount);
|
||||||
|
|
||||||
if ($discussion->exists) {
|
|
||||||
$oldTags = $discussion->tags()->get();
|
|
||||||
$oldTagIds = $oldTags->pluck('id')->all();
|
|
||||||
|
|
||||||
if ($oldTagIds == $newTagIds) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($newTags as $tag) {
|
|
||||||
if (! in_array($tag->id, $oldTagIds) && $actor->cannot('addToDiscussion', $tag)) {
|
|
||||||
throw new PermissionDeniedException;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$discussion->raise(
|
|
||||||
new DiscussionWasTagged($discussion, $actor, $oldTags->all())
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
$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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user