forked from mirror/pixelfed
Update HashtagServices
This commit is contained in:
parent
ce54d29c69
commit
1f35da0d4b
2 changed files with 86 additions and 51 deletions
21
app/Services/HashtagFollowService.php
Normal file
21
app/Services/HashtagFollowService.php
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Services;
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Cache;
|
||||||
|
use Illuminate\Support\Facades\Redis;
|
||||||
|
use App\Hashtag;
|
||||||
|
use App\StatusHashtag;
|
||||||
|
use App\HashtagFollow;
|
||||||
|
|
||||||
|
class HashtagFollowService
|
||||||
|
{
|
||||||
|
const FOLLOW_KEY = 'pf:services:hashtag-follows:v1:';
|
||||||
|
|
||||||
|
public static function getPidByHid($hid)
|
||||||
|
{
|
||||||
|
return Cache::remember(self::FOLLOW_KEY . $hid, 86400, function() use($hid) {
|
||||||
|
return HashtagFollow::whereHashtagId($hid)->pluck('profile_id')->toArray();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
|
@ -8,9 +8,10 @@ use App\Hashtag;
|
||||||
use App\StatusHashtag;
|
use App\StatusHashtag;
|
||||||
use App\HashtagFollow;
|
use App\HashtagFollow;
|
||||||
|
|
||||||
class HashtagService {
|
class HashtagService
|
||||||
|
{
|
||||||
const FOLLOW_KEY = 'pf:services:hashtag:following:';
|
const FOLLOW_KEY = 'pf:services:hashtag:following:v1:';
|
||||||
|
const FOLLOW_PIDS_KEY = 'pf:services:hashtag-follows:v1:';
|
||||||
|
|
||||||
public static function get($id)
|
public static function get($id)
|
||||||
{
|
{
|
||||||
|
@ -35,21 +36,21 @@ class HashtagService {
|
||||||
|
|
||||||
public static function isFollowing($pid, $hid)
|
public static function isFollowing($pid, $hid)
|
||||||
{
|
{
|
||||||
$res = Redis::zscore(self::FOLLOW_KEY . $pid, $hid);
|
$res = Redis::zscore(self::FOLLOW_KEY . $hid, $pid);
|
||||||
if($res) {
|
if($res) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
$synced = Cache::get(self::FOLLOW_KEY . $pid . ':synced');
|
$synced = Cache::get(self::FOLLOW_KEY . 'acct:' . $pid . ':synced');
|
||||||
if(!$synced) {
|
if(!$synced) {
|
||||||
$tags = HashtagFollow::whereProfileId($pid)
|
$tags = HashtagFollow::whereProfileId($pid)
|
||||||
->get()
|
->get()
|
||||||
->each(function($tag) use($pid) {
|
->each(function($tag) use($pid) {
|
||||||
self::follow($pid, $tag->hashtag_id);
|
self::follow($pid, $tag->hashtag_id);
|
||||||
});
|
});
|
||||||
Cache::set(self::FOLLOW_KEY . $pid . ':synced', true, 1209600);
|
Cache::set(self::FOLLOW_KEY . 'acct:' . $pid . ':synced', true, 1209600);
|
||||||
|
|
||||||
return (bool) Redis::zscore(self::FOLLOW_KEY . $pid, $hid) > 1;
|
return (bool) Redis::zscore(self::FOLLOW_KEY . $hid, $pid) >= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
@ -57,16 +58,29 @@ class HashtagService {
|
||||||
|
|
||||||
public static function follow($pid, $hid)
|
public static function follow($pid, $hid)
|
||||||
{
|
{
|
||||||
return Redis::zadd(self::FOLLOW_KEY . $pid, $hid, $hid);
|
Cache::forget(self::FOLLOW_PIDS_KEY . $hid);
|
||||||
|
return Redis::zadd(self::FOLLOW_KEY . $hid, $pid, $pid);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function unfollow($pid, $hid)
|
public static function unfollow($pid, $hid)
|
||||||
{
|
{
|
||||||
return Redis::zrem(self::FOLLOW_KEY . $pid, $hid);
|
Cache::forget(self::FOLLOW_PIDS_KEY . $hid);
|
||||||
|
return Redis::zrem(self::FOLLOW_KEY . $hid, $pid);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function following($pid, $start = 0, $limit = 10)
|
public static function following($hid, $start = 0, $limit = 10)
|
||||||
{
|
{
|
||||||
return Redis::zrevrange(self::FOLLOW_KEY . $pid, $start, $limit);
|
$synced = Cache::get(self::FOLLOW_KEY . 'acct-following:' . $hid . ':synced');
|
||||||
|
if(!$synced) {
|
||||||
|
$tags = HashtagFollow::whereHashtagId($hid)
|
||||||
|
->get()
|
||||||
|
->each(function($tag) use($hid) {
|
||||||
|
self::follow($tag->profile_id, $hid);
|
||||||
|
});
|
||||||
|
Cache::set(self::FOLLOW_KEY . 'acct-following:' . $hid . ':synced', true, 1209600);
|
||||||
|
|
||||||
|
return Redis::zrevrange(self::FOLLOW_KEY . $hid, $start, $limit);
|
||||||
|
}
|
||||||
|
return Redis::zrevrange(self::FOLLOW_KEY . $hid, $start, $limit);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue