1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Transmissor\Http\Controllers\User; |
4
|
|
|
|
5
|
|
|
use App\Jobs\SendNotifyMail; |
6
|
|
|
use App\Models\User; |
7
|
|
|
use App\Phphub\Markdown\Markdown; |
8
|
|
|
use Carbon\Carbon; |
9
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
10
|
|
|
use Illuminate\Http\Request; |
11
|
|
|
use Illuminate\Support\Facades\Auth; |
12
|
|
|
use Illuminate\Support\Facades\Input; |
13
|
|
|
use Illuminate\Support\Facades\Session; |
14
|
|
|
use Support\Http\Requests\MessageRequest; |
15
|
|
|
use Transmissor\Http\Controllers\User\Controller; |
16
|
|
|
use Transmissor\Models\Messenger\Message; |
17
|
|
|
use Transmissor\Models\Messenger\Participant; |
18
|
|
|
use Transmissor\Models\Messenger\Thread; |
19
|
|
|
|
20
|
|
|
class MessagesController extends Controller |
21
|
|
|
{ |
22
|
|
|
public function index(Request $request) |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
$currentUserId = Auth::id(); |
25
|
|
|
$threads = Thread::participateBy($currentUserId); |
26
|
|
|
|
27
|
|
|
// @todo Refazer essa parte |
28
|
|
|
// if (Auth::user()->newThreadsCount() == 0) { |
29
|
|
|
// Auth::user()->message_count = 0; |
30
|
|
|
// Auth::user()->save(); |
31
|
|
|
// } |
32
|
|
|
|
33
|
|
|
$currentActor = Auth::user(); |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
return view( |
37
|
|
|
'transmissor::users.messages.index', |
38
|
|
|
compact( |
39
|
|
|
'threads', |
40
|
|
|
'currentUserId', |
41
|
|
|
'currentActor' |
42
|
|
|
) |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
View Code Duplication |
public function show($id) |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
$thread = Thread::findOrFail($id); |
49
|
|
|
$participant = $thread->participant(); |
50
|
|
|
$messages = $thread->messages()->recent()->get(); |
51
|
|
|
|
52
|
|
|
$this->authorize('show', $thread); |
53
|
|
|
|
54
|
|
|
// counters |
55
|
|
|
$unread_message_count = $thread->userUnreadMessagesCount(Auth::id()); |
56
|
|
|
if ($unread_message_count > 0) { |
57
|
|
|
Auth::user()->message_count -= $unread_message_count; |
|
|
|
|
58
|
|
|
Auth::user()->save(); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
$thread->markAsRead(Auth::id()); |
61
|
|
|
|
62
|
|
|
return view('transmissor::users.messages.show', compact('thread', 'participant', 'messages', 'unread_message_count')); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
View Code Duplication |
public function create($id = null) |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
if ($id) { |
68
|
|
|
$recipient = User::findOrFail($id); |
69
|
|
|
$thread = Thread::between([$recipient->id, Auth::id()])->first(); |
|
|
|
|
70
|
|
|
if ($thread) { |
71
|
|
|
return redirect()->route('profile.transmissor.messages.show', $thread->id); |
72
|
|
|
} |
73
|
|
|
return view('transmissor::users.messages.create', compact('recipient')); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$recipient = User::pluck('name'); |
77
|
|
|
return view('transmissor::users.messages.create', compact('recipient')); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
View Code Duplication |
public function createSecureMessage($id) |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
// @todo Fazer com as seguintes opcoes |
83
|
|
|
// timePraAutoDestruicao |
84
|
|
|
// Grava em disco ou só memoria |
85
|
|
|
// Criptografia ponta a ponta com limite de data |
86
|
|
|
$recipient = User::findOrFail($id); |
87
|
|
|
|
88
|
|
|
$thread = Thread::between([$recipient->id, Auth::id()])->first(); |
|
|
|
|
89
|
|
|
if ($thread) { |
90
|
|
|
return redirect()->route('profile.transmissor.messages.show', $thread->id); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return view('transmissor::users.messages.create', compact('recipient')); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
View Code Duplication |
public function store(MessageRequest $request, Markdown $markdown) |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
$recipient = User::findOrFail($request->recipient_id); |
99
|
|
|
|
100
|
|
|
if ($request->thread_id) { |
101
|
|
|
$thread = Thread::findOrFail($request->thread_id); |
102
|
|
|
} else { |
103
|
|
|
$subject = Auth::user()->name . ' 给 ' . $recipient->name . ' 的私信。'; |
|
|
|
|
104
|
|
|
$thread = Thread::create(['subject' => $subject]); |
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
// Message |
108
|
|
|
$message = $markdown->convertMarkdownToHtml($request->message); |
109
|
|
|
Message::create( |
|
|
|
|
110
|
|
|
[ |
111
|
|
|
'messageable_type' => Thread::class, |
112
|
|
|
'messageable_id' => $thread->id, |
113
|
|
|
'actorable_type' => User::class, |
114
|
|
|
'actorable_id' => Auth::id(), |
115
|
|
|
'body' => $message |
116
|
|
|
] |
117
|
|
|
); |
118
|
|
|
|
119
|
|
|
// Sender |
120
|
|
|
$participant = Participant::firstOrCreate( |
121
|
|
|
[ |
122
|
|
|
'messageable_type' => Thread::class, |
123
|
|
|
'messageable_id' => $thread->id, |
124
|
|
|
'actorable_type' => User::class, |
125
|
|
|
'actorable_id' => Auth::id() |
126
|
|
|
] |
127
|
|
|
); |
128
|
|
|
$participant->last_read = new Carbon; |
129
|
|
|
$participant->save(); |
130
|
|
|
|
131
|
|
|
// Recipient |
132
|
|
|
$thread->addParticipant($recipient->id); |
133
|
|
|
|
134
|
|
|
// Notify user by Email |
135
|
|
|
$job = (new SendNotifyMail('new_message', Auth::user(), $recipient, null, null, $message)) |
136
|
|
|
->delay(\Illuminate\Support\Facades\Config::get('phphub.notify_delay')); |
137
|
|
|
dispatch($job); |
138
|
|
|
|
139
|
|
|
// notifications count |
140
|
|
|
$recipient->message_count++; |
141
|
|
|
$recipient->save(); |
142
|
|
|
|
143
|
|
|
return redirect()->route('profile.transmissor.messages.show', $thread->id); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.