|
7 | 7 | use Ushahidi\Modules\V5\Actions\Post\Handlers\AbstractPostQueryHandler;
|
8 | 8 | use Ushahidi\Modules\V5\Actions\Post\Queries\FindPostByIdQuery;
|
9 | 9 | use Ushahidi\Modules\V5\Repository\Post\PostRepository;
|
10 |
| -use Ushahidi\Modules\V5\Models\Post\Post; |
11 |
| -use Ushahidi\Modules\V5\Models\Contact; |
12 |
| -use Illuminate\Support\Collection; |
13 |
| -use Ushahidi\Modules\V5\Http\Resources\PostValueCollection; |
14 |
| -use Ushahidi\Modules\V5\Http\Resources\ContactPointerResource; |
15 |
| -use Ushahidi\Modules\V5\Http\Resources\MessagePointerResource; |
16 |
| -use Ushahidi\Modules\V5\Http\Resources\LockCollection; |
17 |
| -use Ushahidi\Modules\V5\Http\Resources\Survey\TaskCollection; |
18 |
| -use Ushahidi\Contracts\Sources; |
| 10 | +use Ushahidi\Modules\V5\Actions\Post\HandlePostOnlyParameters; |
19 | 11 |
|
20 | 12 | class FindPostByIdQueryHandler extends AbstractPostQueryHandler
|
21 | 13 | {
|
| 14 | + use HandlePostOnlyParameters; |
22 | 15 | private $postRepository;
|
23 | 16 |
|
24 | 17 | public function __construct(PostRepository $postRepository)
|
@@ -52,140 +45,7 @@ public function __invoke(Action $action)
|
52 | 45 | $post,
|
53 | 46 | array_diff($action->getFieldsForRelationship(), $action->getFields())
|
54 | 47 | );
|
| 48 | + $post = $this->handleSourceField($post); |
55 | 49 | return $this->hideUnwantedRelationships($post, $action->getHydrates());
|
56 | 50 | }
|
57 |
| - |
58 |
| - private function addHydrateRelationships(Post $post, array $hydrates) |
59 |
| - { |
60 |
| - foreach ($hydrates as $hydrate) { |
61 |
| - switch ($hydrate) { |
62 |
| - case 'color': |
63 |
| - $post->color = $post->survey ? $post->survey->color : null; |
64 |
| - break; |
65 |
| - case 'categories': |
66 |
| - // $result['categories'] = $post->categories; |
67 |
| - break; |
68 |
| - case 'sets': |
69 |
| - // $post->sets = $post->sets->pluck('id'); |
70 |
| - break; |
71 |
| - case 'completed_stages': |
72 |
| - $post->completed_stages = $post->postStages; |
73 |
| - |
74 |
| - break; |
75 |
| - case 'post_content': |
76 |
| - $post->post_content = $this->getResourcePostContent($post); |
77 |
| - break; |
78 |
| - case 'translations': |
79 |
| - break; |
80 |
| - case 'contact': |
81 |
| - $post->contact = null; |
82 |
| - if ($post->source === Sources::WHATSAPP) { |
83 |
| - if ($this->userHasManagePostPermissions()) { |
84 |
| - if (isset($post->metadata['contact'])) { |
85 |
| - // $post->contact = Contact::find($post->contact_id); // not good in case there are many whatsapp posts this will be problem for the DB |
86 |
| - $post->contact = (new Contact)->fill($post->metadata['contact']); |
87 |
| - } |
88 |
| - } else { |
89 |
| - if (isset($post->metadata['contact']['id'])) { |
90 |
| - $post->contact = (new Contact)->fill(['id'=>$post->metadata['contact']['id']]); |
91 |
| - } |
92 |
| - } |
93 |
| - } elseif ($post->message) { |
94 |
| - if ($this->userHasManagePostPermissions()) { |
95 |
| - $post->contact = $post->message->contact; |
96 |
| - } else { |
97 |
| - $post->contact = $post->message->contact->setVisible(["id"]); |
98 |
| - } |
99 |
| - } |
100 |
| - break; |
101 |
| - case 'locks': |
102 |
| - $post->locks = new LockCollection($post->locks); |
103 |
| - break; |
104 |
| - |
105 |
| - case 'source': |
106 |
| - $message = $post->message; |
107 |
| - $post->source = $post->source ?? ($message && isset($message->type) |
108 |
| - ? $message->type |
109 |
| - : Post::DEFAULT_SOURCE_TYPE); |
110 |
| - |
111 |
| - break; |
112 |
| - |
113 |
| - case 'data_source_message_id': |
114 |
| - $post->data_source_message_id = null; |
115 |
| - $message = $post->message; |
116 |
| - if ($message) { |
117 |
| - $post->data_source_message_id = $message->data_source_message_id ?? null; |
118 |
| - } |
119 |
| - break; |
120 |
| - case 'message': |
121 |
| - if ($post->message && !$this->userHasManagePostPermissions()) { |
122 |
| - $post->message->makeHidden("contact"); |
123 |
| - } |
124 |
| - break; |
125 |
| - case 'enabled_languages': |
126 |
| - $post->enabled_languages = [ |
127 |
| - 'default' => $post->base_language, |
128 |
| - 'available' => $post->translations->groupBy('language')->keys() |
129 |
| - ]; |
130 |
| - $relations['enabled_languages'] = true; |
131 |
| - break; |
132 |
| - } |
133 |
| - } |
134 |
| - return $post; |
135 |
| - } |
136 |
| - private function hideFieldsUsedByRelationships(Post $post, array $fields = []) |
137 |
| - { |
138 |
| - foreach ($fields as $field) { |
139 |
| - $post->offsetUnset($field); |
140 |
| - } |
141 |
| - $post->offsetUnset('values_int'); |
142 |
| - |
143 |
| - return $post; |
144 |
| - } |
145 |
| - private function hideUnwantedRelationships(Post $post, array $hydrates) |
146 |
| - { |
147 |
| - // hide post_content relationships |
148 |
| - $post->makeHidden('survey'); |
149 |
| - $post->makeHidden('valuesVarchar'); |
150 |
| - $post->makeHidden('valuesInt'); |
151 |
| - $post->makeHidden('valuesText'); |
152 |
| - $post->makeHidden('valuesDatetime'); |
153 |
| - $post->makeHidden('valuesDecimal'); |
154 |
| - $post->makeHidden('valuesDecimal'); |
155 |
| - $post->makeHidden('valuesGeometry'); |
156 |
| - $post->makeHidden('valuesMarkdown'); |
157 |
| - $post->makeHidden('valuesMedia'); |
158 |
| - $post->makeHidden('valuesPoint'); |
159 |
| - $post->makeHidden('valuesRelation'); |
160 |
| - $post->makeHidden('valuesPostsMedia'); |
161 |
| - $post->makeHidden('valuesPostsSet'); |
162 |
| - $post->makeHidden('valuesPostTag'); |
163 |
| - |
164 |
| - // hide source relationships |
165 |
| - if (!in_array('message', $hydrates)) { |
166 |
| - $post->makeHidden('message'); |
167 |
| - } |
168 |
| - |
169 |
| - // hide completed_stages relationships |
170 |
| - $post->makeHidden('postStages'); |
171 |
| - |
172 |
| - |
173 |
| - return $post; |
174 |
| - } |
175 |
| - |
176 |
| - private function getResourcePostContent($post) |
177 |
| - { |
178 |
| - $values = $post->getPostValues(); // Calling method on Post Model |
179 |
| - $no_values = $values->count() === 0 ? true : false; |
180 |
| - $col = new Collection([ |
181 |
| - 'values' => $values, |
182 |
| - 'tasks' => $post->survey ? $post->survey->tasks : [] |
183 |
| - ]); |
184 |
| - if ($post->survey) { |
185 |
| - $post_content = new PostValueCollection($col); |
186 |
| - } else { |
187 |
| - $post_content = Collection::make([]); |
188 |
| - } |
189 |
| - return $post_content; |
190 |
| - } |
191 | 51 | }
|
0 commit comments