I am building a small project using Getstream Laravel package. However I am having a problem trying to display notifications for new followers. I get an empty result set when I call \FeedManager::getNotificationFeed($request->user()->id)->getActivities()
in a controller method. I have my follow
model looking like this:
class Follow extends Model
{
protected $fillable = ['target_id'];
public function user()
{
return $this->belongsTo(User::class);
}
public function target()
{
return $this->belongsTo(User::class);
}
public function activityNotify()
{
$targetFeed = \FeedManager::getNotificationFeed($this->target->id);
return array($targetFeed);
}
}
Then the controller action to get the notifications for new follows looks like this:
public function notification(Request $request)
{
$feed = \FeedManager::getNotificationFeed($request->user()->id);
dd($feed->getActivities());
$activities = $feed->getActivities(0,25)['results'];
return view('feed.notifications', [
'activities' => $activities,
]);
}
In the user model I have defined a relationship that a user has many follows. And lastly the follow and unfollow actions in the FollowController
look like this:
public function follow(Request $request)
{
// Create a new follow instance for the authenticated user
// This target_id will come from a hidden field input after clicking the
// follow button
$request->user()->follows()->create([
'target_id' => $request->target_id,
]);
\FeedManager::followUser($request->user()->id, $request->target_id);
return redirect()->back();
}
public function unfollow($user_id, Request $request)
{
$follow = $request->user()->follows()->where('target_id', $user_id)->first();
\FeedManager::unfollowUser($request->user()->id, $follow->target_id);
$follow->delete();
return redirect()->back();
}
Not sure if there's something I left out but I can't get results for the notification feed. If I head over to the explorer tab from the Stream dashboard, I can see that I got two new follows which generated both timeline and timeline_aggregated type of feed. Or how should I get the notification feed from a controller action? Thanks in advance
The \FeedManager::followUser
method create a two follow relationships: timeline to user and timeline_aggregated to user.
In this case what you want to create a follow relationship between notification and user. Something like this should do it:
\FeedManager:: getNotificationFeed($request->user()->id)
.followFeed('user', $follow->target_id)