Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fleetbase/core-api",
"version": "1.5.32",
"version": "1.6.0",
"description": "Core Framework and Resources for Fleetbase API",
"keywords": [
"fleetbase",
Expand Down
36 changes: 26 additions & 10 deletions src/Http/Controllers/Api/v1/ChatChannelController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,28 @@ class ChatChannelController extends Controller
public function create(CreateChatChannelRequest $request)
{
// get request input
$input = $request->only(['name']);
$name = $request->input('name');
$participants = $request->array('participants');

// create the chat channel
$chatChannel = ChatChannel::create([
'company_uuid' => session('company'),
'created_by_uuid' => session('user'),
'name' => strtoupper($input['name']),
'name' => $name,
]);

// If participants provided add them
foreach ($participants as $userId) {
$user = User::where('public_id', $userId)->first();
if ($user) {
ChatParticipant::create([
'company_uuid' => session('company'),
'user_uuid' => $user->uuid,
'chat_channel_uuid' => $chatChannel->uuid,
]);
}
}

// response the driver resource
return new ChatChannelResource($chatChannel);
}
Expand Down Expand Up @@ -134,20 +147,23 @@ public function delete($id)
}

/**
* Query for Fleetbase Chat Channel resources.
* Query for available chat participants.
*
* @return \Fleetbase\Http\Resources\ChatChannelCollection
*/
public function getAvailablePartificants($id)
public function getAvailablePartificants(Request $request)
{
$chatChannel = ChatChannel::findRecordOrFail($id);
$users = User::where('company_uuid', session('company'))->get();
$chatChannelId = $request->input('channel');
$chatChannel = $chatChannelId ? ChatChannel::where('public_id', $chatChannelId)->first() : null;
$users = User::where('company_uuid', session('company'))->get();

$users->filter(function ($user) use ($chatChannel) {
$isPartificant = $chatChannel->participants->firstWhere('user_uuid', $user->uuid);
if ($chatChannel) {
$users = $users->filter(function ($user) use ($chatChannel) {
$isParticipant = $chatChannel->participants->firstWhere('user_uuid', $user->uuid);

return !$isPartificant;
});
return !$isParticipant;
});
}

return UserResource::collection($users);
}
Expand Down
166 changes: 166 additions & 0 deletions src/Http/Controllers/Api/v1/CommentController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
<?php

namespace Fleetbase\Http\Controllers\Api\v1;

use Fleetbase\Http\Controllers\Controller;
use Fleetbase\Http\Requests\CreateCommentRequest;
use Fleetbase\Http\Requests\UpdateCommentRequest;
use Fleetbase\Http\Resources\Comment as CommentResource;
use Fleetbase\Http\Resources\DeletedResource;
use Fleetbase\Models\Comment;
use Fleetbase\Support\Utils;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\Request;

