mirror of
https://github.com/flarum/core.git
synced 2025-10-10 06:24:26 +02: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:
@@ -9,6 +9,17 @@ class ActivitySerializer extends BaseSerializer
|
||||
*/
|
||||
protected $type = 'activity';
|
||||
|
||||
/**
|
||||
* A map of activity types (key) to the serializer that should be used to
|
||||
* output the activity's subject (value).
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $subjects = [
|
||||
'posted' => 'Flarum\Api\Serializers\PostBasicSerializer',
|
||||
'joined' => 'Flarum\Api\Serializers\UserBasicSerializer'
|
||||
];
|
||||
|
||||
/**
|
||||
* Serialize attributes of an Activity model for JSON output.
|
||||
*
|
||||
@@ -18,9 +29,7 @@ class ActivitySerializer extends BaseSerializer
|
||||
protected function attributes($activity)
|
||||
{
|
||||
$attributes = [
|
||||
'id' => ((int) $activity->id) ?: str_random(5),
|
||||
'contentType' => $activity->type,
|
||||
'content' => json_encode($activity->data),
|
||||
'time' => $activity->time->toRFC3339String()
|
||||
];
|
||||
|
||||
@@ -37,8 +46,10 @@ class ActivitySerializer extends BaseSerializer
|
||||
return $this->hasOne('Flarum\Api\Serializers\UserBasicSerializer');
|
||||
}
|
||||
|
||||
public function post()
|
||||
public function subject()
|
||||
{
|
||||
return $this->hasOne('Flarum\Api\Serializers\PostSerializer');
|
||||
return $this->hasOne(function ($activity) {
|
||||
return static::$subjects[$activity->type];
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user