Skip to content

Commit 868b6ea

Browse files
committed
fix & prevent errors on decrypt data
1 parent bb1d596 commit 868b6ea

File tree

6 files changed

+53
-12
lines changed

6 files changed

+53
-12
lines changed

resources/js/Components/Post/PostPreviewProviders.vue

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,13 @@ const previews = computed(() => {
5050
<template>
5151
<template v-if="selectedAccounts.length">
5252
<template v-for="preview in previews" :key="preview.id">
53-
<Alert v-if="preview.account.errors && preview.account.errors.length" variant="error" :closeable="false" class="mb-1">{{ preview.account.errors.join(',') }}</Alert>
53+
<Alert v-if="preview.account.errors && preview.account.errors.length"
54+
variant="error"
55+
:closeable="false"
56+
class="mb-1">
57+
<span v-html="preview.account.errors.join('</br>')"></span>
58+
</Alert>
59+
5460
<div class="mb-lg last:mb-0">
5561
<component :is="preview.providerComponent"
5662
:name="preview.account.name"

src/Actions/AccountPublishPost.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Inovector\Mixpost\Actions;
44

55
use Illuminate\Database\Eloquent\Collection;
6+
use Illuminate\Support\Facades\App;
67
use Inovector\Mixpost\Support\Log;
78
use Inovector\Mixpost\Facades\SocialProviderManager;
89
use Inovector\Mixpost\Models\Account;
@@ -25,9 +26,9 @@ public function __invoke(Account $account, Post $post): void
2526
$body = $this->cleanBody($content[0]['body']);
2627
$media = $this->collectMedia($content[0]['media']);
2728

28-
$provider = SocialProviderManager::connect($account->provider, $account->values())->useAccessToken($account->access_token->toArray());
29-
3029
try {
30+
$provider = SocialProviderManager::connect($account->provider, $account->values())->useAccessToken($account->access_token->toArray());
31+
3132
$response = $provider->publishPost($body, $media);
3233

3334
if (isset($response['errors'])) {
@@ -38,7 +39,7 @@ public function __invoke(Account $account, Post $post): void
3839
$this->insertProviderPostId($post, $account, $response['id']);
3940
}
4041
} catch (Exception $exception) {
41-
Log::error("Publish: {$exception->getMessage()}",
42+
Log::error($exception->getMessage(),
4243
[
4344
'account_id' => $account->id,
4445
'account_name' => $account->name,
@@ -50,6 +51,10 @@ public function __invoke(Account $account, Post $post): void
5051

5152
$errors = ['Unexpected internal error.'];
5253

54+
if (!App::environment('production')) {
55+
$errors[] = $exception->getMessage();
56+
}
57+
5358
$this->insertErrors($post, $account, $errors);
5459
}
5560
}

src/Casts/EncryptArrayObject.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Inovector\Mixpost\Casts;
4+
5+
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
6+
use Illuminate\Database\Eloquent\Casts\ArrayObject;
7+
use Illuminate\Support\Facades\Crypt;
8+
9+
class EncryptArrayObject implements CastsAttributes
10+
{
11+
public function get($model, $key, $value, $attributes): ?ArrayObject
12+
{
13+
if (isset($attributes[$key])) {
14+
return new ArrayObject(json_decode(Crypt::decryptString($attributes[$key]), true));
15+
}
16+
17+
return null;
18+
}
19+
20+
public function set($model, string $key, $value, array $attributes): ?array
21+
{
22+
if (!is_null($value)) {
23+
return [$key => Crypt::encryptString(json_encode($value))];
24+
}
25+
26+
return null;
27+
}
28+
}

src/Models/Account.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Inovector\Mixpost\Models;
44

5-
use Illuminate\Database\Eloquent\Casts\AsEncryptedArrayObject;
5+
use Inovector\Mixpost\Casts\EncryptArrayObject;
66
use Illuminate\Database\Eloquent\Factories\HasFactory;
77
use Illuminate\Database\Eloquent\Model;
88
use Illuminate\Support\Arr;
@@ -28,7 +28,7 @@ class Account extends Model
2828
protected $casts = [
2929
'media' => AccountMediaCast::class,
3030
'data' => 'array',
31-
'access_token' => AsEncryptedArrayObject::class
31+
'access_token' => EncryptArrayObject::class
3232
];
3333

3434
protected $hidden = [

src/Models/Service.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Inovector\Mixpost\Models;
44

5-
use Illuminate\Database\Eloquent\Casts\AsEncryptedArrayObject;
5+
use Inovector\Mixpost\Casts\EncryptArrayObject;
66
use Illuminate\Database\Eloquent\Factories\HasFactory;
77
use Illuminate\Database\Eloquent\Model;
88
use Inovector\Mixpost\Facades\Services as ServicesFacade;
@@ -19,7 +19,7 @@ class Service extends Model
1919
];
2020

2121
protected $casts = [
22-
'credentials' => AsEncryptedArrayObject::class
22+
'credentials' => EncryptArrayObject::class
2323
];
2424

2525
protected $hidden = [

src/Services.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,21 @@ public function put(string $name, array $value): void
6060

6161
public function get(string $name, null|string $credentialKey = null)
6262
{
63-
$value = $this->getFromCache($name, function () use ($name) {
63+
$defaultPayload = Arr::get($this->form(), $name, []);
64+
65+
$value = $this->getFromCache($name, function () use ($name, $defaultPayload) {
6466
$dbRecord = Service::where('name', $name)->first();
6567

6668
try {
67-
$payload = $dbRecord ? $dbRecord->credentials->toArray() : Arr::get($this->form(), $name, []);
69+
$payload = $dbRecord ? $dbRecord->credentials->toArray() : $defaultPayload;
6870

6971
$this->put($name, $payload);
7072

7173
return $payload;
7274
} catch (DecryptException $exception) {
7375
$this->logDecryptionError($name, $exception);
7476

75-
return [];
77+
return $defaultPayload;
7678
}
7779
});
7880

@@ -82,7 +84,7 @@ public function get(string $name, null|string $credentialKey = null)
8284
} catch (DecryptException $exception) {
8385
$this->logDecryptionError($name, $exception);
8486

85-
$value = [];
87+
$value = $defaultPayload;
8688
}
8789
}
8890

0 commit comments

Comments
 (0)