1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Coyote\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use Coyote\Http\Requests\CommentRequest; |
6
|
|
|
use Coyote\Http\Resources\CommentResource; |
7
|
|
|
use Coyote\Comment; |
8
|
|
|
use Coyote\Notifications\Job\CommentedNotification; |
9
|
|
|
use Coyote\Notifications\Job\RepliedNotification; |
10
|
|
|
use Coyote\Services\Stream\Actor as Stream_Actor; |
11
|
|
|
use Illuminate\Contracts\Notifications\Dispatcher; |
12
|
|
|
use Coyote\Services\Stream\Activities\Create as Stream_Create; |
13
|
|
|
use Coyote\Services\Stream\Activities\Update as Stream_Update; |
14
|
|
|
use Coyote\Services\Stream\Activities\Delete as Stream_Delete; |
15
|
|
|
use Coyote\Services\Stream\Objects\Comment as Stream_Comment; |
16
|
|
|
|
17
|
|
|
class CommentController extends Controller |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @param CommentRequest $request |
21
|
|
|
* @param Dispatcher $dispatcher |
22
|
|
|
* @param Comment|null $comment |
23
|
|
|
* @return CommentResource |
24
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
25
|
|
|
*/ |
26
|
|
|
public function save(CommentRequest $request, Dispatcher $dispatcher, Comment $comment = null) |
27
|
|
|
{ |
28
|
|
|
$this->authorize('update', $comment); |
29
|
|
|
|
30
|
|
|
$comment->fill($request->all())->creating(function (Comment $model) use ($request) { |
|
|
|
|
31
|
|
|
$model->user_id = $this->userId; |
32
|
|
|
$model->forceFill($request->only(['resource_id', 'resource_type'])); |
33
|
|
|
}); |
34
|
|
|
|
35
|
|
|
if ($comment->parent_id) { |
36
|
|
|
$comment->forceFill($comment->parent->only(['resource_id', 'resource_type'])); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$actor = new Stream_Actor($this->auth); |
40
|
|
|
|
41
|
|
|
$this->transaction(function () use ($comment, $dispatcher, $actor) { |
|
|
|
|
42
|
|
|
$comment->save(); |
43
|
|
|
$target = $this->target($comment); |
|
|
|
|
44
|
|
|
|
45
|
|
|
stream( |
46
|
|
|
$comment->wasRecentlyCreated ? new Stream_Create($actor) : new Stream_Update($actor), |
47
|
|
|
(new Stream_Comment())->comment($comment), |
|
|
|
|
48
|
|
|
(new $target)->map($comment->resource) |
49
|
|
|
); |
50
|
|
|
}); |
51
|
|
|
|
52
|
|
|
if ($comment->wasRecentlyCreated) { |
53
|
|
|
$subscribers = $comment |
54
|
|
|
->resource |
55
|
|
|
->subscribers() |
56
|
|
|
->with('user') |
57
|
|
|
->get() |
58
|
|
|
->pluck('user') // get all subscribers |
59
|
|
|
->exceptUser($this->auth); // exclude current logged user |
60
|
|
|
|
61
|
|
|
$dispatcher->send($subscribers, new CommentedNotification($comment)); |
|
|
|
|
62
|
|
|
|
63
|
|
|
// if ($comment->parent_id && $comment->user_id !== $comment->parent->user_id) { |
64
|
|
|
// $comment->parent->notify(new RepliedNotification($comment)); |
65
|
|
|
// } |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
CommentResource::withoutWrapping(); |
69
|
|
|
|
70
|
|
|
return new CommentResource($comment->load(['user', 'children'])); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function delete(Comment $comment) |
74
|
|
|
{ |
75
|
|
|
$this->authorize('delete', $comment); |
76
|
|
|
|
77
|
|
|
$this->transaction(function () use ($comment) { |
78
|
|
|
$comment->children->each(function ($child) { |
79
|
|
|
$child->delete(); |
80
|
|
|
}); |
81
|
|
|
|
82
|
|
|
$comment->delete(); |
83
|
|
|
$target = $this->target($comment); |
84
|
|
|
|
85
|
|
|
stream( |
86
|
|
|
Stream_Delete::class, |
87
|
|
|
(new Stream_Comment())->comment($comment), |
88
|
|
|
(new $target)->map($comment->resource) |
89
|
|
|
); |
90
|
|
|
}); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
private function target(Comment $comment): string |
94
|
|
|
{ |
95
|
|
|
return 'Coyote\\Services\\Stream\\Objects\\' . class_basename($comment->resource_type); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.