2019-11-23 05:34:56 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
use Cache;
|
|
|
|
use App\Profile;
|
2021-07-27 05:59:38 +00:00
|
|
|
use App\Status;
|
2019-11-23 05:34:56 +00:00
|
|
|
use App\Transformer\Api\AccountTransformer;
|
|
|
|
use League\Fractal;
|
|
|
|
use League\Fractal\Serializer\ArraySerializer;
|
2021-10-20 10:31:07 +00:00
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
use Illuminate\Support\Str;
|
2019-11-23 05:34:56 +00:00
|
|
|
|
2021-07-27 00:47:40 +00:00
|
|
|
class AccountService
|
|
|
|
{
|
2019-11-23 05:34:56 +00:00
|
|
|
const CACHE_KEY = 'pf:services:account:';
|
|
|
|
|
|
|
|
public static function get($id)
|
|
|
|
{
|
2021-02-01 07:47:54 +00:00
|
|
|
if($id > PHP_INT_MAX || $id < 1) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$key = self::CACHE_KEY . $id;
|
2021-07-27 00:47:40 +00:00
|
|
|
$ttl = now()->addHours(12);
|
2021-02-01 07:47:54 +00:00
|
|
|
|
|
|
|
return Cache::remember($key, $ttl, function() use($id) {
|
|
|
|
$fractal = new Fractal\Manager();
|
|
|
|
$fractal->setSerializer(new ArraySerializer());
|
2021-09-05 03:12:44 +00:00
|
|
|
$profile = Profile::findOrFail($id);
|
2021-02-01 07:47:54 +00:00
|
|
|
$resource = new Fractal\Resource\Item($profile, new AccountTransformer());
|
|
|
|
return $fractal->createData($resource)->toArray();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function del($id)
|
|
|
|
{
|
|
|
|
return Cache::forget(self::CACHE_KEY . $id);
|
2019-11-23 05:34:56 +00:00
|
|
|
}
|
|
|
|
|
2021-07-27 05:59:38 +00:00
|
|
|
public static function syncPostCount($id)
|
|
|
|
{
|
|
|
|
$profile = Profile::find($id);
|
|
|
|
|
|
|
|
if(!$profile) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$key = self::CACHE_KEY . 'pcs:' . $id;
|
|
|
|
|
|
|
|
if(Cache::has($key)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$count = Status::whereProfileId($id)
|
|
|
|
->whereNull('in_reply_to_id')
|
|
|
|
->whereNull('reblog_of_id')
|
|
|
|
->whereIn('scope', ['public', 'unlisted', 'private'])
|
|
|
|
->count();
|
|
|
|
|
|
|
|
$profile->status_count = $count;
|
|
|
|
$profile->save();
|
|
|
|
|
|
|
|
Cache::put($key, 1, 900);
|
|
|
|
return true;
|
|
|
|
}
|
2021-10-20 10:31:07 +00:00
|
|
|
|
|
|
|
public static function usernameToId($username)
|
|
|
|
{
|
|
|
|
$key = self::CACHE_KEY . 'u2id:' . hash('sha256', $username);
|
|
|
|
return Cache::remember($key, 900, function() use($username) {
|
|
|
|
$s = Str::of($username);
|
|
|
|
if($s->contains('@') && !$s->startsWith('@')) {
|
|
|
|
$username = "@{$username}";
|
|
|
|
}
|
|
|
|
$profile = DB::table('profiles')
|
|
|
|
->whereUsername($username)
|
|
|
|
->first();
|
|
|
|
if(!$profile) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return (string) $profile->id;
|
|
|
|
});
|
|
|
|
}
|
2021-07-27 00:47:40 +00:00
|
|
|
}
|