Skip to content

Commit 95de4c3

Browse files
committed
Abuse the translation engine to handle more of the formatting for us
1 parent b052d29 commit 95de4c3

File tree

4 files changed

+94
-51
lines changed

4 files changed

+94
-51
lines changed

app/Transformers/Api/Client/ActivityLogTransformer.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Pterodactyl\Transformers\Api\Client;
44

5+
use Illuminate\Support\Str;
56
use Pterodactyl\Models\User;
67
use Pterodactyl\Models\ActivityLog;
78

@@ -22,7 +23,7 @@ public function transform(ActivityLog $model): array
2223
'is_api' => !is_null($model->api_key_id),
2324
'ip' => $model->ip,
2425
'description' => $model->description,
25-
'properties' => $model->properties ? $model->properties->toArray() : [],
26+
'properties' => $this->properties($model),
2627
'has_additional_metadata' => $this->hasAdditionalMetadata($model),
2728
'timestamp' => $model->timestamp->toIso8601String(),
2829
];
@@ -37,6 +38,33 @@ public function includeActor(ActivityLog $model)
3738
return $this->item($model->actor, $this->makeTransformer(UserTransformer::class), User::RESOURCE_NAME);
3839
}
3940

41+
/**
42+
* Transforms any array values in the properties into a countable field for easier
43+
* use within the translation outputs.
44+
*/
45+
protected function properties(ActivityLog $model): array
46+
{
47+
if (!$model->properties || $model->properties->isEmpty()) {
48+
return [];
49+
}
50+
51+
$properties = $model->properties
52+
->mapWithKeys(function ($value, $key) {
53+
if (!is_array($value)) {
54+
return [$key => $value];
55+
}
56+
57+
return [$key => $value, "{$key}_count" => count($value)];
58+
});
59+
60+
$keys = $properties->keys()->filter(fn ($key) => Str::endsWith($key, '_count'))->values();
61+
if ($keys->containsOneItem()) {
62+
$properties = $properties->merge(['count' => $properties->get($keys[0])])->except($keys[0]);
63+
}
64+
65+
return $properties->toArray();
66+
}
67+
4068
/**
4169
* Determines if there are any log properties that we've not already exposed
4270
* in the response language string and that are not just the IP address or

resources/lang/en/activity.php

Lines changed: 48 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,20 @@
1515
'checkpoint' => 'Two-factor authentication requested',
1616
'recovery-token' => 'Used two-factor recovery token',
1717
'token' => 'Solved two-factor challenge',
18-
'ip-blocked' => 'Blocked request from unlisted IP address for <strong>:identifier</strong>',
18+
'ip-blocked' => 'Blocked request from unlisted IP address for :identifier',
1919
],
2020
'user' => [
2121
'account' => [
22-
'email-changed' => 'Changed email from <strong>:old</strong> to <strong>:new</strong>',
22+
'email-changed' => 'Changed email from :old to :new',
2323
'password-changed' => 'Changed password',
2424
],
2525
'api-key' => [
26-
'create' => 'Created new API key <strong>:identifier</strong>',
27-
'delete' => 'Deleted API key <strong>:identifier</strong>',
26+
'create' => 'Created new API key :identifier',
27+
'delete' => 'Deleted API key :identifier',
2828
],
2929
'ssh-key' => [
30-
'create' => 'Added SSH key <strong>:fingerprint</strong> to account',
31-
'delete' => 'Removed SSH key <strong>:fingerprint</strong> from account',
30+
'create' => 'Added SSH key :fingerprint to account',
31+
'delete' => 'Removed SSH key :fingerprint from account',
3232
],
3333
'two-factor' => [
3434
'create' => 'Enabled two-factor auth',
@@ -37,64 +37,66 @@
3737
],
3838
'server' => [
3939
'backup' => [
40-
'download' => 'Downloaded the <strong>:name</strong> backup',
41-
'delete' => 'Deleted the <strong>:name</strong> backup',
42-
'restore' => 'Restored the <strong>:name</strong> backup (deleted files: :truncate)',
43-
'restore-complete' => 'Completed restoration of the <strong>:name</strong> backup',
44-
'restore-failed' => 'Failed to complete restoration of the <strong>:name</strong> backup',
45-
'start' => 'Started a new backup <strong>:name</strong>',
46-
'complete' => 'Marked the <strong>:name</strong> backup as complete',
47-
'fail' => 'Marked the <strong>:name</strong> backup as failed',
48-
'lock' => 'Locked the <strong>:name</strong> backup',
49-
'unlock' => 'Unlocked the <strong>:name</strong> backup',
40+
'download' => 'Downloaded the :name backup',
41+
'delete' => 'Deleted the :name backup',
42+
'restore' => 'Restored the :name backup (deleted files: :truncate)',
43+
'restore-complete' => 'Completed restoration of the :name backup',
44+
'restore-failed' => 'Failed to complete restoration of the :name backup',
45+
'start' => 'Started a new backup :name',
46+
'complete' => 'Marked the :name backup as complete',
47+
'fail' => 'Marked the :name backup as failed',
48+
'lock' => 'Locked the :name backup',
49+
'unlock' => 'Unlocked the :name backup',
5050
],
5151
'database' => [
52-
'create' => 'Created new database <strong>:name</strong>',
53-
'rotate-password' => 'Password rotated for database <strong>:name</strong>',
54-
'delete' => 'Deleted database <strong>:name</strong>',
52+
'create' => 'Created new database :name',
53+
'rotate-password' => 'Password rotated for database :name',
54+
'delete' => 'Deleted database :name',
5555
],
5656
'file' => [
57-
'compress' => 'Created new file archive of files in <strong>:directory</strong>',
58-
'read' => 'Viewed the contents of <strong>:file</strong>',
59-
'copy' => 'Created a copy of <strong>:file</strong>',
60-
'create-directory' => 'Created a new directory <strong>:name</strong> in <strong>:directory</strong>',
61-
'decompress' => 'Decompressed a file archive in <strong>:directory</strong>',
62-
'delete' => 'Deleted files in <strong>:directory</strong>',
63-
'download' => 'Downloaded <strong>:file</strong>',
64-
'pull' => 'Downloaded a remote file from :url to <strong>:directory</strong>',
65-
'rename' => 'Renamed files in <strong>:directory</strong>',
66-
'write' => 'Wrote new content to <strong>:file</strong>',
57+
'compress_one' => 'Compressed :directory/:file',
58+
'compress_other' => 'Compressed :count files in :directory',
59+
'read' => 'Viewed the contents of :file',
60+
'copy' => 'Created a copy of :file',
61+
'create-directory' => 'Created a new directory :name in :directory',
62+
'decompress' => 'Decompressed :files in :directory',
63+
'delete_one' => 'Deleted :directory/:files',
64+
'delete_other' => 'Deleted :count files in :directory',
65+
'download' => 'Downloaded :file',
66+
'pull' => 'Downloaded a remote file from :url to :directory',
67+
'rename' => 'Renamed files in :directory',
68+
'write' => 'Wrote new content to :file',
6769
'upload' => 'Began a file upload',
6870
],
6971
'allocation' => [
70-
'create' => 'Added <strong>:allocation</strong> to the server',
71-
'notes' => 'Updated the notes for <strong>:allocation</strong> from ":old" to ":new"',
72-
'primary' => 'Set <strong>:allocation</strong> as the primary server allocation',
73-
'delete' => 'Deleted the <strong>:allocation</strong> allocation',
72+
'create' => 'Added :allocation to the server',
73+
'notes' => 'Updated the notes for :allocation from ":old" to ":new"',
74+
'primary' => 'Set :allocation as the primary server allocation',
75+
'delete' => 'Deleted the :allocation allocation',
7476
],
7577
'schedule' => [
76-
'store' => 'Created the <strong>:name</strong> schedule',
77-
'update' => 'Updated the <strong>:name</strong> schedule',
78-
'execute' => 'Manually executed the <strong>:name</strong> schedule',
79-
'delete' => 'Deleted the <strong>:name</strong> schedule',
78+
'store' => 'Created the :name schedule',
79+
'update' => 'Updated the :name schedule',
80+
'execute' => 'Manually executed the :name schedule',
81+
'delete' => 'Deleted the :name schedule',
8082
],
8183
'task' => [
82-
'create' => 'Created a new ":action" task for the <strong>:name</strong> schedule',
83-
'update' => 'Updated the ":action" task for the <strong>:name</strong> schedule',
84-
'delete' => 'Deleted a task for the <strong>:name</strong> schedule',
84+
'create' => 'Created a new ":action" task for the :name schedule',
85+
'update' => 'Updated the ":action" task for the :name schedule',
86+
'delete' => 'Deleted a task for the :name schedule',
8587
],
8688
'settings' => [
87-
'rename' => 'Renamed the server from <strong>:old</strong> to <strong>:new</strong>',
89+
'rename' => 'Renamed the server from :old to :new',
8890
'reinstall' => 'Triggered a server reinstall',
8991
],
9092
'startup' => [
91-
'edit' => 'Edited the <strong>:variable</strong> startup variable for the server from ":old" to ":new"',
92-
'image' => 'Updated the Docker Image for the server from <strong>:old</strong> to <strong>:new</strong>',
93+
'edit' => 'Changed the :variable variable from ":old" to ":new"',
94+
'image' => 'Updated the Docker Image for the server from :old to :new',
9395
],
9496
'subuser' => [
95-
'create' => 'Added <strong>:email</strong> as a subuser',
96-
'update' => 'Updated the subuser permissions for <strong>:email</strong>',
97-
'delete' => 'Removed <strong>:email</strong> as a subuser',
97+
'create' => 'Added :email as a subuser',
98+
'update' => 'Updated the subuser permissions for :email',
99+
'delete' => 'Removed :email as a subuser',
98100
],
99101
],
100102
];

resources/scripts/components/elements/activity/ActivityLogEntry.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ export default ({ activity, children }: Props) => {
2727
return current.toString();
2828
};
2929

30+
const properties = Object.keys(activity.properties).reduce((obj, key) => ({
31+
...obj,
32+
[key]: key === 'count' || key.endsWith('_count')
33+
? activity.properties[key]
34+
: `<strong>${activity.properties[key]}</strong>`,
35+
}), {});
36+
3037
return (
3138
<div className={'grid grid-cols-10 py-4 border-b-2 border-gray-800 last:rounded-b last:border-0 group'}>
3239
<div className={'hidden sm:flex sm:col-span-1 items-center justify-center select-none'}>
@@ -60,10 +67,8 @@ export default ({ activity, children }: Props) => {
6067
{children}
6168
</div>
6269
</div>
63-
<p className={'mt-1 text-sm break-words line-clamp-2 pr-4'}>
64-
<Translate ns={'activity'} values={activity.properties}>
65-
{activity.event.replace(':', '.')}
66-
</Translate>
70+
<p className={style.description}>
71+
<Translate ns={'activity'} values={properties} i18nKey={activity.event.replace(':', '.')}/>
6772
</p>
6873
<div className={'mt-1 flex items-center text-sm'}>
6974
<Link

resources/scripts/components/elements/activity/style.module.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,11 @@
99
@apply w-4 h-4;
1010
}
1111
}
12+
13+
.description {
14+
@apply mt-1 text-sm break-words line-clamp-2 pr-4;
15+
16+
& strong {
17+
@apply text-gray-50 font-semibold;
18+
}
19+
}

0 commit comments

Comments
 (0)