Skip to content

Commit 86f5aaf

Browse files
committed
fix(动态): 修复动态话题未关注状态下发布动态后无法关注该话题
fixed #579
1 parent c13db22 commit 86f5aaf

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

app/API2/Controllers/Feed/TopicFollow.php

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,21 +59,25 @@ public function follow(Request $request, FeedTopicModel $model, int $topicID): R
5959
// If the topic Non-existent, throw a not found exception.
6060
if (! $topic) {
6161
throw new NotFoundHttpException('关注的话题不存在');
62-
} elseif ($topic->users()->newPivotStatementForId($user->id)->exists()) {
62+
} elseif (($link = $topic->users()->newPivotStatementForId($user->id)->first())->following_at ?? false) {
6363
return (new Response())->setStatusCode(Response::HTTP_NO_CONTENT /* 204 */);
6464
}
6565

6666
$feedsCount = $topic->feeds()->where('user_id', $user->id)->count();
6767

68-
return $user->getConnection()->transaction(function () use ($topic, $user, $feedsCount): Response {
69-
$topic->users()->attach($user, [
70-
'following_at' => new Carbon(),
71-
'feeds_count' => $feedsCount,
72-
]);
73-
$topic->followers_count += 1;
74-
$topic->save();
68+
return $user->getConnection()->transaction(function () use ($topic, $user, $feedsCount, $link): Response {
69+
if ($link) {
70+
$link->following_at = new Carbon;
71+
$link->save();
72+
} else {
73+
$topic->users()->attach($user, [
74+
'following_at' => new Carbon(),
75+
'feeds_count' => $feedsCount,
76+
]);
77+
}
78+
$topic->increment('followers_count', 1);
7579

76-
return (new Response())->setStatusCode(Response::HTTP_NO_CONTENT /* 204 */);
80+
return (new Response)->setStatusCode(Response::HTTP_NO_CONTENT /* 204 */);
7781
});
7882
}
7983

@@ -102,19 +106,21 @@ public function unfollow(Request $request, FeedTopicModel $model, int $topicID):
102106
}
103107

104108
// Create success 204 response
105-
$response = (new Response())->setStatusCode(Response::HTTP_NO_CONTENT /* 204 */);
109+
$response = (new Response)->setStatusCode(Response::HTTP_NO_CONTENT /* 204 */);
106110

107111
// If not followed, return 204 response.
108-
if (! $topic->users()->newPivotStatementForId($user->id)->first()) {
112+
if (! (($link = $topic->users()->newPivotStatementForId($user->id)->first())->following_at ?? false)) {
109113
return $response;
110114
}
111115

112-
return $user->getConnection()->transaction(function () use ($topic, $response, $user): Response {
113-
$topic->users()->detach($user);
116+
return $user->getConnection()->transaction(function () use ($topic, $response, $user, $link): Response {
114117
if ($topic->followers_count > 0) {
115118
$topic->decrement('followers_count', 1);
116119
}
117120

121+
$link->following_at = null;
122+
$link->save();
123+
118124
return $response;
119125
});
120126
}

0 commit comments

Comments
 (0)