2019-05-01 04:40:15 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
2019-12-11 06:04:03 +00:00
|
|
|
use Cache;
|
|
|
|
use Illuminate\Support\Facades\Redis;
|
2019-05-01 04:40:15 +00:00
|
|
|
use App\{
|
|
|
|
Notification,
|
|
|
|
Profile
|
|
|
|
};
|
|
|
|
use App\Transformer\Api\NotificationTransformer;
|
|
|
|
use League\Fractal;
|
|
|
|
use League\Fractal\Serializer\ArraySerializer;
|
|
|
|
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
|
2023-11-13 08:59:38 +00:00
|
|
|
use App\Jobs\InternalPipeline\NotificationEpochUpdatePipeline;
|
2019-05-01 04:40:15 +00:00
|
|
|
|
|
|
|
class NotificationService {
|
|
|
|
|
|
|
|
const CACHE_KEY = 'pf:services:notifications:ids:';
|
2023-09-18 05:51:42 +00:00
|
|
|
const EPOCH_CACHE_KEY = 'pf:services:notifications:epoch-id:by-months:';
|
|
|
|
const ITEM_CACHE_TTL = 86400;
|
2022-03-23 06:28:39 +00:00
|
|
|
const MASTODON_TYPES = [
|
|
|
|
'follow',
|
|
|
|
'follow_request',
|
|
|
|
'mention',
|
|
|
|
'reblog',
|
|
|
|
'favourite',
|
|
|
|
'poll',
|
|
|
|
'status'
|
|
|
|
];
|
2019-05-01 04:40:15 +00:00
|
|
|
|
2019-06-05 07:18:09 +00:00
|
|
|
public static function get($id, $start = 0, $stop = 400)
|
2019-05-01 04:40:15 +00:00
|
|
|
{
|
|
|
|
$res = collect([]);
|
|
|
|
$key = self::CACHE_KEY . $id;
|
2019-06-05 07:18:09 +00:00
|
|
|
$stop = $stop > 400 ? 400 : $stop;
|
2019-05-01 04:40:15 +00:00
|
|
|
$ids = Redis::zrangebyscore($key, $start, $stop);
|
|
|
|
if(empty($ids)) {
|
|
|
|
$ids = self::coldGet($id, $start, $stop);
|
|
|
|
}
|
|
|
|
foreach($ids as $id) {
|
2021-09-20 05:05:56 +00:00
|
|
|
$n = self::getNotification($id);
|
|
|
|
if($n != null) {
|
|
|
|
$res->push($n);
|
|
|
|
}
|
2019-05-01 04:40:15 +00:00
|
|
|
}
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
2023-09-18 05:51:42 +00:00
|
|
|
public static function getEpochId($months = 6)
|
|
|
|
{
|
2023-11-13 08:59:38 +00:00
|
|
|
$epoch = Cache::get(self::EPOCH_CACHE_KEY . $months);
|
|
|
|
if(!$epoch) {
|
|
|
|
NotificationEpochUpdatePipeline::dispatch();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return $epoch;
|
2023-09-18 05:51:42 +00:00
|
|
|
}
|
|
|
|
|
2019-06-05 07:18:09 +00:00
|
|
|
public static function coldGet($id, $start = 0, $stop = 400)
|
2019-05-01 04:40:15 +00:00
|
|
|
{
|
2019-06-05 07:18:09 +00:00
|
|
|
$stop = $stop > 400 ? 400 : $stop;
|
2023-09-18 05:51:42 +00:00
|
|
|
$ids = Notification::where('id', '>', self::getEpochId())
|
|
|
|
->where('profile_id', $id)
|
2023-09-18 06:25:01 +00:00
|
|
|
->orderByDesc('id')
|
2019-05-01 04:40:15 +00:00
|
|
|
->skip($start)
|
|
|
|
->take($stop)
|
|
|
|
->pluck('id');
|
|
|
|
foreach($ids as $key) {
|
|
|
|
self::set($id, $key);
|
|
|
|
}
|
|
|
|
return $ids;
|
|
|
|
}
|
|
|
|
|
2021-08-31 06:39:03 +00:00
|
|
|
public static function getMax($id = false, $start = 0, $limit = 10)
|
2021-07-07 02:10:42 +00:00
|
|
|
{
|
|
|
|
$ids = self::getRankedMaxId($id, $start, $limit);
|
|
|
|
|
|
|
|
if(empty($ids)) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$res = collect([]);
|
|
|
|
foreach($ids as $id) {
|
2021-09-20 05:05:56 +00:00
|
|
|
$n = self::getNotification($id);
|
|
|
|
if($n != null) {
|
|
|
|
$res->push($n);
|
|
|
|
}
|
2021-07-07 02:10:42 +00:00
|
|
|
}
|
|
|
|
return $res->toArray();
|
|
|
|
}
|
|
|
|
|
2021-08-31 06:39:03 +00:00
|
|
|
public static function getMin($id = false, $start = 0, $limit = 10)
|
2021-07-07 02:10:42 +00:00
|
|
|
{
|
|
|
|
$ids = self::getRankedMinId($id, $start, $limit);
|
|
|
|
|
|
|
|
if(empty($ids)) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$res = collect([]);
|
|
|
|
foreach($ids as $id) {
|
2021-09-20 05:05:56 +00:00
|
|
|
$n = self::getNotification($id);
|
|
|
|
if($n != null) {
|
|
|
|
$res->push($n);
|
|
|
|
}
|
2021-07-07 02:10:42 +00:00
|
|
|
}
|
|
|
|
return $res->toArray();
|
|
|
|
}
|
|
|
|
|
2022-03-23 06:28:39 +00:00
|
|
|
|
|
|
|
public static function getMaxMastodon($id = false, $start = 0, $limit = 10)
|
|
|
|
{
|
|
|
|
$ids = self::getRankedMaxId($id, $start, $limit);
|
|
|
|
|
|
|
|
if(empty($ids)) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$res = collect([]);
|
|
|
|
foreach($ids as $id) {
|
2022-08-04 08:13:33 +00:00
|
|
|
$n = self::rewriteMastodonTypes(self::getNotification($id));
|
2022-03-23 06:28:39 +00:00
|
|
|
if($n != null && in_array($n['type'], self::MASTODON_TYPES)) {
|
2022-08-30 02:06:59 +00:00
|
|
|
if(isset($n['account'])) {
|
|
|
|
$n['account'] = AccountService::getMastodon($n['account']['id']);
|
|
|
|
}
|
2022-03-23 06:28:39 +00:00
|
|
|
|
|
|
|
if(isset($n['relationship'])) {
|
|
|
|
unset($n['relationship']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(isset($n['status'])) {
|
|
|
|
$n['status'] = StatusService::getMastodon($n['status']['id'], false);
|
|
|
|
}
|
|
|
|
|
|
|
|
$res->push($n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $res->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getMinMastodon($id = false, $start = 0, $limit = 10)
|
|
|
|
{
|
|
|
|
$ids = self::getRankedMinId($id, $start, $limit);
|
|
|
|
|
|
|
|
if(empty($ids)) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$res = collect([]);
|
|
|
|
foreach($ids as $id) {
|
2022-08-04 08:13:33 +00:00
|
|
|
$n = self::rewriteMastodonTypes(self::getNotification($id));
|
2022-03-23 06:28:39 +00:00
|
|
|
if($n != null && in_array($n['type'], self::MASTODON_TYPES)) {
|
2022-08-30 02:06:59 +00:00
|
|
|
if(isset($n['account'])) {
|
|
|
|
$n['account'] = AccountService::getMastodon($n['account']['id']);
|
|
|
|
}
|
2022-03-23 06:28:39 +00:00
|
|
|
|
|
|
|
if(isset($n['relationship'])) {
|
|
|
|
unset($n['relationship']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(isset($n['status'])) {
|
|
|
|
$n['status'] = StatusService::getMastodon($n['status']['id'], false);
|
|
|
|
}
|
|
|
|
|
|
|
|
$res->push($n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $res->toArray();
|
|
|
|
}
|
|
|
|
|
2021-07-07 02:10:42 +00:00
|
|
|
public static function getRankedMaxId($id = false, $start = null, $limit = 10)
|
|
|
|
{
|
|
|
|
if(!$start || !$id) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_keys(Redis::zrevrangebyscore(self::CACHE_KEY.$id, $start, '-inf', [
|
|
|
|
'withscores' => true,
|
|
|
|
'limit' => [1, $limit]
|
|
|
|
]));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getRankedMinId($id = false, $end = null, $limit = 10)
|
|
|
|
{
|
|
|
|
if(!$end || !$id) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_keys(Redis::zrevrangebyscore(self::CACHE_KEY.$id, '+inf', $end, [
|
|
|
|
'withscores' => true,
|
|
|
|
'limit' => [0, $limit]
|
|
|
|
]));
|
|
|
|
}
|
|
|
|
|
2022-08-04 08:13:33 +00:00
|
|
|
public static function rewriteMastodonTypes($notification)
|
|
|
|
{
|
|
|
|
if(!$notification || !isset($notification['type'])) {
|
|
|
|
return $notification;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($notification['type'] === 'comment') {
|
|
|
|
$notification['type'] = 'mention';
|
|
|
|
}
|
|
|
|
|
|
|
|
if($notification['type'] === 'share') {
|
|
|
|
$notification['type'] = 'reblog';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $notification;
|
|
|
|
}
|
|
|
|
|
2019-05-01 04:40:15 +00:00
|
|
|
public static function set($id, $val)
|
|
|
|
{
|
2022-08-18 05:08:40 +00:00
|
|
|
if(self::count($id) > 400) {
|
|
|
|
Redis::zpopmin(self::CACHE_KEY . $id);
|
|
|
|
}
|
2019-05-01 04:40:15 +00:00
|
|
|
return Redis::zadd(self::CACHE_KEY . $id, $val, $val);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function del($id, $val)
|
|
|
|
{
|
2021-07-07 02:10:42 +00:00
|
|
|
Cache::forget('service:notification:' . $val);
|
2019-05-01 04:40:15 +00:00
|
|
|
return Redis::zrem(self::CACHE_KEY . $id, $val);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function add($id, $val)
|
|
|
|
{
|
|
|
|
return self::set($id, $val);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function rem($id, $val)
|
|
|
|
{
|
|
|
|
return self::del($id, $val);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function count($id)
|
|
|
|
{
|
|
|
|
return Redis::zcount(self::CACHE_KEY . $id, '-inf', '+inf');
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getNotification($id)
|
|
|
|
{
|
2023-09-18 05:51:42 +00:00
|
|
|
$notification = Cache::remember('service:notification:'.$id, self::ITEM_CACHE_TTL, function() use($id) {
|
2021-09-20 05:05:56 +00:00
|
|
|
$n = Notification::with('item')->find($id);
|
|
|
|
|
|
|
|
if(!$n) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-07-22 02:12:07 +00:00
|
|
|
$account = AccountService::get($n->actor_id, true);
|
|
|
|
|
|
|
|
if(!$account) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-05-01 04:40:15 +00:00
|
|
|
$fractal = new Fractal\Manager();
|
|
|
|
$fractal->setSerializer(new ArraySerializer());
|
|
|
|
$resource = new Fractal\Resource\Item($n, new NotificationTransformer());
|
|
|
|
return $fractal->createData($resource)->toArray();
|
|
|
|
});
|
2022-08-18 05:08:40 +00:00
|
|
|
|
|
|
|
if(!$notification) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(isset($notification['account'])) {
|
|
|
|
$notification['account'] = AccountService::get($notification['account']['id'], true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $notification;
|
2019-05-21 03:32:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function setNotification(Notification $notification)
|
|
|
|
{
|
2023-09-18 05:51:42 +00:00
|
|
|
return Cache::remember('service:notification:'.$notification->id, self::ITEM_CACHE_TTL, function() use($notification) {
|
2019-05-21 03:32:35 +00:00
|
|
|
$fractal = new Fractal\Manager();
|
|
|
|
$fractal->setSerializer(new ArraySerializer());
|
|
|
|
$resource = new Fractal\Resource\Item($notification, new NotificationTransformer());
|
|
|
|
return $fractal->createData($resource)->toArray();
|
|
|
|
});
|
2023-09-18 05:51:42 +00:00
|
|
|
}
|
2019-05-01 04:40:15 +00:00
|
|
|
|
2019-06-05 07:18:09 +00:00
|
|
|
public static function warmCache($id, $stop = 400, $force = false)
|
2019-05-01 04:40:15 +00:00
|
|
|
{
|
|
|
|
if(self::count($id) == 0 || $force == true) {
|
2023-09-18 05:51:42 +00:00
|
|
|
$ids = Notification::where('profile_id', $id)
|
|
|
|
->where('id', '>', self::getEpochId())
|
2023-09-18 06:25:01 +00:00
|
|
|
->orderByDesc('id')
|
2019-05-01 04:40:15 +00:00
|
|
|
->limit($stop)
|
|
|
|
->pluck('id');
|
|
|
|
foreach($ids as $key) {
|
|
|
|
self::set($id, $key);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2021-07-07 02:10:42 +00:00
|
|
|
}
|