1
0
mirror of https://github.com/flarum/core.git synced 2025-10-27 05:31:29 +01:00

Add user activity system

This commit is contained in:
Toby Zerner
2015-03-17 17:06:12 +10:30
parent 5055377eb1
commit 3880ce70f0
27 changed files with 508 additions and 72 deletions

View 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);
}

View 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();
}
}

View File

@@ -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);

View File

@@ -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