mirror of
https://github.com/flarum/core.git
synced 2025-10-27 05:31:29 +01:00
New user activity feed API.
Originally the user activity feed was implemented using UNIONs. I was looking at make an API to add activity “sources”, or extra UNION queries (select from posts, mentions, etc.) but quickly realised that this is too slow and there’s no way to make it scale. So I’ve implemented an API which is very similar to how notifications work (see previous commit). The `activity` table is an aggregation of stuff that happens, and it’s kept in sync by an ActivitySyncer which is used whenever a post it created/edited/deleted, a user is mentioned/unmentioned, etc. Again, the API is very simple (see Core\Activity\PostedActivity + Core\Handlers\Events\UserActivitySyncer)
This commit is contained in:
@@ -1,39 +1,22 @@
|
||||
<?php namespace Flarum\Core\Repositories;
|
||||
|
||||
use Flarum\Core\Models\Activity;
|
||||
use Flarum\Core\Models\Post;
|
||||
use Flarum\Core\Models\User;
|
||||
|
||||
class EloquentActivityRepository implements ActivityRepositoryInterface
|
||||
{
|
||||
public function findByUser($userId, User $viewer, $count = null, $start = 0, $type = null)
|
||||
public function findByUser($userId, User $viewer, $limit = null, $offset = 0, $type = null)
|
||||
{
|
||||
// This is all very rough and needs to be cleaned up
|
||||
$query = Activity::where('user_id', $userId)
|
||||
->whereIn('type', array_keys(Activity::getTypes()))
|
||||
->orderBy('time', 'desc')
|
||||
->skip($offset)
|
||||
->take($limit);
|
||||
|
||||
$null = \DB::raw('NULL');
|
||||
$query = Activity::with('sender')->select('id', 'user_id', 'sender_id', 'type', 'data', 'time', \DB::raw('NULL as post_id'))->where('user_id', $userId);
|
||||
|
||||
if ($type) {
|
||||
if ($type !== null) {
|
||||
$query->where('type', $type);
|
||||
}
|
||||
|
||||
$posts = Post::whereCan($viewer, 'view')->with('post', 'post.discussion', 'post.user', 'post.discussion.startUser', 'post.discussion.lastUser')->select(\DB::raw("CONCAT('post', id)"), 'user_id', $null, \DB::raw("'post'"), $null, 'time', 'id')->where('user_id', $userId)->where('type', 'comment')->whereNull('hide_time');
|
||||
|
||||
if ($type === 'post') {
|
||||
$posts->where('number', '>', 1);
|
||||
} elseif ($type === 'discussion') {
|
||||
$posts->where('number', 1);
|
||||
}
|
||||
|
||||
if (!$type) {
|
||||
$join = User::select(\DB::raw("CONCAT('join', id)"), 'id', 'id', \DB::raw("'join'"), $null, 'join_time', $null)->where('id', $userId);
|
||||
$query->union($join->getQuery());
|
||||
}
|
||||
|
||||
return $query->union($posts->getQuery())
|
||||
->orderBy('time', 'desc')
|
||||
->skip($start)
|
||||
->take($count)
|
||||
->get();
|
||||
return $query->get();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user