Browse Source

Merge pull request #195 from dansup/frontend-ui-refactor

Add comment notifications
daniel 7 years ago
parent
commit
297118527b

+ 3 - 1
app/Http/Controllers/AccountController.php

@@ -16,7 +16,9 @@ class AccountController extends Controller
     public function notifications(Request $request)
     {
       $profile = Auth::user()->profile;
-      $notifications = $this->fetchNotifications($profile->id);
+      //$notifications = $this->fetchNotifications($profile->id);
+      $notifications = Notification::whereProfileId($profile->id)
+          ->orderBy('id','desc')->take(30)->simplePaginate();
       return view('account.activity', compact('profile', 'notifications'));
     }
 

+ 2 - 0
app/Http/Controllers/CommentController.php

@@ -3,6 +3,7 @@
 namespace App\Http\Controllers;
 
 use Illuminate\Http\Request;
+use App\Jobs\CommentPipeline\CommentPipeline;
 use App\Jobs\StatusPipeline\NewStatusPipeline;
 use Auth, Hashids;
 use App\{Comment, Profile, Status};
@@ -40,6 +41,7 @@ class CommentController extends Controller
       $reply->save();
 
       NewStatusPipeline::dispatch($reply, false);
+      CommentPipeline::dispatch($status, $reply);
 
       if($request->ajax()) {
         $response = ['code' => 200, 'msg' => 'Comment saved', 'username' => $profile->username, 'url' => $reply->url(), 'profile' => $profile->url()];

+ 2 - 0
app/Jobs/FollowPipeline/FollowPipeline.php

@@ -46,6 +46,8 @@ class FollowPipeline implements ShouldQueue
             $notification->action = 'follow';
             $notification->message = $follower->toText();
             $notification->rendered = $follower->toHtml();
+            $notification->item_id = $target->id;
+            $notification->item_type = "App\Profile";
             $notification->save();
 
             Cache::forever('notification.' . $notification->id, $notification);

+ 2 - 0
app/Jobs/LikePipeline/LikePipeline.php

@@ -49,6 +49,8 @@ class LikePipeline implements ShouldQueue
             $notification->action = 'like';
             $notification->message = $like->toText();
             $notification->rendered = $like->toHtml();
+            $notification->item_id = $status->id;
+            $notification->item_type = "App\Status";
             $notification->save();
 
             Cache::forever('notification.' . $notification->id, $notification);

+ 10 - 0
app/Notification.php

@@ -17,4 +17,14 @@ class Notification extends Model
     return $this->belongsTo(Profile::class, 'profile_id', 'id');
   }
 
+  public function item()
+  {
+    return $this->morphTo();
+  }
+
+  public function status()
+  {
+    return $this->belongsTo(Status::class, 'item_id', 'id');
+  }
+
 }

+ 13 - 0
app/Status.php

@@ -101,4 +101,17 @@ class Status extends Model
       return $obj;
     }
 
+    public function replyToText()
+    {
+      $actorName = $this->profile->username;
+      return "{$actorName} " . __('notification.commented');
+    }
+
+    public function replyToHtml()
+    {
+      $actorName = $this->profile->username;
+      $actorUrl = $this->profile->url();
+      return "<a href='{$actorUrl}' class='profile-link'>{$actorName}</a> " .
+          __('notification.commented');
+    }
 }

+ 31 - 0
database/migrations/2018_06_04_061435_update_notifications_table_add_polymorphic_relationship.php

@@ -0,0 +1,31 @@
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class UpdateNotificationsTableAddPolymorphicRelationship extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('notifications', function (Blueprint $table) {
+            $table->bigInteger('item_id')->unsigned()->nullable()->after('actor_id');
+            $table->string('item_type')->nullable()->after('item_id');
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        //
+    }
+}

+ 1 - 0
resources/lang/en/notification.php

@@ -4,5 +4,6 @@ return [
 
   'likedPhoto' => 'liked your photo.',
   'startedFollowingYou' => 'started following you.',
+  'commented' => 'commented on your post.',
 
 ];

+ 20 - 0
resources/views/account/activity.blade.php

@@ -18,8 +18,12 @@
             <span class="text-muted notification-timestamp pl-1">{{$notification->created_at->diffForHumans(null, true, true, true)}}</span>
           </span>
           <span class="float-right notification-action">
+            @if($notification->item_id)
+              <a href="{{$notification->status->url()}}"><img src="{{$notification->status->thumb()}}" width="32px" height="32px"></a>
+            @endif
           </span>
         @break
+
         @case('follow')
           <span class="notification-icon pr-3">
             <img src="{{$notification->actor->avatarUrl()}}" width="32px" class="rounded-circle">
@@ -38,6 +42,22 @@
           </span>
           @endif
         @break
+
+        @case('comment')
+          <span class="notification-icon pr-3">
+            <img src="{{$notification->actor->avatarUrl()}}" width="32px" class="rounded-circle">
+          </span>
+          <span class="notification-text">
+            {!! $notification->rendered !!}
+            <span class="text-muted notification-timestamp pl-1">{{$notification->created_at->diffForHumans(null, true, true, true)}}</span>
+          </span>
+          <span class="float-right notification-action">
+            @if($notification->item_id)
+              <a href="{{$notification->status->parent()->url()}}"><img src="{{$notification->status->parent()->thumb()}}" width="32px" height="32px"></a>
+            @endif
+          </span>
+        @break
+
         @endswitch
       </li>
       @endforeach