pixelfed/app/Services/LikeService.php

89 lines
1.9 KiB
PHP
Raw Normal View History

2021-05-01 21:51:02 +00:00
<?php
namespace App\Services;
use App\Util\ActivityPub\Helpers;
2021-07-02 07:31:45 +00:00
use Illuminate\Support\Facades\Cache;
2021-05-01 21:51:02 +00:00
use Illuminate\Support\Facades\Redis;
use App\Like;
class LikeService {
const CACHE_KEY = 'pf:services:likes:ids:';
2021-07-02 07:31:45 +00:00
public static function add($profileId, $statusId)
2021-05-01 21:51:02 +00:00
{
2021-07-02 07:31:45 +00:00
$key = self::CACHE_KEY . $profileId . ':' . $statusId;
$ttl = now()->addHours(2);
return Cache::put($key, true, $ttl);
2021-05-01 21:51:02 +00:00
}
2021-07-02 07:31:45 +00:00
public static function remove($profileId, $statusId)
2021-05-01 21:51:02 +00:00
{
2021-07-02 07:31:45 +00:00
$key = self::CACHE_KEY . $profileId . ':' . $statusId;
$ttl = now()->addHours(2);
return Cache::put($key, false, $ttl);
2021-05-01 21:51:02 +00:00
}
public static function liked($profileId, $statusId)
{
2021-07-02 07:31:45 +00:00
$key = self::CACHE_KEY . $profileId . ':' . $statusId;
$ttl = now()->addMinutes(30);
return Cache::remember($key, $ttl, function() use($profileId, $statusId) {
return Like::whereProfileId($profileId)->whereStatusId($statusId)->exists();
});
2021-05-01 21:51:02 +00:00
}
public static function likedBy($status)
{
2021-05-03 23:55:06 +00:00
$empty = [
'username' => null,
'others' => false
];
if(!$status) {
return $empty;
}
2021-05-01 21:51:02 +00:00
if(!$status->likes_count) {
2021-05-03 23:55:06 +00:00
return $empty;
2021-05-01 21:51:02 +00:00
}
2021-06-24 03:26:45 +00:00
$user = request()->user();
2021-05-03 23:55:06 +00:00
2021-06-24 03:26:45 +00:00
if($user) {
$like = Like::whereStatusId($status->id)
->where('profile_id', '!=', $user->profile_id)
->first();
} else {
$like = Like::whereStatusId($status->id)
->first();
}
2021-05-03 23:55:06 +00:00
if(!$like) {
return $empty;
}
$id = $like->profile_id;
$profile = ProfileService::get($id);
$profileUrl = $profile['local'] ? $profile['url'] : '/i/web/profile/_/' . $profile['id'];
$res = [
'username' => $profile['username'],
'url' => $profileUrl,
'others' => $status->likes_count >= 3,
2021-05-01 21:51:02 +00:00
];
if(request()->user() && request()->user()->profile_id == $status->profile_id) {
$res['total_count'] = ($status->likes_count - 1);
$res['total_count_pretty'] = number_format($res['total_count']);
}
return $res;
2021-05-01 21:51:02 +00:00
}
2021-08-31 06:37:40 +00:00
public static function count($id)
{
return Like::whereStatusId($id)->count();
}
2021-05-01 21:51:02 +00:00
}