1 <?php namespace BookStack\Services;
3 use Illuminate\Support\Facades\Auth;
4 use BookStack\Activity;
14 * ActivityService constructor.
17 public function __construct(Activity $activity)
19 $this->activity = $activity;
20 $this->user = auth()->user();
24 * Add activity data to database.
25 * @param Entity $entity
30 public function add(Entity $entity, $activityKey, $bookId = 0, $extra = false)
32 $activity = $this->activity->newInstance();
33 $activity->user_id = $this->user->id;
34 $activity->book_id = $bookId;
35 $activity->key = strtolower($activityKey);
36 if ($extra !== false) {
37 $activity->extra = $extra;
39 $entity->activity()->save($activity);
40 $this->setNotification($activityKey);
44 * Adds a activity history with a message & without binding to a entity.
47 * @param bool|false $extra
49 public function addMessage($activityKey, $bookId = 0, $extra = false)
51 $this->activity->user_id = $this->user->id;
52 $this->activity->book_id = $bookId;
53 $this->activity->key = strtolower($activityKey);
54 if ($extra !== false) {
55 $this->activity->extra = $extra;
57 $this->activity->save();
58 $this->setNotification($activityKey);
63 * Removes the entity attachment from each of its activities
64 * and instead uses the 'extra' field with the entities name.
65 * Used when an entity is deleted.
66 * @param Entity $entity
69 public function removeEntity(Entity $entity)
71 $activities = $entity->activity;
72 foreach ($activities as $activity) {
73 $activity->extra = $entity->name;
74 $activity->entity_id = 0;
75 $activity->entity_type = null;
82 * Gets the latest activity.
87 public function latest($count = 20, $page = 0)
89 $activityList = $this->activity->orderBy('created_at', 'desc')
90 ->skip($count * $page)->take($count)->get();
91 return $this->filterSimilar($activityList);
95 * Gets the latest activity for an entity, Filtering out similar
96 * items to prevent a message activity list.
97 * @param Entity $entity
102 public function entityActivity($entity, $count = 20, $page = 0)
104 $activity = $entity->hasMany('BookStack\Activity')->orderBy('created_at', 'desc')
105 ->skip($count * $page)->take($count)->get();
107 return $this->filterSimilar($activity);
111 * Get latest activity for a user, Filtering out similar
118 public function userActivity($user, $count = 20, $page = 0)
120 $activity = $this->activity->where('user_id', '=', $user->id)
121 ->orderBy('created_at', 'desc')->skip($count * $page)->take($count)->get();
122 return $this->filterSimilar($activity);
126 * Filters out similar activity.
127 * @param Activity[] $activities
130 protected function filterSimilar($activities)
133 $previousItem = false;
134 foreach ($activities as $activityItem) {
135 if ($previousItem === false) {
136 $previousItem = $activityItem;
137 $newActivity[] = $activityItem;
140 if (!$activityItem->isSimilarTo($previousItem)) {
141 $newActivity[] = $activityItem;
143 $previousItem = $activityItem;
149 * Flashes a notification message to the session if an appropriate message is available.
150 * @param $activityKey
152 protected function setNotification($activityKey)
154 $notificationTextKey = 'activities.' . $activityKey . '_notification';
155 if (trans()->has($notificationTextKey)) {
156 $message = trans($notificationTextKey);
157 Session::flash('success', $message);