mirror of
https://github.com/flarum/core.git
synced 2025-08-04 23:47:32 +02:00
Add user activity system
This commit is contained in:
47
src/Api/Actions/Activity/IndexAction.php
Normal file
47
src/Api/Actions/Activity/IndexAction.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php namespace Flarum\Api\Actions\Activity;
|
||||
|
||||
use Flarum\Core\Repositories\UserRepositoryInterface;
|
||||
use Flarum\Core\Repositories\ActivityRepositoryInterface;
|
||||
use Flarum\Core\Support\Actor;
|
||||
use Flarum\Api\Actions\BaseAction;
|
||||
use Flarum\Api\Actions\ApiParams;
|
||||
use Flarum\Api\Serializers\ActivitySerializer;
|
||||
|
||||
class IndexAction extends BaseAction
|
||||
{
|
||||
/**
|
||||
* Instantiate the action.
|
||||
*
|
||||
* @param \Flarum\Core\Search\Discussions\UserSearcher $searcher
|
||||
*/
|
||||
public function __construct(Actor $actor, UserRepositoryInterface $users, ActivityRepositoryInterface $activity)
|
||||
{
|
||||
$this->actor = $actor;
|
||||
$this->users = $users;
|
||||
$this->activity = $activity;
|
||||
}
|
||||
|
||||
/**
|
||||
* Show a user's activity feed.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
protected function run(ApiParams $params)
|
||||
{
|
||||
$start = $params->start();
|
||||
$count = $params->count(20, 50);
|
||||
$type = $params->get('type');
|
||||
$id = $params->get('users');
|
||||
|
||||
$user = $this->users->findOrFail($id, $this->actor->getUser());
|
||||
|
||||
$activity = $this->activity->findByUser($user->id, $this->actor->getUser(), $count, $start, $type);
|
||||
|
||||
// Finally, we can set up the activity serializer and use it to create
|
||||
// a collection of activity results.
|
||||
$serializer = new ActivitySerializer(['sender', 'post', 'post.discussion', 'post.user', 'post.discussion.startUser', 'post.discussion.lastUser'], ['user']);
|
||||
$document = $this->document()->setPrimaryElement($serializer->collection($activity));
|
||||
|
||||
return $this->respondWithDocument($document);
|
||||
}
|
||||
}
|
@@ -5,12 +5,12 @@ use Flarum\Core\Repositories\DiscussionRepositoryInterface as DiscussionReposito
|
||||
use Flarum\Core\Repositories\PostRepositoryInterface as PostRepository;
|
||||
use Flarum\Api\Actions\BaseAction;
|
||||
use Flarum\Api\Actions\ApiParams;
|
||||
use Flarum\Api\Actions\Posts\GetsPostsForDiscussion;
|
||||
use Flarum\Api\Actions\Posts\GetsPosts;
|
||||
use Flarum\Api\Serializers\DiscussionSerializer;
|
||||
|
||||
class ShowAction extends BaseAction
|
||||
{
|
||||
use GetsPostsForDiscussion;
|
||||
use GetsPosts;
|
||||
|
||||
/**
|
||||
* The discussion repository.
|
||||
@@ -51,7 +51,7 @@ class ShowAction extends BaseAction
|
||||
|
||||
if (in_array('posts', $include)) {
|
||||
$relations = ['user', 'user.groups', 'editUser', 'hideUser'];
|
||||
$discussion->posts = $this->getPostsForDiscussion($params, $discussion->id)->load($relations);
|
||||
$discussion->posts = $this->getPosts($params, ['discussion_id' => $discussion->id])->load($relations);
|
||||
|
||||
$include = array_merge($include, array_map(function ($relation) {
|
||||
return 'posts.'.$relation;
|
||||
|
@@ -3,23 +3,23 @@
|
||||
use Flarum\Core\Models\User;
|
||||
use Flarum\Api\Actions\ApiParams;
|
||||
|
||||
trait GetsPostsForDiscussion
|
||||
trait GetsPosts
|
||||
{
|
||||
protected function getPostsForDiscussion(ApiParams $params, $discussionId)
|
||||
protected function getPosts(ApiParams $params, $where)
|
||||
{
|
||||
$sort = $params->sort(['time']);
|
||||
$count = $params->count(20, 50);
|
||||
$user = $this->actor->getUser();
|
||||
|
||||
if (($near = $params->get('near')) > 1) {
|
||||
$start = $this->posts->getIndexForNumber($discussionId, $near, $user);
|
||||
if (isset($where['discussion_id']) && ($near = $params->get('near')) > 1) {
|
||||
$start = $this->posts->getIndexForNumber($where['discussion_id'], $near, $user);
|
||||
$start = max(0, $start - $count / 2);
|
||||
} else {
|
||||
$start = 0;
|
||||
}
|
||||
|
||||
return $this->posts->findByDiscussion(
|
||||
$discussionId,
|
||||
return $this->posts->findWhere(
|
||||
$where,
|
||||
$user,
|
||||
$sort['field'],
|
||||
$sort['order'] ?: 'asc',
|
@@ -9,7 +9,7 @@ use Flarum\Api\Serializers\PostSerializer;
|
||||
|
||||
class IndexAction extends BaseAction
|
||||
{
|
||||
use GetsPostsForDiscussion;
|
||||
use GetsPosts;
|
||||
|
||||
/**
|
||||
* The post repository.
|
||||
@@ -37,14 +37,19 @@ class IndexAction extends BaseAction
|
||||
protected function run(ApiParams $params)
|
||||
{
|
||||
$postIds = (array) $params->get('ids');
|
||||
$include = ['user', 'user.groups', 'editUser', 'hideUser'];
|
||||
$include = ['user', 'user.groups', 'editUser', 'hideUser', 'discussion'];
|
||||
$user = $this->actor->getUser();
|
||||
|
||||
if (count($postIds)) {
|
||||
$posts = $this->posts->findByIds($postIds, $user);
|
||||
} else {
|
||||
$discussionId = $params->get('discussions');
|
||||
$posts = $this->getPostsForDiscussion($params, $discussionId, $user);
|
||||
if ($discussionId = $params->get('discussions')) {
|
||||
$where['discussion_id'] = $discussionId;
|
||||
}
|
||||
if ($userId = $params->get('users')) {
|
||||
$where['user_id'] = $userId;
|
||||
}
|
||||
$posts = $this->getPosts($params, $where, $user);
|
||||
}
|
||||
|
||||
if (! count($posts)) {
|
||||
|
@@ -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);
|
||||
}
|
||||
}
|
||||
|
@@ -79,7 +79,7 @@ class PostSerializer extends PostBasicSerializer
|
||||
*/
|
||||
public function includeDiscussion(Post $post, $relations = [])
|
||||
{
|
||||
return (new DiscussionBasicSerializer($relations))->resource($post->discussion);
|
||||
return (new DiscussionSerializer($relations))->resource($post->discussion);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -73,6 +73,14 @@ class CoreServiceProvider extends ServiceProvider
|
||||
'Flarum\Core\Repositories\UserRepositoryInterface',
|
||||
'Flarum\Core\Repositories\EloquentUserRepository'
|
||||
);
|
||||
$this->app->bind(
|
||||
'Flarum\Core\Repositories\ActivityRepositoryInterface',
|
||||
'Flarum\Core\Repositories\EloquentActivityRepository'
|
||||
);
|
||||
$this->app->bind(
|
||||
'Flarum\Core\Repositories\NotificationRepositoryInterface',
|
||||
'Flarum\Core\Repositories\EloquentNotificationRepository'
|
||||
);
|
||||
}
|
||||
|
||||
public function registerGambits()
|
||||
|
@@ -1,36 +1,69 @@
|
||||
<?php namespace Flarum\Core\Activity;
|
||||
<?php namespace Flarum\Core\Models;
|
||||
|
||||
use Flarum\Core\Entity;
|
||||
use Illuminate\Support\Str;
|
||||
use Auth;
|
||||
class Activity extends Model
|
||||
{
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'activity';
|
||||
|
||||
class Activity extends Entity {
|
||||
/**
|
||||
* The attributes that should be mutated to dates.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $dates = ['time'];
|
||||
|
||||
protected $table = 'activity';
|
||||
/**
|
||||
* Unserialize the data attribute.
|
||||
*
|
||||
* @param string $value
|
||||
* @return string
|
||||
*/
|
||||
public function getDataAttribute($value)
|
||||
{
|
||||
return json_decode($value);
|
||||
}
|
||||
|
||||
public function getDates()
|
||||
{
|
||||
return ['time'];
|
||||
}
|
||||
/**
|
||||
* Serialize the data attribute.
|
||||
*
|
||||
* @param string $value
|
||||
*/
|
||||
public function setDataAttribute($value)
|
||||
{
|
||||
$this->attributes['data'] = json_encode($value);
|
||||
}
|
||||
|
||||
public function fromUser()
|
||||
{
|
||||
return $this->belongsTo('Flarum\Core\Models\User', 'from_user_id');
|
||||
}
|
||||
/**
|
||||
* Define the relationship with the activity's recipient.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
*/
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo('Flarum\Core\Models\User', 'user_id');
|
||||
}
|
||||
|
||||
public function permission($permission)
|
||||
{
|
||||
return User::current()->can($permission, 'activity', $this);
|
||||
}
|
||||
|
||||
public function editable()
|
||||
{
|
||||
return $this->permission('edit');
|
||||
}
|
||||
|
||||
public function deletable()
|
||||
{
|
||||
return $this->permission('delete');
|
||||
}
|
||||
/**
|
||||
* Define the relationship with the activity's sender.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
*/
|
||||
public function sender()
|
||||
{
|
||||
return $this->belongsTo('Flarum\Core\Models\User', 'sender_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the relationship with the activity's sender.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
*/
|
||||
public function post()
|
||||
{
|
||||
return $this->belongsTo('Flarum\Core\Models\Post', 'post_id');
|
||||
}
|
||||
}
|
||||
|
8
src/Core/Repositories/ActivityRepositoryInterface.php
Normal file
8
src/Core/Repositories/ActivityRepositoryInterface.php
Normal file
@@ -0,0 +1,8 @@
|
||||
<?php namespace Flarum\Core\Repositories;
|
||||
|
||||
use Flarum\Core\Models\User;
|
||||
|
||||
interface ActivityRepositoryInterface
|
||||
{
|
||||
public function findByUser($userId, User $user, $count = null, $start = 0, $type = null);
|
||||
}
|
39
src/Core/Repositories/EloquentActivityRepository.php
Normal file
39
src/Core/Repositories/EloquentActivityRepository.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?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)
|
||||
{
|
||||
// This is all very rough and needs to be cleaned up
|
||||
|
||||
$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) {
|
||||
$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);
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
@@ -24,10 +24,10 @@ class EloquentPostRepository implements PostRepositoryInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* Find posts in a discussion, optionally making sure they are visible to
|
||||
* a certain user, and/or using other criteria.
|
||||
* Find posts that match certain conditions, optionally making sure they
|
||||
* are visible to a certain user, and/or using other criteria.
|
||||
*
|
||||
* @param integer $discussionId
|
||||
* @param array $where
|
||||
* @param \Flarum\Core\Models\User|null $user
|
||||
* @param string $sort
|
||||
* @param string $order
|
||||
@@ -35,9 +35,9 @@ class EloquentPostRepository implements PostRepositoryInterface
|
||||
* @param integer $start
|
||||
* @return \Illuminate\Database\Eloquent\Collection
|
||||
*/
|
||||
public function findByDiscussion($discussionId, User $user = null, $sort = 'time', $order = 'asc', $count = null, $start = 0)
|
||||
public function findWhere($where = [], User $user = null, $sort = 'time', $order = 'asc', $count = null, $start = 0)
|
||||
{
|
||||
$query = Post::where('discussion_id', $discussionId)
|
||||
$query = Post::where($where)
|
||||
->orderBy($sort, $order)
|
||||
->skip($start)
|
||||
->take($count);
|
||||
|
@@ -17,10 +17,10 @@ interface PostRepositoryInterface
|
||||
public function findOrFail($id, User $user = null);
|
||||
|
||||
/**
|
||||
* Find posts in a discussion, optionally making sure they are visible to
|
||||
* a certain user, and/or using other criteria.
|
||||
* Find posts that match certain conditions, optionally making sure they
|
||||
* are visible to a certain user, and/or using other criteria.
|
||||
*
|
||||
* @param integer $discussionId
|
||||
* @param array $where
|
||||
* @param \Flarum\Core\Models\User|null $user
|
||||
* @param string $sort
|
||||
* @param string $order
|
||||
@@ -28,7 +28,7 @@ interface PostRepositoryInterface
|
||||
* @param integer $start
|
||||
* @return \Illuminate\Database\Eloquent\Collection
|
||||
*/
|
||||
public function findByDiscussion($discussionId, User $user = null, $sort = 'time', $order = 'asc', $count = null, $start = 0);
|
||||
public function findWhere($where = [], User $user = null, $sort = 'time', $order = 'asc', $count = null, $start = 0);
|
||||
|
||||
/**
|
||||
* Find posts by their IDs, optionally making sure they are visible to a
|
||||
|
Reference in New Issue
Block a user