Skip to content

Commit 68a654f

Browse files
committed
Selectively show the additional metadata if it isn't in the display string at all
1 parent 88987fb commit 68a654f

File tree

5 files changed

+33
-7
lines changed

5 files changed

+33
-7
lines changed

app/Transformers/Api/Client/ActivityLogTransformer.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ public function transform(ActivityLog $model): array
2121
'event' => $model->event,
2222
'ip' => $model->ip,
2323
'description' => $model->description,
24-
'properties' => $model->properties,
24+
'properties' => $model->properties ? $model->properties->toArray() : [],
25+
'has_additional_metadata' => $this->hasAdditionalMetadata($model),
2526
'timestamp' => $model->timestamp->toIso8601String(),
2627
];
2728
}
@@ -34,4 +35,32 @@ public function includeActor(ActivityLog $model)
3435

3536
return $this->item($model->actor, $this->makeTransformer(UserTransformer::class), User::RESOURCE_NAME);
3637
}
38+
39+
/**
40+
* Determines if there are any log properties that we've not already exposed
41+
* in the response language string and that are not just the IP address or
42+
* the browser useragent.
43+
*
44+
* This is used by the front-end to selectively display an "additional metadata"
45+
* button that is pointless if there is nothing the user can't already see from
46+
* the event description.
47+
*/
48+
protected function hasAdditionalMetadata(ActivityLog $model): bool
49+
{
50+
if (is_null($model->properties) || $model->properties->isEmpty()) {
51+
return false;
52+
}
53+
54+
$str = trans('activity.' . str_replace(':', '.', $model->event));
55+
preg_match_all('/:(?<key>[\w-]+)(?:\W?|$)/', $str, $matches);
56+
57+
$exclude = array_merge($matches['key'], ['ip', 'useragent']);
58+
foreach ($model->properties->keys() as $key) {
59+
if (!in_array($key, $exclude, true)) {
60+
return true;
61+
}
62+
}
63+
64+
return false;
65+
}
3766
}

resources/scripts/api/definitions/user/models.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ interface ActivityLog extends Model<'actor'> {
2525
ip: string;
2626
description: string | null;
2727
properties: Record<string, string | unknown>;
28+
hasAdditionalMetadata: boolean;
2829
timestamp: Date;
2930
relationships: {
3031
actor: User | null;

resources/scripts/api/definitions/user/transformers.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export default class Transformers {
3636
ip: attributes.ip,
3737
description: attributes.description,
3838
properties: attributes.properties,
39+
hasAdditionalMetadata: attributes.has_additional_metadata ?? false,
3940
timestamp: new Date(attributes.timestamp),
4041
relationships: {
4142
actor: transform(actor as FractalResponseData, this.toUser, null),

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export default ({ activity, children }: Props) => {
7373
</Tooltip>
7474
</div>
7575
</div>
76-
<ActivityLogMetaButton meta={activity.properties}/>
76+
{activity.hasAdditionalMetadata && <ActivityLogMetaButton meta={activity.properties}/>}
7777
</div>
7878
</div>
7979
);

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
import React, { useState } from 'react';
2-
import { isEmptyObject } from '@/helpers';
32
import { ClipboardListIcon } from '@heroicons/react/outline';
43
import { Dialog } from '@/components/elements/dialog';
54
import { Button } from '@/components/elements/button/index';
65

76
export default ({ meta }: { meta: Record<string, unknown> }) => {
87
const [ open, setOpen ] = useState(false);
98

10-
if (isEmptyObject(meta)) {
11-
return null;
12-
}
13-
149
return (
1510
<div className={'self-center mx-4'}>
1611
<Dialog

0 commit comments

Comments
 (0)