Skip to content

Commit

Permalink
Update PublicApiController, use AccountService in accountStatuses method
Browse files Browse the repository at this point in the history
  • Loading branch information
dansup committed Oct 7, 2021
1 parent 80d9b93 commit bef959f
Showing 1 changed file with 71 additions and 78 deletions.
149 changes: 71 additions & 78 deletions app/Http/Controllers/PublicApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ public function __construct()

protected function getUserData($user)
{
if(!$user) {
return [];
} else {
if(!$user) {
return [];
} else {
return AccountService::get($user->profile_id);
}
}
}

protected function getLikes($status)
Expand Down Expand Up @@ -94,12 +94,12 @@ public function status(Request $request, $username, int $postid)
$status = Status::whereProfileId($profile->id)->findOrFail($postid);
$this->scopeCheck($profile, $status);
if(!$request->user()) {
$res = ['status' => StatusService::get($status->id)];
$res = ['status' => StatusService::get($status->id)];
} else {
$item = new Fractal\Resource\Item($status, new StatusStatelessTransformer());
$res = [
'status' => $this->fractal->createData($item)->toArray(),
];
$item = new Fractal\Resource\Item($status, new StatusStatelessTransformer());
$res = [
'status' => $this->fractal->createData($item)->toArray(),
];
}

return response()->json($res);
Expand Down Expand Up @@ -200,14 +200,14 @@ public function statusComments(Request $request, $username, int $postId)

public function statusLikes(Request $request, $username, $id)
{
abort_if(!$request->user(), 404);
abort_if(!$request->user(), 404);
$status = Status::findOrFail($id);
$this->scopeCheck($status->profile, $status);
$page = $request->input('page');
if($page && $page >= 3 && $request->user()->profile_id != $status->profile_id) {
return response()->json([
'data' => []
]);
return response()->json([
'data' => []
]);
}
$likes = $this->getLikes($status);
return response()->json([
Expand All @@ -217,15 +217,15 @@ public function statusLikes(Request $request, $username, $id)

public function statusShares(Request $request, $username, $id)
{
abort_if(!$request->user(), 404);
abort_if(!$request->user(), 404);
$profile = Profile::whereUsername($username)->whereNull('status')->firstOrFail();
$status = Status::whereProfileId($profile->id)->findOrFail($id);
$this->scopeCheck($profile, $status);
$page = $request->input('page');
if($page && $page >= 3 && $request->user()->profile_id != $status->profile_id) {
return response()->json([
'data' => []
]);
return response()->json([
'data' => []
]);
}
$shares = $this->getShares($status);
return response()->json([
Expand Down Expand Up @@ -300,7 +300,7 @@ public function publicTimelineApi(Request $request)
'scope',
'local'
)
->where('id', $dir, $id)
->where('id', $dir, $id)
->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
->whereNotIn('profile_id', $filtered)
->whereLocal(true)
Expand Down Expand Up @@ -335,16 +335,16 @@ public function publicTimelineApi(Request $request)
'reblogs_count',
'updated_at'
)
->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
->whereNotIn('profile_id', $filtered)
->with('profile', 'hashtags', 'mentions')
->whereLocal(true)
->whereScope('public')
->orderBy('id', 'desc')
->simplePaginate($limit);

$fractal = new Fractal\Resource\Collection($timeline, new StatusTransformer());
$res = $this->fractal->createData($fractal)->toArray();
$fractal = new Fractal\Resource\Collection($timeline, new StatusTransformer());
$res = $this->fractal->createData($fractal)->toArray();
}

return response()->json($res);
Expand Down Expand Up @@ -389,12 +389,12 @@ public function homeTimelineApi(Request $request)
});

if($recentFeed == true) {
$key = 'profile:home-timeline-cursor:'.$user->id;
$ttl = now()->addMinutes(30);
$min = Cache::remember($key, $ttl, function() use($pid) {
$res = StatusView::whereProfileId($pid)->orderByDesc('status_id')->first();
return $res ? $res->status_id : null;
});
$key = 'profile:home-timeline-cursor:'.$user->id;
$ttl = now()->addMinutes(30);
$min = Cache::remember($key, $ttl, function() use($pid) {
$res = StatusView::whereProfileId($pid)->orderByDesc('status_id')->first();
return $res ? $res->status_id : null;
});
}

$filtered = $user ? UserFilterService::filters($user->profile_id) : [];
Expand All @@ -403,16 +403,16 @@ public function homeTimelineApi(Request $request)
$textOnlyReplies = false;

if(config('exp.top')) {
$textOnlyPosts = (bool) Redis::zscore('pf:tl:top', $pid);
$textOnlyReplies = (bool) Redis::zscore('pf:tl:replies', $pid);
$textOnlyPosts = (bool) Redis::zscore('pf:tl:top', $pid);
$textOnlyReplies = (bool) Redis::zscore('pf:tl:replies', $pid);

if($textOnlyPosts) {
array_push($types, 'text');
}
if($textOnlyPosts) {
array_push($types, 'text');
}
}

if(config('exp.polls') == true) {
array_push($types, 'poll');
array_push($types, 'poll');
}

if($min || $max) {
Expand All @@ -438,10 +438,10 @@ public function homeTimelineApi(Request $request)
'created_at',
'updated_at'
)
->whereIn('type', $types)
->whereIn('type', $types)
->when($textOnlyReplies != true, function($q, $textOnlyReplies) {
return $q->whereNull('in_reply_to_id');
})
return $q->whereNull('in_reply_to_id');
})
->with('profile', 'hashtags', 'mentions')
->where('id', $dir, $id)
->whereIn('profile_id', $following)
Expand Down Expand Up @@ -471,10 +471,10 @@ public function homeTimelineApi(Request $request)
'created_at',
'updated_at'
)
->whereIn('type', $types)
->when(!$textOnlyReplies, function($q, $textOnlyReplies) {
return $q->whereNull('in_reply_to_id');
})
->whereIn('type', $types)
->when(!$textOnlyReplies, function($q, $textOnlyReplies) {
return $q->whereNull('in_reply_to_id');
})
->with('profile', 'hashtags', 'mentions')
->whereIn('profile_id', $following)
->whereNotIn('profile_id', $filtered)
Expand Down Expand Up @@ -527,7 +527,7 @@ public function networkTimelineApi(Request $request)
'scope',
'created_at',
)
->where('id', $dir, $id)
->where('id', $dir, $id)
->whereNotIn('profile_id', $filtered)
->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
->whereNotNull('uri')
Expand All @@ -543,27 +543,27 @@ public function networkTimelineApi(Request $request)
});
$res = $timeline->toArray();
} else {
$timeline = Status::select(
'id',
'uri',
'type',
'scope',
'created_at',
)
->whereNotIn('profile_id', $filtered)
->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
->whereNotNull('uri')
->whereScope('public')
->where('id', '>', $amin)
->orderBy('created_at', 'desc')
$timeline = Status::select(
'id',
'uri',
'type',
'scope',
'created_at',
)
->whereNotIn('profile_id', $filtered)
->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
->whereNotNull('uri')
->whereScope('public')
->where('id', '>', $amin)
->orderBy('created_at', 'desc')
->limit($limit)
->get()
->map(function($s) use ($user) {
$status = StatusService::get($s->id);
$status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
return $status;
});
$res = $timeline->toArray();
$res = $timeline->toArray();
}

