Skip to content

Commit

Permalink
Merge pull request #3 from patrickomeara/master
Browse files Browse the repository at this point in the history
Add grace time for updating
  • Loading branch information
DivineOmega authored Jan 27, 2021
2 parents 61ee343 + fddd559 commit 5722d5c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
11 changes: 8 additions & 3 deletions src/Http/Middleware/LastActivity.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,14 @@ public function handle($request, Closure $next)
*
* @param Model $user
*/
private function updateLastActivityField(Model $user)
protected function updateLastActivityField(Model $user)
{
$lastActivityField = config('last-activity.field');
$graceTime = config('last-activity.grace_time');

if ($graceTime > 0 && $user->$lastActivityField && ($user->$lastActivityField)->addSeconds($graceTime) > now()) {
return;
}

$this->hideFromEvents($user, function() use ($user, $lastActivityField) {
$user->$lastActivityField = now();
Expand All @@ -53,7 +58,7 @@ private function updateLastActivityField(Model $user)
* @param callable $callback
* @return mixed
*/
private function hideFromEvents(Model $model, callable $callback)
protected function hideFromEvents(Model $model, callable $callback)
{
$dispatcher = $model::getEventDispatcher();
$model::unsetEventDispatcher();
Expand All @@ -64,4 +69,4 @@ private function hideFromEvents(Model $model, callable $callback)

return $result;
}
}
}
8 changes: 7 additions & 1 deletion src/config/last-activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,10 @@
// Field in which the last activity date time will be stored.
'field' => 'last_activity',

];
/**
* The amount of seconds the field is considered stale and should update.
* This prevents a write on every request, particularly useful with read replica databases.
* Set to 0 to disable.
*/
'grace_time' => 0,
];

0 comments on commit 5722d5c

Please sign in to comment.