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;
|
2021-12-05 00:27:58 +00:00
|
|
|
use App\UserSetting;
|
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:';
|
|
|
|
|
2021-11-04 05:29:12 +00:00
|
|
|
public static function get($id, $softFail = false)
|
2019-11-23 05:34:56 +00:00
|
|
|
{
|
2021-12-05 00:27:58 +00:00
|
|
|
return Cache::remember(self::CACHE_KEY . $id, 43200, function() use($id, $softFail) {
|
2021-02-01 07:47:54 +00:00
|
|
|
$fractal = new Fractal\Manager();
|
|
|
|
$fractal->setSerializer(new ArraySerializer());
|
2021-11-04 05:29:12 +00:00
|
|
|
$profile = Profile::find($id);
|
|
|
|
if(!$profile) {
|
|
|
|
if($softFail) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
abort(404);
|
|
|
|
}
|
2021-02-01 07:47:54 +00:00
|
|
|
$resource = new Fractal\Resource\Item($profile, new AccountTransformer());
|
|
|
|
return $fractal->createData($resource)->toArray();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-01-03 07:53:15 +00:00
|
|
|
public static function getMastodon($id, $softFail = false)
|
|
|
|
{
|
|
|
|
$account = self::get($id, $softFail);
|
|
|
|
if(!$account) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-01-06 04:36:02 +00:00
|
|
|
if(config('exp.emc') == false) {
|
|
|
|
return $account;
|
|
|
|
}
|
|
|
|
|
2022-01-03 07:53:15 +00:00
|
|
|
unset(
|
|
|
|
$account['header_bg'],
|
|
|
|
$account['is_admin'],
|
|
|
|
$account['last_fetched_at'],
|
|
|
|
$account['local'],
|
|
|
|
$account['location'],
|
|
|
|
$account['note_text'],
|
|
|
|
$account['pronouns'],
|
|
|
|
$account['website']
|
|
|
|
);
|
|
|
|
|
|
|
|
$account['avatar_static'] = $account['avatar'];
|
|
|
|
$account['bot'] = false;
|
|
|
|
$account['emojis'] = [];
|
|
|
|
$account['fields'] = [];
|
|
|
|
$account['header'] = url('/storage/headers/missing.png');
|
|
|
|
$account['header_static'] = url('/storage/headers/missing.png');
|
|
|
|
$account['last_status_at'] = null;
|
|
|
|
|
|
|
|
return $account;
|
|
|
|
}
|
|
|
|
|
2021-02-01 07:47:54 +00:00
|
|
|
public static function del($id)
|
|
|
|
{
|
|
|
|
return Cache::forget(self::CACHE_KEY . $id);
|
2019-11-23 05:34:56 +00:00
|
|
|
}
|
|
|
|
|
2021-12-05 00:27:58 +00:00
|
|
|
public static function settings($id)
|
|
|
|
{
|
|
|
|
$settings = UserSetting::whereUserId($id)->first();
|
|
|
|
if(!$settings) {
|
|
|
|
return self::defaultSettings();
|
|
|
|
}
|
|
|
|
return collect($settings)
|
|
|
|
->filter(function($item, $key) {
|
|
|
|
return in_array($key, array_keys(self::defaultSettings())) == true;
|
|
|
|
})
|
|
|
|
->map(function($item, $key) {
|
|
|
|
if($key == 'compose_settings') {
|
|
|
|
$cs = self::defaultSettings()['compose_settings'];
|
2021-12-31 06:11:46 +00:00
|
|
|
$ms = is_array($item) ? $item : [];
|
|
|
|
return array_merge($cs, $ms);
|
2021-12-05 00:27:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if($key == 'other') {
|
|
|
|
$other = self::defaultSettings()['other'];
|
2021-12-31 06:11:46 +00:00
|
|
|
$mo = is_array($item) ? $item : [];
|
|
|
|
return array_merge($other, $mo);
|
2021-12-05 00:27:58 +00:00
|
|
|
}
|
|
|
|
return $item;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function canEmbed($id)
|
|
|
|
{
|
|
|
|
return self::settings($id)['other']['disable_embeds'] == false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function defaultSettings()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'crawlable' => true,
|
|
|
|
'public_dm' => false,
|
|
|
|
'reduce_motion' => false,
|
|
|
|
'high_contrast_mode' => false,
|
|
|
|
'video_autoplay' => false,
|
|
|
|
'show_profile_follower_count' => true,
|
|
|
|
'show_profile_following_count' => true,
|
|
|
|
'compose_settings' => [
|
|
|
|
'default_scope' => 'public',
|
|
|
|
'default_license' => 1,
|
|
|
|
'media_descriptions' => false
|
|
|
|
],
|
|
|
|
'other' => [
|
|
|
|
'advanced_atom' => false,
|
|
|
|
'disable_embeds' => false,
|
|
|
|
'mutual_mention_notifications' => false,
|
|
|
|
'hide_collections' => false,
|
|
|
|
'hide_like_counts' => false,
|
|
|
|
'hide_groups' => false,
|
|
|
|
'hide_stories' => false,
|
|
|
|
'disable_cw' => false,
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|