return response()->json($res);
Expand Down Expand Up @@ -605,10 +605,10 @@ public function accountFollowers(Request $request, $id)
return response()->json([]);
}
if(!$profile->domain && !$profile->user->settings->show_profile_followers) {
return response()->json([]);
return response()->json([]);
}
if(!$owner && $request->page > 5) {
return [];
return [];
}

$res = Follower::select('id', 'profile_id', 'following_id')
Expand Down Expand Up @@ -639,11 +639,11 @@ public function accountFollowing(Request $request, $id)
abort_if($owner == false && $profile->is_private == true && !$profile->followedBy(Auth::user()->profile), 404);

if(!$profile->domain) {
abort_if($profile->user->settings->show_profile_following == false && $owner == false, 404);
abort_if($profile->user->settings->show_profile_following == false && $owner == false, 404);
}

if(!$owner && $request->page > 5) {
return [];
return [];
}

if($search) {
Expand Down Expand Up @@ -676,14 +676,15 @@ public function accountStatuses(Request $request, $id)
]);

$user = $request->user();
$profile = Profile::whereNull('status')->findOrFail($id);
$profile = AccountService::get($id);
abort_if(!$profile, 404);

$limit = $request->limit ?? 9;
$max_id = $request->max_id;
$min_id = $request->min_id;
$scope = ['photo', 'photo:album', 'video', 'video:album'];

if($profile->is_private) {
if($profile['locked']) {
if(!$user) {
return response()->json([]);
}
Expand All @@ -700,23 +701,15 @@ public function accountStatuses(Request $request, $id)
$following = Follower::whereProfileId($pid)->pluck('following_id');
return $following->push($pid)->toArray();
});
$visibility = true == in_array($profile->id, $following) ? ['public', 'unlisted', 'private'] : ['public', 'unlisted'];
$visibility = true == in_array($profile['id'], $following) ? ['public', 'unlisted', 'private'] : ['public', 'unlisted'];
} else {
$visibility = ['public', 'unlisted'];
}
}

$dir = $min_id ? '>' : '<';
$id = $min_id ?? $max_id;
$res = Status::select(
'id',
'profile_id',
'type',
'scope',
'local',
'created_at'
)
->whereProfileId($profile->id)
$res = Status::whereProfileId($profile['id'])
->whereNull('in_reply_to_id')
->whereNull('reblog_of_id')
->whereIn('type', $scope)
Expand All @@ -726,18 +719,18 @@ public function accountStatuses(Request $request, $id)
->orderByDesc('id')
->get()
->map(function($s) use($user) {
try {
$status = StatusService::get($s->id, false);
} catch (\Exception $e) {
$status = false;
}
try {
$status = StatusService::get($s->id, false);
} catch (\Exception $e) {
$status = false;
}
if($user && $status) {
$status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
$status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
}
return $status;
})
->filter(function($s) {
return $s;
return $s;
})
->values();

Expand Down

0 comments on commit bef959f

Please sign in to comment.