Passing photos_reblogs_only onto the API and formatting database incoming data accordingly

This commit is contained in:
Mehdi Benadel 2024-11-16 05:18:12 +01:00
parent 3749267260
commit cfff0089c3
2 changed files with 86 additions and 134 deletions

View file

@ -2469,6 +2469,7 @@ class ApiV1Controller extends Controller
'max_id' => 'sometimes|integer|min:0|max:'.PHP_INT_MAX, 'max_id' => 'sometimes|integer|min:0|max:'.PHP_INT_MAX,
'limit' => 'sometimes|integer|min:1', 'limit' => 'sometimes|integer|min:1',
'include_reblogs' => 'sometimes', 'include_reblogs' => 'sometimes',
'photos_reblogs_only' => 'sometimes',
]); ]);
$napi = $request->has(self::PF_API_ENTITY_KEY); $napi = $request->has(self::PF_API_ENTITY_KEY);
@ -2481,12 +2482,10 @@ class ApiV1Controller extends Controller
} }
$pid = $request->user()->profile_id; $pid = $request->user()->profile_id;
$includeReblogs = $request->filled('include_reblogs') ? $request->boolean('include_reblogs') : false; $includeReblogs = $request->filled('include_reblogs') ? $request->boolean('include_reblogs') : false;
$nullFields = $includeReblogs ? $PhotosReblogsOnly = $request->filled('photos_reblogs_only') ? $request->boolean('photos_reblogs_only') : true;
['in_reply_to_id'] : $nullFields = $includeReblogs ? ['statuses.in_reply_to_id'] : ['statuses.in_reply_to_id', 'statuses.reblog_of_id'];
['in_reply_to_id', 'reblog_of_id']; $inTypesStrict = ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'];
$inTypes = $includeReblogs ? $inTypes = $includeReblogs ? [...$inTypesStrict, 'share'] : $inTypesStrict;
['photo', 'photo:album', 'video', 'video:album', 'photo:video:album', 'share'] :
['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'];
AccountService::setLastActive($request->user()->id); AccountService::setLastActive($request->user()->id);
if (config('exp.cached_home_timeline')) { if (config('exp.cached_home_timeline')) {
@ -2578,23 +2577,36 @@ class ApiV1Controller extends Controller
$following = array_diff($following, $muted); $following = array_diff($following, $muted);
} }
$query = Status::select(
'statuses.id as id',
'statuses.profile_id as profile_id',
'statuses.type as type',
'statuses.visibility as visibility',
'statuses.in_reply_to_id as in_reply_to_id',
'statuses.reblog_of_id as reblog_of_id',
'reblog.type as reblog_type'
);
if ($min || $max) { if ($min || $max) {
$dir = $min ? '>' : '<'; $dir = $min ? '>' : '<';
$id = $min ?? $max; $id = $min ?? $max;
$res = Status::select( $query = $query->where('statuses.id', $dir, $id);
'id', }
'profile_id',
'type', $query = $query->whereNull($nullFields)
'visibility', ->whereIntegerInRaw('statuses.profile_id', $following)
'in_reply_to_id', ->whereIn('statuses.type', $inTypes)
'reblog_of_id' ->whereIn('statuses.visibility', ['public', 'unlisted', 'private'])
) ->leftJoin('statuses as reblog','reblog.id', '=', 'statuses.reblog_of_id');
->where('id', $dir, $id)
->whereNull($nullFields) if ($PhotosReblogsOnly) {
->whereIntegerInRaw('profile_id', $following) $query = $query->where(function ($query) use ($inTypesStrict) {
->whereIn('type', $inTypes) $query->whereNull('statuses.reblog_of_id')
->whereIn('visibility', ['public', 'unlisted', 'private']) ->orWhereIn('reblog.type', $inTypesStrict);
->orderByDesc('id') });
}
$res = $query->orderByDesc('id')
->take(($limit * 2)) ->take(($limit * 2))
->get() ->get()
->map(function ($s) use ($pid, $napi) { ->map(function ($s) use ($pid, $napi) {
@ -2635,61 +2647,6 @@ class ApiV1Controller extends Controller
}) })
->take($limit) ->take($limit)
->values(); ->values();
} else {
$res = Status::select(
'id',
'profile_id',
'type',
'visibility',
'in_reply_to_id',
'reblog_of_id',
)
->whereNull($nullFields)
->whereIntegerInRaw('profile_id', $following)
->whereIn('type', $inTypes)
->whereIn('visibility', ['public', 'unlisted', 'private'])
->orderByDesc('id')
->take(($limit * 2))
->get()
->map(function ($s) use ($pid, $napi) {
try {
$account = $napi ? AccountService::get($s['profile_id'], true) : AccountService::getMastodon($s['profile_id'], true);
if (! $account) {
return false;
}
$status = $napi ? StatusService::get($s['id'], false) : StatusService::getMastodon($s['id'], false);
if (! $status || ! isset($status['account']) || ! isset($status['account']['id'])) {
return false;
}
} catch (\Exception $e) {
return false;
}
$status['account'] = $account;
if ($pid) {
$status['favourited'] = (bool) LikeService::liked($pid, $s['id']);
$status['reblogged'] = (bool) ReblogService::get($pid, $status['id']);
$status['bookmarked'] = (bool) BookmarkService::get($pid, $status['id']);
}
return $status;
})
->filter(function ($status) {
return $status && isset($status['account']);
})
->map(function ($status) use ($pid) {
if (! empty($status['reblog'])) {
$status['reblog']['favourited'] = (bool) LikeService::liked($pid, $status['reblog']['id']);
$status['reblog']['reblogged'] = (bool) ReblogService::get($pid, $status['reblog']['id']);
$status['bookmarked'] = (bool) BookmarkService::get($pid, $status['id']);
}
return $status;
})
->take($limit)
->values();
}
$baseUrl = config('app.url').'/api/v1/timelines/home?limit='.$limit.'&'; $baseUrl = config('app.url').'/api/v1/timelines/home?limit='.$limit.'&';
$minId = $res->map(function ($s) { $minId = $res->map(function ($s) {

View file

@ -325,29 +325,24 @@
this.isFetchingMore = true; this.isFetchingMore = true;
let url, params; let url = this.baseApi + this.getScope();
if(this.getScope() === 'home' && this.settings && this.settings.hasOwnProperty('enable_reblogs') && this.settings.enable_reblogs) { let params = {
url = this.baseApi + `home`; max_id: this.max_id,
limit: 6,
'_pe': 1,
};
params = { switch(this.getScope()) {
'_pe': 1, case 'home':
max_id: this.max_id, params.include_reblogs = this?.settings?.enable_reblogs ?? false;
limit: 6, params.photos_reblogs_only = this?.settings?.photo_reblogs_only ?? true;
include_reblogs: true, break;
} case 'network':
} else {
url = this.baseApi + this.getScope();
params = {
max_id: this.max_id,
limit: 6,
'_pe': 1,
}
}
if(this.getScope() === 'network') {
params.remote = true; params.remote = true;
url = this.baseApi + `public`; url = this.baseApi + `public`;
break;
} }
axios.get(url, { axios.get(url, {
params: params params: params
}).then(res => { }).then(res => {