public function updateNotifications($_activity_log) { \Func::logAppActivity(); /* *** activity log list *** 1. From Store store_bookmark_store destroy_bookmark_store 2. From Post -store_post -update_post destroy_post - need to take care store_like_post destroy_like_post store_save_post store_comment store_like_comment destroy_like_comment destroy_comment 3. From User -update_user_profile_picture -update_user destroy_user -update_user_password -update_user_location -update_user_about -update_user_social_network store_follow_user destroy_follow_user store_bookmark_group update_bookmark_group destroy_bookmark_group store_post_group update_post_group destroy_post_group store_follow_post_group destroy_follow_post_group store_save_group update_save_group destroy_save_group store_follow_save_group destroy_follow_save_group */ switch ($_activity_log->action) { /* Store Starts */ case "store_bookmark_store": case "destroy_bookmark_store": break; /* Store Ends, Post Starts */ /* Store Ends, Post Starts */ case "store_post": case "update_post": break; case "destroy_post": // need to modify if ($_activity_log->obj_type == "post") { $notifications = $this->notification->where('obj_id', '=', $_activity_log->obj_id)->where('obj_type', '=', 'App\\Post')->get(); foreach ($notifications as $item) { $item->delete(); } } break; case "store_like_post": if ($_activity_log->obj_type == "post") { $post = $this->post->with('user', 'likesCount')->where('posts.id', '=', $_activity_log->obj_id)->first(); if ($post) { if ($post->user_id == $_activity_log->user_id) { break; } $likes = $this->like->with('user')->where('post_id', '=', $post->id)->orderBy('created_at', 'DESC')->get(); $content = ""; $counter = 0; // Start foreach ($likes as $item) { if ($counter != 0) { $content .= ", "; } $content .= "<a href=\"#/tab/account/" . $item->user->slug . "/notification\" onclick=\"event.stopPropagation();\">" . $item->user->first_name . " " . $item->user->last_name . "</a>"; if ($post->likesCount > 3) { if ($counter >= 1) { $content .= " and " . ($post->likesCount - 2) . " more people"; break; } } $counter++; } $content .= " likes your post."; // End $notification = $this->notification->where('user_id', '=', $post->user_id)->where('obj_id', '=', $_activity_log->obj_id)->where('action', '=', "like_post")->where('obj_type', '=', 'App\\Post')->first(); if ($notification) { $notification->delete(); } $notification = $this->notification->firstOrCreate(['initiator_id' => \Auth::user()->id, 'user_id' => $post->user_id, 'target_src' => "/post/" . $post->id, 'action' => "like_post", 'obj_id' => $_activity_log->obj_id, 'obj_type' => "App\\Post"]); $notification->contents = $content; $notification->save(); $user = $this->user->where('id', '=', $post->user_id)->first(); \Func::sendPushNotification($user, strip_tags($content)); } } break; case "destroy_like_post": if ($_activity_log->obj_type == "post") { $post = $this->post->with('user', 'likesCount')->where('posts.id', '=', $_activity_log->obj_id)->first(); if ($post) { $notification = $this->notification->where('user_id', '=', $post->user_id)->where('action', '=', "like_post")->where('obj_id', '=', $_activity_log->obj_id)->where('obj_type', '=', 'App\\Post')->first(); if ($notification && $post->likesCount == 0) { $notification->delete(); break; } else { if (!$notification) { $notification = $this->notification->firstOrCreate(['initiator_id' => \Auth::user()->id, 'user_id' => $post->user_id, 'target_src' => "/post/" . $post->id, 'action' => "like_post", 'obj_id' => $_activity_log->obj_id, 'obj_type' => "App\\Post"]); } } $likes = $this->like->with('user')->where('post_id', '=', $post->id)->orderBy('created_at', 'DESC')->get(); $content = ""; $counter = 0; // start foreach ($likes as $item) { if ($counter != 0) { $content .= ", "; } $content .= "<a href=\"#/tab/account/" . $item->user->slug . "/notification\" onclick=\"event.stopPropagation();\">" . $item->user->first_name . " " . $item->user->last_name . "</a>"; if ($post->likesCount > 3) { if ($counter >= 1) { $content .= " and " . ($post->likesCount - 2) . " more people"; break; } } $counter++; } $content .= " likes your post."; // end $notification->contents = $content; $notification->save(); } } break; case "store_save_post": if ($_activity_log->obj_type == "post") { $post = $this->post->with('user', 'saveGroups')->where('posts.id', '=', $_activity_log->obj_id)->first(); if ($post) { if ($post->user_id == $_activity_log->user_id) { break; } $content = ""; $user_list = array(); $user_id_list = array(); // Start foreach ($post->saveGroups as $item) { if (in_array($item->user->first_name . " " . $item->user->last_name, $user_list)) { continue; } array_push($user_list, $item->user->first_name . " " . $item->user->last_name); array_push($user_id_list, $item->user->id); } $counter = count($user_list); if ($counter <= 3) { foreach ($user_list as $key => $user) { if ($key != 0) { $content .= ", "; } $user_obj = $this->user->where('id', '=', $user_id_list[$key])->first(); $content .= "<a href=\"#/tab/account/" . $user_obj->slug . "/notification\" onclick=\"event.stopPropagation();\">" . $user . "</a>"; } } else { foreach ($user_list as $key => $user) { if ($key != 0) { $content .= ", "; } else { if ($key == 2) { break; } } $content .= "<a href=\"#/tab/account/" . $user->slug . "/notification\" onclick=\"event.stopPropagation();\">" . $user . "</a>"; } $content .= " and " . ($counter - 2) . " more people"; } $content .= " save your post."; // end $notification = $this->notification->where('user_id', '=', $post->user_id)->where('obj_id', '=', $_activity_log->obj_id)->where('action', '=', "save_post")->where('obj_type', '=', 'App\\Post')->first(); if ($notification) { $notification->delete(); } $notification = $this->notification->create(['initiator_id' => \Auth::user()->id, 'user_id' => $post->user_id, 'target_src' => "/post/" . $post->id, 'action' => "save_post", 'obj_id' => $_activity_log->obj_id, 'obj_type' => "App\\Post"]); $notification->contents = $content; $notification->save(); $user = $this->user->where('id', '=', $post->user_id)->first(); \Func::sendPushNotification($user, strip_tags($content)); } } break; case "store_comment": if ($_activity_log->obj_type == "comment") { $comment = $this->comment->where('id', '=', $_activity_log->obj_id)->first(); if ($comment) { $post = $this->post->where('id', '=', $comment->post_id)->first(); $comments = $this->comment->where('post_id', '=', $comment->post_id)->orderBy('updated_at', 'DESC')->get(); $post_owner_id = $post->user->id; $user_list = array(); $user_id_list = array(); foreach ($comments as $item) { if (in_array($item->user->first_name . " " . $item->user->last_name, $user_list)) { continue; } array_push($user_list, $item->user->first_name . " " . $item->user->last_name); array_push($user_id_list, $item->user->id); } $counter = count($user_list); $content = ""; if ($counter <= 3) { foreach ($user_list as $key => $user) { if ($key != 0) { $content .= ", "; } $user_obj = $this->user->where('id', '=', $user_id_list[$key])->first(); $content .= "<a href=\"#/tab/account/" . $user_obj->slug . "/notification\" onclick=\"event.stopPropagation();\">" . $user . "</a>"; } } else { foreach ($user_list as $key => $user) { if ($key == 2) { break; } if ($key != 0) { $content .= ", "; } $user_obj = $this->user->where('id', '=', $user_id_list[$key])->first(); $content .= "<a href=\"#/tab/account/" . $user_obj->slug . "/notification\" onclick=\"event.stopPropagation();\">" . $user . "</a>"; } $content .= " and " . ($counter - 2) . " more people"; } foreach ($user_list as $key => $user) { if ($user_id_list[$key] == $_activity_log->user_id) { continue; } $notification = $this->notification->where('user_id', '=', $user_id_list[$key])->where('obj_id', '=', $comment->id)->where('action', '=', "leave_comment")->where('obj_type', '=', 'App\\Comment')->first(); if ($notification) { $notification->delete(); } $notification = $this->notification->create(['initiator_id' => \Auth::user()->id, 'user_id' => $user_id_list[$key], 'target_src' => "/post/" . $post->id, 'obj_id' => $comment->id, 'action' => "leave_comment", 'obj_type' => "App\\Comment"]); if ($post_owner_id == $user_id_list[$key]) { $content2 = $content . " commented on your post."; } else { $content2 = $content . " commented on the post that you left a comment."; } $notification->contents = $content2; $notification->save(); $user = $this->user->where('id', '=', $user_id_list[$key])->first(); \Func::sendPushNotification($user, strip_tags($content2)); } } } break; case "destroy_comment": // need to modify if ($_activity_log->obj_type == "comment") { $notifications = $this->notification->where('obj_id', '=', $_activity_log->obj_id)->where('obj_type', '=', 'App\\Comment')->get(); foreach ($notifications as $item) { $item->delete(); } } break; case "store_like_comment": if ($_activity_log->obj_type == "comment") { $comment = $this->comment->where('id', '=', $_activity_log->obj_id)->first(); if ($comment) { if ($comment->user_id == $_activity_log->user_id) { break; } $like_count = $comment->likes->count(); $content = ""; $counter = 0; // Start foreach ($comment->likes as $item) { if ($counter != 0) { $content .= ", "; } $content .= "<a href=\"#/tab/account/" . $item->user->slug . "/notification\" onclick=\"event.stopPropagation();\">" . $item->user->first_name . " " . $item->user->last_name . "</a>"; if ($like_count > 3) { if ($counter >= 1) { $content .= " and " . ($like_count - 2) . " more people"; break; } } $counter++; } $content .= " likes your comment."; // End $notification = $this->notification->where('user_id', '=', $comment->user_id)->where('obj_id', '=', $comment->id)->where('action', '=', "like_comment")->where('obj_type', '=', 'App\\Comment')->first(); if ($notification) { $notification->delete(); } $notification = $this->notification->create(['initiator_id' => \Auth::user()->id, 'user_id' => $comment->user_id, 'target_src' => "/post/" . $comment->post->id, 'obj_id' => $comment->id, 'action' => "like_comment", 'obj_type' => "App\\Comment"]); $notification->contents = $content; $notification->save(); $user = $this->user->where('id', '=', $comment->user_id)->first(); \Func::sendPushNotification($user, strip_tags($content)); } } break; case "destroy_like_comment": if ($_activity_log->obj_type == "comment") { $comment = $this->comment->where('id', '=', $_activity_log->obj_id)->first(); if ($comment) { if ($comment->user_id == $_activity_log->user_id) { break; } $like_count = $comment->likes->count(); $content = ""; $counter = 0; // Start foreach ($comment->likes as $item) { if ($counter != 0) { $content .= ", "; } $content .= "<a href=\"#/tab/account/" . $item->user->slug . "/notification\" onclick=\"event.stopPropagation();\">" . $item->user->first_name . " " . $item->user->last_name . "</a>"; if ($like_count > 3) { if ($counter >= 1) { $content .= " and " . ($like_count - 2) . " more people"; break; } } $counter++; } $content .= " likes your comment."; // End $notification = $this->notification->where('user_id', '=', $comment->user_id)->where('obj_id', '=', $comment->id)->where('action', '=', "like_comment")->where('obj_type', '=', 'App\\Comment')->first(); if (!$notification) { $notification = $this->notification->create(['initiator_id' => \Auth::user()->id, 'user_id' => $comment->user_id, 'obj_id' => $comment->id, 'target_src' => "/post/" . $comment->post->id, 'action' => "like_comment", 'obj_type' => "App\\Comment"]); } $notification->contents = $content; $notification->save(); } } break; /* Post Ends, User Starts */ /* Post Ends, User Starts */ case "update_user_profile_picture": case "update_user": case "update_user_password": case "update_user_location": case "update_user_about": case "update_user_social_network": case "store_post_group": case "update_post_group": case "store_save_group": case "update_save_group": case "store_bookmark_group": case "update_bookmark_group": case "destroy_bookmark_group": break; case "store_follow_user": $notification = $this->notification->firstOrCreate(['initiator_id' => \Auth::user()->id, 'user_id' => $_activity_log->obj_id, 'obj_id' => $_activity_log->user_id, 'target_src' => "/account/" . \Auth::user()->slug, 'action' => "follow_user", 'obj_type' => "App\\User"]); $user = $this->user->where('id', '=', $_activity_log->user_id)->first(); $content = "<a href=\"#/tab/account/" . $user->slug . "/notification\" onclick=\"event.stopPropagation();\">" . $user->first_name . " " . $user->last_name . "</a> started following you."; $notification->contents = $content; $notification->save(); $user = $this->user->where('id', '=', $_activity_log->obj_id)->first(); \Func::sendPushNotification($user, strip_tags($content)); break; case "destroy_follow_user": $notification = $this->notification->where('user_id', '=', $_activity_log->obj_id)->where('obj_id', '=', $_activity_log->user_id)->where('action', '=', "follow_user")->where('obj_type', '=', 'App\\User')->first(); if ($notification) { $notification->delete(); } break; case "store_follow_post_group": /* $user = $this->user->with('follows') ->where('id', '=', $_activity_log->user_id) ->first(); $post_group = $this->post_group->with('user') ->where('id', '=', $_activity_log->obj_id) ->first(); if (in_array($post_group->user->id, $user->follows->lists('followee_id'))) { break; } $notification = $this->notification->firstOrCreate([ 'user_id' => $post_group->user->id, 'obj_id' => $_activity_log->obj_id, 'action' => "follow_post_group", 'obj_type' => "App\PostGroup" ]); $notification->contents = $user->first_name." ".$user->last_name." started following your post group ".$post_group->name."."; $notification->save(); */ break; case "destroy_follow_post_group": /* $post_group = $this->post_group->with('user') ->where('id', '=', $_activity_log->obj_id) ->first(); $notification = $this->notification ->where('user_id', '=',$post_group->user->id) ->where('obj_id', '=',$_activity_log->obj_id) ->where('action', '=',"follow_post_group") ->where('obj_type', '=', 'App\PostGroup') ->first(); if ($notification) { $notification->delete(); } */ break; case "store_follow_save_group": /* $user = $this->user->with('follows') ->where('id', '=', $_activity_log->user_id) ->first(); $save_group = $this->save_group->with('user') ->where('id', '=', $_activity_log->obj_id) ->first(); if (in_array($save_group->user->id, $user->follows->lists('followee_id'))) { break; } $notification = $this->notification->firstOrCreate([ 'user_id' => $save_group->user->id, 'obj_id' => $_activity_log->obj_id, 'action' => "follow_save_group", 'obj_type' => "App\SaveGroup" ]); $notification->contents = $user->first_name." ".$user->last_name." started following your save group ".$save_group->name."."; $notification->save(); */ break; case "destroy_follow_save_group": /* $save_group = $this->save_group->with('user') ->where('id', '=', $_activity_log->obj_id) ->first(); $notification = $this->notification ->where('user_id', '=',$save_group->user->id) ->where('obj_id', '=',$_activity_log->obj_id) ->where('action', '=',"follow_save_group") ->where('obj_type', '=', 'App\SaveGroup') ->first(); if ($notification) { $notification->delete(); } */ break; case "destroy_post_group": /* $notification = $this->notification ->where('obj_id', '=',$_activity_log->obj_id) ->where('obj_type', '=', 'App\PostGroup') ->first(); if ($notification) { $notification->delete(); } */ break; case "destroy_save_group": /* $notification = $this->notification ->where('obj_id', '=',$_activity_log->obj_id) ->where('obj_type', '=', 'App\SaveGroup') ->first(); if ($notification) { $notification->delete(); } */ break; case "destroy_user": // need to implement /* User Ends */ // need to implement /* User Ends */ default: break; } return; }