1
0
Fork 0
forked from mirror/pixelfed

Update DiscoverController, handle discover hashtag redirects

This commit is contained in:
Daniel Supernault 2024-03-07 02:38:50 -07:00
parent 592c84125c
commit 18382e8a1f
No known key found for this signature in database
GPG key ID: 23740873EE6F76A1

View file

@ -2,366 +2,379 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\{ use App\Hashtag;
DiscoverCategory, use App\Instance;
Follower, use App\Like;
Hashtag,
HashtagFollow,
Instance,
Like,
Profile,
Status,
StatusHashtag,
UserFilter
};
use Auth, DB, Cache;
use Illuminate\Http\Request;
use App\Services\BookmarkService; use App\Services\BookmarkService;
use App\Services\ConfigCacheService; use App\Services\ConfigCacheService;
use App\Services\HashtagService; use App\Services\HashtagService;
use App\Services\LikeService; use App\Services\LikeService;
use App\Services\ReblogService; use App\Services\ReblogService;
use App\Services\StatusHashtagService;
use App\Services\SnowflakeService; use App\Services\SnowflakeService;
use App\Services\StatusHashtagService;
use App\Services\StatusService; use App\Services\StatusService;
use App\Services\TrendingHashtagService; use App\Services\TrendingHashtagService;
use App\Services\UserFilterService; use App\Services\UserFilterService;
use App\Status;
use Auth;
use Cache;
use DB;
use Illuminate\Http\Request;
class DiscoverController extends Controller class DiscoverController extends Controller
{ {
public function home(Request $request) public function home(Request $request)
{ {
abort_if(!Auth::check() && config('instance.discover.public') == false, 403); abort_if(! Auth::check() && config('instance.discover.public') == false, 403);
return view('discover.home');
}
public function showTags(Request $request, $hashtag) return view('discover.home');
{ }
abort_if(!config('instance.discover.tags.is_public') && !Auth::check(), 403);
$tag = Hashtag::whereName($hashtag) public function showTags(Request $request, $hashtag)
->orWhere('slug', $hashtag) {
->where('is_banned', '!=', true) if ($request->user()) {
->firstOrFail(); return redirect('/i/web/hashtag/'.$hashtag.'?src=pd');
$tagCount = StatusHashtagService::count($tag->id); }
return view('discover.tags.show', compact('tag', 'tagCount')); abort_if(! config('instance.discover.tags.is_public') && ! Auth::check(), 403);
}
public function getHashtags(Request $request) $tag = Hashtag::whereName($hashtag)
{ ->orWhere('slug', $hashtag)
$user = $request->user(); ->where('is_banned', '!=', true)
abort_if(!config('instance.discover.tags.is_public') && !$user, 403); ->firstOrFail();
$tagCount = $tag->cached_count ?? 0;
$this->validate($request, [ return view('discover.tags.show', compact('tag', 'tagCount'));
'hashtag' => 'required|string|min:1|max:124', }
'page' => 'nullable|integer|min:1|max:' . ($user ? 29 : 3)
]);
$page = $request->input('page') ?? '1'; public function getHashtags(Request $request)
$end = $page > 1 ? $page * 9 : 0; {
$tag = $request->input('hashtag'); $user = $request->user();
abort_if(! config('instance.discover.tags.is_public') && ! $user, 403);
if(config('database.default') === 'pgsql') { $this->validate($request, [
$hashtag = Hashtag::where('name', 'ilike', $tag)->firstOrFail(); 'hashtag' => 'required|string|min:1|max:124',
} else { 'page' => 'nullable|integer|min:1|max:'.($user ? 29 : 3),
$hashtag = Hashtag::whereName($tag)->firstOrFail(); ]);
}
if($hashtag->is_banned == true) { $page = $request->input('page') ?? '1';
return []; $end = $page > 1 ? $page * 9 : 0;
} $tag = $request->input('hashtag');
if($user) {
$res['follows'] = HashtagService::isFollowing($user->profile_id, $hashtag->id);
}
$res['hashtag'] = [
'name' => $hashtag->name,
'url' => $hashtag->url()
];
if($user) {
$tags = StatusHashtagService::get($hashtag->id, $page, $end);
$res['tags'] = collect($tags)
->map(function($tag) use($user) {
$tag['status']['favourited'] = (bool) LikeService::liked($user->profile_id, $tag['status']['id']);
$tag['status']['reblogged'] = (bool) ReblogService::get($user->profile_id, $tag['status']['id']);
$tag['status']['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $tag['status']['id']);
return $tag;
})
->filter(function($tag) {
if(!StatusService::get($tag['status']['id'])) {
return false;
}
return true;
})
->values();
} else {
if($page != 1) {
$res['tags'] = [];
return $res;
}
$key = 'discover:tags:public_feed:' . $hashtag->id . ':page:' . $page;
$tags = Cache::remember($key, 43200, function() use($hashtag, $page, $end) {
return collect(StatusHashtagService::get($hashtag->id, $page, $end))
->filter(function($tag) {
if(!$tag['status']['local']) {
return false;
}
return true;
})
->values();
});
$res['tags'] = collect($tags)
->filter(function($tag) {
if(!StatusService::get($tag['status']['id'])) {
return false;
}
return true;
})
->values();
}
return $res;
}
public function profilesDirectory(Request $request) if (config('database.default') === 'pgsql') {
{ $hashtag = Hashtag::where('name', 'ilike', $tag)->firstOrFail();
return redirect('/')->with('statusRedirect', 'The Profile Directory is unavailable at this time.'); } else {
} $hashtag = Hashtag::whereName($tag)->firstOrFail();
}
public function profilesDirectoryApi(Request $request) if ($hashtag->is_banned == true) {
{ return [];
return ['error' => 'Temporarily unavailable.']; }
} if ($user) {
$res['follows'] = HashtagService::isFollowing($user->profile_id, $hashtag->id);
}
$res['hashtag'] = [
'name' => $hashtag->name,
'url' => $hashtag->url(),
];
if ($user) {
$tags = StatusHashtagService::get($hashtag->id, $page, $end);
$res['tags'] = collect($tags)
->map(function ($tag) use ($user) {
$tag['status']['favourited'] = (bool) LikeService::liked($user->profile_id, $tag['status']['id']);
$tag['status']['reblogged'] = (bool) ReblogService::get($user->profile_id, $tag['status']['id']);
$tag['status']['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $tag['status']['id']);
public function trendingApi(Request $request) return $tag;
{ })
abort_if(config('instance.discover.public') == false && !$request->user(), 403); ->filter(function ($tag) {
if (! StatusService::get($tag['status']['id'])) {
return false;
}
$this->validate($request, [ return true;
'range' => 'nullable|string|in:daily,monthly,yearly', })
]); ->values();
} else {
if ($page != 1) {
$res['tags'] = [];
$range = $request->input('range'); return $res;
$days = $range == 'monthly' ? 31 : ($range == 'daily' ? 1 : 365); }
$ttls = [ $key = 'discover:tags:public_feed:'.$hashtag->id.':page:'.$page;
1 => 1500, $tags = Cache::remember($key, 43200, function () use ($hashtag, $page, $end) {
31 => 14400, return collect(StatusHashtagService::get($hashtag->id, $page, $end))
365 => 86400 ->filter(function ($tag) {
]; if (! $tag['status']['local']) {
$key = ':api:discover:trending:v2.12:range:' . $days; return false;
}
$ids = Cache::remember($key, $ttls[$days], function() use($days) { return true;
$min_id = SnowflakeService::byDate(now()->subDays($days)); })
return DB::table('statuses') ->values();
->select( });
'id', $res['tags'] = collect($tags)
'scope', ->filter(function ($tag) {
'type', if (! StatusService::get($tag['status']['id'])) {
'is_nsfw', return false;
'likes_count', }
'created_at'
)
->where('id', '>', $min_id)
->whereNull('uri')
->whereScope('public')
->whereIn('type', [
'photo',
'photo:album',
'video'
])
->whereIsNsfw(false)
->orderBy('likes_count','desc')
->take(30)
->pluck('id');
});
$filtered = Auth::check() ? UserFilterService::filters(Auth::user()->profile_id) : []; return true;
})
->values();
}
$res = $ids->map(function($s) { return $res;
return StatusService::get($s); }
})->filter(function($s) use($filtered) {
return
$s &&
!in_array($s['account']['id'], $filtered) &&
isset($s['account']);
})->values();
return response()->json($res); public function profilesDirectory(Request $request)
} {
return redirect('/')->with('statusRedirect', 'The Profile Directory is unavailable at this time.');
}
public function trendingHashtags(Request $request) public function profilesDirectoryApi(Request $request)
{ {
abort_if(!$request->user(), 403); return ['error' => 'Temporarily unavailable.'];
}
$res = TrendingHashtagService::getTrending(); public function trendingApi(Request $request)
return $res; {
} abort_if(config('instance.discover.public') == false && ! $request->user(), 403);
public function trendingPlaces(Request $request) $this->validate($request, [
{ 'range' => 'nullable|string|in:daily,monthly,yearly',
return []; ]);
}
public function myMemories(Request $request) $range = $request->input('range');
{ $days = $range == 'monthly' ? 31 : ($range == 'daily' ? 1 : 365);
abort_if(!$request->user(), 404); $ttls = [
$pid = $request->user()->profile_id; 1 => 1500,
abort_if(!$this->config()['memories']['enabled'], 404); 31 => 14400,
$type = $request->input('type') ?? 'posts'; 365 => 86400,
];
$key = ':api:discover:trending:v2.12:range:'.$days;
switch($type) { $ids = Cache::remember($key, $ttls[$days], function () use ($days) {
case 'posts': $min_id = SnowflakeService::byDate(now()->subDays($days));
$res = Status::whereProfileId($pid)
->whereDay('created_at', date('d'))
->whereMonth('created_at', date('m'))
->whereYear('created_at', '!=', date('Y'))
->whereNull(['reblog_of_id', 'in_reply_to_id'])
->limit(20)
->pluck('id')
->map(function($id) {
return StatusService::get($id, false);
})
->filter(function($post) {
return $post && isset($post['account']);
})
->values();
break;
case 'liked': return DB::table('statuses')
$res = Like::whereProfileId($pid) ->select(
->whereDay('created_at', date('d')) 'id',
->whereMonth('created_at', date('m')) 'scope',
->whereYear('created_at', '!=', date('Y')) 'type',
->orderByDesc('status_id') 'is_nsfw',
->limit(20) 'likes_count',
->pluck('status_id') 'created_at'
->map(function($id) { )
$status = StatusService::get($id, false); ->where('id', '>', $min_id)
$status['favourited'] = true; ->whereNull('uri')
return $status; ->whereScope('public')
}) ->whereIn('type', [
->filter(function($post) { 'photo',
return $post && isset($post['account']); 'photo:album',
}) 'video',
->values(); ])
break; ->whereIsNsfw(false)
} ->orderBy('likes_count', 'desc')
->take(30)
->pluck('id');
});
return $res; $filtered = Auth::check() ? UserFilterService::filters(Auth::user()->profile_id) : [];
}
public function accountInsightsPopularPosts(Request $request) $res = $ids->map(function ($s) {
{ return StatusService::get($s);
abort_if(!$request->user(), 404); })->filter(function ($s) use ($filtered) {
$pid = $request->user()->profile_id; return
abort_if(!$this->config()['insights']['enabled'], 404); $s &&
$posts = Cache::remember('pf:discover:metro2:accinsights:popular:' . $pid, 43200, function() use ($pid) { ! in_array($s['account']['id'], $filtered) &&
return Status::whereProfileId($pid) isset($s['account']);
->whereNotNull('likes_count') })->values();
->orderByDesc('likes_count')
->limit(12)
->pluck('id')
->map(function($id) {
return StatusService::get($id, false);
})
->filter(function($post) {
return $post && isset($post['account']);
})
->values();
});
return $posts; return response()->json($res);
} }
public function config() public function trendingHashtags(Request $request)
{ {
$cc = ConfigCacheService::get('config.discover.features'); abort_if(! $request->user(), 403);
if($cc) {
return is_string($cc) ? json_decode($cc, true) : $cc;
}
return [
'hashtags' => [
'enabled' => false,
],
'memories' => [
'enabled' => false,
],
'insights' => [
'enabled' => false,
],
'friends' => [
'enabled' => false,
],
'server' => [
'enabled' => false,
'mode' => 'allowlist',
'domains' => []
]
];
}
public function serverTimeline(Request $request) $res = TrendingHashtagService::getTrending();
{
abort_if(!$request->user(), 404);
abort_if(!$this->config()['server']['enabled'], 404);
$pid = $request->user()->profile_id;
$domain = $request->input('domain');
$config = $this->config();
$domains = explode(',', $config['server']['domains']);
abort_unless(in_array($domain, $domains), 400);
$res = Status::whereNotNull('uri') return $res;
->where('uri', 'like', 'https://' . $domain . '%') }
->whereNull(['in_reply_to_id', 'reblog_of_id'])
->orderByDesc('id')
->limit(12)
->pluck('id')
->map(function($id) {
return StatusService::get($id);
})
->filter(function($post) {
return $post && isset($post['account']);
})
->values();
return $res;
}
public function enabledFeatures(Request $request) public function trendingPlaces(Request $request)
{ {
abort_if(!$request->user(), 404); return [];
return $this->config(); }
}
public function updateFeatures(Request $request) public function myMemories(Request $request)
{ {
abort_if(!$request->user(), 404); abort_if(! $request->user(), 404);
abort_if(!$request->user()->is_admin, 404); $pid = $request->user()->profile_id;
$pid = $request->user()->profile_id; abort_if(! $this->config()['memories']['enabled'], 404);
$this->validate($request, [ $type = $request->input('type') ?? 'posts';
'features.friends.enabled' => 'boolean',
'features.hashtags.enabled' => 'boolean', switch ($type) {
'features.insights.enabled' => 'boolean', case 'posts':
'features.memories.enabled' => 'boolean', $res = Status::whereProfileId($pid)
'features.server.enabled' => 'boolean', ->whereDay('created_at', date('d'))
]); ->whereMonth('created_at', date('m'))
$res = $request->input('features'); ->whereYear('created_at', '!=', date('Y'))
if($res['server'] && isset($res['server']['domains']) && !empty($res['server']['domains'])) { ->whereNull(['reblog_of_id', 'in_reply_to_id'])
$parts = explode(',', $res['server']['domains']); ->limit(20)
$parts = array_filter($parts, function($v) { ->pluck('id')
$len = strlen($v); ->map(function ($id) {
$pos = strpos($v, '.'); return StatusService::get($id, false);
$domain = trim($v); })
if($pos == false || $pos == ($len + 1)) { ->filter(function ($post) {
return false; return $post && isset($post['account']);
} })
if(!Instance::whereDomain($domain)->exists()) { ->values();
return false; break;
}
return true; case 'liked':
}); $res = Like::whereProfileId($pid)
$parts = array_slice($parts, 0, 10); ->whereDay('created_at', date('d'))
$d = implode(',', array_map('trim', $parts)); ->whereMonth('created_at', date('m'))
$res['server']['domains'] = $d; ->whereYear('created_at', '!=', date('Y'))
} ->orderByDesc('status_id')
ConfigCacheService::put('config.discover.features', json_encode($res)); ->limit(20)
return $res; ->pluck('status_id')
} ->map(function ($id) {
$status = StatusService::get($id, false);
$status['favourited'] = true;
return $status;
})
->filter(function ($post) {
return $post && isset($post['account']);
})
->values();
break;
}
return $res;
}
public function accountInsightsPopularPosts(Request $request)
{
abort_if(! $request->user(), 404);
$pid = $request->user()->profile_id;
abort_if(! $this->config()['insights']['enabled'], 404);
$posts = Cache::remember('pf:discover:metro2:accinsights:popular:'.$pid, 43200, function () use ($pid) {
return Status::whereProfileId($pid)
->whereNotNull('likes_count')
->orderByDesc('likes_count')
->limit(12)
->pluck('id')
->map(function ($id) {
return StatusService::get($id, false);
})
->filter(function ($post) {
return $post && isset($post['account']);
})
->values();
});
return $posts;
}
public function config()
{
$cc = ConfigCacheService::get('config.discover.features');
if ($cc) {
return is_string($cc) ? json_decode($cc, true) : $cc;
}
return [
'hashtags' => [
'enabled' => false,
],
'memories' => [
'enabled' => false,
],
'insights' => [
'enabled' => false,
],
'friends' => [
'enabled' => false,
],
'server' => [
'enabled' => false,
'mode' => 'allowlist',
'domains' => [],
],
];
}
public function serverTimeline(Request $request)
{
abort_if(! $request->user(), 404);
abort_if(! $this->config()['server']['enabled'], 404);
$pid = $request->user()->profile_id;
$domain = $request->input('domain');
$config = $this->config();
$domains = explode(',', $config['server']['domains']);
abort_unless(in_array($domain, $domains), 400);
$res = Status::whereNotNull('uri')
->where('uri', 'like', 'https://'.$domain.'%')
->whereNull(['in_reply_to_id', 'reblog_of_id'])
->orderByDesc('id')
->limit(12)
->pluck('id')
->map(function ($id) {
return StatusService::get($id);
})
->filter(function ($post) {
return $post && isset($post['account']);
})
->values();
return $res;
}
public function enabledFeatures(Request $request)
{
abort_if(! $request->user(), 404);
return $this->config();
}
public function updateFeatures(Request $request)
{
abort_if(! $request->user(), 404);
abort_if(! $request->user()->is_admin, 404);
$pid = $request->user()->profile_id;
$this->validate($request, [
'features.friends.enabled' => 'boolean',
'features.hashtags.enabled' => 'boolean',
'features.insights.enabled' => 'boolean',
'features.memories.enabled' => 'boolean',
'features.server.enabled' => 'boolean',
]);
$res = $request->input('features');
if ($res['server'] && isset($res['server']['domains']) && ! empty($res['server']['domains'])) {
$parts = explode(',', $res['server']['domains']);
$parts = array_filter($parts, function ($v) {
$len = strlen($v);
$pos = strpos($v, '.');
$domain = trim($v);
if ($pos == false || $pos == ($len + 1)) {
return false;
}
if (! Instance::whereDomain($domain)->exists()) {
return false;
}
return true;
});
$parts = array_slice($parts, 0, 10);
$d = implode(',', array_map('trim', $parts));
$res['server']['domains'] = $d;
}
ConfigCacheService::put('config.discover.features', json_encode($res));
return $res;
}
} }