A decentralized music tracking and discovery platform built on AT Protocol 🎵 rocksky.app
spotify atproto lastfm musicbrainz scrobbling listenbrainz

fix: enhance user profile handling by inserting new users and updating existing ones in the refreshProfile function

+36 -5
+36 -5
apps/api/src/xrpc/app/rocksky/actor/getProfile.ts
··· 33 Effect.catchAll((err) => { 34 console.error(err); 35 return Effect.succeed({}); 36 - }), 37 ); 38 server.app.rocksky.actor.getProfile({ 39 auth: ctx.authVerifier, ··· 192 .from(tables.spotifyAccounts) 193 .leftJoin( 194 tables.users, 195 - eq(tables.spotifyAccounts.userId, tables.users.id), 196 ) 197 .where(eq(tables.users.did, did)) 198 .execute() ··· 202 .from(tables.spotifyTokens) 203 .leftJoin( 204 tables.users, 205 - eq(tables.spotifyTokens.userId, tables.users.id), 206 ) 207 .where(eq(tables.users.did, did)) 208 .execute() ··· 212 .from(tables.googleDriveAccounts) 213 .leftJoin( 214 tables.users, 215 - eq(tables.googleDriveAccounts.userId, tables.users.id), 216 ) 217 .where(eq(tables.users.did, did)) 218 .execute() ··· 222 .from(tables.dropboxAccounts) 223 .leftJoin( 224 tables.users, 225 - eq(tables.dropboxAccounts.userId, tables.users.id), 226 ) 227 .where(eq(tables.users.did, did)) 228 .execute() ··· 250 ]) => { 251 return Effect.tryPromise({ 252 try: async () => { 253 return [ 254 profile, 255 handle,
··· 33 Effect.catchAll((err) => { 34 console.error(err); 35 return Effect.succeed({}); 36 + }) 37 ); 38 server.app.rocksky.actor.getProfile({ 39 auth: ctx.authVerifier, ··· 192 .from(tables.spotifyAccounts) 193 .leftJoin( 194 tables.users, 195 + eq(tables.spotifyAccounts.userId, tables.users.id) 196 ) 197 .where(eq(tables.users.did, did)) 198 .execute() ··· 202 .from(tables.spotifyTokens) 203 .leftJoin( 204 tables.users, 205 + eq(tables.spotifyTokens.userId, tables.users.id) 206 ) 207 .where(eq(tables.users.did, did)) 208 .execute() ··· 212 .from(tables.googleDriveAccounts) 213 .leftJoin( 214 tables.users, 215 + eq(tables.googleDriveAccounts.userId, tables.users.id) 216 ) 217 .where(eq(tables.users.did, did)) 218 .execute() ··· 222 .from(tables.dropboxAccounts) 223 .leftJoin( 224 tables.users, 225 + eq(tables.dropboxAccounts.userId, tables.users.id) 226 ) 227 .where(eq(tables.users.did, did)) 228 .execute() ··· 250 ]) => { 251 return Effect.tryPromise({ 252 try: async () => { 253 + if (!profile.user) { 254 + await profile.ctx.db 255 + .insert(tables.users) 256 + .values({ 257 + id: "", 258 + did: profile.did, 259 + handle, 260 + avatar: `https://cdn.bsky.app/img/avatar/plain/${profile.did}/${_.get(profile, "profileRecord.value.avatar.ref", "").toString()}@jpeg`, 261 + displayName: _.get(profile, "profileRecord.value.displayName"), 262 + }) 263 + .execute(); 264 + const users = await profile.ctx.db 265 + .select() 266 + .from(tables.users) 267 + .where(eq(tables.users.did, profile.did)) 268 + .execute(); 269 + profile.user = users[0]; 270 + } else { 271 + // Update existing user in background 272 + profile.ctx.db 273 + .update(tables.users) 274 + .set({ 275 + handle, 276 + avatar: `https://cdn.bsky.app/img/avatar/plain/${profile.did}/${_.get(profile, "profileRecord.value.avatar.ref", "").toString()}@jpeg`, 277 + displayName: _.get(profile, "profileRecord.value.displayName"), 278 + updatedAt: new Date(), 279 + }) 280 + .where(eq(tables.users.id, profile.user.id)) 281 + .execute(); 282 + } 283 + 284 return [ 285 profile, 286 handle,