|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: me |
|
5
|
|
|
* Date: 08.07.2017 |
|
6
|
|
|
* Time: 23:25 |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace Evilnet\Inbox\Services; |
|
9
|
|
|
|
|
10
|
|
|
use Evilnet\Inbox\Conversation; |
|
11
|
|
|
use Evilnet\Inbox\Messages; |
|
12
|
|
|
use Evilnet\Inbox\Repository\InboxRepository; |
|
13
|
|
|
use Evilnet\Inbox\User; |
|
14
|
|
|
|
|
15
|
|
|
class InboxService{ |
|
16
|
|
|
|
|
17
|
|
|
protected $inboxRepository; |
|
18
|
|
|
function __construct(InboxRepository $inboxRepository) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->inboxRepository = $inboxRepository; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function addMessage($request, $id){ |
|
24
|
|
|
if($request->get('conv_id') != $id->id){ |
|
25
|
|
|
redirect()->back()->withErrors('Something went wrong...')->send(); |
|
|
|
|
|
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
$a = new Messages(); |
|
29
|
|
|
$a->name = auth()->user()->name; |
|
|
|
|
|
|
30
|
|
|
$a->message = $request->get('message'); |
|
31
|
|
|
$a->conversation_id = $request->get('conv_id'); |
|
32
|
|
|
$a->save(); |
|
33
|
|
|
return redirect()->back()->send(); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function addConversation($user,$messg,$subject){ |
|
37
|
|
|
|
|
38
|
|
|
if($this->inboxRepository->userExists($user)){ |
|
39
|
|
|
$from = auth()->id(); |
|
|
|
|
|
|
40
|
|
|
$to = User::where('name', $user)->first(); |
|
41
|
|
|
if(!$this->inboxRepository->conversationExists($to,$from)){ |
|
42
|
|
|
$message = new Messages(); |
|
43
|
|
|
$message->name = auth()->user()->name; |
|
44
|
|
|
$message->message = $messg; |
|
45
|
|
|
$conversation = new Conversation(); |
|
46
|
|
|
$conversation->subject = $subject; |
|
47
|
|
|
$conversation->id_from = $from; |
|
48
|
|
|
$conversation->id_to = $to->id; |
|
49
|
|
|
$conversation->save(); |
|
50
|
|
|
$message->conversation_id = $conversation->id; |
|
51
|
|
|
$message->save(); |
|
52
|
|
|
return redirect()->to(url('/').'/conversation/'.$conversation->id)->send(); |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
else{ |
|
55
|
|
|
return redirect()->back()->withErrors('Conversation already exists')->send(); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
else{ |
|
59
|
|
|
return redirect()->back()->withErrors('User not found')->send(); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
public function fetchAllConversation(){ |
|
63
|
|
|
return $this->inboxRepository->fetchAll(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function deleteConversation($conversation){ |
|
67
|
|
|
//it will delete conversation row and all related rows in messages table |
|
68
|
|
|
foreach ($conversation->messages as $messages){ |
|
69
|
|
|
$messages->forceDelete(); |
|
70
|
|
|
} |
|
71
|
|
|
$conversation->forceDelete(); |
|
72
|
|
|
return redirect()->back()->send(); |
|
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
//return array with users that match to display for logged user |
|
77
|
|
|
public function getInboxUsers($conversations){ |
|
78
|
|
|
$users = []; |
|
79
|
|
|
foreach ($conversations as $date => $thread){ |
|
80
|
|
|
foreach ($thread as $conversation){ |
|
81
|
|
|
if( auth()->id() == $conversation->id_to) { |
|
|
|
|
|
|
82
|
|
|
$users[] = \Evilnet\Inbox\User::where('id', $conversation->id_from)->first(); |
|
83
|
|
|
} |
|
84
|
|
|
elseif(auth()->id() == $conversation->id_from) |
|
85
|
|
|
$users[] = \Evilnet\Inbox\User::where('id', $conversation->id_to)->first(); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
return $users; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
} |