diff --git a/app/Services/TrendingHashtagService.php b/app/Services/TrendingHashtagService.php new file mode 100644 index 000000000..c27b2f3c3 --- /dev/null +++ b/app/Services/TrendingHashtagService.php @@ -0,0 +1,96 @@ +pluck('id')->toArray(); + }); + } + + public static function getNonTrendingHashtags() + { + return Cache::remember(self::key(':can_trend'), 1209600, function() { + return Hashtag::whereCanTrend(false)->pluck('id')->toArray(); + }); + } + + public static function getNsfwHashtags() + { + return Cache::remember(self::key(':is_nsfw'), 1209600, function() { + return Hashtag::whereIsNsfw(true)->pluck('id')->toArray(); + }); + } + + public static function getMinRecentId() + { + return Cache::remember(self::key('-min-id'), 86400, function() { + $minId = StatusHashtag::where('created_at', '>', now()->subMinutes(config('trending.hashtags.recency_mins')))->first(); + if(!$minId) { + return 0; + } + return $minId->id; + }); + } + + public static function getTrending() + { + $minId = self::getMinRecentId(); + + $skipIds = array_merge(self::getBlockedHashtags(), self::getNonTrendingHashtags(), self::getNsfwHashtags()); + + return Cache::remember(self::CACHE_KEY, config('trending.hashtags.ttl'), function() use($minId, $skipIds) { + return StatusHashtag::select('hashtag_id', \DB::raw('count(*) as total')) + ->whereNotIn('hashtag_id', $skipIds) + ->where('id', '>', $minId) + ->groupBy('hashtag_id') + ->orderBy('total', 'desc') + ->take(config('trending.hashtags.limit')) + ->get() + ->map(function($h) { + $hashtag = Hashtag::find($h->hashtag_id); + if(!$hashtag) { + return; + } + return [ + 'id' => $h->hashtag_id, + 'total' => $h->total, + 'name' => '#'.$hashtag->name, + 'hashtag' => $hashtag->name, + 'url' => $hashtag->url() + ]; + }) + ->filter() + ->values(); + }); + } + + public static function del($k) + { + return Cache::forget(self::key($k)); + } + + public static function refresh() + { + Cache::forget(self::key(':is_banned')); + Cache::forget(self::key(':is_nsfw')); + Cache::forget(self::key(':can_trend')); + Cache::forget(self::key('-min-id')); + Cache::forget(self::key()); + } +}