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
1 change: 1 addition & 0 deletions dist/build/assets/cp-CYSYRrr0.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion dist/build/assets/cp-peOtejVL.js

This file was deleted.

2 changes: 1 addition & 1 deletion dist/build/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
]
},
"resources/js/cp.js": {
"file": "assets/cp-peOtejVL.js",
"file": "assets/cp-CYSYRrr0.js",
"name": "cp",
"src": "resources/js/cp.js",
"isEntry": true
Expand Down
27 changes: 8 additions & 19 deletions resources/js/cp.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
Statamic.booted(() => {
let frontConfig = Statamic.$config.get('front');
const frontConfig = Statamic.$config.get('front');

if (frontConfig && frontConfig.chatId) {
var script = document.createElement("script");
script.src = 'https://chat-assets.frontapp.com/v1/chat.bundle.js';

// add it to the end of the head section of the page (could change 'head' to 'body' to add it to the end of the body section instead)
document.body.appendChild(script);
script.addEventListener('load', () => {
window.FrontChat(
'init',
{
chatId: frontConfig.chatId,
email: frontConfig.email,
name: frontConfig.name,
userHash: frontConfig.hash,
useDefaultLauncher: true
}
);
});
if (! frontConfig) {
return;
}

const script = document.createElement('script');
script.src = 'https://chat-assets.frontapp.com/v1/chat.bundle.js';
script.addEventListener('load', () => window.FrontChat('init', { ...frontConfig, useDefaultLauncher: true }));
document.body.appendChild(script);
});
18 changes: 0 additions & 18 deletions resources/views/head.antlers.html

This file was deleted.

6 changes: 0 additions & 6 deletions routes/actions.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/ChatConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public static function forCurrentUser(): array
return [
'chatId' => config('front.chat.id'),
'email' => $email,
'hash' => $email ? hash_hmac('sha256', $email, config('front.secret_key')) : null,
'name' => User::current()?->name(),
'userHash' => $email ? hash_hmac('sha256', $email, config('front.secret_key')) : null,
];
}
}
24 changes: 0 additions & 24 deletions src/Http/Controllers/ConfigController.php

This file was deleted.

4 changes: 0 additions & 4 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@

class ServiceProvider extends AddonServiceProvider
{
protected $routes = [
'actions' => __DIR__.'/../routes/actions.php',
];

protected $vite = [
'input' => [
'resources/css/cp.css',
Expand Down
34 changes: 34 additions & 0 deletions src/Tags/Front.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace TransformStudios\Front\Tags;

use Illuminate\Support\Js;
use Statamic\Support\Arr;
use Statamic\Tags\Tags;
use TransformStudios\Front\ChatConfig;

class Front extends Tags
{
public function chat(): string
{
if (! $this->isConfigured()) {
return '';
}

$options = Js::from(Arr::removeNullValues(ChatConfig::forCurrentUser()));

return <<<HTML
<script src="https://chat-assets.frontapp.com/v1/chat.bundle.js"></script>
<script>
window.addEventListener('load', () => window.FrontChat('init', { ...{$options}, useDefaultLauncher: true }));
</script>
HTML;
}

private function isConfigured(): bool
{
return config('front.chat.show_on_front_end')
&& config('front.chat.id')
&& config('front.secret_key');
}
}
42 changes: 0 additions & 42 deletions tests/Http/Controllers/ConfigControllerTest.php

This file was deleted.

2 changes: 1 addition & 1 deletion tests/ServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
expect(Statamic::jsonVariables(request())['front'])->toBe([
'chatId' => 'chat-id',
'email' => 'erin@transformstudios.com',
'hash' => hash_hmac('sha256', 'erin@transformstudios.com', 'secret'),
'name' => 'Erin',
'userHash' => hash_hmac('sha256', 'erin@transformstudios.com', 'secret'),
]);
});

Expand Down
43 changes: 43 additions & 0 deletions tests/Tags/FrontTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

use Statamic\Facades\Antlers;
use Statamic\Facades\User;

beforeEach(function () {
config()->set('front.secret_key', 'secret');
config()->set('front.chat.id', 'chat-id');
config()->set('front.chat.show_on_front_end', true);
});

it('renders the chat script for a guest', function () {
$html = (string) Antlers::parse('{{ front:chat }}', trusted: true);

expect($html)
->toContain('https://chat-assets.frontapp.com/v1/chat.bundle.js')
->toContain('chatId')
->toContain('chat-id')
->not->toContain('userHash');
});

it('identifies the current user', function () {
$user = tap(User::make()->email('erin@transformstudios.com')->set('name', 'Erin'))->save();

$this->actingAs($user);

expect((string) Antlers::parse('{{ front:chat }}', trusted: true))
->toContain('erin@transformstudios.com')
->toContain('Erin')
->toContain(hash_hmac('sha256', 'erin@transformstudios.com', 'secret'));
});

it('renders nothing when hidden on the front end', function () {
config()->set('front.chat.show_on_front_end', false);

expect((string) Antlers::parse('{{ front:chat }}', trusted: true))->toBe('');
});

it('renders nothing without a chat id', function () {
config()->set('front.chat.id', null);

expect((string) Antlers::parse('{{ front:chat }}', trusted: true))->toBe('');
});
Loading