2019-06-11 02:10:57 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
2021-01-30 23:05:18 +00:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
2020-12-13 23:08:59 +00:00
|
|
|
use Illuminate\Support\Facades\Redis;
|
2021-12-13 05:30:55 +00:00
|
|
|
use DB;
|
2019-06-11 02:10:57 +00:00
|
|
|
use App\Status;
|
|
|
|
use App\Transformer\Api\StatusStatelessTransformer;
|
2020-12-13 23:08:59 +00:00
|
|
|
use App\Transformer\Api\StatusTransformer;
|
2019-06-11 02:10:57 +00:00
|
|
|
use League\Fractal;
|
|
|
|
use League\Fractal\Serializer\ArraySerializer;
|
|
|
|
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
|
|
|
|
|
2021-12-13 05:30:55 +00:00
|
|
|
class StatusService
|
|
|
|
{
|
2019-06-11 02:10:57 +00:00
|
|
|
const CACHE_KEY = 'pf:services:status:';
|
|
|
|
|
2021-08-31 06:40:07 +00:00
|
|
|
public static function key($id, $publicOnly = true)
|
2019-06-11 02:10:57 +00:00
|
|
|
{
|
2021-09-05 03:12:44 +00:00
|
|
|
$p = $publicOnly ? 'pub:' : 'all:';
|
2021-08-31 06:40:07 +00:00
|
|
|
return self::CACHE_KEY . $p . $id;
|
2019-06-11 02:10:57 +00:00
|
|
|
}
|
|
|
|
|
2021-07-27 01:02:11 +00:00
|
|
|
public static function get($id, $publicOnly = true)
|
2019-06-11 02:10:57 +00:00
|
|
|
{
|
2021-08-31 06:40:07 +00:00
|
|
|
return Cache::remember(self::key($id, $publicOnly), now()->addDays(7), function() use($id, $publicOnly) {
|
2021-07-27 01:02:11 +00:00
|
|
|
if($publicOnly) {
|
|
|
|
$status = Status::whereScope('public')->find($id);
|
|
|
|
} else {
|
2021-08-31 06:40:07 +00:00
|
|
|
$status = Status::whereIn('scope', ['public', 'private', 'unlisted', 'group'])->find($id);
|
2021-07-27 01:02:11 +00:00
|
|
|
}
|
2021-01-30 23:05:18 +00:00
|
|
|
if(!$status) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$fractal = new Fractal\Manager();
|
|
|
|
$fractal->setSerializer(new ArraySerializer());
|
|
|
|
$resource = new Fractal\Resource\Item($status, new StatusStatelessTransformer());
|
|
|
|
return $fractal->createData($resource)->toArray();
|
|
|
|
});
|
2019-06-11 02:10:57 +00:00
|
|
|
}
|
|
|
|
|
2022-01-03 06:47:08 +00:00
|
|
|
public static function getMastodon($id, $publicOnly = true)
|
|
|
|
{
|
|
|
|
$status = self::get($id, $publicOnly);
|
|
|
|
if(!$status) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-01-06 04:36:02 +00:00
|
|
|
|
2023-05-09 04:45:02 +00:00
|
|
|
if(!isset($status['account'])) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-03-07 07:21:05 +00:00
|
|
|
$status['replies_count'] = $status['reply_count'];
|
|
|
|
|
2022-01-06 04:36:02 +00:00
|
|
|
if(config('exp.emc') == false) {
|
|
|
|
return $status;
|
|
|
|
}
|
|
|
|
|
2022-01-03 06:47:08 +00:00
|
|
|
unset(
|
|
|
|
$status['_v'],
|
|
|
|
$status['comments_disabled'],
|
|
|
|
$status['content_text'],
|
|
|
|
$status['gid'],
|
|
|
|
$status['label'],
|
|
|
|
$status['liked_by'],
|
|
|
|
$status['local'],
|
|
|
|
$status['parent'],
|
|
|
|
$status['pf_type'],
|
|
|
|
$status['place'],
|
|
|
|
$status['replies'],
|
|
|
|
$status['reply_count'],
|
|
|
|
$status['shortcode'],
|
|
|
|
$status['taggedPeople'],
|
|
|
|
$status['thread'],
|
2022-01-08 02:59:29 +00:00
|
|
|
$status['pinned'],
|
2022-01-03 06:47:08 +00:00
|
|
|
$status['account']['header_bg'],
|
|
|
|
$status['account']['is_admin'],
|
|
|
|
$status['account']['last_fetched_at'],
|
|
|
|
$status['account']['local'],
|
|
|
|
$status['account']['location'],
|
|
|
|
$status['account']['note_text'],
|
|
|
|
$status['account']['pronouns'],
|
|
|
|
$status['account']['website'],
|
2022-01-03 09:31:03 +00:00
|
|
|
$status['media_attachments'],
|
2022-01-03 06:47:08 +00:00
|
|
|
);
|
|
|
|
$status['account']['avatar_static'] = $status['account']['avatar'];
|
|
|
|
$status['account']['bot'] = false;
|
|
|
|
$status['account']['emojis'] = [];
|
|
|
|
$status['account']['fields'] = [];
|
|
|
|
$status['account']['header'] = url('/storage/headers/missing.png');
|
|
|
|
$status['account']['header_static'] = url('/storage/headers/missing.png');
|
|
|
|
$status['account']['last_status_at'] = null;
|
|
|
|
|
2022-01-03 09:31:03 +00:00
|
|
|
$status['media_attachments'] = array_values(MediaService::getMastodon($status['id']));
|
2022-01-08 02:59:29 +00:00
|
|
|
$status['muted'] = false;
|
|
|
|
$status['reblogged'] = false;
|
2022-01-03 09:31:03 +00:00
|
|
|
|
2022-01-03 06:47:08 +00:00
|
|
|
return $status;
|
|
|
|
}
|
|
|
|
|
2021-12-22 04:40:10 +00:00
|
|
|
public static function getState($id, $pid)
|
|
|
|
{
|
|
|
|
$status = self::get($id, false);
|
|
|
|
|
|
|
|
if(!$status) {
|
|
|
|
return [
|
|
|
|
'liked' => false,
|
|
|
|
'shared' => false,
|
|
|
|
'bookmarked' => false
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'liked' => LikeService::liked($pid, $id),
|
|
|
|
'shared' => self::isShared($id, $pid),
|
|
|
|
'bookmarked' => self::isBookmarked($id, $pid)
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2021-10-07 10:49:33 +00:00
|
|
|
public static function getFull($id, $pid, $publicOnly = true)
|
|
|
|
{
|
|
|
|
$res = self::get($id, $publicOnly);
|
|
|
|
$res['relationship'] = RelationshipService::get($pid, $res['account']['id']);
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
2021-12-05 00:33:32 +00:00
|
|
|
public static function getDirectMessage($id)
|
|
|
|
{
|
|
|
|
$status = Status::whereScope('direct')->find($id);
|
|
|
|
|
|
|
|
if(!$status) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$fractal = new Fractal\Manager();
|
|
|
|
$fractal->setSerializer(new ArraySerializer());
|
2022-04-18 07:59:27 +00:00
|
|
|
$resource = new Fractal\Resource\Item($status, new StatusStatelessTransformer());
|
2021-12-05 00:33:32 +00:00
|
|
|
return $fractal->createData($resource)->toArray();
|
|
|
|
}
|
|
|
|
|
2021-12-13 05:30:55 +00:00
|
|
|
public static function del($id, $purge = false)
|
2019-06-11 02:10:57 +00:00
|
|
|
{
|
2021-12-13 05:30:55 +00:00
|
|
|
if($purge) {
|
2022-12-29 02:42:25 +00:00
|
|
|
$status = self::get($id);
|
2021-12-13 05:30:55 +00:00
|
|
|
if($status && isset($status['account']) && isset($status['account']['id'])) {
|
|
|
|
Cache::forget('profile:embed:' . $status['account']['id']);
|
|
|
|
}
|
|
|
|
Cache::forget('status:transformer:media:attachments:' . $id);
|
|
|
|
MediaService::del($id);
|
|
|
|
Cache::forget('status:thumb:nsfw0' . $id);
|
|
|
|
Cache::forget('status:thumb:nsfw1' . $id);
|
|
|
|
Cache::forget('pf:services:sh:id:' . $id);
|
|
|
|
PublicTimelineService::rem($id);
|
2023-03-01 07:40:25 +00:00
|
|
|
NetworkTimelineService::rem($id);
|
2021-07-27 06:13:03 +00:00
|
|
|
}
|
2021-12-13 05:30:55 +00:00
|
|
|
|
2021-08-31 06:40:07 +00:00
|
|
|
Cache::forget(self::key($id, false));
|
2021-01-30 23:05:18 +00:00
|
|
|
return Cache::forget(self::key($id));
|
2019-06-11 02:10:57 +00:00
|
|
|
}
|
2021-10-22 01:02:15 +00:00
|
|
|
|
|
|
|
public static function refresh($id)
|
|
|
|
{
|
|
|
|
Cache::forget(self::key($id, false));
|
|
|
|
Cache::forget(self::key($id, true));
|
|
|
|
self::get($id, false);
|
|
|
|
self::get($id, true);
|
|
|
|
}
|
2021-12-22 04:40:10 +00:00
|
|
|
|
|
|
|
public static function isShared($id, $pid = null)
|
|
|
|
{
|
|
|
|
return $pid ?
|
2022-01-29 05:06:43 +00:00
|
|
|
ReblogService::get($pid, $id) :
|
2021-12-22 04:40:10 +00:00
|
|
|
false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function isBookmarked($id, $pid = null)
|
|
|
|
{
|
|
|
|
return $pid ?
|
2022-01-29 03:57:25 +00:00
|
|
|
BookmarkService::get($pid, $id) :
|
2021-12-22 04:40:10 +00:00
|
|
|
false;
|
|
|
|
}
|
2021-07-06 08:01:24 +00:00
|
|
|
}
|