Skip to content

Commit 703d69d

Browse files
committed
Conversations page added
1 parent 73823a4 commit 703d69d

File tree

7 files changed

+235
-9
lines changed

7 files changed

+235
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
}

public/css/kchat.css

+4
Original file line numberDiff line numberDiff line change
@@ -738,4 +738,8 @@ element {
738738
.mail-sidebar{
739739
display: block;
740740
}
741+
}
742+
743+
.convophoto{
744+
width:48px;
741745
}

public/js/kchat.js

+24-9
Original file line numberDiff line numberDiff line change
@@ -351,16 +351,21 @@ function getRelativeTime(dateTime) {
351351
}
352352
}
353353

354-
const elements = $(".timestamp");
354+
function timestamp(){
355+
356+
const elements = $(".timestamp");
355357

356-
// Loop through each element and update its content
357-
elements.each(function() {
358-
const currentContent = $(this).html();
359-
const updatedContent = getRelativeTime(currentContent); // Example update
358+
// Loop through each element and update its content
359+
elements.each(function() {
360+
const currentContent = $(this).html();
361+
const updatedContent = getRelativeTime(currentContent); // Example update
360362

361-
// Set the updated content to the same class
362-
$(this).html(updatedContent);
363-
});
363+
// Set the updated content to the same class
364+
$(this).html(updatedContent);
365+
});
366+
}
367+
368+
timestamp();
364369

