/**
  * Publikowanie komentarza na mikroblogu
  *
  * @param null|int $id
  * @return $this
  */
 public function save($id = null)
 {
     $this->validate(request(), ['text' => 'required|string', 'parent_id' => 'sometimes|integer|exists:microblogs,id']);
     $microblog = $this->microblog->findOrNew($id);
     if ($id === null) {
         $user = auth()->user();
         $data = request()->only(['text', 'parent_id']) + ['user_id' => $user->id];
     } else {
         $this->authorize('update', $microblog);
         $user = $this->user->find($microblog->user_id, ['id', 'name', 'is_blocked', 'is_active', 'photo']);
         $data = request()->only(['text']);
     }
     $microblog->fill($data);
     $parent = $this->microblog->find($microblog->parent_id);
     $object = new Stream_Comment();
     $target = (new Stream_Microblog())->map($parent);
     $actor = new Stream_Actor($user);
     $alert = new Alert_Watch($this->alert);
     $stream = new Stream_Activity($this->stream);
     if (!$id) {
         $activity = new Stream_Create($actor, $object, $target);
     } else {
         $activity = new Stream_Update($actor, $object, $target);
     }
     \DB::transaction(function () use($id, &$microblog, $activity, $alert, $stream, $object, $user, $parent) {
         $microblog->save();
         // we need to parse text first (and store it in cache)
         $parser = app()->make('Parser\\Comment');
         $microblog->text = $parser->parse($microblog->text);
         if (!$id) {
             $watchers = $this->microblog->getWatchers($microblog->parent_id);
             $alert = new Alerts();
             // we need to send alerts AFTER saving comment to database because we need ID of comment
             $alertData = ['microblog_id' => $microblog->id, 'content' => $microblog->text, 'excerpt' => excerpt($microblog->text), 'sender_id' => $user->id, 'sender_name' => $user->name, 'subject' => excerpt($parent->text, 48), 'url' => route('microblog.view', [$parent->id], false) . '#comment-' . $microblog->id];
             if ($watchers) {
                 // new comment. should we send a notification?
                 $alert->attach((new Alert_Watch($this->alert, $alertData))->setUsersId($watchers));
             }
             $ref = new Ref_Login();
             // get id of users that were mentioned in the text
             $usersId = $ref->grab($microblog->text);
             if ($usersId) {
                 $alert->attach((new Alert_Login($this->alert, $alertData))->setUsersId($usersId));
             }
             // send a notify
             $alert->notify();
         }
         $ref = new Ref_Hash();
         $this->microblog->setTags($microblog->id, $ref->grab($microblog->text));
         // map microblog object into stream activity object
         $object->map($microblog);
         // put item into stream activity
         $stream->add($activity);
     });
     foreach (['name', 'is_blocked', 'is_active', 'photo'] as $key) {
         $microblog->{$key} = $user->{$key};
     }
     return view('microblog._comment')->with('comment', $microblog)->with('microblog', ['id' => $microblog->parent_id]);
 }
 /**
  * Publikowanie wpisu na mikroblogu
  *
  * @param null|int $id
  * @return $this
  */
 public function save($id = null)
 {
     $this->validate(request(), ['text' => 'required|string']);
     $microblog = $this->microblog->findOrNew($id);
     $data = request()->only(['text']);
     if ($id === null) {
         $user = auth()->user();
         $data['user_id'] = $user->id;
         $media = ['image' => []];
     } else {
         $this->authorize('update', $microblog);
         $user = $this->user->find($microblog->user_id, ['id', 'name', 'is_blocked', 'is_active', 'photo']);
         $media = $microblog->media;
         if (empty($media['image'])) {
             $media = ['image' => []];
         }
     }
     if (request()->has('thumbnail') || count($media['image']) > 0) {
         $delete = array_diff($media['image'], (array) request()->get('thumbnail'));
         foreach ($delete as $name) {
             unlink(public_path('storage/microblog/' . $name));
             unset($media['image'][array_search($name, $media['image'])]);
         }
         $insert = array_diff((array) request()->get('thumbnail'), $media['image']);
         foreach ($insert as $name) {
             rename(public_path('storage/tmp/' . $name), public_path('storage/microblog/' . $name));
             $media['image'][] = $name;
         }
         $microblog->media = $media;
     }
     $microblog->fill($data);
     \DB::transaction(function () use(&$microblog, $id, $user) {
         $microblog->save();
         // parsing text and store it in cache
         $microblog->text = app()->make('Parser\\Microblog')->parse($microblog->text);
         $object = (new Stream_Microblog())->map($microblog);
         $actor = new Stream_Actor($user);
         $stream = new Stream_Activity($this->stream);
         if (!$id) {
             // increase reputation points
             (new Reputation_Create($this->reputation))->map($microblog)->save();
             // put this to activity stream
             $stream->add(new Stream_Create($actor, $object));
             $ref = new Ref_Login();
             // get id of users that were mentioned in the text
             $usersId = $ref->grab($microblog->text);
             if ($usersId) {
                 (new Alert_Login($this->alert))->with(['users_id' => $usersId, 'sender_id' => $user->id, 'sender_name' => $user->name, 'subject' => excerpt($microblog->text, 48), 'url' => route('microblog.view', [$microblog->id], false)])->notify();
             }
         } else {
             $stream->add(new Stream_Update($actor, $object));
         }
         $ref = new Ref_Hash();
         $this->microblog->setTags($microblog->id, $ref->grab($microblog->text));
     });
     // jezeli wpis zawiera jakies zdjecia, generujemy linki do miniatur
     // metoda thumbnails() przyjmuje w parametrze tablice tablic (wpisow, tj. mikroblogow)
     // stad takie zapis:
     $microblog = $this->microblog->thumbnails($microblog);
     // do przekazania do widoku...
     foreach (['name', 'is_blocked', 'is_active', 'photo'] as $key) {
         $microblog->{$key} = $user->{$key};
     }
     return view($id ? 'microblog._microblog_text' : 'microblog._microblog')->with('microblog', $microblog);
 }