class CommentController extends Controller
{
/**
* Creates a new Fleetbase Comment resource.
*
* @return CommentResource
*/
public function create(CreateCommentRequest $request)
{
$content = $request->input('content');
$subject = $request->input('subject', [
'id' => $request->input('subject_id'),
'type' => $request->input('subject_type'),
]);
$parent = $request->input('parent');

// Prepare comment creation data
$data = [
'company_uuid' => session('company'),
'author_uuid' => session('user'),
'content' => $content,
];

// Resolve the parent
$parentComment = null;
if ($parent) {
$parentComment = Comment::where(['public_id' => $parent, 'company_uuid' => session('company')])->first();
if ($parentComment) {
$data['parent_comment_uuid'] = $parentComment->uuid;
$data['subject_uuid'] = $parentComment->subject_uuid;
$data['subject_type'] = $parentComment->subject_type;
}
}

// Resolve the subject
if ($subject && !$parentComment) {
$subjectClass = Utils::getMutationType(data_get($subject, 'type'));
$subjectUuid = null;
if ($subjectClass) {
$subjectUuid = Utils::getUuid(app($subjectClass)->getTable(), [
'public_id' => data_get($subject, 'id'),
'company_uuid' => session('company'),
]);
}

// If on subject found
if ((!$subjectClass || !$subjectUuid) && !$parentComment) {
return response()->apiError('Invalid subject provided for comment.');
}

$data['subject_uuid'] = $subjectUuid;
$data['subject_type'] = $subjectClass;
}

// create the comment
try {
$comment = Comment::publish($data);
} catch (\Throwable $e) {
return response()->apiError('Uknown error attempting to create comment.');
}

// response the new comment
return new CommentResource($comment);
}

/**
* Updates a Fleetbase Comment resource.
*
* @param string $id
*
* @return CommentResource
*/
public function update($id, UpdateCommentRequest $request)
{
// find for the comment
try {
$comment = Comment::findRecordOrFail($id);
} catch (ModelNotFoundException $exception) {
return response()->json(
[
'error' => 'Comment resource not found.',
],
404
);
}

try {
$content = $request->input('content');
$comment->update(['content' => $content]);
} catch (\Throwable $e) {
return response()->apiError('Uknown error attempting to update comment.');
}

// response the comment resource
return new CommentResource($comment);
}

/**
* Query for Fleetbase Comment resources.
*
* @return \Fleetbase\Http\Resources\CommentResourceCollection
*/
public function query(Request $request)
{
$results = Comment::queryWithRequest($request);

return CommentResource::collection($results);
}

/**
* Finds a single Fleetbase Comment resources.
*
* @return \Fleetbase\Http\Resources\CommentCollection
*/
public function find($id)
{
// find for the Comment
try {
$comment = Comment::findRecordOrFail($id);
} catch (ModelNotFoundException $exception) {
return response()->apiError('Comment resource not found.', 404);
} catch (\Throwable $e) {
return response()->apiError('Uknown error occured trying to find the comment.', 404);
}

// response the comment resource
return new CommentResource($comment);
}

/**
* Deletes a Fleetbase Comment resources.
*
* @return DeletedResource
*/
public function delete($id)
{
// find for the comment
try {
$comment = Comment::findRecordOrFail($id);
} catch (ModelNotFoundException $exception) {
return response()->apiError('Comment resource not found.', 404);
} catch (\Throwable $e) {
return response()->apiError('Uknown error occured trying to find the comment.', 404);
}

// delete the comment
$comment->delete();

// response the comment resource
return new DeletedResource($comment);
}
}
8 changes: 8 additions & 0 deletions src/Http/Filter/CommentFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ public function queryForPublic()
$this->queryForInternal();
}

public function subject(string $id)
{
$this->builder->whereHas('subject', function ($query) use ($id) {
$query->where('uuid', $id);
$query->orWhere('public_id', $id);
});
}

public function parent(string $id)
{
if (Str::isUuid($id)) {
Expand Down
7 changes: 5 additions & 2 deletions src/Http/Middleware/LogApiRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Fleetbase\Http\Middleware;

use Fleetbase\Jobs\LogApiRequest;
use Fleetbase\Support\Http;
use Fleetbase\Traits\CustomMiddleware;

class LogApiRequests
Expand All @@ -23,8 +24,10 @@ public function handle($request, \Closure $next)
// get the response
$response = $next($request);

// log api request
$this->logApiRequest($request, $response);
// Only log public api request - do not log internal requests
if (Http::isPublicRequest($request)) {
$this->logApiRequest($request, $response);
}

return $response;
}
Expand Down
42 changes: 42 additions & 0 deletions src/Http/Requests/CreateCommentRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Fleetbase\Http\Requests;

use Illuminate\Validation\Rule;

class CreateCommentRequest extends FleetbaseRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return request()->session()->has('api_credential');
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'subject' => [Rule::requiredIf(function () {
return !$this->filled('subject_id') && !$this->filled('subject_type') && !$this->filled('parent') && $this->isMethod('POST');
})],
'subject_id' => [Rule::requiredIf(function () {
return !$this->filled('parent') && !$this->filled('subject') && $this->isMethod('POST');
})],
'subject_type' => [Rule::requiredIf(function () {
return !$this->filled('parent') && !$this->filled('subject') && $this->isMethod('POST');
})],
'parent' => [Rule::requiredIf(function () {
return !$this->filled('subject') && !$this->filled('subject_type') && !$this->filled('subject_id') && $this->isMethod('POST');
})],
'content' => ['required'],
];
}
}
7 changes: 7 additions & 0 deletions src/Http/Requests/UpdateCommentRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Fleetbase\Http\Requests;

