Skip to content

Commit b0ffaee

Browse files
committed
fixed some liking logic a bit hopefully
1 parent d4584fe commit b0ffaee

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

server/api/social/like.delete.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ export default eventHandlerWithOAuthSession(async (event, oAuthSession) => {
3131
})
3232
const result = await likesUtil.unlikeAPackageAndReturnLikes(body.packageName, loggedInUsersDid)
3333
return result
34+
} else {
35+
// Always unlike in the cache if this endpoint is called. May be a mismatch
36+
await likesUtil.setUnlikeInCache(body.packageName, loggedInUsersDid)
3437
}
3538

3639
console.warn(

server/utils/atproto/utils/likes.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,10 @@ export class PackageLikesUtils {
168168
let totalLikes = await this.cache.get<number>(totalLikesKey)
169169
if (!totalLikes) {
170170
totalLikes = await this.constellationLikes(subjectRef)
171-
totalLikes = totalLikes + 1
172-
await this.cache.set(totalLikesKey, totalLikes, CACHE_MAX_AGE)
173171
}
172+
totalLikes = totalLikes + 1
173+
await this.cache.set(totalLikesKey, totalLikes, CACHE_MAX_AGE)
174+
174175
// We already know the user has not liked the package before so set in the cache
175176
await this.cache.set(CACHE_USER_LIKES_KEY(packageName, usersDid), true, CACHE_MAX_AGE)
176177
return {
@@ -212,6 +213,15 @@ export class PackageLikesUtils {
212213
}
213214
}
214215

216+
/**
217+
* Access to unlike a package for a user in the cache.
218+
* @param packageName
219+
* @param usersDid
220+
*/
221+
async setUnlikeInCache(packageName: string, usersDid: string) {
222+
await this.cache.set(CACHE_USER_LIKES_KEY(packageName, usersDid), false, CACHE_MAX_AGE)
223+
}
224+
215225
/**
216226
* At this point you should have checked if the user had a record for the package on the network and removed it before updating the cache
217227
* @param packageName
@@ -230,7 +240,7 @@ export class PackageLikesUtils {
230240
await this.cache.set(totalLikesKey, totalLikes, CACHE_MAX_AGE)
231241

232242
//Clean up
233-
await this.cache.set(CACHE_USER_LIKES_KEY(packageName, usersDid), false, CACHE_MAX_AGE)
243+
await this.setUnlikeInCache(packageName, usersDid)
234244
await this.cache.delete(CACHE_USERS_BACK_LINK(packageName, usersDid))
235245

236246
return {

0 commit comments

Comments
 (0)