mirror of https://github.com/pixelfed/pixelfed
Merge branch 'dev' of github.com:pixelfed/pixelfed into jippi-fork
commit
890827d60e
@ -0,0 +1,118 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Models\CustomEmoji;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class ImportEmojis extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'import:emojis
|
||||
{path : Path to a tar.gz archive with the emojis}
|
||||
{--prefix : Define a prefix for the emjoi shortcode}
|
||||
{--suffix : Define a suffix for the emjoi shortcode}
|
||||
{--overwrite : Overwrite existing emojis}
|
||||
{--disabled : Import all emojis as disabled}';
|
||||
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Import emojis to the database';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$path = $this->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);
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use App\Services\UserRoleService;
|
||||
|
||||
class UserRolesController extends Controller
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
$this->middleware('auth');
|
||||
}
|
||||
|
||||
public function getRoles(Request $request)
|
||||
{
|
||||
$this->validate($request, [
|
||||
'id' => 'required'
|
||||
]);
|
||||
|
||||
return UserRoleService::getRoles($request->user()->id);
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use App\User;
|
||||
|
||||
class UserRoles extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $guarded = [];
|
||||
|
||||
protected $casts = [
|
||||
'roles' => 'array'
|
||||
];
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
}
|
@ -0,0 +1,119 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\UserRoles;
|
||||
|
||||
class UserRoleService
|
||||
{
|
||||
public static function can($action, $id, $useDefaultFallback = true)
|
||||
{
|
||||
$default = self::defaultRoles();
|
||||
$roles = self::get($id);
|
||||
return
|
||||
in_array($action, array_keys($roles)) ?
|
||||
$roles[$action] :
|
||||
(
|
||||
$useDefaultFallback ?
|
||||
$default[$action] :
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
public static function get($id)
|
||||
{
|
||||
if($roles = UserRoles::whereUserId($id)->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'
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('user_roles', function (Blueprint $table) {
|
||||
$table->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');
|
||||
}
|
||||
};
|
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('users', function (Blueprint $table) {
|
||||
$table->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');
|
||||
});
|
||||
}
|
||||
};
|
Loading…
Reference in New Issue