mirror of
https://github.com/pixelfed/pixelfed.git
synced 2024-11-22 14:31:26 +00:00
commit
5d7e091978
2 changed files with 411 additions and 397 deletions
|
@ -2,48 +2,47 @@
|
|||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\{
|
||||
DiscoverCategory,
|
||||
Follower,
|
||||
Hashtag,
|
||||
HashtagFollow,
|
||||
Instance,
|
||||
Like,
|
||||
Profile,
|
||||
Status,
|
||||
StatusHashtag,
|
||||
UserFilter
|
||||
};
|
||||
use Auth, DB, Cache;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Hashtag;
|
||||
use App\Instance;
|
||||
use App\Like;
|
||||
use App\Services\BookmarkService;
|
||||
use App\Services\ConfigCacheService;
|
||||
use App\Services\HashtagService;
|
||||
use App\Services\LikeService;
|
||||
use App\Services\ReblogService;
|
||||
use App\Services\StatusHashtagService;
|
||||
use App\Services\SnowflakeService;
|
||||
use App\Services\StatusHashtagService;
|
||||
use App\Services\StatusService;
|
||||
use App\Services\TrendingHashtagService;
|
||||
use App\Services\UserFilterService;
|
||||
use App\Status;
|
||||
use Auth;
|
||||
use Cache;
|
||||
use DB;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class DiscoverController extends Controller
|
||||
{
|
||||
public function home(Request $request)
|
||||
{
|
||||
abort_if(! Auth::check() && config('instance.discover.public') == false, 403);
|
||||
|
||||
return view('discover.home');
|
||||
}
|
||||
|
||||
public function showTags(Request $request, $hashtag)
|
||||
{
|
||||
if ($request->user()) {
|
||||
return redirect('/i/web/hashtag/'.$hashtag.'?src=pd');
|
||||
}
|
||||
abort_if(! config('instance.discover.tags.is_public') && ! Auth::check(), 403);
|
||||
|
||||
$tag = Hashtag::whereName($hashtag)
|
||||
->orWhere('slug', $hashtag)
|
||||
->where('is_banned', '!=', true)
|
||||
->firstOrFail();
|
||||
$tagCount = StatusHashtagService::count($tag->id);
|
||||
$tagCount = $tag->cached_count ?? 0;
|
||||
|
||||
return view('discover.tags.show', compact('tag', 'tagCount'));
|
||||
}
|
||||
|
||||
|
@ -54,7 +53,7 @@ class DiscoverController extends Controller
|
|||
|
||||
$this->validate($request, [
|
||||
'hashtag' => 'required|string|min:1|max:124',
|
||||
'page' => 'nullable|integer|min:1|max:' . ($user ? 29 : 3)
|
||||
'page' => 'nullable|integer|min:1|max:'.($user ? 29 : 3),
|
||||
]);
|
||||
|
||||
$page = $request->input('page') ?? '1';
|
||||
|
@ -75,7 +74,7 @@ class DiscoverController extends Controller
|
|||
}
|
||||
$res['hashtag'] = [
|
||||
'name' => $hashtag->name,
|
||||
'url' => $hashtag->url()
|
||||
'url' => $hashtag->url(),
|
||||
];
|
||||
if ($user) {
|
||||
$tags = StatusHashtagService::get($hashtag->id, $page, $end);
|
||||
|
@ -84,18 +83,21 @@ class DiscoverController extends Controller
|
|||
$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;
|
||||
|
@ -105,6 +107,7 @@ class DiscoverController extends Controller
|
|||
if (! $tag['status']['local']) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
})
|
||||
->values();
|
||||
|
@ -114,10 +117,12 @@ class DiscoverController extends Controller
|
|||
if (! StatusService::get($tag['status']['id'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
})
|
||||
->values();
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
|
@ -144,12 +149,13 @@ class DiscoverController extends Controller
|
|||
$ttls = [
|
||||
1 => 1500,
|
||||
31 => 14400,
|
||||
365 => 86400
|
||||
365 => 86400,
|
||||
];
|
||||
$key = ':api:discover:trending:v2.12:range:'.$days;
|
||||
|
||||
$ids = Cache::remember($key, $ttls[$days], function () use ($days) {
|
||||
$min_id = SnowflakeService::byDate(now()->subDays($days));
|
||||
|
||||
return DB::table('statuses')
|
||||
->select(
|
||||
'id',
|
||||
|
@ -165,7 +171,7 @@ class DiscoverController extends Controller
|
|||
->whereIn('type', [
|
||||
'photo',
|
||||
'photo:album',
|
||||
'video'
|
||||
'video',
|
||||
])
|
||||
->whereIsNsfw(false)
|
||||
->orderBy('likes_count', 'desc')
|
||||
|
@ -192,6 +198,7 @@ class DiscoverController extends Controller
|
|||
abort_if(! $request->user(), 403);
|
||||
|
||||
$res = TrendingHashtagService::getTrending();
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
|
@ -236,6 +243,7 @@ class DiscoverController extends Controller
|
|||
->map(function ($id) {
|
||||
$status = StatusService::get($id, false);
|
||||
$status['favourited'] = true;
|
||||
|
||||
return $status;
|
||||
})
|
||||
->filter(function ($post) {
|
||||
|
@ -277,6 +285,7 @@ class DiscoverController extends Controller
|
|||
if ($cc) {
|
||||
return is_string($cc) ? json_decode($cc, true) : $cc;
|
||||
}
|
||||
|
||||
return [
|
||||
'hashtags' => [
|
||||
'enabled' => false,
|
||||
|
@ -293,8 +302,8 @@ class DiscoverController extends Controller
|
|||
'server' => [
|
||||
'enabled' => false,
|
||||
'mode' => 'allowlist',
|
||||
'domains' => []
|
||||
]
|
||||
'domains' => [],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -321,12 +330,14 @@ class DiscoverController extends Controller
|
|||
return $post && isset($post['account']);
|
||||
})
|
||||
->values();
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
public function enabledFeatures(Request $request)
|
||||
{
|
||||
abort_if(! $request->user(), 404);
|
||||
|
||||
return $this->config();
|
||||
}
|
||||
|
||||
|
@ -355,6 +366,7 @@ class DiscoverController extends Controller
|
|||
if (! Instance::whereDomain($domain)->exists()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
$parts = array_slice($parts, 0, 10);
|
||||
|
@ -362,6 +374,7 @@ class DiscoverController extends Controller
|
|||
$res['server']['domains'] = $d;
|
||||
}
|
||||
ConfigCacheService::put('config.discover.features', json_encode($res));
|
||||
|
||||
return $res;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,17 +2,15 @@
|
|||
|
||||
namespace App\Services;
|
||||
|
||||
use Cache;
|
||||
use Illuminate\Support\Facades\Redis;
|
||||
use App\{Status, StatusHashtag};
|
||||
use App\Transformer\Api\StatusHashtagTransformer;
|
||||
use App\Hashtag;
|
||||
use App\Status;
|
||||
use App\StatusHashtag;
|
||||
use App\Transformer\Api\HashtagTransformer;
|
||||
use League\Fractal;
|
||||
use League\Fractal\Serializer\ArraySerializer;
|
||||
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
|
||||
|
||||
class StatusHashtagService {
|
||||
|
||||
class StatusHashtagService
|
||||
{
|
||||
const CACHE_KEY = 'pf:services:status-hashtag:collection:';
|
||||
|
||||
public static function get($id, $page = 1, $stop = 9)
|
||||
|
@ -55,6 +53,7 @@ class StatusHashtagService {
|
|||
foreach ($ids as $key) {
|
||||
self::set($id, $key);
|
||||
}
|
||||
|
||||
return $ids;
|
||||
}
|
||||
|
||||
|
@ -70,11 +69,12 @@ class StatusHashtagService {
|
|||
|
||||
public static function count($id)
|
||||
{
|
||||
$key = 'pf:services:status-hashtag:count:' . $id;
|
||||
$ttl = now()->addMinutes(5);
|
||||
return Cache::remember($key, $ttl, function() use($id) {
|
||||
return StatusHashtag::whereHashtagId($id)->has('media')->count();
|
||||
});
|
||||
$cc = Hashtag::find($id);
|
||||
if (! $cc) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return $cc->cached_count ?? 0;
|
||||
}
|
||||
|
||||
public static function getStatus($statusId, $hashtagId)
|
||||
|
@ -92,6 +92,7 @@ class StatusHashtagService {
|
|||
$fractal = new Fractal\Manager();
|
||||
$fractal->setSerializer(new ArraySerializer());
|
||||
$resource = new Fractal\Resource\Collection($status->hashtags, new HashtagTransformer());
|
||||
|
||||
return $fractal->createData($resource)->toArray();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue