3 namespace BookStack\Activity\Models;
5 use BookStack\App\Model;
6 use BookStack\Entities\Models\Entity;
7 use BookStack\Permissions\Models\JointPermission;
8 use BookStack\Users\Models\User;
9 use Illuminate\Database\Eloquent\Factories\HasFactory;
10 use Illuminate\Database\Eloquent\Relations\BelongsTo;
11 use Illuminate\Database\Eloquent\Relations\HasMany;
12 use Illuminate\Database\Eloquent\Relations\MorphTo;
13 use Illuminate\Support\Carbon;
14 use Illuminate\Support\Str;
17 * @property string $type
18 * @property User $user
19 * @property Entity $loggable
20 * @property string $detail
21 * @property string $loggable_type
22 * @property int $loggable_id
23 * @property int $user_id
24 * @property Carbon $created_at
26 class Activity extends Model
31 * Get the loggable model related to this activity.
32 * Currently only used for entities (previously entity_[id/type] columns).
33 * Could be used for others but will need an audit of uses where assumed
36 public function loggable(): MorphTo
38 return $this->morphTo('loggable');
42 * Get the user this activity relates to.
44 public function user(): BelongsTo
46 return $this->belongsTo(User::class);
49 public function jointPermissions(): HasMany
51 return $this->hasMany(JointPermission::class, 'entity_id', 'loggable_id')
52 ->whereColumn('activities.loggable_type', '=', 'joint_permissions.entity_type');
56 * Returns text from the language files, Looks up by using the activity key.
58 public function getText(): string
60 return trans('activities.' . $this->type);
64 * Check if this activity is intended to be for an entity.
66 public function isForEntity(): bool
68 return Str::startsWith($this->type, [
69 'page_', 'chapter_', 'book_', 'bookshelf_',
74 * Checks if another Activity matches the general information of another.
76 public function isSimilarTo(self $activityB): bool
78 return [$this->type, $this->loggable_type, $this->loggable_id] === [$activityB->type, $activityB->loggable_type, $activityB->loggable_id];