diff --git a/app/Services/MediaTagService.php b/app/Services/MediaTagService.php new file mode 100644 index 000000000..087487d44 --- /dev/null +++ b/app/Services/MediaTagService.php @@ -0,0 +1,78 @@ +addMinutes(60), function() use($mediaId, $usernames) { + $key = self::CACHE_KEY . $mediaId; + if(Redis::zCount($key, '-inf', '+inf') == 0) { + return []; + } + $res = Redis::zRange($key, 0, -1); + if(!$usernames) { + return $res; + } + $usernames = []; + foreach ($res as $k) { + $username = (new self)->idToUsername($k); + array_push($usernames, $username); + } + + return $usernames; + }); + } + + public static function set($mediaId, $profileId) + { + $key = self::CACHE_KEY . $mediaId; + Redis::zAdd($key, $profileId, $profileId); + return true; + } + + protected function idToUsername($id) + { + $profile = ProfileService::build()->profileId($id); + + if(!$profile) { + return 'unavailable'; + } + + return [ + 'username' => $profile->username, + 'avatar' => $profile->avatarUrl() + ]; + } + + public static function sendNotification(MediaTag $tag) + { + $p = $tag->status->profile; + $actor = $p->username; + $message = "{$actor} tagged you in a post."; + $rendered = "{$actor} tagged you in a post."; + $n = new Notification; + $n->profile_id = $tag->profile_id; + $n->actor_id = $p->id; + $n->item_id = $tag->id; + $n->item_type = 'App\MediaTag'; + $n->action = 'tagged'; + $n->message = $message; + $n->rendered = $rendered; + $n->save(); + return; + } + +} \ No newline at end of file