diff --git a/CHANGELOG.md b/CHANGELOG.md index 10b13f261..13baca035 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -84,6 +84,7 @@ - Update AdminShadowFilter, fix deleted profile bug ([a492a95a](https://github.com/pixelfed/pixelfed/commit/a492a95a)) - Update FollowerService, add $silent param to remove method to more efficently purge relationships ([1664a5bc](https://github.com/pixelfed/pixelfed/commit/1664a5bc)) - Update AP ProfileTransformer, add published attribute ([adfaa2b1](https://github.com/pixelfed/pixelfed/commit/adfaa2b1)) +- Update meta tags, improve descriptions and seo/og tags ([fd44c80c](https://github.com/pixelfed/pixelfed/commit/fd44c80c)) - ([](https://github.com/pixelfed/pixelfed/commit/)) ## [v0.11.9 (2023-08-21)](https://github.com/pixelfed/pixelfed/compare/v0.11.8...v0.11.9) diff --git a/app/Console/Commands/ImportEmojis.php b/app/Console/Commands/ImportEmojis.php new file mode 100644 index 000000000..77a0c29a4 --- /dev/null +++ b/app/Console/Commands/ImportEmojis.php @@ -0,0 +1,118 @@ +argument('path'); + + if (!file_exists($path) || !mime_content_type($path) == 'application/x-tar') { + $this->error('Path does not exist or is not a tarfile'); + return Command::FAILURE; + } + + $imported = 0; + $skipped = 0; + $failed = 0; + + $tar = new \PharData($path); + $tar->decompress(); + + foreach (new \RecursiveIteratorIterator($tar) as $entry) { + $this->line("Processing {$entry->getFilename()}"); + if (!$entry->isFile() || !$this->isImage($entry) || !$this->isEmoji($entry->getPathname())) { + $failed++; + continue; + } + + $filename = pathinfo($entry->getFilename(), PATHINFO_FILENAME); + $extension = pathinfo($entry->getFilename(), PATHINFO_EXTENSION); + + // Skip macOS shadow files + if (str_starts_with($filename, '._')) { + continue; + } + + $shortcode = implode('', [ + $this->option('prefix'), + $filename, + $this->option('suffix'), + ]); + + $customEmoji = CustomEmoji::whereShortcode($shortcode)->first(); + + if ($customEmoji && !$this->option('overwrite')) { + $skipped++; + continue; + } + + $emoji = $customEmoji ?? new CustomEmoji(); + $emoji->shortcode = $shortcode; + $emoji->domain = config('pixelfed.domain.app'); + $emoji->disabled = $this->option('disabled'); + $emoji->save(); + + $fileName = $emoji->id . '.' . $extension; + Storage::putFileAs('public/emoji', $entry->getPathname(), $fileName); + $emoji->media_path = 'emoji/' . $fileName; + $emoji->save(); + $imported++; + Cache::forget('pf:custom_emoji'); + } + + $this->line("Imported: {$imported}"); + $this->line("Skipped: {$skipped}"); + $this->line("Failed: {$failed}"); + + //delete file + unlink(str_replace('.tar.gz', '.tar', $path)); + + return Command::SUCCESS; + } + + private function isImage($file) + { + $image = getimagesize($file->getPathname()); + return $image !== false; + } + + private function isEmoji($filename) + { + $allowedMimeTypes = ['image/png', 'image/jpeg', 'image/webp']; + $mimeType = mime_content_type($filename); + + return in_array($mimeType, $allowedMimeTypes); + } +} diff --git a/app/Http/Controllers/Api/ApiV1Controller.php b/app/Http/Controllers/Api/ApiV1Controller.php index 798d9ee55..c1dd8cbf4 100644 --- a/app/Http/Controllers/Api/ApiV1Controller.php +++ b/app/Http/Controllers/Api/ApiV1Controller.php @@ -98,6 +98,7 @@ use App\Jobs\MediaPipeline\MediaSyncLicensePipeline; use App\Services\DiscoverService; use App\Services\CustomEmojiService; use App\Services\MarkerService; +use App\Services\UserRoleService; use App\Models\Conversation; use App\Jobs\FollowPipeline\FollowAcceptPipeline; use App\Jobs\FollowPipeline\FollowRejectPipeline; @@ -1244,6 +1245,7 @@ class ApiV1Controller extends Controller abort_if(!$request->user(), 403); $user = $request->user(); + abort_if($user->has_roles && !UserRoleService::can('can-like', $user->id), 403, 'Invalid permissions for this action'); AccountService::setLastActive($user->id); @@ -1305,6 +1307,7 @@ class ApiV1Controller extends Controller abort_if(!$request->user(), 403); $user = $request->user(); + abort_if($user->has_roles && !UserRoleService::can('can-like', $user->id), 403, 'Invalid permissions for this action'); AccountService::setLastActive($user->id); @@ -1623,6 +1626,8 @@ class ApiV1Controller extends Controller ]); $user = $request->user(); + abort_if($user->has_roles && !UserRoleService::can('can-post', $user->id), 403, 'Invalid permissions for this action'); + AccountService::setLastActive($user->id); if($user->last_active_at == null) { @@ -1792,6 +1797,7 @@ class ApiV1Controller extends Controller abort_if(!$request->user(), 403); $user = $request->user(); + abort_if($user->has_roles && !UserRoleService::can('can-post', $user->id), 403, 'Invalid permissions for this action'); AccountService::setLastActive($user->id); $media = Media::whereUserId($user->id) @@ -1831,6 +1837,7 @@ class ApiV1Controller extends Controller ]); $user = $request->user(); + abort_if($user->has_roles && !UserRoleService::can('can-post', $user->id), 403, 'Invalid permissions for this action'); if($user->last_active_at == null) { return []; @@ -2419,8 +2426,13 @@ class ApiV1Controller extends Controller $max = $request->input('max_id'); $limit = $request->input('limit') ?? 20; $user = $request->user(); + $remote = $request->has('remote'); $local = $request->has('local'); + $userRoleKey = $remote ? 'can-view-network-feed' : 'can-view-public-feed'; + if($user->has_roles && !UserRoleService::can($userRoleKey, $user->id)) { + return []; + } $filtered = $user ? UserFilterService::filters($user->profile_id) : []; AccountService::setLastActive($user->id); $domainBlocks = UserFilterService::domainBlocks($user->profile_id); @@ -3165,6 +3177,7 @@ class ApiV1Controller extends Controller abort_if(!$request->user(), 403); $user = $request->user(); + abort_if($user->has_roles && !UserRoleService::can('can-share', $user->id), 403, 'Invalid permissions for this action'); AccountService::setLastActive($user->id); $status = Status::whereScope('public')->findOrFail($id); @@ -3212,6 +3225,7 @@ class ApiV1Controller extends Controller abort_if(!$request->user(), 403); $user = $request->user(); + abort_if($user->has_roles && !UserRoleService::can('can-share', $user->id), 403, 'Invalid permissions for this action'); AccountService::setLastActive($user->id); $status = Status::whereScope('public')->findOrFail($id); @@ -3262,6 +3276,13 @@ class ApiV1Controller extends Controller '_pe' => 'sometimes' ]); + $user = $request->user(); + abort_if( + $user->has_roles && !UserRoleService::can('can-view-hashtag-feed', $user->id), + 403, + 'Invalid permissions for this action' + ); + if(config('database.default') === 'pgsql') { $tag = Hashtag::where('name', 'ilike', $hashtag) ->orWhere('slug', 'ilike', $hashtag) diff --git a/app/Http/Controllers/ComposeController.php b/app/Http/Controllers/ComposeController.php index 9be50f346..e79625861 100644 --- a/app/Http/Controllers/ComposeController.php +++ b/app/Http/Controllers/ComposeController.php @@ -54,6 +54,7 @@ use App\Util\Lexer\Autolink; use App\Util\Lexer\Extractor; use App\Util\Media\License; use Image; +use App\Services\UserRoleService; class ComposeController extends Controller { @@ -92,6 +93,7 @@ class ComposeController extends Controller $user = Auth::user(); $profile = $user->profile; + abort_if($user->has_roles && !UserRoleService::can('can-post', $user->id), 403, 'Invalid permissions for this action'); $limitKey = 'compose:rate-limit:media-upload:' . $user->id; $limitTtl = now()->addMinutes(15); @@ -184,6 +186,7 @@ class ComposeController extends Controller ]); $user = Auth::user(); + abort_if($user->has_roles && !UserRoleService::can('can-post', $user->id), 403, 'Invalid permissions for this action'); $limitKey = 'compose:rate-limit:media-updates:' . $user->id; $limitTtl = now()->addMinutes(15); diff --git a/app/Http/Controllers/UserRolesController.php b/app/Http/Controllers/UserRolesController.php new file mode 100644 index 000000000..65a71d19d --- /dev/null +++ b/app/Http/Controllers/UserRolesController.php @@ -0,0 +1,23 @@ +middleware('auth'); + } + + public function getRoles(Request $request) + { + $this->validate($request, [ + 'id' => 'required' + ]); + + return UserRoleService::getRoles($request->user()->id); + } +} diff --git a/app/Models/UserRoles.php b/app/Models/UserRoles.php new file mode 100644 index 000000000..8d289971a --- /dev/null +++ b/app/Models/UserRoles.php @@ -0,0 +1,23 @@ + 'array' + ]; + + public function user() + { + return $this->belongsTo(User::class); + } +} diff --git a/app/Providers/AuthServiceProvider.php b/app/Providers/AuthServiceProvider.php index 0ec8c1895..8e1a6a98d 100644 --- a/app/Providers/AuthServiceProvider.php +++ b/app/Providers/AuthServiceProvider.php @@ -14,7 +14,7 @@ class AuthServiceProvider extends ServiceProvider * @var array */ protected $policies = [ - 'App\Model' => 'App\Policies\ModelPolicy', + // 'App\Model' => 'App\Policies\ModelPolicy', ]; /** diff --git a/app/Services/AccountService.php b/app/Services/AccountService.php index 98e878845..5ffc1e9b5 100644 --- a/app/Services/AccountService.php +++ b/app/Services/AccountService.php @@ -13,6 +13,7 @@ use League\Fractal; use League\Fractal\Serializer\ArraySerializer; use Illuminate\Support\Facades\DB; use Illuminate\Support\Str; +use \NumberFormatter; class AccountService { @@ -244,4 +245,38 @@ class AccountService return UserDomainBlock::whereProfileId($pid)->whereDomain($domain)->exists(); } + + public static function formatNumber($num) { + if(!$num || $num < 1) { + return "0"; + } + $num = intval($num); + $formatter = new NumberFormatter('en_US', NumberFormatter::DECIMAL); + $formatter->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, 1); + + if ($num >= 1000000000) { + return $formatter->format($num / 1000000000) . 'B'; + } else if ($num >= 1000000) { + return $formatter->format($num / 1000000) . 'M'; + } elseif ($num >= 1000) { + return $formatter->format($num / 1000) . 'K'; + } else { + return $formatter->format($num); + } + } + + public static function getMetaDescription($id) + { + $account = self::get($id, true); + + if(!$account) return ""; + + $posts = self::formatNumber($account['statuses_count']) . ' Posts, '; + $following = self::formatNumber($account['following_count']) . ' Following, '; + $followers = self::formatNumber($account['followers_count']) . ' Followers'; + $note = $account['note'] && strlen($account['note']) ? + ' · ' . \Purify::clean(strip_tags(str_replace("\n", '', str_replace("\r", '', $account['note'])))) : + ''; + return $posts . $following . $followers . $note; + } } diff --git a/app/Services/UserRoleService.php b/app/Services/UserRoleService.php new file mode 100644 index 000000000..500a4666e --- /dev/null +++ b/app/Services/UserRoleService.php @@ -0,0 +1,119 @@ +first()) { + return $roles->roles; + } + + return self::defaultRoles(); + } + + public static function roleKeys() + { + return array_keys(self::defaultRoles()); + } + + public static function defaultRoles() + { + return [ + 'account-force-private' => true, + 'account-ignore-follow-requests' => true, + + 'can-view-public-feed' => true, + 'can-view-network-feed' => true, + 'can-view-discover' => true, + 'can-view-hashtag-feed' => false, + + 'can-post' => true, + 'can-comment' => true, + 'can-like' => true, + 'can-share' => true, + + 'can-follow' => false, + 'can-make-public' => false, + ]; + } + + public static function getRoles($id) + { + $myRoles = self::get($id); + $roleData = collect(self::roleData()) + ->map(function($role, $k) use($myRoles) { + $role['value'] = $myRoles[$k]; + return $role; + }) + ->toArray(); + return $roleData; + } + + public static function roleData() + { + return [ + 'account-force-private' => [ + 'title' => 'Force Private Account', + 'action' => 'Prevent changing account from private' + ], + 'account-ignore-follow-requests' => [ + 'title' => 'Ignore Follow Requests', + 'action' => 'Hide follow requests and associated notifications' + ], + 'can-view-public-feed' => [ + 'title' => 'Hide Public Feed', + 'action' => 'Hide the public feed timeline' + ], + 'can-view-network-feed' => [ + 'title' => 'Hide Network Feed', + 'action' => 'Hide the network feed timeline' + ], + 'can-view-discover' => [ + 'title' => 'Hide Discover', + 'action' => 'Hide the discover feature' + ], + 'can-post' => [ + 'title' => 'Can post', + 'action' => 'Allows new posts to be shared' + ], + 'can-comment' => [ + 'title' => 'Can comment', + 'action' => 'Allows new comments to be posted' + ], + 'can-like' => [ + 'title' => 'Can Like', + 'action' => 'Allows the ability to like posts and comments' + ], + 'can-share' => [ + 'title' => 'Can Share', + 'action' => 'Allows the ability to share posts and comments' + ], + 'can-follow' => [ + 'title' => 'Can Follow', + 'action' => 'Allows the ability to follow accounts' + ], + 'can-make-public' => [ + 'title' => 'Can make account public', + 'action' => 'Allows the ability to make account public' + ], + ]; + } +} diff --git a/app/Util/Webfinger/Webfinger.php b/app/Util/Webfinger/Webfinger.php index 879103332..c900358e6 100644 --- a/app/Util/Webfinger/Webfinger.php +++ b/app/Util/Webfinger/Webfinger.php @@ -4,43 +4,60 @@ namespace App\Util\Webfinger; class Webfinger { - protected $user; - protected $subject; - protected $aliases; - protected $links; + protected $user; + protected $subject; + protected $aliases; + protected $links; - public function __construct($user) - { - $this->subject = 'acct:'.$user->username.'@'.parse_url(config('app.url'), PHP_URL_HOST); - $this->aliases = [ - $user->url(), - $user->permalink(), - ]; - $this->links = [ - [ - 'rel' => 'http://webfinger.net/rel/profile-page', - 'type' => 'text/html', - 'href' => $user->url(), - ], - [ - 'rel' => 'http://schemas.google.com/g/2010#updates-from', - 'type' => 'application/atom+xml', - 'href' => $user->permalink('.atom'), - ], - [ - 'rel' => 'self', - 'type' => 'application/activity+json', - 'href' => $user->permalink(), - ], - ]; - } + public function __construct($user) + { + $avatar = $user ? $user->avatarUrl() : url('/storage/avatars/default.jpg'); + $avatarPath = parse_url($avatar, PHP_URL_PATH); + $extension = pathinfo($avatarPath, PATHINFO_EXTENSION); + $mimeTypes = [ + 'jpg' => 'image/jpeg', + 'jpeg' => 'image/jpeg', + 'png' => 'image/png', + 'gif' => 'image/gif', + 'svg' => 'image/svg', + ]; + $avatarType = $mimeTypes[$extension] ?? 'application/octet-stream'; - public function generate() - { - return [ - 'subject' => $this->subject, - 'aliases' => $this->aliases, - 'links' => $this->links, - ]; - } + $this->subject = 'acct:'.$user->username.'@'.parse_url(config('app.url'), PHP_URL_HOST); + $this->aliases = [ + $user->url(), + $user->permalink(), + ]; + $this->links = [ + [ + 'rel' => 'http://webfinger.net/rel/profile-page', + 'type' => 'text/html', + 'href' => $user->url(), + ], + [ + 'rel' => 'http://schemas.google.com/g/2010#updates-from', + 'type' => 'application/atom+xml', + 'href' => $user->permalink('.atom'), + ], + [ + 'rel' => 'self', + 'type' => 'application/activity+json', + 'href' => $user->permalink(), + ], + [ + 'rel' => 'http://webfinger.net/rel/avatar', + 'type' => $avatarType, + 'href' => $avatar, + ], + ]; + } + + public function generate() + { + return [ + 'subject' => $this->subject, + 'aliases' => $this->aliases, + 'links' => $this->links, + ]; + } } diff --git a/database/migrations/2023_12_27_081801_create_user_roles_table.php b/database/migrations/2023_12_27_081801_create_user_roles_table.php new file mode 100644 index 000000000..59b8ab390 --- /dev/null +++ b/database/migrations/2023_12_27_081801_create_user_roles_table.php @@ -0,0 +1,31 @@ +id(); + $table->unsignedBigInteger('profile_id')->unique()->index(); + $table->unsignedInteger('user_id')->unique()->index(); + $table->json('roles')->nullable(); + $table->json('meta')->nullable(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('user_roles'); + } +}; diff --git a/database/migrations/2023_12_27_082024_add_has_roles_to_users_table.php b/database/migrations/2023_12_27_082024_add_has_roles_to_users_table.php new file mode 100644 index 000000000..09246e37b --- /dev/null +++ b/database/migrations/2023_12_27_082024_add_has_roles_to_users_table.php @@ -0,0 +1,32 @@ +boolean('has_roles')->default(false); + $table->unsignedInteger('parent_id')->nullable(); + $table->tinyInteger('role_id')->unsigned()->nullable()->index(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('users', function (Blueprint $table) { + $table->dropColumn('has_roles'); + $table->dropColumn('parent_id'); + $table->dropColumn('role_id'); + }); + } +}; diff --git a/resources/assets/js/components/NotificationCard.vue b/resources/assets/js/components/NotificationCard.vue index b2230cbca..5c6eb6da5 100644 --- a/resources/assets/js/components/NotificationCard.vue +++ b/resources/assets/js/components/NotificationCard.vue @@ -34,7 +34,16 @@

- {{n.account.local == false ? '@':''}}{{truncate(n.account.username)}} commented on your post. + {{n.account.local == false ? '@':''}}{{truncate(n.account.username)}} commented on your + + post. + + + + + + post. +

@@ -64,7 +73,16 @@

- {{n.account.local == false ? '@':''}}{{truncate(n.account.username)}} shared your post. + {{n.account.local == false ? '@':''}}{{truncate(n.account.username)}} shared your + + post. + + + + + + post. +

diff --git a/resources/views/layouts/app.blade.php b/resources/views/layouts/app.blade.php index 94f90ed97..0136842bb 100644 --- a/resources/views/layouts/app.blade.php +++ b/resources/views/layouts/app.blade.php @@ -12,9 +12,9 @@ {{ $title ?? config_cache('app.name') }} - - - + + + @stack('meta') @@ -73,9 +73,9 @@ {{ $title ?? config('app.name', 'Pixelfed') }} - - - + + + @stack('meta') diff --git a/resources/views/profile/show.blade.php b/resources/views/profile/show.blade.php index 5107c0ab3..dfa8edabb 100644 --- a/resources/views/profile/show.blade.php +++ b/resources/views/profile/show.blade.php @@ -1,4 +1,13 @@ -@extends('layouts.app',['title' => $profile->username . " on " . config('app.name')]) +@extends('layouts.app', [ + 'title' => $profile->name . ' (@' . $acct . ') - Pixelfed', + 'ogTitle' => $profile->name . ' (@' . $acct . ')', + 'ogType' => 'profile' +]) + +@php +$acct = $profile->username . '@' . config('pixelfed.domain.app'); +$metaDescription = \App\Services\AccountService::getMetaDescription($profile->id); +@endphp @section('content') @if (session('error')) @@ -8,9 +17,6 @@ @endif -@if($profile->website) -{{$profile->website}} -@endif
- + @endsection -@push('meta') - - - - - - @if($status->viewType() == "video" || $status->viewType() == "video:album") - - @endif +@push('meta')@if($mediaCount && $s['pf_type'] === "photo" || $s['pf_type'] === "photo:album") + + @elseif($mediaCount && $s['pf_type'] === "video" || $s['pf_type'] === "video:album") + @endif + + + + + @endpush @push('scripts')