-
Notifications
You must be signed in to change notification settings - Fork 45
Feature: User Activity for #15 #19
base: master
Are you sure you want to change the base?
Changes from 2 commits
150fc23
532f9e7
5136b6b
fa8ee4b
ab88192
e61b607
cdfc38d
cc00a85
30386b6
1112a42
21d40b7
01ca97e
673b3ed
6d37209
31d4fba
eabfe83
6fac0c9
9412ccf
fafb4e9
33e6fdf
cce3a86
a9ef59f
e88043b
bcae5c7
a10328f
3a2cdd2
f0f1700
25ac9a1
be0c29b
a72f406
6f5a673
258c9c5
c9ca049
2f9dea2
a94eb61
3b8dc21
1a5b91f
1b9ebfa
2440360
f6c2c78
baa739e
3df880c
9b544d3
a8f35f3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
/** | ||
* User activity model. | ||
* | ||
* @author MyBB Group | ||
* @version 2.0.0 | ||
* @package mybb/settings | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Damn copy and paste. It's probably in a few files ;)
|
||
* @copyright Copyright (c) 2014, MyBB Group | ||
* @license http://www.mybb.com/about/license GNU LESSER GENERAL PUBLIC LICENSE | ||
* @link http://www.mybb.com | ||
*/ | ||
|
||
namespace MyBB\Core\UserActivity\Database\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use McCool\LaravelAutoPresenter\HasPresenter; | ||
|
||
class UserActivity extends Model implements HasPresenter | ||
{ | ||
/** | ||
* The table associated with the model. | ||
* | ||
* @var string | ||
*/ | ||
protected $table = 'user_activity'; | ||
|
||
/** | ||
* The attributes that should be casted to native types. | ||
* | ||
* @var array | ||
*/ | ||
protected $casts = [ | ||
'extra_details' => 'array', | ||
]; | ||
|
||
/** | ||
* The attributes that aren't mass assignable. | ||
* | ||
* @var array | ||
*/ | ||
protected $guarded = []; | ||
|
||
/** | ||
* The relations to eager load on every query. | ||
* | ||
* @var array | ||
*/ | ||
protected $with = ['user', 'activityHistorable']; | ||
|
||
public function user() | ||
{ | ||
return $this->belongsTo('MyBB\Core\Database\Models\User'); | ||
} | ||
|
||
public function activityHistorable() | ||
{ | ||
return $this->morphTo(null, 'activity_type', 'activity_id'); | ||
} | ||
|
||
/** | ||
* Get the presenter class. | ||
* | ||
* @return string | ||
*/ | ||
public function getPresenterClass() | ||
{ | ||
return 'MyBB\Core\UserActivity\Presenters\UserActivityPresenter'; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
<?php | ||
/** | ||
* User Activity repository using the Eloquent ORM. | ||
* | ||
* @author MyBB Group | ||
* @version 2.0.0 | ||
* @package mybb/settings | ||
* @copyright Copyright (c) 2014, MyBB Group | ||
* @license http://www.mybb.com/about/license GNU LESSER GENERAL PUBLIC LICENSE | ||
* @link http://www.mybb.com | ||
*/ | ||
|
||
namespace MyBB\Core\UserActivity\Database\Repositories\Eloquent; | ||
|
||
use MyBB\Core\Database\Models\User; | ||
use MyBB\Core\UserActivity\Database\Models\UserActivity; | ||
use UserActivity\Database\Repositories\UserActivityRepositoryInterface; | ||
|
||
class UserActivityRepository implements UserActivityRepositoryInterface | ||
{ | ||
/** | ||
* @var UserActivity $userActivityModel | ||
*/ | ||
private $userActivityModel; | ||
|
||
/** | ||
* @param UserActivity $userActivityModel | ||
*/ | ||
public function __construct(UserActivity $userActivityModel) | ||
{ | ||
$this->userActivityModel = $userActivityModel; | ||
} | ||
|
||
/** | ||
* Get all user activity entries. | ||
* | ||
* @return mixed | ||
*/ | ||
public function all() | ||
{ | ||
return $this->userActivityModel->all(); | ||
} | ||
|
||
/** | ||
* Get all user activity entries for a specific user. | ||
* | ||
* @param int|User $user The user to retrieve activity entries for. | ||
* | ||
* @return mixed | ||
*/ | ||
public function allForUser($user = -1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Never going to be assigned by Database as a valid auto increment value. 0 could be. I'll be putting a check in for less than 0 to avoid DB queries for invalid user IDs.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, the guest user is assigned -1 as the user ID by default: /**
* Initialise the default (guest) user, using the custom Guard implementation.
*/
private function initDefaultUser()
{
/** @var \MyBB\Auth\Contracts\Guard $guard */
$guard = $this->app->make('Illuminate\Auth\Guard');
$defaultUser = new User();
$defaultUser->name = 'Guest';
$defaultUser->id = -1;
$guard->registerDefaultUser($defaultUser);
} |
||
{ | ||
$user = $this->getUserIdFromUser($user); | ||
|
||
return $this->userActivityModel->where('user_id', '=', $user)->get(); | ||
} | ||
|
||
/** | ||
* Delete all activity entries for a user where the creation date is older than a given time-span. | ||
* | ||
* @param int|User $user The user to delete activity entries for. | ||
* @param \DateInterval $timeSpan The maximum age of user activity entries to keep. | ||
* | ||
* @return int The number of deleted user activity entries. | ||
*/ | ||
public function deleteForUserOlderThan($user = -1, \DateInterval $timeSpan) | ||
{ | ||
$before = $this->getDateTimeFromInterval($timeSpan); | ||
|
||
$user = $this->getUserIdFromUser($user); | ||
|
||
if ($before !== false) { | ||
return $this->userActivityModel->where('created_at', '<', $before)->where('user_id', '=', $user)->delete(); | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
/** | ||
* Delete all activity entries for a user. | ||
* | ||
* @param int|User $user The user to delete activity entries for. | ||
* | ||
* @return int The number of deleted user activity entries. | ||
*/ | ||
public function deleteAllForUser($user = -1) | ||
{ | ||
$user = $this->getUserIdFromUser($user); | ||
|
||
return $this->userActivityModel->where('user_id', '=', $user)->delete(); | ||
} | ||
|
||
/** | ||
* Get the ID of a user from either an integer or User model. | ||
* | ||
* @param int|User $user The user to retrieve the User ID for. | ||
* | ||
* @return int | ||
*/ | ||
private function getUserIdFromUser($user) | ||
{ | ||
if (is_object($user) && $user instanceof User) { | ||
$user = $user->getAuthIdentifier(); | ||
} | ||
|
||
return (int) $user; | ||
} | ||
|
||
/** | ||
* Get a date representation of a date time interval. | ||
* | ||
* @param \DateInterval $interval The interval to subtract or add. | ||
* | ||
* @return \DateTime|false The calculated Date or false upon failure. | ||
*/ | ||
private function getDateTimeFromInterval(\DateInterval $interval) | ||
{ | ||
$now = new \DateTime(); | ||
|
||
return $now->sub($interval); | ||
} | ||
|
||
/** | ||
* Get a paginated list of all user activity entries. | ||
* | ||
* @param int $perPage The number of activity entries per page. | ||
* | ||
* @return mixed | ||
*/ | ||
public function paginateAll($perPage = 20) | ||
{ | ||
return $this->userActivityModel->paginate($perPage); | ||
} | ||
|
||
/** | ||
* Get a paginated list of activity entries for a specific user. | ||
* | ||
* @param int|User $user The user to retrieve activity entries for. | ||
* @param int $perPage The number of activity entries per page. | ||
* | ||
* @return mixed | ||
*/ | ||
public function paginateForUser($user = -1, $perPage = 20) | ||
{ | ||
$user = $this->getUserIdFromUser($user); | ||
|
||
return $this->userActivityModel->where('user_id', '=', $user)->paginate($perPage); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
/** | ||
* User Activity repository contract. | ||
* | ||
* Used to retrieve user activity entries from a data store. | ||
* | ||
* @author MyBB Group | ||
* @version 2.0.0 | ||
* @package mybb/settings | ||
* @copyright Copyright (c) 2014, MyBB Group | ||
* @license http://www.mybb.com/about/license GNU LESSER GENERAL PUBLIC LICENSE | ||
* @link http://www.mybb.com | ||
*/ | ||
|
||
namespace MyBB\Core\UserActivity\Database\Repositories; | ||
|
||
use MyBB\Core\Database\Models\User; | ||
|
||
interface UserActivityRepositoryInterface | ||
{ | ||
/** | ||
* Get all user activity entries. | ||
* | ||
* @return mixed | ||
*/ | ||
public function all(); | ||
|
||
/** | ||
* Get a paginated list of all user activity entries. | ||
* | ||
* @param int $perPage The number of activity entries per page. | ||
* | ||
* @return mixed | ||
*/ | ||
public function paginateAll($perPage = 20); | ||
|
||
/** | ||
* Get all user activity entries for a specific user. | ||
* | ||
* @param int|User $user The user to retrieve activity entries for. | ||
* | ||
* @return mixed | ||
*/ | ||
public function allForUser($user = -1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I quite like prefixing methods that retrieve stuff with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Either is fine for me. To me, a repository mostly does data retrieval, so the retrieval is implied.
|
||
|
||
/** | ||
* Get a paginated list of activity entries for a specific user. | ||
* | ||
* @param int|User $user The user to retrieve activity entries for. | ||
* @param int $perPage The number of activity entries per page. | ||
* | ||
* @return mixed | ||
*/ | ||
public function paginateForUser($user = -1, $perPage = 20); | ||
|
||
/** | ||
* Delete all activity entries for a user where the creation date is older than a given time-span. | ||
* | ||
* @param int|User $user The user to delete activity entries for. | ||
* @param \DateInterval $timeSpan The maximum age of user activity entries to keep. | ||
* | ||
* @return int The number of deleted user activity entries. | ||
*/ | ||
public function deleteForUserOlderThan($user = -1, \DateInterval $timeSpan); | ||
|
||
/** | ||
* Delete all activity entries for a user. | ||
* | ||
* @param int|User $user The user to delete activity entries for. | ||
* | ||
* @return int The number of deleted user activity entries. | ||
*/ | ||
public function deleteAllForUser($user = -1); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
/** | ||
* User activity controller. | ||
* | ||
* Shows recent user activity. | ||
* | ||
* @author MyBB Group | ||
* @version 2.0.0 | ||
* @package mybb/settings | ||
* @copyright Copyright (c) 2014, MyBB Group | ||
* @license http://www.mybb.com/about/license GNU LESSER GENERAL PUBLIC LICENSE | ||
* @link http://www.mybb.com | ||
*/ | ||
|
||
namespace MyBB\Core\UserActivity\Http\Controllers; | ||
|
||
use Illuminate\Http\Request; | ||
use MyBB\Auth\Contracts\Guard; | ||
use MyBB\Core\Http\Controllers\Controller; | ||
use MyBB\Settings\Store; | ||
use MyBB\Core\UserActivity\Database\Repositories\UserActivityRepositoryInterface; | ||
|
||
class UserActivityController extends Controller | ||
{ | ||
/** | ||
* @var UserActivityRepositoryInterface $userActivityRepository | ||
*/ | ||
private $userActivityRepository; | ||
/** | ||
* @var Store $settings | ||
*/ | ||
private $settings; | ||
|
||
/** | ||
* @param Guard $guard | ||
* @param Request $request | ||
* @param UserActivityRepositoryInterface $userActivityRepository | ||
* @param Store $settings | ||
*/ | ||
public function __construct(Guard $guard, Request $request, UserActivityRepositoryInterface $userActivityRepository, Store $settings) | ||
{ | ||
parent::__construct($guard, $request); | ||
|
||
$this->userActivityRepository = $userActivityRepository; | ||
$this->settings = $settings; | ||
} | ||
|
||
/** | ||
* Get the index list showing all user activity. | ||
* | ||
* @return \Illuminate\View\View | ||
*/ | ||
public function getIndex() | ||
{ | ||
$perPage = $this->settings->get('user_activity.per_page', 20); | ||
|
||
$activity = $this->userActivityRepository->paginateAll($perPage); | ||
|
||
return view('user_activity.index', compact('activity')); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Controllers
is mispelled here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤦