Skip to content
This repository has been archived by the owner on Apr 3, 2020. It is now read-only.

Feature: User Activity for #15 #19

Open
wants to merge 44 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
150fc23
Starting work on User Activity for #15.
euantorano Mar 20, 2015
532f9e7
Working on user activity. Added user activity controller.
euantorano Mar 20, 2015
5136b6b
Fixed package naming in docblocks. Also added repository binding to s…
euantorano Mar 21, 2015
fa8ee4b
Starting work on User Activity for #15.
euantorano Mar 20, 2015
ab88192
Working on user activity. Added user activity controller.
euantorano Mar 20, 2015
e61b607
Fixed package naming in docblocks. Also added repository binding to s…
euantorano Mar 21, 2015
cdfc38d
Merge branch 'feature-user-activity' of github.com:euantorano/mybb2 i…
euantorano Mar 21, 2015
cc00a85
Update Post.php
euantorano Mar 21, 2015
30386b6
Merge branch 'master' of github.com:mybb/mybb2 into feature-user-acti…
euantorano Mar 23, 2015
1112a42
Working on formatting user activity entries.
euantorano Mar 23, 2015
21d40b7
Merge branch 'master' of github.com:mybb/mybb2 into feature-user-acti…
euantorano Mar 23, 2015
01ca97e
Slight code reformatting.
euantorano Mar 23, 2015
673b3ed
Merge branch 'master' of github.com:mybb/mybb2 into feature-user-acti…
euantorano Mar 29, 2015
6d37209
Merge branch 'master' of github.com:mybb/mybb2 into feature-user-acti…
euantorano Apr 7, 2015
31d4fba
Merge branch 'master' of github.com:mybb/mybb2 into feature-user-acti…
euantorano Apr 9, 2015
eabfe83
Tidying up model observing.
euantorano Apr 9, 2015
6fac0c9
Merge branch 'master' of github.com:mybb/mybb2 into feature-user-acti…
euantorano Apr 9, 2015
9412ccf
Fixed syntax error.
euantorano Apr 9, 2015
fafb4e9
Cleaned up presenter for activity.
euantorano Apr 9, 2015
33e6fdf
Cleaning up and refactoring code.
euantorano Apr 9, 2015
cce3a86
Added settings.
euantorano Apr 9, 2015
a9ef59f
Merge branch 'master' of github.com:mybb/mybb2 into feature-user-acti…
euantorano Apr 19, 2015
e88043b
Merge branch 'master' of github.com:mybb/mybb2 into feature-user-acti…
euantorano Apr 19, 2015
bcae5c7
Added activity for liking content.
euantorano Apr 19, 2015
a10328f
Added basic user activity list to profile.
euantorano Apr 19, 2015
3a2cdd2
Refactoring code.
euantorano Apr 19, 2015
f0f1700
Fixing indents in templates.
euantorano Apr 19, 2015
25ac9a1
Fixing profile template.
euantorano Apr 19, 2015
be0c29b
Reformatting seeds.
euantorano Apr 19, 2015
a72f406
Removed unused `$guard` and `$request`.
euantorano Apr 19, 2015
6f5a673
Removed old imports.
euantorano Apr 19, 2015
258c9c5
Merge branch 'master' of github.com:mybb/mybb2 into feature-user-acti…
euantorano Apr 19, 2015
c9ca049
Auto-loading the likeable relation for the Likes model, saving 3 quer…
euantorano Apr 19, 2015
2f9dea2
Merge branch 'master' of github.com:mybb/mybb2 into feature-user-acti…
euantorano Apr 24, 2015
a94eb61
Add `is_topic_starter` to `posts` table.
euantorano Apr 24, 2015
3b8dc21
Added activity for new topics.
euantorano Apr 24, 2015
1a5b91f
Merge branch 'master' of github.com:mybb/mybb2 into feature-user-acti…
euantorano May 19, 2015
1b9ebfa
Fixed copyright header years.
euantorano May 19, 2015
2440360
Converting indents to tabs.
euantorano May 19, 2015
f6c2c78
Updating license headers, added registration activity.
euantorano May 19, 2015
baa739e
Added twig test to check if a variable is a user.
euantorano May 19, 2015
3df880c
Added page to show all activity for a user.
euantorano May 19, 2015
9b544d3
Starting reformatting code.
euantorano May 19, 2015
a8f35f3
Fixed code style issues.
euantorano May 19, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/Database/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class Post extends Model implements HasPresenter
*/
public function getPresenterClass()
{
return \MyBB\Core\Presenters\Post::class; // TODO: Are we using PHP 5.5 as minimum? If so, this is fine...
return '\MyBB\Core\Presenters\Post';
}

/**
Expand Down
2 changes: 2 additions & 0 deletions app/Http/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,5 @@
Route::post('/privacy', ['as' => 'account.privacy', 'uses' => 'AccountController@postPrivacy']);
Route::get('/drafts', ['as' => 'account.drafts', 'uses' => 'AccountController@getDrafts']);
});

Route::get('/activity', ['as' => 'user_activity.index', 'uses' => 'MyBB\Core\UserActivity\Http\Controlelrs\UserActivityController@getIndex']);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Controllers is mispelled here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦

On 21 Mar 2015, at 11:25, Will Pillar notifications@github.com wrote:

In app/Http/routes.php:

@@ -98,3 +98,5 @@
Route::post('/privacy', ['as' => 'account.privacy', 'uses' => 'AccountController@postPrivacy']);
Route::get('/drafts', ['as' => 'account.drafts', 'uses' => 'AccountController@getDrafts']);
});
+
+Route::get('/activity', ['as' => 'user_activity.index', 'uses' => 'MyBB\Core\UserActivity\Http\Controlelrs\UserActivityController@getIndex']);
Controllers is mispelled here.


Reply to this email directly or view it on GitHub.

69 changes: 69 additions & 0 deletions app/UserActivity/Database/Models/UserActivity.php
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Damn copy and paste. It's probably in a few files ;)

On 21 Mar 2015, at 11:25, Will Pillar notifications@github.com wrote:

In app/UserActivity/Database/Models/UserActivity.php:

@@ -0,0 +1,69 @@
+<?php
+/**


Reply to this email directly or view it on GitHub.

* @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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why -1 ?

Copy link
Member Author

Choose a reason for hiding this comment

The 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.

On 21 Mar 2015, at 11:25, Will Pillar notifications@github.com wrote:

In app/UserActivity/Database/Repositories/Eloquent/UserActivityRepository.php:

  • * @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)
    why -1 ?


Reply to this email directly or view it on GitHub.

Copy link
Member Author

Choose a reason for hiding this comment

The 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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I quite like prefixing methods that retrieve stuff with get. getAllForUser(), getAll().

Copy link
Member Author

Choose a reason for hiding this comment

The 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.

On 21 Mar 2015, at 11:27, Will Pillar notifications@github.com wrote:

In app/UserActivity/Database/Repositories/UserActivityRepositoryInterface.php:

  • * 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);
    I quite like prefixing methods that retrieve stuff with get. getAllForUser(), getAll().


Reply to this email directly or view it on GitHub.


/**
* 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);
}
61 changes: 61 additions & 0 deletions app/UserActivity/Http/Controllers/UserActivityController.php
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'));
}
}