Skip to content
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions server/api/social/like.delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ export default eventHandlerWithOAuthSession(async (event, oAuthSession) => {
})
const result = await likesUtil.unlikeAPackageAndReturnLikes(body.packageName, loggedInUsersDid)
return result
} else {
// Always unlike in the cache if this endpoint is called. May be a mismatch
await likesUtil.setUnlikeInCache(body.packageName, loggedInUsersDid)
}

console.warn(
Expand Down
16 changes: 13 additions & 3 deletions server/utils/atproto/utils/likes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,10 @@ export class PackageLikesUtils {
let totalLikes = await this.cache.get<number>(totalLikesKey)
if (!totalLikes) {
totalLikes = await this.constellationLikes(subjectRef)
totalLikes = totalLikes + 1
await this.cache.set(totalLikesKey, totalLikes, CACHE_MAX_AGE)
}
totalLikes = totalLikes + 1
await this.cache.set(totalLikesKey, totalLikes, CACHE_MAX_AGE)

// We already know the user has not liked the package before so set in the cache
await this.cache.set(CACHE_USER_LIKES_KEY(packageName, usersDid), true, CACHE_MAX_AGE)
return {
Expand Down Expand Up @@ -212,6 +213,15 @@ export class PackageLikesUtils {
}
}

/**
* Access to unlike a package for a user in the cache.
* @param packageName
* @param usersDid
*/
async setUnlikeInCache(packageName: string, usersDid: string) {
await this.cache.set(CACHE_USER_LIKES_KEY(packageName, usersDid), false, CACHE_MAX_AGE)
}

/**
* 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
* @param packageName
Expand All @@ -230,7 +240,7 @@ export class PackageLikesUtils {
await this.cache.set(totalLikesKey, totalLikes, CACHE_MAX_AGE)

//Clean up
await this.cache.set(CACHE_USER_LIKES_KEY(packageName, usersDid), false, CACHE_MAX_AGE)
await this.setUnlikeInCache(packageName, usersDid)
await this.cache.delete(CACHE_USERS_BACK_LINK(packageName, usersDid))

return {
Expand Down
Loading