mirror of
https://github.com/flarum/core.git
synced 2025-07-25 18:51:40 +02:00
Add user activity system
This commit is contained in:
@@ -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