mirror of
https://github.com/flarum/core.git
synced 2025-10-10 06:24:26 +02:00
Add user activity system
This commit is contained in:
@@ -1,19 +1,65 @@
|
||||
<?php namespace Flarum\Api\Serializers;
|
||||
|
||||
use Flarum\Core\Models\Activity;
|
||||
use Event;
|
||||
|
||||
class ActivitySerializer extends BaseSerializer {
|
||||
|
||||
public function serialize(Activity $activity)
|
||||
{
|
||||
$serialized = [
|
||||
'id' => (int) $activity->id
|
||||
];
|
||||
class ActivitySerializer extends BaseSerializer
|
||||
{
|
||||
/**
|
||||
* The resource type.
|
||||
* @var string
|
||||
*/
|
||||
protected $type = 'activity';
|
||||
|
||||
Event::fire('flarum.api.serialize.activity', [&$serialized]);
|
||||
/**
|
||||
* Serialize attributes of an Activity model for JSON output.
|
||||
*
|
||||
* @param Activity $activity The Activity model to serialize.
|
||||
* @return array
|
||||
*/
|
||||
protected function attributes(Activity $activity)
|
||||
{
|
||||
$attributes = [
|
||||
'id' => ((int) $activity->id) ?: str_random(5),
|
||||
'type' => $activity->type,
|
||||
'content' => json_encode($activity->data),
|
||||
'time' => $activity->time->toRFC3339String()
|
||||
];
|
||||
|
||||
return $serialized;
|
||||
}
|
||||
return $this->attributesEvent($activity, $attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a resource containing an activity's sender.
|
||||
*
|
||||
* @param Activity $activity
|
||||
* @return Tobscure\JsonApi\Resource
|
||||
*/
|
||||
public function linkUser(Activity $activity)
|
||||
{
|
||||
return (new UserBasicSerializer)->resource($activity->user_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a resource containing an activity's sender.
|
||||
*
|
||||
* @param Activity $activity
|
||||
* @param array $relations
|
||||
* @return Tobscure\JsonApi\Resource
|
||||
*/
|
||||
public function includeSender(Activity $activity, $relations)
|
||||
{
|
||||
return (new UserBasicSerializer($relations))->resource($activity->sender);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a resource containing an activity's sender.
|
||||
*
|
||||
* @param Activity $activity
|
||||
* @param array $relations
|
||||
* @return Tobscure\JsonApi\Resource
|
||||
*/
|
||||
public function includePost(Activity $activity, $relations)
|
||||
{
|
||||
return (new PostSerializer($relations))->resource($activity->post);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user