Bladeren bron

Update FollowerService, cache audience

Daniel Supernault 4 jaren geleden
bovenliggende
commit
22257cc2a7
2 gewijzigde bestanden met toevoegingen van 12 en 46 verwijderingen
  1. 2 9
      app/Profile.php
  2. 10 37
      app/Services/FollowerService.php

+ 2 - 9
app/Profile.php

@@ -6,6 +6,7 @@ use Auth, Cache, Storage;
 use App\Util\Lexer\PrettyNumber;
 use Pixelfed\Snowflake\HasSnowflakePrimary;
 use Illuminate\Database\Eloquent\{Model, SoftDeletes};
+use App\Services\FollowerService;
 
 class Profile extends Model
 {
@@ -276,15 +277,7 @@ class Profile extends Model
 
     public function getAudienceInbox($scope = 'public')
     {
-        return $this
-            ->followers()
-            ->whereLocalProfile(false)
-            ->get()
-            ->map(function($follow) {
-                return $follow->sharedInbox ?? $follow->inbox_url;
-             })
-            ->unique()
-            ->toArray();
+        return FollowerService::audience($this->id, $scope);
     }
 
     public function circles()

+ 10 - 37
app/Services/FollowerService.php

@@ -3,7 +3,7 @@
 namespace App\Services;
 
 use Illuminate\Support\Facades\Redis;
-
+use Cache;
 use App\{
 	Follower,
 	Profile,
@@ -25,6 +25,8 @@ class FollowerService
 	{
 		Redis::zrem(self::FOLLOWING_KEY . $actor, $target);
 		Redis::zrem(self::FOLLOWERS_KEY . $target, $actor);
+		Cache::forget('pf:services:follow:audience:' . $actor);
+		Cache::forget('pf:services:follow:audience:' . $target);
 	}
 
 	public static function followers($id, $start = 0, $stop = 10)
@@ -42,46 +44,19 @@ class FollowerService
 		return Follower::whereProfileId($actor)->whereFollowingId($target)->exists();
 	}
 
-	public static function audience($profile)
+	public static function audience($profile, $scope = null)
 	{
 		return (new self)->getAudienceInboxes($profile);
 	}
 
-	protected function getAudienceInboxes($profile)
+	protected function getAudienceInboxes($profile, $scope = null)
 	{
-		if($profile instanceOf User) {
-			return $profile
-				->profile
-				->followers()
-				->whereLocalProfile(false)
-				->get()
-				->map(function($follow) {
-					return $follow->sharedInbox ?? $follow->inbox_url;
-				})
-				->unique()
-				->values()
-				->toArray();
-		}
-
-		if($profile instanceOf Profile) {
-			return $profile
-				->followers()
-				->whereLocalProfile(false)
-				->get()
-				->map(function($follow) {
-					return $follow->sharedInbox ?? $follow->inbox_url;
-				})
-				->unique()
-				->values()
-				->toArray();
+		if(!$profile instanceOf Profile) {
+			return [];
 		}
 
-		if(is_string($profile) || is_integer($profile)) {
-			$profile = Profile::whereNull('domain')->find($profile);
-			if(!$profile) {
-				return [];
-			}
-
+		$key = 'pf:services:follow:audience:' . $profile->id;
+		return Cache::remember($key, 86400, function() use($profile) {
 			return $profile
 				->followers()
 				->whereLocalProfile(false)
@@ -92,9 +67,7 @@ class FollowerService
 				->unique()
 				->values()
 				->toArray();
-		}
-
-		return [];
+		});
 	}
 
 }