|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Controllers; |
| 4 | + |
| 5 | +use Illuminate\Support\Facades\DB; |
| 6 | +use Illuminate\Http\Request; |
| 7 | +use KChat\ActivityLog; |
| 8 | +use KChat\NotificationsLog; |
| 9 | + |
| 10 | +class ConversationsController extends Controller |
| 11 | +{ |
| 12 | + function Conversations(Request $request){ |
| 13 | + |
| 14 | + $conversations = DB::table('participants') |
| 15 | + ->select('conversations.id as id','conversations.conversation_name as name','conversations.photo as photo','conversations.created_at as created_at',DB::raw('COUNT(p.user_id) as members')) |
| 16 | + ->rightJoin('conversations', 'participants.conversation_id', '=', 'conversations.id') |
| 17 | + ->rightJoin('participants as p', 'p.conversation_id', '=', 'conversations.id') |
| 18 | + ->where('participants.user_id',Auth()->user()->id) |
| 19 | + ->orderBy('conversations.created_at','DESC') |
| 20 | + ->groupBy('p.conversation_id') |
| 21 | + ->paginate(10); |
| 22 | + |
| 23 | + $pages = range(1, $conversations->lastPage()); |
| 24 | + |
| 25 | + if($request->role == 'admin'){ |
| 26 | + return view('common.conversations',compact('conversations','pages')); |
| 27 | + } |
| 28 | + |
| 29 | + return view('common.conversations',compact('conversations','pages')); |
| 30 | + } |
| 31 | + |
| 32 | + function delete(Request $request){ |
| 33 | + |
| 34 | + if(isset($request->id)){ |
| 35 | + $request->ids = [$request->id]; |
| 36 | + } |
| 37 | + |
| 38 | + if(count($request->ids) == 0){ |
| 39 | + return json_encode(['error' => 'No conversation is selected']); |
| 40 | + } |
| 41 | + |
| 42 | + //This is to ensure that a user can only delete conversations in which he/she is a participant. |
| 43 | + $conversations = DB::table('participants') |
| 44 | + ->select('conversation_id') |
| 45 | + ->where('user_id',Auth()->user()->id) |
| 46 | + ->whereIn('conversation_id',$request->ids) |
| 47 | + ->get() |
| 48 | + ->toArray(); |
| 49 | + |
| 50 | + $ids = []; |
| 51 | + |
| 52 | + foreach($conversations as $k => $conversation){ |
| 53 | + $ids[] = $conversation->conversation_id; |
| 54 | + } |
| 55 | + |
| 56 | + $conversations = DB::table('conversations') |
| 57 | + ->select('id','conversation_name') |
| 58 | + ->whereIn('id',$ids) |
| 59 | + ->get() |
| 60 | + ->toArray(); |
| 61 | + |
| 62 | + $conversation_name = []; |
| 63 | + |
| 64 | + foreach($conversations as $conversation){ |
| 65 | + $conversation_name[$conversation->id] = $conversation->conversation_name; |
| 66 | + } |
| 67 | + |
| 68 | + $participants = DB::table('participants') |
| 69 | + ->select('conversation_id','user_id') |
| 70 | + ->whereIn('conversation_id',$ids) |
| 71 | + ->get() |
| 72 | + ->toArray(); |
| 73 | + |
| 74 | + ActivityLog::log()->save('Conversation','You have deleted conversation '.implode(',',$conversation_name).'.'); |
| 75 | + |
| 76 | + DB::table('conversations') |
| 77 | + ->whereIn('id',$ids) |
| 78 | + ->delete(); |
| 79 | + |
| 80 | + $tmp = []; |
| 81 | + |
| 82 | + foreach($participants as $participant){ |
| 83 | + $tmp[$conversation_name[$participant->conversation_id]][] = $participant->user_id; |
| 84 | + } |
| 85 | + |
| 86 | + foreach($tmp as $group => $ids){ |
| 87 | + NotificationsLog::log()->save($ids,'Conversation',Auth()->user()->email.' have deleted conversation '.$group.'.'); |
| 88 | + } |
| 89 | + |
| 90 | + return json_encode($participants); |
| 91 | + } |
| 92 | +} |
0 commit comments