Merge pull request #4973 from pixelfed/staging

Update ApiV1Controller, fix Notifications endpoint
This commit is contained in:
daniel 2024-03-05 02:11:07 -07:00 committed by GitHub
commit eb19c35343
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 1450 additions and 1369 deletions

View File

@ -26,6 +26,8 @@
- Update AdminCuratedRegisterController, filter confirmation activities from activitylog ([ab9ecb6e](https://github.com/pixelfed/pixelfed/commit/ab9ecb6e)) - Update AdminCuratedRegisterController, filter confirmation activities from activitylog ([ab9ecb6e](https://github.com/pixelfed/pixelfed/commit/ab9ecb6e))
- Update Inbox, fix flag validation condition, allow profile reports ([402a4607](https://github.com/pixelfed/pixelfed/commit/402a4607)) - Update Inbox, fix flag validation condition, allow profile reports ([402a4607](https://github.com/pixelfed/pixelfed/commit/402a4607))
- Update AccountTransformer, fix follower/following count visibility bug ([542d1106](https://github.com/pixelfed/pixelfed/commit/542d1106)) - Update AccountTransformer, fix follower/following count visibility bug ([542d1106](https://github.com/pixelfed/pixelfed/commit/542d1106))
- Update ProfileMigration model, add target relation ([3f053997](https://github.com/pixelfed/pixelfed/commit/3f053997))
- Update ApiV1Controller, update Notifications endpoint to filter notifications with missing activities ([a933615b](https://github.com/pixelfed/pixelfed/commit/a933615b))
- ([](https://github.com/pixelfed/pixelfed/commit/)) - ([](https://github.com/pixelfed/pixelfed/commit/))
## [v0.11.12 (2024-02-16)](https://github.com/pixelfed/pixelfed/compare/v0.11.11...v0.11.12) ## [v0.11.12 (2024-02-16)](https://github.com/pixelfed/pixelfed/compare/v0.11.11...v0.11.12)

File diff suppressed because it is too large Load Diff

View File

@ -8,6 +8,12 @@ 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);

View File

@ -2,23 +2,22 @@
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 EPOCH_CACHE_KEY = 'pf:services:notifications:epoch-id:by-months:';
const ITEM_CACHE_TTL = 86400; const ITEM_CACHE_TTL = 86400;
const MASTODON_TYPES = [ const MASTODON_TYPES = [
'follow', 'follow',
'follow_request', 'follow_request',
@ -26,34 +25,37 @@ class NotificationService {
'reblog', 'reblog',
'favourite', 'favourite',
'poll', 'poll',
'status' 'status',
]; ];
public static function get($id, $start = 0, $stop = 400) public static function get($id, $start = 0, $stop = 400)
{ {
$res = collect([]); $res = collect([]);
$key = self::CACHE_KEY . $id; $key = self::CACHE_KEY.$id;
$stop = $stop > 400 ? 400 : $stop; $stop = $stop > 400 ? 400 : $stop;
$ids = Redis::zrangebyscore($key, $start, $stop); $ids = Redis::zrangebyscore($key, $start, $stop);
if(empty($ids)) { if (empty($ids)) {
$ids = self::coldGet($id, $start, $stop); $ids = self::coldGet($id, $start, $stop);
} }
foreach($ids as $id) { foreach ($ids as $id) {
$n = self::getNotification($id); $n = self::getNotification($id);
if($n != null) { if ($n != null) {
$res->push($n); $res->push($n);
} }
} }
return $res; return $res;
} }
public static function getEpochId($months = 6) public static function getEpochId($months = 6)
{ {
$epoch = Cache::get(self::EPOCH_CACHE_KEY . $months); $epoch = Cache::get(self::EPOCH_CACHE_KEY.$months);
if(!$epoch) { if (! $epoch) {
NotificationEpochUpdatePipeline::dispatch(); NotificationEpochUpdatePipeline::dispatch();
return 1; return 1;
} }
return $epoch; return $epoch;
} }
@ -66,9 +68,10 @@ class NotificationService {
->skip($start) ->skip($start)
->take($stop) ->take($stop)
->pluck('id'); ->pluck('id');
foreach($ids as $key) { foreach ($ids as $key) {
self::set($id, $key); self::set($id, $key);
} }
return $ids; return $ids;
} }
@ -76,17 +79,18 @@ class NotificationService {
{ {
$ids = self::getRankedMaxId($id, $start, $limit); $ids = self::getRankedMaxId($id, $start, $limit);
if(empty($ids)) { if (empty($ids)) {
return []; return [];
} }
$res = collect([]); $res = collect([]);
foreach($ids as $id) { foreach ($ids as $id) {
$n = self::getNotification($id); $n = self::getNotification($id);
if($n != null) { if ($n != null) {
$res->push($n); $res->push($n);
} }
} }
return $res->toArray(); return $res->toArray();
} }
@ -94,48 +98,54 @@ class NotificationService {
{ {
$ids = self::getRankedMinId($id, $start, $limit); $ids = self::getRankedMinId($id, $start, $limit);
if(empty($ids)) { if (empty($ids)) {
return []; return [];
} }
$res = collect([]); $res = collect([]);
foreach($ids as $id) { foreach ($ids as $id) {
$n = self::getNotification($id); $n = self::getNotification($id);
if($n != null) { if ($n != null) {
$res->push($n); $res->push($n);
} }
} }
return $res->toArray(); return $res->toArray();
} }
public static function getMaxMastodon($id = false, $start = 0, $limit = 10) public static function getMaxMastodon($id = false, $start = 0, $limit = 10)
{ {
$ids = self::getRankedMaxId($id, $start, $limit); $ids = self::getRankedMaxId($id, $start, $limit);
if(empty($ids)) { if (empty($ids)) {
return []; return [];
} }
$res = collect([]); $res = collect([]);
foreach($ids as $id) { foreach ($ids as $id) {
$n = self::rewriteMastodonTypes(self::getNotification($id)); $n = self::rewriteMastodonTypes(self::getNotification($id));
if($n != null && in_array($n['type'], self::MASTODON_TYPES)) { if ($n != null && in_array($n['type'], self::MASTODON_TYPES)) {
if(isset($n['account'])) { if (isset($n['account'])) {
$n['account'] = AccountService::getMastodon($n['account']['id']); $n['account'] = AccountService::getMastodon($n['account']['id']);
} }
if(isset($n['relationship'])) { if (isset($n['relationship'])) {
unset($n['relationship']); unset($n['relationship']);
} }
if(isset($n['status'])) { if ($n['type'] === 'mention' && isset($n['tagged'], $n['tagged']['status_id'])) {
$n['status'] = StatusService::getMastodon($n['tagged']['status_id'], false);
unset($n['tagged']);
}
if (isset($n['status'])) {
$n['status'] = StatusService::getMastodon($n['status']['id'], false); $n['status'] = StatusService::getMastodon($n['status']['id'], false);
} }
$res->push($n); $res->push($n);
} }
} }
return $res->toArray(); return $res->toArray();
} }
@ -143,85 +153,97 @@ class NotificationService {
{ {
$ids = self::getRankedMinId($id, $start, $limit); $ids = self::getRankedMinId($id, $start, $limit);
if(empty($ids)) { if (empty($ids)) {
return []; return [];
} }
$res = collect([]); $res = collect([]);
foreach($ids as $id) { foreach ($ids as $id) {
$n = self::rewriteMastodonTypes(self::getNotification($id)); $n = self::rewriteMastodonTypes(self::getNotification($id));
if($n != null && in_array($n['type'], self::MASTODON_TYPES)) { if ($n != null && in_array($n['type'], self::MASTODON_TYPES)) {
if(isset($n['account'])) { if (isset($n['account'])) {
$n['account'] = AccountService::getMastodon($n['account']['id']); $n['account'] = AccountService::getMastodon($n['account']['id']);
} }
if(isset($n['relationship'])) { if (isset($n['relationship'])) {
unset($n['relationship']); unset($n['relationship']);
} }
if(isset($n['status'])) { if ($n['type'] === 'mention' && isset($n['tagged'], $n['tagged']['status_id'])) {
$n['status'] = StatusService::getMastodon($n['tagged']['status_id'], false);
unset($n['tagged']);
}
if (isset($n['status'])) {
$n['status'] = StatusService::getMastodon($n['status']['id'], false); $n['status'] = StatusService::getMastodon($n['status']['id'], false);
} }
$res->push($n); $res->push($n);
} }
} }
return $res->toArray(); return $res->toArray();
} }
public static function getRankedMaxId($id = false, $start = null, $limit = 10) public static function getRankedMaxId($id = false, $start = null, $limit = 10)
{ {
if(!$start || !$id) { if (! $start || ! $id) {
return []; return [];
} }
return array_keys(Redis::zrevrangebyscore(self::CACHE_KEY.$id, $start, '-inf', [ return array_keys(Redis::zrevrangebyscore(self::CACHE_KEY.$id, $start, '-inf', [
'withscores' => true, 'withscores' => true,
'limit' => [1, $limit] 'limit' => [1, $limit],
])); ]));
} }
public static function getRankedMinId($id = false, $end = null, $limit = 10) public static function getRankedMinId($id = false, $end = null, $limit = 10)
{ {
if(!$end || !$id) { if (! $end || ! $id) {
return []; return [];
} }
return array_keys(Redis::zrevrangebyscore(self::CACHE_KEY.$id, '+inf', $end, [ return array_keys(Redis::zrevrangebyscore(self::CACHE_KEY.$id, '+inf', $end, [
'withscores' => true, 'withscores' => true,
'limit' => [0, $limit] 'limit' => [0, $limit],
])); ]));
} }
public static function rewriteMastodonTypes($notification) public static function rewriteMastodonTypes($notification)
{ {
if(!$notification || !isset($notification['type'])) { if (! $notification || ! isset($notification['type'])) {
return $notification; return $notification;
} }
if($notification['type'] === 'comment') { if ($notification['type'] === 'comment') {
$notification['type'] = 'mention'; $notification['type'] = 'mention';
} }
if($notification['type'] === 'share') { if ($notification['type'] === 'share') {
$notification['type'] = 'reblog'; $notification['type'] = 'reblog';
} }
if ($notification['type'] === 'tagged') {
$notification['type'] = 'mention';
}
return $notification; return $notification;
} }
public static function set($id, $val) public static function set($id, $val)
{ {
if(self::count($id) > 400) { if (self::count($id) > 400) {
Redis::zpopmin(self::CACHE_KEY . $id); Redis::zpopmin(self::CACHE_KEY.$id);
} }
return Redis::zadd(self::CACHE_KEY . $id, $val, $val);
return Redis::zadd(self::CACHE_KEY.$id, $val, $val);
} }
public static function del($id, $val) public static function del($id, $val)
{ {
Cache::forget('service:notification:' . $val); Cache::forget('service:notification:'.$val);
return Redis::zrem(self::CACHE_KEY . $id, $val);
return Redis::zrem(self::CACHE_KEY.$id, $val);
} }
public static function add($id, $val) public static function add($id, $val)
@ -236,35 +258,36 @@ class NotificationService {
public static function count($id) public static function count($id)
{ {
return Redis::zcount(self::CACHE_KEY . $id, '-inf', '+inf'); return Redis::zcount(self::CACHE_KEY.$id, '-inf', '+inf');
} }
public static function getNotification($id) public static function getNotification($id)
{ {
$notification = Cache::remember('service:notification:'.$id, self::ITEM_CACHE_TTL, function() use($id) { $notification = Cache::remember('service:notification:'.$id, self::ITEM_CACHE_TTL, function () use ($id) {
$n = Notification::with('item')->find($id); $n = Notification::with('item')->find($id);
if(!$n) { if (! $n) {
return null; return null;
} }
$account = AccountService::get($n->actor_id, true); $account = AccountService::get($n->actor_id, true);
if(!$account) { if (! $account) {
return null; return null;
} }
$fractal = new Fractal\Manager(); $fractal = new Fractal\Manager();
$fractal->setSerializer(new ArraySerializer()); $fractal->setSerializer(new ArraySerializer());
$resource = new Fractal\Resource\Item($n, new NotificationTransformer()); $resource = new Fractal\Resource\Item($n, new NotificationTransformer());
return $fractal->createData($resource)->toArray(); return $fractal->createData($resource)->toArray();
}); });
if(!$notification) { if (! $notification) {
return; return;
} }
if(isset($notification['account'])) { if (isset($notification['account'])) {
$notification['account'] = AccountService::get($notification['account']['id'], true); $notification['account'] = AccountService::get($notification['account']['id'], true);
} }
@ -273,27 +296,30 @@ class NotificationService {
public static function setNotification(Notification $notification) public static function setNotification(Notification $notification)
{ {
return Cache::remember('service:notification:'.$notification->id, self::ITEM_CACHE_TTL, function() use($notification) { return Cache::remember('service:notification:'.$notification->id, self::ITEM_CACHE_TTL, function () use ($notification) {
$fractal = new Fractal\Manager(); $fractal = new Fractal\Manager();
$fractal->setSerializer(new ArraySerializer()); $fractal->setSerializer(new ArraySerializer());
$resource = new Fractal\Resource\Item($notification, new NotificationTransformer()); $resource = new Fractal\Resource\Item($notification, new NotificationTransformer());
return $fractal->createData($resource)->toArray(); return $fractal->createData($resource)->toArray();
}); });
} }
public static function warmCache($id, $stop = 400, $force = false) public static function warmCache($id, $stop = 400, $force = false)
{ {
if(self::count($id) == 0 || $force == true) { if (self::count($id) == 0 || $force == true) {
$ids = Notification::where('profile_id', $id) $ids = Notification::where('profile_id', $id)
->where('id', '>', self::getEpochId()) ->where('id', '>', self::getEpochId())
->orderByDesc('id') ->orderByDesc('id')
->limit($stop) ->limit($stop)
->pluck('id'); ->pluck('id');
foreach($ids as $key) { foreach ($ids as $key) {
self::set($id, $key); self::set($id, $key);
} }
return 1; return 1;
} }
return 0; return 0;
} }
} }

View File

@ -4,7 +4,6 @@ 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;
@ -21,36 +20,41 @@ class NotificationTransformer extends Fractal\TransformerAbstract
$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) {
$np = StatusService::get($ml->status_id, false);
if ($np && isset($np['id'])) {
$res['tagged'] = [ $res['tagged'] = [
'username' => $ml->tagged_username, 'username' => $ml->tagged_username,
'post_url' => '/p/'.HashidService::encode($ml->status_id) 'post_url' => $np['url'],
'status_id' => $ml->status_id,
'profile_id' => $ml->profile_id,
]; ];
} }
} }
}
return $res; return $res;
} }
@ -64,7 +68,6 @@ class NotificationTransformer extends Fractal\TransformerAbstract
'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',
@ -72,7 +75,7 @@ class NotificationTransformer extends Fractal\TransformerAbstract
'story:comment' => 'story:comment', 'story:comment' => 'story:comment',
]; ];
if(!isset($verbs[$verb])) { if (! isset($verbs[$verb])) {
return $verb; return $verb;
} }