mirror of
https://github.com/pixelfed/pixelfed.git
synced 2024-11-22 14:31:26 +00:00
Update AccountService, add setLastActive method
This commit is contained in:
parent
85839b220a
commit
ebbd98e743
1 changed files with 195 additions and 181 deletions
|
@ -15,209 +15,223 @@ use Illuminate\Support\Str;
|
||||||
|
|
||||||
class AccountService
|
class AccountService
|
||||||
{
|
{
|
||||||
const CACHE_KEY = 'pf:services:account:';
|
const CACHE_KEY = 'pf:services:account:';
|
||||||
|
|
||||||
public static function get($id, $softFail = false)
|
public static function get($id, $softFail = false)
|
||||||
{
|
{
|
||||||
$res = Cache::remember(self::CACHE_KEY . $id, 43200, function() use($id) {
|
$res = Cache::remember(self::CACHE_KEY . $id, 43200, function() use($id) {
|
||||||
$fractal = new Fractal\Manager();
|
$fractal = new Fractal\Manager();
|
||||||
$fractal->setSerializer(new ArraySerializer());
|
$fractal->setSerializer(new ArraySerializer());
|
||||||
$profile = Profile::find($id);
|
$profile = Profile::find($id);
|
||||||
if(!$profile || $profile->status === 'delete') {
|
if(!$profile || $profile->status === 'delete') {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
$resource = new Fractal\Resource\Item($profile, new AccountTransformer());
|
$resource = new Fractal\Resource\Item($profile, new AccountTransformer());
|
||||||
return $fractal->createData($resource)->toArray();
|
return $fractal->createData($resource)->toArray();
|
||||||
});
|
});
|
||||||
|
|
||||||
if(!$res) {
|
if(!$res) {
|
||||||
return $softFail ? null : abort(404);
|
return $softFail ? null : abort(404);
|
||||||
}
|
}
|
||||||
return $res;
|
return $res;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getMastodon($id, $softFail = false)
|
public static function getMastodon($id, $softFail = false)
|
||||||
{
|
{
|
||||||
$account = self::get($id, $softFail);
|
$account = self::get($id, $softFail);
|
||||||
if(!$account) {
|
if(!$account) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(config('exp.emc') == false) {
|
if(config('exp.emc') == false) {
|
||||||
return $account;
|
return $account;
|
||||||
}
|
}
|
||||||
|
|
||||||
unset(
|
unset(
|
||||||
$account['header_bg'],
|
$account['header_bg'],
|
||||||
$account['is_admin'],
|
$account['is_admin'],
|
||||||
$account['last_fetched_at'],
|
$account['last_fetched_at'],
|
||||||
$account['local'],
|
$account['local'],
|
||||||
$account['location'],
|
$account['location'],
|
||||||
$account['note_text'],
|
$account['note_text'],
|
||||||
$account['pronouns'],
|
$account['pronouns'],
|
||||||
$account['website']
|
$account['website']
|
||||||
);
|
);
|
||||||
|
|
||||||
$account['avatar_static'] = $account['avatar'];
|
$account['avatar_static'] = $account['avatar'];
|
||||||
$account['bot'] = false;
|
$account['bot'] = false;
|
||||||
$account['emojis'] = [];
|
$account['emojis'] = [];
|
||||||
$account['fields'] = [];
|
$account['fields'] = [];
|
||||||
$account['header'] = url('/storage/headers/missing.png');
|
$account['header'] = url('/storage/headers/missing.png');
|
||||||
$account['header_static'] = url('/storage/headers/missing.png');
|
$account['header_static'] = url('/storage/headers/missing.png');
|
||||||
$account['last_status_at'] = null;
|
$account['last_status_at'] = null;
|
||||||
|
|
||||||
return $account;
|
return $account;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function del($id)
|
public static function del($id)
|
||||||
{
|
{
|
||||||
Cache::forget('pf:activitypub:user-object:by-id:' . $id);
|
Cache::forget('pf:activitypub:user-object:by-id:' . $id);
|
||||||
return Cache::forget(self::CACHE_KEY . $id);
|
return Cache::forget(self::CACHE_KEY . $id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function settings($id)
|
public static function settings($id)
|
||||||
{
|
{
|
||||||
return Cache::remember('profile:compose:settings:' . $id, 604800, function() use($id) {
|
return Cache::remember('profile:compose:settings:' . $id, 604800, function() use($id) {
|
||||||
$settings = UserSetting::whereUserId($id)->first();
|
$settings = UserSetting::whereUserId($id)->first();
|
||||||
if(!$settings) {
|
if(!$settings) {
|
||||||
return self::defaultSettings();
|
return self::defaultSettings();
|
||||||
}
|
}
|
||||||
return collect($settings)
|
return collect($settings)
|
||||||
->filter(function($item, $key) {
|
->filter(function($item, $key) {
|
||||||
return in_array($key, array_keys(self::defaultSettings())) == true;
|
return in_array($key, array_keys(self::defaultSettings())) == true;
|
||||||
})
|
})
|
||||||
->map(function($item, $key) {
|
->map(function($item, $key) {
|
||||||
if($key == 'compose_settings') {
|
if($key == 'compose_settings') {
|
||||||
$cs = self::defaultSettings()['compose_settings'];
|
$cs = self::defaultSettings()['compose_settings'];
|
||||||
$ms = is_array($item) ? $item : [];
|
$ms = is_array($item) ? $item : [];
|
||||||
return array_merge($cs, $ms);
|
return array_merge($cs, $ms);
|
||||||
}
|
}
|
||||||
|
|
||||||
if($key == 'other') {
|
if($key == 'other') {
|
||||||
$other = self::defaultSettings()['other'];
|
$other = self::defaultSettings()['other'];
|
||||||
$mo = is_array($item) ? $item : [];
|
$mo = is_array($item) ? $item : [];
|
||||||
return array_merge($other, $mo);
|
return array_merge($other, $mo);
|
||||||
}
|
}
|
||||||
return $item;
|
return $item;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function canEmbed($id)
|
public static function canEmbed($id)
|
||||||
{
|
{
|
||||||
return self::settings($id)['other']['disable_embeds'] == false;
|
return self::settings($id)['other']['disable_embeds'] == false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function defaultSettings()
|
public static function defaultSettings()
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'crawlable' => true,
|
'crawlable' => true,
|
||||||
'public_dm' => false,
|
'public_dm' => false,
|
||||||
'reduce_motion' => false,
|
'reduce_motion' => false,
|
||||||
'high_contrast_mode' => false,
|
'high_contrast_mode' => false,
|
||||||
'video_autoplay' => false,
|
'video_autoplay' => false,
|
||||||
'show_profile_follower_count' => true,
|
'show_profile_follower_count' => true,
|
||||||
'show_profile_following_count' => true,
|
'show_profile_following_count' => true,
|
||||||
'compose_settings' => [
|
'compose_settings' => [
|
||||||
'default_scope' => 'public',
|
'default_scope' => 'public',
|
||||||
'default_license' => 1,
|
'default_license' => 1,
|
||||||
'media_descriptions' => false
|
'media_descriptions' => false
|
||||||
],
|
],
|
||||||
'other' => [
|
'other' => [
|
||||||
'advanced_atom' => false,
|
'advanced_atom' => false,
|
||||||
'disable_embeds' => false,
|
'disable_embeds' => false,
|
||||||
'mutual_mention_notifications' => false,
|
'mutual_mention_notifications' => false,
|
||||||
'hide_collections' => false,
|
'hide_collections' => false,
|
||||||
'hide_like_counts' => false,
|
'hide_like_counts' => false,
|
||||||
'hide_groups' => false,
|
'hide_groups' => false,
|
||||||
'hide_stories' => false,
|
'hide_stories' => false,
|
||||||
'disable_cw' => false,
|
'disable_cw' => false,
|
||||||
]
|
]
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function syncPostCount($id)
|
public static function syncPostCount($id)
|
||||||
{
|
{
|
||||||
$profile = Profile::find($id);
|
$profile = Profile::find($id);
|
||||||
|
|
||||||
if(!$profile) {
|
if(!$profile) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$key = self::CACHE_KEY . 'pcs:' . $id;
|
$key = self::CACHE_KEY . 'pcs:' . $id;
|
||||||
|
|
||||||
if(Cache::has($key)) {
|
if(Cache::has($key)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$count = Status::whereProfileId($id)
|
$count = Status::whereProfileId($id)
|
||||||
->whereNull('in_reply_to_id')
|
->whereNull('in_reply_to_id')
|
||||||
->whereNull('reblog_of_id')
|
->whereNull('reblog_of_id')
|
||||||
->whereIn('scope', ['public', 'unlisted', 'private'])
|
->whereIn('scope', ['public', 'unlisted', 'private'])
|
||||||
->count();
|
->count();
|
||||||
|
|
||||||
$profile->status_count = $count;
|
$profile->status_count = $count;
|
||||||
$profile->save();
|
$profile->save();
|
||||||
|
|
||||||
Cache::put($key, 1, 900);
|
Cache::put($key, 1, 900);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function usernameToId($username)
|
public static function usernameToId($username)
|
||||||
{
|
{
|
||||||
$key = self::CACHE_KEY . 'u2id:' . hash('sha256', $username);
|
$key = self::CACHE_KEY . 'u2id:' . hash('sha256', $username);
|
||||||
return Cache::remember($key, 900, function() use($username) {
|
return Cache::remember($key, 14400, function() use($username) {
|
||||||
$s = Str::of($username);
|
$s = Str::of($username);
|
||||||
if($s->contains('@') && !$s->startsWith('@')) {
|
if($s->contains('@') && !$s->startsWith('@')) {
|
||||||
$username = "@{$username}";
|
$username = "@{$username}";
|
||||||
}
|
}
|
||||||
$profile = DB::table('profiles')
|
$profile = DB::table('profiles')
|
||||||
->whereUsername($username)
|
->whereUsername($username)
|
||||||
->first();
|
->first();
|
||||||
if(!$profile) {
|
if(!$profile) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return (string) $profile->id;
|
return (string) $profile->id;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function hiddenFollowers($id)
|
public static function hiddenFollowers($id)
|
||||||
{
|
{
|
||||||
$account = self::get($id, true);
|
$account = self::get($id, true);
|
||||||
if(!$account || !isset($account['local']) || $account['local'] == false) {
|
if(!$account || !isset($account['local']) || $account['local'] == false) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return Cache::remember('pf:acct:settings:hidden-followers:' . $id, 43200, function() use($id) {
|
return Cache::remember('pf:acct:settings:hidden-followers:' . $id, 43200, function() use($id) {
|
||||||
$user = User::whereProfileId($id)->first();
|
$user = User::whereProfileId($id)->first();
|
||||||
if(!$user) {
|
if(!$user) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
$settings = UserSetting::whereUserId($user->id)->first();
|
$settings = UserSetting::whereUserId($user->id)->first();
|
||||||
if($settings) {
|
if($settings) {
|
||||||
return $settings->show_profile_follower_count == false;
|
return $settings->show_profile_follower_count == false;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function hiddenFollowing($id)
|
public static function hiddenFollowing($id)
|
||||||
{
|
{
|
||||||
$account = self::get($id, true);
|
$account = self::get($id, true);
|
||||||
if(!$account || !isset($account['local']) || $account['local'] == false) {
|
if(!$account || !isset($account['local']) || $account['local'] == false) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return Cache::remember('pf:acct:settings:hidden-following:' . $id, 43200, function() use($id) {
|
return Cache::remember('pf:acct:settings:hidden-following:' . $id, 43200, function() use($id) {
|
||||||
$user = User::whereProfileId($id)->first();
|
$user = User::whereProfileId($id)->first();
|
||||||
if(!$user) {
|
if(!$user) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
$settings = UserSetting::whereUserId($user->id)->first();
|
$settings = UserSetting::whereUserId($user->id)->first();
|
||||||
if($settings) {
|
if($settings) {
|
||||||
return $settings->show_profile_following_count == false;
|
return $settings->show_profile_following_count == false;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function setLastActive($id = false)
|
||||||
|
{
|
||||||
|
if(!$id) { return; }
|
||||||
|
$key = 'user:last_active_at:id:' . $id;
|
||||||
|
if(!Cache::has($key)) {
|
||||||
|
$user = User::find($id);
|
||||||
|
if(!$user) { return; }
|
||||||
|
$user->last_active_at = now();
|
||||||
|
$user->save();
|
||||||
|
Cache::put($key, 1, 14400);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue