Skip to content

Commit 10aadaa

Browse files
committed
Merge branch 'develop' into dev-lb/gdpr-export
2 parents f0e7b10 + 37aae2b commit 10aadaa

File tree

21 files changed

+1687
-461
lines changed

21 files changed

+1687
-461
lines changed

.github/workflows/phpunit.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99

1010
strategy:
1111
matrix:
12-
php: [ 8.3 ]
12+
php: [ 8.3, 8.4 ]
1313
dependency-version: [ prefer-stable ]
1414

1515
steps:

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ WORKDIR /usr/src/trwl
99
RUN composer install --ignore-platform-reqs --no-interaction --no-progress --no-suggest --optimize-autoloader
1010
RUN php artisan optimize
1111

12-
FROM php:8.3.13-apache
12+
FROM php:8.4.1-apache
1313
ENV APACHE_DOCUMENT_ROOT=/var/www/html/public
1414

1515
RUN apt update && \

app/Http/Controllers/Backend/Social/MastodonController.php

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use App\Models\Status;
1111
use App\Models\User;
1212
use App\Notifications\MastodonNotSent;
13+
use App\Services\MastodonDomainExtractionService;
1314
use Error;
1415
use Exception;
1516
use GuzzleHttp\Exception\GuzzleException;
@@ -54,7 +55,7 @@ public static function getUserFromSocialite(SocialiteUser $socialiteUser, Mastod
5455
* @throws InvalidMastodonException
5556
*/
5657
public static function getMastodonServer(string $domain): ?MastodonServer {
57-
$domain = self::formatDomain($domain);
58+
$domain = (new MastodonDomainExtractionService())->formatDomain($domain);
5859

5960
$mastodonServer = MastodonServer::where('domain', $domain)->first();
6061

@@ -67,22 +68,6 @@ public static function getMastodonServer(string $domain): ?MastodonServer {
6768
return $mastodonServer ?? self::createMastodonServer($domain);
6869
}
6970

70-
public static function formatDomain(string $domain): string {
71-
$domain = strtolower($domain);
72-
73-
// remove leading usernames
74-
if (str_contains($domain, '@')) {
75-
$domain = last(explode('@', $domain));
76-
}
77-
78-
// Force HTTPS
79-
$domain = str_replace('http://', 'https://', $domain);
80-
if (!str_starts_with($domain, 'https://')) {
81-
$domain = 'https://' . $domain;
82-
}
83-
return $domain;
84-
}
85-
8671
/**
8772
* @param string $domain
8873
*

app/Http/Controllers/Frontend/Social/MastodonController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\Exceptions\SocialAuth\InvalidMastodonException;
66
use App\Http\Controllers\Backend\Social\MastodonController as MastodonBackend;
77
use App\Http\Controllers\Controller;
8+
use App\Services\MastodonDomainExtractionService;
89
use Carbon\Carbon;
910
use Exception;
1011
use Illuminate\Http\RedirectResponse;
@@ -25,7 +26,7 @@ class MastodonController extends Controller
2526
* @throws ValidationException
2627
*/
2728
public function redirect(Request $request): SympfonyRedirectResponse|RedirectResponse {
28-
$domain = MastodonBackend::formatDomain($request->input('domain') ?? '');
29+
$domain = (new MastodonDomainExtractionService)->formatDomain($request->input('domain') ?? '');
2930
$validator = Validator::make(['domain' => $domain], ['domain' => ['required', 'active_url']]);
3031
$validated = $validator->validate();
3132

app/Http/Controllers/FrontendUserController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function getProfilePage(string $username): View {
3030
$statuses = null;
3131
}
3232

33-
return view('profile', [
33+
return view('profile.profile', [
3434
'statuses' => $statuses,
3535
'user' => $user,
3636
]);
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace App\Services;
4+
5+
class MastodonDomainExtractionService
6+
{
7+
private const string HTTP_PREFIX = 'https://';
8+
private const string REPLACE_PREFIX = 'http://';
9+
10+
public function formatDomain(string $domain): string {
11+
$domain = strtolower(trim($domain));
12+
13+
if (empty($domain)) {
14+
return '';
15+
}
16+
17+
$domain = $this->removeProtocolFromUrl($domain);
18+
$domain = $this->removePathFromUrl($domain);
19+
$domain = $this->extractUrlFromUserHandleFormat($domain);
20+
21+
return $this->forceHttps($domain);
22+
}
23+
24+
private function extractUrlFromUserHandleFormat(string $domain): string {
25+
if (str_contains($domain, '@')) {
26+
$domain = last(explode('@', $domain));
27+
}
28+
29+
return $domain;
30+
}
31+
32+
private function removePathFromUrl(string $domain): string {
33+
return explode('/', $domain)[0];
34+
}
35+
36+
private function removeProtocolFromUrl(string $domain): string {
37+
return preg_replace('/^https?:\/\//', '', $domain);
38+
}
39+
40+
private function forceHttps(string $domain): string {
41+
$domain = str_replace(self::REPLACE_PREFIX, self::HTTP_PREFIX, $domain);
42+
if (!str_starts_with($domain, self::HTTP_PREFIX)) {
43+
$domain = self::HTTP_PREFIX . $domain;
44+
}
45+
46+
return $domain;
47+
}
48+
}

0 commit comments

Comments
 (0)