Update ApiV1Controller, add custom_emoji endpoint

This commit is contained in:
Daniel Supernault 2022-01-25 02:08:50 -07:00
parent d1ff86ee34
commit 16e725183e
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7
3 changed files with 30 additions and 2 deletions

View file

@ -360,6 +360,7 @@ class AdminController extends Controller
if($request->has('cc')) { if($request->has('cc')) {
Cache::forget('pf:admin:custom_emoji:stats'); Cache::forget('pf:admin:custom_emoji:stats');
Cache::forget('pf:custom_emoji');
return redirect(route('admin.custom-emoji')); return redirect(route('admin.custom-emoji'));
} }
@ -463,6 +464,7 @@ class AdminController extends Controller
$request->emoji->storeAs('public/emoji', $fileName); $request->emoji->storeAs('public/emoji', $fileName);
$emoji->media_path = 'emoji/' . $fileName; $emoji->media_path = 'emoji/' . $fileName;
$emoji->save(); $emoji->save();
Cache::forget('pf:custom_emoji');
return redirect(route('admin.custom-emoji')); return redirect(route('admin.custom-emoji'));
} }
@ -471,6 +473,7 @@ class AdminController extends Controller
abort_unless(config('federation.custom_emoji.enabled'), 404); abort_unless(config('federation.custom_emoji.enabled'), 404);
$emoji = CustomEmoji::findOrFail($id); $emoji = CustomEmoji::findOrFail($id);
Storage::delete("public/{$emoji->media_path}"); Storage::delete("public/{$emoji->media_path}");
Cache::forget('pf:custom_emoji');
$emoji->delete(); $emoji->delete();
return redirect(route('admin.custom-emoji')); return redirect(route('admin.custom-emoji'));
} }

View file

@ -77,6 +77,7 @@ use App\Util\Localization\Localization;
use App\Util\Media\License; use App\Util\Media\License;
use App\Jobs\MediaPipeline\MediaSyncLicensePipeline; use App\Jobs\MediaPipeline\MediaSyncLicensePipeline;
use App\Services\DiscoverService; use App\Services\DiscoverService;
use App\Services\CustomEmojiService;
class ApiV1Controller extends Controller class ApiV1Controller extends Controller
{ {
@ -920,7 +921,7 @@ class ApiV1Controller extends Controller
*/ */
public function customEmojis() public function customEmojis()
{ {
return response()->json([]); return response(CustomEmojiService::all())->header('Content-Type', 'application/json');
} }
/** /**

View file

@ -71,8 +71,8 @@ class CustomEmojiService
$emoji->save(); $emoji->save();
$name = str_replace(':', '', $json['name']); $name = str_replace(':', '', $json['name']);
Cache::forget('pf:custom_emoji');
Cache::forget('pf:custom_emoji:' . $name); Cache::forget('pf:custom_emoji:' . $name);
if($id) { if($id) {
StatusService::del($id); StatusService::del($id);
} }
@ -104,4 +104,28 @@ class CustomEmojiService
return true; return true;
} }
public static function all()
{
return Cache::rememberForever('pf:custom_emoji', function() {
$pgsql = config('database.default') === 'pgsql';
return CustomEmoji::when(!$pgsql, function($q, $pgsql) {
return $q->groupBy('shortcode');
})
->get()
->map(function($emojo) {
$url = url('storage/' . $emojo->media_path);
return [
'shortcode' => str_replace(':', '', $emojo->shortcode),
'url' => $url,
'static_path' => $url,
'visible_in_picker' => $emojo->disabled == false
];
})
->when($pgsql, function($collection) {
return $collection->unique('shortcode');
})
->toJson(JSON_UNESCAPED_SLASHES);
});
}
} }