3 namespace BookStack\Actions;
5 use BookStack\Auth\Permissions\JointPermission;
6 use BookStack\Auth\User;
7 use BookStack\Entities\Models\Entity;
9 use Illuminate\Database\Eloquent\Relations\BelongsTo;
10 use Illuminate\Database\Eloquent\Relations\HasMany;
11 use Illuminate\Database\Eloquent\Relations\MorphTo;
12 use Illuminate\Support\Str;
15 * @property string $type
16 * @property User $user
17 * @property Entity $entity
18 * @property string $detail
19 * @property string $entity_type
20 * @property int $entity_id
21 * @property int $user_id
23 class Activity extends Model
26 * Get the entity for this activity.
28 public function entity(): MorphTo
30 if ($this->entity_type === '') {
31 $this->entity_type = null;
34 return $this->morphTo('entity');
38 * Get the user this activity relates to.
40 public function user(): BelongsTo
42 return $this->belongsTo(User::class);
45 public function jointPermissions(): HasMany
47 return $this->hasMany(JointPermission::class, 'entity_id', 'entity_id')
48 ->whereColumn('activities.entity_type', '=', 'joint_permissions.entity_type');
52 * Returns text from the language files, Looks up by using the activity key.
54 public function getText(): string
56 return trans('activities.' . $this->type);
60 * Check if this activity is intended to be for an entity.
62 public function isForEntity(): bool
64 return Str::startsWith($this->type, [
65 'page_', 'chapter_', 'book_', 'bookshelf_',
70 * Checks if another Activity matches the general information of another.
72 public function isSimilarTo(self $activityB): bool
74 return [$this->type, $this->entity_type, $this->entity_id] === [$activityB->type, $activityB->entity_type, $activityB->entity_id];