Update ApiV1Controller, improve notification filtering

pull/4973/head
Daniel Supernault 1 year ago
parent 31e6487dc9
commit 01535a6cfe
No known key found for this signature in database
GPG Key ID: 23740873EE6F76A1

@ -2247,7 +2247,8 @@ class ApiV1Controller extends Controller
'max_id' => 'nullable|integer|min:1|max:'.PHP_INT_MAX, 'max_id' => 'nullable|integer|min:1|max:'.PHP_INT_MAX,
'since_id' => 'nullable|integer|min:1|max:'.PHP_INT_MAX, 'since_id' => 'nullable|integer|min:1|max:'.PHP_INT_MAX,
'types[]' => 'sometimes|array', 'types[]' => 'sometimes|array',
'type' => 'sometimes|string|in:mention,reblog,follow,favourite' 'type' => 'sometimes|string|in:mention,reblog,follow,favourite',
'_pe' => 'sometimes',
]); ]);
$pid = $request->user()->profile_id; $pid = $request->user()->profile_id;
@ -2259,6 +2260,7 @@ class ApiV1Controller extends Controller
$since = $request->input('since_id'); $since = $request->input('since_id');
$min = $request->input('min_id'); $min = $request->input('min_id');
$max = $request->input('max_id'); $max = $request->input('max_id');
$pe = $request->filled('_pe');
if(!$since && !$min && !$max) { if(!$since && !$min && !$max) {
$min = 1; $min = 1;
@ -2298,12 +2300,39 @@ class ApiV1Controller extends Controller
$minId = null; $minId = null;
} }
$res = collect($res)->filter(function($n) { $res = collect($res)
->map(function($n) use($pe) {
if(!$pe) {
if($n['type'] == 'comment') {
$n['type'] = 'mention';
return $n;
}
return $n;
}
return $n;
})
->filter(function($n) use($pe) {
if(in_array($n['type'], ['mention', 'reblog', 'favourite'])) { if(in_array($n['type'], ['mention', 'reblog', 'favourite'])) {
return isset($n['status'], $n['status']['id']); return isset($n['status'], $n['status']['id']);
} }
return isset($n['account'], $n['account']['id']); if(!$pe) {
if(in_array($n['type'], [
'tagged',
'modlog',
'story:react',
'story:comment',
'group:comment',
'group:join:approved',
'group:join:rejected',
])) {
return false;
}
return isset($n['account'], $n['account']['id']);
}
return true;
})->values(); })->values();
if($maxId) { if($maxId) {

@ -8,8 +8,14 @@ class MediaTag extends Model
{ {
protected $guarded = []; protected $guarded = [];
protected $visible = [
'status_id',
'profile_id',
'tagged_username',
];
public function status() public function status()
{ {
return $this->belongsTo(Status::class); return $this->belongsTo(Status::class);
} }
} }

@ -2,298 +2,324 @@
namespace App\Services; namespace App\Services;
use App\Jobs\InternalPipeline\NotificationEpochUpdatePipeline;
use App\Notification;
use App\Transformer\Api\NotificationTransformer;
use Cache; use Cache;
use Illuminate\Support\Facades\Redis; use Illuminate\Support\Facades\Redis;
use App\{
Notification,
Profile
};
use App\Transformer\Api\NotificationTransformer;
use League\Fractal; use League\Fractal;
use League\Fractal\Serializer\ArraySerializer; use League\Fractal\Serializer\ArraySerializer;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
use App\Jobs\InternalPipeline\NotificationEpochUpdatePipeline;
class NotificationService { class NotificationService
{
const CACHE_KEY = 'pf:services:notifications:ids:'; const CACHE_KEY = 'pf:services:notifications:ids:';
const EPOCH_CACHE_KEY = 'pf:services:notifications:epoch-id:by-months:';
const ITEM_CACHE_TTL = 86400; const EPOCH_CACHE_KEY = 'pf:services:notifications:epoch-id:by-months:';
const MASTODON_TYPES = [
'follow', const ITEM_CACHE_TTL = 86400;
'follow_request',
'mention', const MASTODON_TYPES = [
'reblog', 'follow',
'favourite', 'follow_request',
'poll', 'mention',
'status' 'reblog',
]; 'favourite',
'poll',
public static function get($id, $start = 0, $stop = 400) 'status',
{ ];
$res = collect([]);
$key = self::CACHE_KEY . $id; public static function get($id, $start = 0, $stop = 400)
$stop = $stop > 400 ? 400 : $stop; {
$ids = Redis::zrangebyscore($key, $start, $stop); $res = collect([]);
if(empty($ids)) { $key = self::CACHE_KEY.$id;
$ids = self::coldGet($id, $start, $stop); $stop = $stop > 400 ? 400 : $stop;
} $ids = Redis::zrangebyscore($key, $start, $stop);
foreach($ids as $id) { if (empty($ids)) {
$n = self::getNotification($id); $ids = self::coldGet($id, $start, $stop);
if($n != null) { }
$res->push($n); foreach ($ids as $id) {
} $n = self::getNotification($id);
} if ($n != null) {
return $res; $res->push($n);
} }
}
public static function getEpochId($months = 6)
{ return $res;
$epoch = Cache::get(self::EPOCH_CACHE_KEY . $months); }
if(!$epoch) {
NotificationEpochUpdatePipeline::dispatch(); public static function getEpochId($months = 6)
return 1; {
} $epoch = Cache::get(self::EPOCH_CACHE_KEY.$months);
return $epoch; if (! $epoch) {
} NotificationEpochUpdatePipeline::dispatch();
public static function coldGet($id, $start = 0, $stop = 400) return 1;
{ }
$stop = $stop > 400 ? 400 : $stop;
$ids = Notification::where('id', '>', self::getEpochId()) return $epoch;
->where('profile_id', $id) }
->orderByDesc('id')
->skip($start) public static function coldGet($id, $start = 0, $stop = 400)
->take($stop) {
->pluck('id'); $stop = $stop > 400 ? 400 : $stop;
foreach($ids as $key) { $ids = Notification::where('id', '>', self::getEpochId())
self::set($id, $key); ->where('profile_id', $id)
} ->orderByDesc('id')
return $ids; ->skip($start)
} ->take($stop)
->pluck('id');
public static function getMax($id = false, $start = 0, $limit = 10) foreach ($ids as $key) {
{ self::set($id, $key);
$ids = self::getRankedMaxId($id, $start, $limit); }
if(empty($ids)) { return $ids;
return []; }
}
public static function getMax($id = false, $start = 0, $limit = 10)
$res = collect([]); {
foreach($ids as $id) { $ids = self::getRankedMaxId($id, $start, $limit);
$n = self::getNotification($id);
if($n != null) { if (empty($ids)) {
$res->push($n); return [];
} }
}
return $res->toArray(); $res = collect([]);
} foreach ($ids as $id) {
$n = self::getNotification($id);
public static function getMin($id = false, $start = 0, $limit = 10) if ($n != null) {
{ $res->push($n);
$ids = self::getRankedMinId($id, $start, $limit); }
}
if(empty($ids)) {
return []; return $res->toArray();
} }
$res = collect([]); public static function getMin($id = false, $start = 0, $limit = 10)
foreach($ids as $id) { {
$n = self::getNotification($id); $ids = self::getRankedMinId($id, $start, $limit);
if($n != null) {
$res->push($n); if (empty($ids)) {
} return [];
} }
return $res->toArray();
} $res = collect([]);
foreach ($ids as $id) {
$n = self::getNotification($id);
public static function getMaxMastodon($id = false, $start = 0, $limit = 10) if ($n != null) {
{ $res->push($n);
$ids = self::getRankedMaxId($id, $start, $limit); }
}
if(empty($ids)) {
return []; return $res->toArray();
} }
$res = collect([]); public static function getMaxMastodon($id = false, $start = 0, $limit = 10)
foreach($ids as $id) { {
$n = self::rewriteMastodonTypes(self::getNotification($id)); $ids = self::getRankedMaxId($id, $start, $limit);
if($n != null && in_array($n['type'], self::MASTODON_TYPES)) {
if(isset($n['account'])) { if (empty($ids)) {
$n['account'] = AccountService::getMastodon($n['account']['id']); return [];
} }
if(isset($n['relationship'])) { $res = collect([]);
unset($n['relationship']); foreach ($ids as $id) {
} $n = self::rewriteMastodonTypes(self::getNotification($id));
if ($n != null && in_array($n['type'], self::MASTODON_TYPES)) {
if(isset($n['status'])) { if (isset($n['account'])) {
$n['status'] = StatusService::getMastodon($n['status']['id'], false); $n['account'] = AccountService::getMastodon($n['account']['id']);
} }
$res->push($n); if (isset($n['relationship'])) {
} unset($n['relationship']);
} }
return $res->toArray();
} if ($n['type'] === 'mention' && isset($n['tagged'], $n['tagged']['status_id'])) {
$n['status'] = StatusService::getMastodon($n['tagged']['status_id'], false);
public static function getMinMastodon($id = false, $start = 0, $limit = 10) unset($n['tagged']);
{ }
$ids = self::getRankedMinId($id, $start, $limit);
if (isset($n['status'])) {
if(empty($ids)) { $n['status'] = StatusService::getMastodon($n['status']['id'], false);
return []; }
}
$res->push($n);
$res = collect([]); }
foreach($ids as $id) { }
$n = self::rewriteMastodonTypes(self::getNotification($id));
if($n != null && in_array($n['type'], self::MASTODON_TYPES)) { return $res->toArray();
if(isset($n['account'])) { }
$n['account'] = AccountService::getMastodon($n['account']['id']);
} public static function getMinMastodon($id = false, $start = 0, $limit = 10)
{
if(isset($n['relationship'])) { $ids = self::getRankedMinId($id, $start, $limit);
unset($n['relationship']);
} if (empty($ids)) {
return [];
if(isset($n['status'])) { }
$n['status'] = StatusService::getMastodon($n['status']['id'], false);
} $res = collect([]);
foreach ($ids as $id) {
$res->push($n); $n = self::rewriteMastodonTypes(self::getNotification($id));
} if ($n != null && in_array($n['type'], self::MASTODON_TYPES)) {
} if (isset($n['account'])) {
return $res->toArray(); $n['account'] = AccountService::getMastodon($n['account']['id']);
} }
public static function getRankedMaxId($id = false, $start = null, $limit = 10) if (isset($n['relationship'])) {
{ unset($n['relationship']);
if(!$start || !$id) { }
return [];
} if ($n['type'] === 'mention' && isset($n['tagged'], $n['tagged']['status_id'])) {
$n['status'] = StatusService::getMastodon($n['tagged']['status_id'], false);
return array_keys(Redis::zrevrangebyscore(self::CACHE_KEY.$id, $start, '-inf', [ unset($n['tagged']);
'withscores' => true, }
'limit' => [1, $limit]
])); if (isset($n['status'])) {
} $n['status'] = StatusService::getMastodon($n['status']['id'], false);
}
public static function getRankedMinId($id = false, $end = null, $limit = 10)
{ $res->push($n);
if(!$end || !$id) { }
return []; }
}
return $res->toArray();
return array_keys(Redis::zrevrangebyscore(self::CACHE_KEY.$id, '+inf', $end, [ }
'withscores' => true,
'limit' => [0, $limit] public static function getRankedMaxId($id = false, $start = null, $limit = 10)
])); {
} if (! $start || ! $id) {
return [];
public static function rewriteMastodonTypes($notification) }
{
if(!$notification || !isset($notification['type'])) { return array_keys(Redis::zrevrangebyscore(self::CACHE_KEY.$id, $start, '-inf', [
return $notification; 'withscores' => true,
} 'limit' => [1, $limit],
]));
if($notification['type'] === 'comment') { }
$notification['type'] = 'mention';
} public static function getRankedMinId($id = false, $end = null, $limit = 10)
{
if($notification['type'] === 'share') { if (! $end || ! $id) {
$notification['type'] = 'reblog'; return [];
} }
return $notification; return array_keys(Redis::zrevrangebyscore(self::CACHE_KEY.$id, '+inf', $end, [
} 'withscores' => true,
'limit' => [0, $limit],
public static function set($id, $val) ]));
{ }
if(self::count($id) > 400) {
Redis::zpopmin(self::CACHE_KEY . $id); public static function rewriteMastodonTypes($notification)
} {
return Redis::zadd(self::CACHE_KEY . $id, $val, $val); if (! $notification || ! isset($notification['type'])) {
} return $notification;
}
public static function del($id, $val)
{ if ($notification['type'] === 'comment') {
Cache::forget('service:notification:' . $val); $notification['type'] = 'mention';
return Redis::zrem(self::CACHE_KEY . $id, $val); }
}
if ($notification['type'] === 'share') {
public static function add($id, $val) $notification['type'] = 'reblog';
{ }
return self::set($id, $val);
} if ($notification['type'] === 'tagged') {
$notification['type'] = 'mention';
public static function rem($id, $val) }
{
return self::del($id, $val); return $notification;
} }
public static function count($id) public static function set($id, $val)
{ {
return Redis::zcount(self::CACHE_KEY . $id, '-inf', '+inf'); if (self::count($id) > 400) {
} Redis::zpopmin(self::CACHE_KEY.$id);
}
public static function getNotification($id)
{ return Redis::zadd(self::CACHE_KEY.$id, $val, $val);
$notification = Cache::remember('service:notification:'.$id, self::ITEM_CACHE_TTL, function() use($id) { }
$n = Notification::with('item')->find($id);
public static function del($id, $val)
if(!$n) { {
return null; Cache::forget('service:notification:'.$val);
}
return Redis::zrem(self::CACHE_KEY.$id, $val);
$account = AccountService::get($n->actor_id, true); }
if(!$account) { public static function add($id, $val)
return null; {
} return self::set($id, $val);
}
$fractal = new Fractal\Manager();
$fractal->setSerializer(new ArraySerializer()); public static function rem($id, $val)
$resource = new Fractal\Resource\Item($n, new NotificationTransformer()); {
return $fractal->createData($resource)->toArray(); return self::del($id, $val);
}); }
if(!$notification) { public static function count($id)
return; {
} return Redis::zcount(self::CACHE_KEY.$id, '-inf', '+inf');
}
if(isset($notification['account'])) {
$notification['account'] = AccountService::get($notification['account']['id'], true); public static function getNotification($id)
} {
$notification = Cache::remember('service:notification:'.$id, self::ITEM_CACHE_TTL, function () use ($id) {
return $notification; $n = Notification::with('item')->find($id);
}
if (! $n) {
public static function setNotification(Notification $notification) return null;
{ }
return Cache::remember('service:notification:'.$notification->id, self::ITEM_CACHE_TTL, function() use($notification) {
$fractal = new Fractal\Manager(); $account = AccountService::get($n->actor_id, true);
$fractal->setSerializer(new ArraySerializer());
$resource = new Fractal\Resource\Item($notification, new NotificationTransformer()); if (! $account) {
return $fractal->createData($resource)->toArray(); return null;
}); }
}
$fractal = new Fractal\Manager();
public static function warmCache($id, $stop = 400, $force = false) $fractal->setSerializer(new ArraySerializer());
{ $resource = new Fractal\Resource\Item($n, new NotificationTransformer());
if(self::count($id) == 0 || $force == true) {
$ids = Notification::where('profile_id', $id) return $fractal->createData($resource)->toArray();
->where('id', '>', self::getEpochId()) });
->orderByDesc('id')
->limit($stop) if (! $notification) {
->pluck('id'); return;
foreach($ids as $key) { }
self::set($id, $key);
} if (isset($notification['account'])) {
return 1; $notification['account'] = AccountService::get($notification['account']['id'], true);
} }
return 0;
} return $notification;
}
public static function setNotification(Notification $notification)
{
return Cache::remember('service:notification:'.$notification->id, self::ITEM_CACHE_TTL, function () use ($notification) {
$fractal = new Fractal\Manager();
$fractal->setSerializer(new ArraySerializer());
$resource = new Fractal\Resource\Item($notification, new NotificationTransformer());
return $fractal->createData($resource)->toArray();
});
}
public static function warmCache($id, $stop = 400, $force = false)
{
if (self::count($id) == 0 || $force == true) {
$ids = Notification::where('profile_id', $id)
->where('id', '>', self::getEpochId())
->orderByDesc('id')
->limit($stop)
->pluck('id');
foreach ($ids as $key) {
self::set($id, $key);
}
return 1;
}
return 0;
}
} }

@ -4,78 +4,81 @@ namespace App\Transformer\Api;
use App\Notification; use App\Notification;
use App\Services\AccountService; use App\Services\AccountService;
use App\Services\HashidService;
use App\Services\RelationshipService; use App\Services\RelationshipService;
use App\Services\StatusService; use App\Services\StatusService;
use League\Fractal; use League\Fractal;
class NotificationTransformer extends Fractal\TransformerAbstract class NotificationTransformer extends Fractal\TransformerAbstract
{ {
public function transform(Notification $notification) public function transform(Notification $notification)
{ {
$res = [ $res = [
'id' => (string) $notification->id, 'id' => (string) $notification->id,
'type' => $this->replaceTypeVerb($notification->action), 'type' => $this->replaceTypeVerb($notification->action),
'created_at' => (string) str_replace('+00:00', 'Z', $notification->created_at->format(DATE_RFC3339_EXTENDED)), 'created_at' => (string) str_replace('+00:00', 'Z', $notification->created_at->format(DATE_RFC3339_EXTENDED)),
]; ];
$n = $notification; $n = $notification;
if($n->actor_id) { if ($n->actor_id) {
$res['account'] = AccountService::get($n->actor_id); $res['account'] = AccountService::get($n->actor_id);
if($n->profile_id != $n->actor_id) { if ($n->profile_id != $n->actor_id) {
$res['relationship'] = RelationshipService::get($n->actor_id, $n->profile_id); $res['relationship'] = RelationshipService::get($n->actor_id, $n->profile_id);
} }
} }
if($n->item_id && $n->item_type == 'App\Status') { if ($n->item_id && $n->item_type == 'App\Status') {
$res['status'] = StatusService::get($n->item_id, false); $res['status'] = StatusService::get($n->item_id, false);
} }
if($n->item_id && $n->item_type == 'App\ModLog') { if ($n->item_id && $n->item_type == 'App\ModLog') {
$ml = $n->item; $ml = $n->item;
if($ml && $ml->object_uid) { if ($ml && $ml->object_uid) {
$res['modlog'] = [ $res['modlog'] = [
'id' => $ml->object_uid, 'id' => $ml->object_uid,
'url' => url('/i/admin/users/modlogs/' . $ml->object_uid) 'url' => url('/i/admin/users/modlogs/'.$ml->object_uid),
]; ];
} }
} }
if($n->item_id && $n->item_type == 'App\MediaTag') { if ($n->item_id && $n->item_type == 'App\MediaTag') {
$ml = $n->item; $ml = $n->item;
if($ml && $ml->tagged_username) { if ($ml && $ml->tagged_username) {
$res['tagged'] = [ $np = StatusService::get($ml->status_id, false);
'username' => $ml->tagged_username, if ($np && isset($np['id'])) {
'post_url' => '/p/'.HashidService::encode($ml->status_id) $res['tagged'] = [
]; 'username' => $ml->tagged_username,
} 'post_url' => $np['url'],
} 'status_id' => $ml->status_id,
'profile_id' => $ml->profile_id,
];
}
}
}
return $res; return $res;
} }
public function replaceTypeVerb($verb) public function replaceTypeVerb($verb)
{ {
$verbs = [ $verbs = [
'dm' => 'direct', 'dm' => 'direct',
'follow' => 'follow', 'follow' => 'follow',
'mention' => 'mention', 'mention' => 'mention',
'reblog' => 'share', 'reblog' => 'share',
'share' => 'share', 'share' => 'share',
'like' => 'favourite', 'like' => 'favourite',
'group:like' => 'favourite', 'comment' => 'comment',
'comment' => 'comment', 'admin.user.modlog.comment' => 'modlog',
'admin.user.modlog.comment' => 'modlog', 'tagged' => 'tagged',
'tagged' => 'tagged', 'story:react' => 'story:react',
'story:react' => 'story:react', 'story:comment' => 'story:comment',
'story:comment' => 'story:comment', ];
];
if(!isset($verbs[$verb])) { if (! isset($verbs[$verb])) {
return $verb; return $verb;
} }
return $verbs[$verb]; return $verbs[$verb];
} }
} }

Loading…
Cancel
Save