Skip to content

Commit 7d892de

Browse files
committed
feat(farcaster): enhance sync functionality by adding linkedAccountId to mutations and updating user profile with Farcaster data
1 parent 127e47b commit 7d892de

3 files changed

Lines changed: 26 additions & 13 deletions

File tree

packages/app/screens/profile/profile-screen.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,15 @@ export const ProfileScreen = ({ id }: { id: string }) => {
334334
@{linkedAccount.account.username}
335335
</SizableText>
336336
</YStack>
337-
<Button onPress={handleSyncWithFarcaster} disabled={isSyncing}>
337+
<Button
338+
onPress={handleSyncWithFarcaster}
339+
disabled={
340+
(linkedAccount.account.lastSyncedAt !== undefined &&
341+
linkedAccount.account.lastSyncedAt <
342+
Date.now() - 1000 * 60 * 60 * 1) ||
343+
isSyncing
344+
}
345+
>
338346
{isSyncing ? 'Syncing...' : 'Sync using Farcaster'}
339347
</Button>
340348
</XStack>

packages/backend/convex/farcaster.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export const syncWithFarcaster = mutation({
6262
await ctx.scheduler.runAfter(0, internal.farcaster.syncUserInformationWithFarcaster, {
6363
userId: args.userId,
6464
fid: linkedAccount.account.fid,
65+
linkedAccountId: linkedAccount._id,
6566
});
6667
},
6768
});
@@ -70,6 +71,7 @@ export const syncUserInformationWithFarcaster = internalAction({
7071
args: {
7172
userId: v.id('users'),
7273
fid: v.number(),
74+
linkedAccountId: v.id('linkedAccounts'),
7375
},
7476
handler: async (ctx, args) => {
7577
if (!process.env.NEYNAR_API_KEY) {
@@ -133,8 +135,10 @@ export const syncUserInformationWithFarcaster = internalAction({
133135
}
134136

135137
// Use internal mutation to update user profile since this is an internalAction
136-
await ctx.runMutation(internal.users.updateUserProfileInternal, {
138+
await ctx.runMutation(internal.users.updateUserProfileInternalWithFarcaster, {
137139
userId: args.userId,
140+
fid: args.fid,
141+
linkedAccountId: args.linkedAccountId,
138142
...fieldsToUpdate,
139143
});
140144

packages/backend/convex/users.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -200,25 +200,18 @@ export const updateUserSocials = mutation({
200200
* Internal mutation to update user profile with Neynar data
201201
* This is specifically for syncing user information from Farcaster/Neynar
202202
*/
203-
export const updateUserProfileInternal = internalMutation({
203+
export const updateUserProfileInternalWithFarcaster = internalMutation({
204204
args: {
205205
userId: v.id('users'),
206+
fid: v.number(),
206207
bio: v.optional(v.string()),
207208
displayName: v.optional(v.string()),
208209
username: v.optional(v.string()),
209210
pfpUrl: v.optional(v.string()),
211+
linkedAccountId: v.id('linkedAccounts'),
210212
},
211213
handler: async (ctx, args) => {
212-
const { userId, ...updateFields } = args;
213-
214-
// Check if user exists
215-
const existingUser = await ctx.db.get(userId);
216-
if (!existingUser) {
217-
throw new ConvexError({
218-
message: 'User not found',
219-
code: 'USER_NOT_FOUND',
220-
});
221-
}
214+
const { userId, linkedAccountId, ...updateFields } = args;
222215

223216
// Filter out undefined values to avoid removing fields unintentionally
224217
const fieldsToUpdate = Object.fromEntries(
@@ -231,6 +224,14 @@ export const updateUserProfileInternal = internalMutation({
231224
return;
232225
}
233226

227+
await ctx.db.patch(linkedAccountId, {
228+
account: {
229+
protocol: 'farcaster',
230+
fid: args.fid,
231+
...fieldsToUpdate,
232+
lastSyncedAt: Date.now(),
233+
},
234+
});
234235
await ctx.db.patch(userId, fieldsToUpdate);
235236
},
236237
});

0 commit comments

Comments
 (0)