Fix crash when loading notifications in some instances

Specifically, the crash would occur when the first notification had a subject without a discussion relationship (e.g. the Subscriptions extension's newPost notification, where the subject itself was a discussion). Instead of simply eager loading the nested subject.discussion relationship, we load discussions manually instead.
This commit is contained in:
Toby Zerner 2015-12-03 15:10:05 +10:30
parent cea1cbc2d6
commit 287ce2fddd
2 changed files with 33 additions and 1 deletions

View File

@ -17,6 +17,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- Fix error when sorting discussions by "oldest" (#627)
- Fix composer preview button on mobile (#196)
- Enable "Start a Discussion" button if global permissions are restricted but tag-specific permissions are granted (#640)
- Fix crash when loading notifications in some instances
- Improve composer appearance/usability on mobile
- Show "reply" action in discussion menu on mobile
- Fix some issues with dropdown positioning

View File

@ -10,6 +10,7 @@
namespace Flarum\Api\Controller;
use Flarum\Core\Discussion;
use Flarum\Core\Repository\NotificationRepository;
use Flarum\Core\Exception\PermissionDeniedException;
use Psr\Http\Message\ServerRequestInterface;
@ -66,6 +67,36 @@ class ListNotificationsController extends AbstractCollectionController
$offset = $this->extractOffset($request);
$include = $this->extractInclude($request);
return $this->notifications->findByUser($actor, $limit, $offset)->load($include);
$notifications = $this->notifications->findByUser($actor, $limit, $offset)
->load(array_diff($include, ['subject.discussion']))
->all();
if (in_array('subject.discussion', $include)) {
$this->loadSubjectDiscussions($notifications);
}
return $notifications;
}
/**
* @param \Flarum\Core\Notification[] $notifications
*/
private function loadSubjectDiscussions(array $notifications)
{
$ids = [];
foreach ($notifications as $notification) {
if ($notification->subject && $notification->subject->discussion_id) {
$ids[] = $notification->subject->discussion_id;
}
}
$discussions = Discussion::find(array_unique($ids));
foreach ($notifications as $notification) {
if ($notification->subject && $notification->subject->discussion_id) {
$notification->subject->setRelation('discussion', $discussions->find($notification->subject->discussion_id));
}
}
}
}