365370
// Remove Blank values from cookie
366371
localStorage.setItem('selected',localStorage.getItem('selected').split(","), function(value) {
@@ -377,4 +382,14 @@ function setSelectedCount(){
377382
}
378383
}
379384

380-
setSelectedCount();
385+
setSelectedCount();
386+
387+
// Conversation page
388+
389+
function delete_convos(){
390+
kchat_alert("Are you sure you want to <strong>delete</strong> conversations?",(function(){__post('/conversations/delete',{'ids':getSelectedID()});}));
391+
}
392+
393+
function delete_convo(id){
394+
kchat_alert("Are you sure you want to <strong>delete</strong> conversations?",(function(){__post('/conversations/delete',{'ids':[id]});}));
395+
}

resources/views/admin/master.blade.php

+4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333
<a href="/members">
3434
<i class="fa fa-users"></i> Members </a>
3535
</li>
36+
<li class="{{ request()->is('conversations') ? 'active' : '' }}">
37+
<a href="/conversations">
38+
<i class="fa fa-comments"></i> Conversations </a>
39+
</li>
3640
<li class="{{ request()->is('settings') ? 'active' : '' }}">
3741
<a href="/settings">
3842
<i class="fa fa-cog"></i> Settings </a>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
@extends('admin.master')
2+
3+
@section('title', 'Conversations\'s')
4+
5+
@section('header')
6+
<link href="css/bootstrap.min.css" rel="stylesheet">
7+
<link href="css/kchat.css" rel="stylesheet">
8+
<link href="css/font-awesome.min.css" rel="stylesheet" />
9+
<script src="js/jquery.min.js"></script>
10+
<script src="js/bootstrap.bundle.min.js"></script>
11+
<link rel="stylesheet" href="//cdn.materialdesignicons.com/3.7.95/css/materialdesignicons.min.css">
12+
@endsection
13+
14+
@section('body')
15+
<div class="col-md-10 pt-3">
16+
<div class="card shadow-sm rounded bg-white mb-3">
17+
<!-- ---------------------------------------------------------------------------- -->
18+
<table class="table"style="width:100%">
19+
<thead>
20+
<tr>
21+
<th></th>
22+
<th></th>
23+
<th></th>
24+
<th>
25+
<select class="pages mb-3 float-right">
26+
@foreach($pages as $page)
27+
<option value="{{ $page }}" >{{ $page }}</option>
28+
@endforeach
29+
</select>
30+
</th>
31+
<th>
32+
<div class="dropdown show">
33+
<a href="#" data-toggle="dropdown" data-display="static">
34+
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-more-horizontal align-middle">
35+
<circle cx="12" cy="12" r="1"></circle>
36+
<circle cx="19" cy="12" r="1"></circle>
37+
<circle cx="5" cy="12" r="1"></circle>
38+
</svg>
39+
</a>
40+
<div class="dropdown-menu dropdown-menu-right">
41+
<a class="dropdown-item" onclick="SelectAll()" >Select All</a>
42+
<a class="dropdown-item" onclick="delete_convos()" >Delete</a>
43+
</div>
44+
</div>
45+
</th>
46+
</tr>
47+
</thead>
48+
<thead>
49+
<tr>
50+
<th>#</th>
51+
<th>Name</th>
52+
<th>Members</th>
53+
<th>Created at</th>
54+
<th>Action</th>
55+
</tr>
56+
</thead>
57+
<tbody id="member_table" >
58+
@foreach($conversations as $conversation)
59+
<tr class="select member" id="{{ $conversation->id }}" >
60+
<td><a href="/messages?chat={{ $conversation->id }}" ><img src="{{ $conversation->photo }}" width="32" height="32" class="rounded-circle my-n1" alt="[Photo]" onerror="this.onerror=null; this.src='/logo/KChat.svg';"/></a></td>
61+
<td><a href="/messages?chat={{ $conversation->id }}" >{{ $conversation->name }}</a></td>
62+
<td>{{ $conversation->members }}</td>
63+
<td class="timestamp" >{{ $conversation->created_at }}</span></td>
64+
<td>
65+
<div class="dropdown show">
66+
<a href="#" data-toggle="dropdown" data-display="static">
67+
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-more-horizontal align-middle">
68+
<circle cx="12" cy="12" r="1"></circle>
69+
<circle cx="19" cy="12" r="1"></circle>
70+
<circle cx="5" cy="12" r="1"></circle>
71+
</svg>
72+
</a>
73+
<div class="dropdown-menu dropdown-menu-right">
74+
<a class="dropdown-item" data-convoid="{{ $conversation->id }}" onclick="delete_convo({{ $conversation->id }});" >Delete</a>
75+
</div>
76+
</div>
77+
</td>
78+
</tr>
79+
@endforeach
80+
</tbody>
81+
<thead>
82+
<tr>
83+
<th></th>
84+
<th></th>
85+
<th></th>
86+
<th></th>
87+
<th>
88+
<select class="pages mb-3 float-right">
89+
@foreach($pages as $page)
90+
<option value="{{ $page }}" >{{ $page }}</option>
91+
@endforeach
92+
</select>
93+
</th>
94+
</tr>
95+
</thead>
96+
</table>
97+
</div>
98+
</div>
99+
@endsection
100+
101+
@section('script')
102+
@endsection

resources/views/user/master.blade.php

+4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333
<a href="/members">
3434
<i class="fa fa-users"></i> Members </a>
3535
</li>
36+
<li class="{{ request()->is('conversations') ? 'active' : '' }}">
37+
<a href="/conversations">
38+
<i class="fa fa-comments"></i> Conversations </a>
39+
</li>
3640
<li class="{{ request()->is('notification') ? 'active' : '' }}">
3741
<a href="/notification">
3842
<i class="fa fa-exclamation-circle"></i> Notification </a>

routes/web.php

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use App\Http\Controllers\KchatController;
1010
use App\Http\Controllers\MessageController;
1111
use App\Http\Controllers\DashboardController;
12+
use App\Http\Controllers\ConversationsController;
1213

1314
/*
1415
|--------------------------------------------------------------------------
@@ -50,6 +51,8 @@
5051
Route::get('/profile', [UserController::class, 'profile'])->name('Profile');
5152

5253
Route::get('/messages', [MessageController::class, 'messages'])->name('Messages Controller');
54+
55+
Route::get('/conversations', [ConversationsController::class, 'Conversations'])->name('Conversations Controller');
5356

5457
Route::get('/messages/downattch/{uuid}', [KchatController::class, 'downattch'])->name('Attachments Download')->where('uuid', '[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}')->withoutMiddleware('GetCounts');
5558

@@ -96,5 +99,7 @@
9699
Route::post('/getConvo', [KchatController::class, 'getConvo'])->name('get Conversations list via search');
97100

98101
Route::post('/ajax_members', [UserController::class, 'members_ajax'])->name('Members List on Ajax call');
102+
103+
Route::post('/conversations/delete', [ConversationsController::class, 'delete'])->name('Conversations Controller');
99104

100105
});

0 commit comments

Comments
 (0)