/**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     \DB::table('permissions')->insert(['name' => 'microblog-update', 'description' => 'Edycja wpisów mikrobloga', 'default' => false]);
     \DB::table('permissions')->insert(['name' => 'microblog-delete', 'description' => 'Usuwanie wpisów mikrobloga', 'default' => false]);
     $user = DB::table('users')->orderBy('id')->first();
     \Coyote\Microblog::create(['user_id' => $user->id, 'text' => 'Lorem ipsum lores']);
 }
 public static function boot()
 {
     parent::boot();
     static::creating(function ($model) {
         // nadajemy domyslna wartosc sortowania przy dodawaniu elementu
         $model->score = Microblog::getScore(0, (int) $model->bonus, time());
     });
 }
 /**
  * @param $id
  * @param Request $request
  * @return \Illuminate\Http\JsonResponse
  * @throws \Exception
  */
 public function post($id, Request $request)
 {
     if (auth()->guest()) {
         return response()->json(['error' => 'Musisz być zalogowany, aby oddać ten głos.'], 500);
     }
     $microblog = Microblog::findOrFail($id);
     $vote = Vote::where('microblog_id', $id)->where('user_id', auth()->user()->id)->first();
     if (!config('app.debug') && auth()->user()->id === $microblog->user_id) {
         return response()->json(['error' => 'Nie możesz głosować na wpisy swojego autorstwa'], 500);
     }
     \DB::beginTransaction();
     try {
         if ($vote) {
             $vote->delete();
             $microblog->votes--;
         } else {
             Vote::create(['microblog_id' => $id, 'user_id' => auth()->user()->id, 'ip' => $request->getClientIp()]);
             $microblog->votes++;
         }
         $microblog->score = Microblog::getScore($microblog->votes, $microblog->bonus, $microblog->created_at->getTimestamp());
         // reputacje przypisujemy tylko za ocene wpisu a nie komentarza!!
         if (!$microblog->parent_id) {
             $url = route('microblog.view', [$microblog->id], false) . '#entry-' . $microblog->id;
             (new Reputation_Vote($this->reputation))->map($microblog)->setUrl($url)->setIsPositive(!$vote)->save();
         } else {
             $url = route('microblog.view', [$microblog->parent_id], false) . '#comment-' . $microblog->id;
         }
         $microblog->save();
         // put this to activity stream
         $activity = new Stream_Vote(new Stream_Actor(auth()->user()), (new Stream_Microblog())->map($microblog));
         (new Stream_Activity($this->stream))->add($activity);
         if (!$vote) {
             (new Alert_Vote($this->alert))->setMicroblogId($microblog->id)->addUserId($microblog->user_id)->setSubject(excerpt($microblog->text))->setSenderId(auth()->user()->id)->setSenderName(auth()->user()->name)->setUrl($url)->notify();
         }
         \DB::commit();
     } catch (\Exception $e) {
         \DB::rollBack();
         throw $e;
     }
     return response()->json(['count' => Vote::where('microblog_id', $id)->count()]);
 }