Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 38 additions & 23 deletions apps/backend/src/routes/follow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,23 @@ export async function followRoutes(app: FastifyInstance) {
// Decrypt the stored token
const accessToken = decrypt(oauthToken.accessToken);

try {
try {
let result;
let succeeded = false;

switch (platform) {
case 'github':
result = await followGitHub(accessToken, targetUsername, reply);
succeeded = result.success === true;
break;
default:
return reply.status(400).send({
error: `API follow not supported for ${platform}. Use WebView or link instead.`,
});
}

// If follow succeeded (or was handled by the function without throwing), log it
if (reply.statusCode === 200 || reply.statusCode === 204) {
// Log only genuine successes — not based on reply.statusCode default
if (succeeded) {
app.prisma.followLog.create({
data: {
followerId: userId,
Expand All @@ -56,7 +59,7 @@ export async function followRoutes(app: FastifyInstance) {
}).catch(err => app.log.error('Failed to log follow:', err));
}

return result;
return result.response;
} catch (err: any) {
app.log.error(`Follow error for ${platform}:`, err);

Expand All @@ -81,7 +84,7 @@ async function followGitHub(
accessToken: string,
targetUsername: string,
reply: FastifyReply
) {
): Promise<{ success: boolean; response: FastifyReply }> {
const response = await fetch(`https://api.github.com/user/following/${targetUsername}`, {
method: 'PUT',
headers: {
Expand All @@ -92,30 +95,42 @@ async function followGitHub(
});

if (response.status === 204) {
return reply.send({
status: 'success',
platform: 'github',
targetUsername,
message: `Now following ${targetUsername} on GitHub`,
});
return {
success: true,
response: reply.send({
status: 'success',
platform: 'github',
targetUsername,
message: `Now following ${targetUsername} on GitHub`,
}),
};
}

if (response.status === 401 || response.status === 403) {
return reply.status(401).send({
error: 'GitHub token expired or insufficient permissions',
requiresAuth: true,
});
return {
success: false,
response: reply.status(401).send({
error: 'GitHub token expired or insufficient permissions',
requiresAuth: true,
}),
};
}

if (response.status === 404) {
return reply.status(404).send({
error: `GitHub user '${targetUsername}' not found`,
});
return {
success: false,
response: reply.status(404).send({
error: `GitHub user '${targetUsername}' not found`,
}),
};
}

const errorBody = await response.text();
return reply.status(response.status).send({
error: 'GitHub follow failed',
details: errorBody,
});
}
return {
success: false,
response: reply.status(response.status).send({
error: 'GitHub follow failed',
details: errorBody,
}),
};
}