BookStack/app/Activity/Models/View.php

58 lines
1.5 KiB
PHP
Raw Normal View History

2021-06-26 11:23:15 -04:00
<?php
2023-05-17 12:56:55 -04:00
namespace BookStack\Activity\Models;
2023-05-17 12:56:55 -04:00
use BookStack\App\Model;
use BookStack\Permissions\Models\JointPermission;
use Illuminate\Database\Eloquent\Relations\HasMany;
2021-05-15 19:29:56 -04:00
use Illuminate\Database\Eloquent\Relations\MorphTo;
/**
* Class View
* Views are stored per-item per-person within the database.
* They can be used to find popular items or recently viewed items
* at a per-person level. They do not record every view instance as an
* activity. Only the latest and original view times could be recognised.
*
* @property int $views
* @property int $user_id
*/
class View extends Model
{
protected $fillable = ['user_id', 'views'];
/**
* Get all owning viewable models.
*/
2021-05-15 19:29:56 -04:00
public function viewable(): MorphTo
{
return $this->morphTo();
}
public function jointPermissions(): HasMany
{
return $this->hasMany(JointPermission::class, 'entity_id', 'viewable_id')
->whereColumn('views.viewable_type', '=', 'joint_permissions.entity_type');
}
/**
* Increment the current user's view count for the given viewable model.
*/
public static function incrementFor(Viewable $viewable): int
{
$user = user();
2023-09-16 08:49:03 -04:00
if ($user->isGuest()) {
return 0;
}
/** @var View $view */
$view = $viewable->views()->firstOrNew([
'user_id' => $user->id,
], ['views' => 0]);
$view->forceFill(['views' => $view->views + 1])->save();
return $view->views;
}
}