class UpdateCommentRequest extends CreateCommentRequest
{
}
2 changes: 1 addition & 1 deletion src/Http/Resources/ChatChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function toArray($request)
'public_id' => $this->when(Http::isInternalRequest(), $this->public_id),
'company_uuid' => $this->when(Http::isInternalRequest(), $this->company_uuid),
'created_by_uuid' => $this->when(Http::isInternalRequest(), $this->created_by_uuid),
'created_by' => $this->when(Http::isInternalRequest(), $this->createdBy ? $this->createdBy->public_id : null),
'created_by' => $this->when(Http::isPublicRequest(), fn () => $this->createdBy ? $this->createdBy->public_id : null),
'name' => $this->name,
'title' => $this->title,
'last_message' => new ChatMessage($this->last_message),
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Resources/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function toArray($request)
'meta' => $this->meta,
'author' => new Author($this->author),
'replies' => static::collection($this->replies),
'editable' => $this->when(Http::isInternalRequest(), $request->hasSession() && session('user') === $this->author_uuid),
'editable' => $request->hasSession() && session('user') === $this->author_uuid,
'updated_at' => $this->updated_at,
'created_at' => $this->created_at,
'deleted_at' => $this->deleted_at,
Expand Down
2 changes: 1 addition & 1 deletion src/Models/ChatMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class ChatMessage extends Model
*/
public function sender()
{
return $this->belongsTo(ChatParticipant::class, 'sender_uuid', 'uuid');
return $this->belongsTo(ChatParticipant::class, 'sender_uuid', 'uuid')->withTrashed();
}

/**
Expand Down
36 changes: 36 additions & 0 deletions src/Models/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,42 @@ public static function createFromUpload(UploadedFile $file, $path, $type = null,
return static::create($data);
}

public static function createFromBase64(string $base64, ?string $fileName = null, string $path = 'uploads', ?string $type = 'image', ?string $contentType = 'image/png', ?int $size = null, ?string $disk = null, ?string $bucket = null): bool|File
{
$disk = is_null($disk) ? config('filesystems.default') : $disk;
$bucket = is_null($bucket) ? config('filesystems.disks.' . $disk . '.bucket', config('filesystems.disks.s3.bucket')) : $bucket;
$size = is_null($size) ? Utils::getBase64ImageSize($base64) : $size;
$fileName = is_null($fileName) ? static::randomFileName() : $fileName;

// Correct $path for uploads
if (Str::startsWith($path, 'uploads') && $disk === 'uploads') {
$path = str_replace('uploads/', '', $path);
}

// Set the full file path
$fullPath = $path . '/' . $fileName;
$uploaded = Storage::disk($disk)->put($fullPath, base64_decode($base64));
if (!$uploaded) {
return false;
}

// Prepare file data
$data = [
'company_uuid' => session('company'),
'uploader_uuid' => session('user'),
'disk' => $disk,
'original_filename' => basename($fullPath),
'extension' => 'png',
'content_type' => $contentType,
'path' => $fullPath,
'bucket' => $bucket,
'type' => $type,
'size' => $size,
];

return static::create($data);
}

/**
* Retrieves the hash name of the file based on its path.
*
Expand Down
Loading