|
| 1 | +import { getCacheAdatper } from '../../cache' |
| 2 | +import { $nsid as likeNsid } from '#shared/types/lexicons/dev/npmx/feed/like.defs' |
| 3 | +import { SUBJECT_REF_PREFIX } from '~~/shared/utils/constants' |
| 4 | + |
| 5 | +/** |
| 6 | + * Likes for a npm package on npmx |
| 7 | + */ |
| 8 | +export type PackageLikes = { |
| 9 | + // The total likes found for the package |
| 10 | + totalLikes: number |
| 11 | + // If the logged in user has liked the package, false if not logged in |
| 12 | + userHasLiked: boolean |
| 13 | +} |
| 14 | + |
| 15 | +const CACHE_PREFIX = 'atproto-likes:' |
| 16 | +const CACHE_PACKAGE_TOTAL_KEY = (packageName: string) => `${CACHE_PREFIX}:${packageName}:total` |
| 17 | +const CACHE_USER_LIKES_KEY = (packageName: string, did: string) => |
| 18 | + `${CACHE_PREFIX}${packageName}users:${did}` |
| 19 | + |
| 20 | +const CACHE_MAX_AGE = CACHE_MAX_AGE_ONE_MINUTE * 5 |
| 21 | + |
| 22 | +export class PackageLikesService { |
| 23 | + private readonly constellation: Constellation |
| 24 | + private readonly cache: CacheAdapter |
| 25 | + |
| 26 | + constructor(cachedFunction: CachedFetchFunction) { |
| 27 | + this.constellation = new Constellation(cachedFunction) |
| 28 | + this.cache = getCacheAdatper(CACHE_PREFIX) |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Gets the true total count of likes for a npm package from the network |
| 33 | + * @param subjectRef |
| 34 | + * @returns |
| 35 | + */ |
| 36 | + private async constellationLikes(subjectRef: string) { |
| 37 | + // TODO: I need to see what failed fetch calls do here |
| 38 | + const { data: totalLinks } = await this.constellation.getLinksDistinctDids( |
| 39 | + subjectRef, |
| 40 | + likeNsid, |
| 41 | + '.subjectRef', |
| 42 | + //Limit doesn't matter here since we are just counting the total likes |
| 43 | + 1, |
| 44 | + undefined, |
| 45 | + CACHE_MAX_AGE_ONE_MINUTE * 10, |
| 46 | + ) |
| 47 | + return totalLinks.total |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Checks if the user has liked the npm package from the network |
| 52 | + * @param subjectRef |
| 53 | + * @param usersDid |
| 54 | + * @returns |
| 55 | + */ |
| 56 | + private async constellationUserHasLiked(subjectRef: string, usersDid: string) { |
| 57 | + const { data: userLikes } = await this.constellation.getBackLinks( |
| 58 | + subjectRef, |
| 59 | + likeNsid, |
| 60 | + 'subjectRef', |
| 61 | + //Limit doesn't matter here since we are just counting the total likes |
| 62 | + 1, |
| 63 | + undefined, |
| 64 | + false, |
| 65 | + [[usersDid]], |
| 66 | + ) |
| 67 | + //TODO: need to double check this logic |
| 68 | + return userLikes.total > 0 |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Gets the likes for a npm package on npmx. Tries a local cahce first, if not found uses constellation |
| 73 | + * @param packageName |
| 74 | + * @param usersDid |
| 75 | + * @returns |
| 76 | + */ |
| 77 | + async getLikes(packageName: string, usersDid?: string) { |
| 78 | + //TODO: May need to do some clean up on the package name, and maybe even hash it? some of the charcteres may be a bit odd as keys |
| 79 | + const cache = getCacheAdatper(CACHE_PREFIX) |
| 80 | + |
| 81 | + const cachedLikes = await cache.get<PackageLikes>(packageName) |
| 82 | + if (cachedLikes) { |
| 83 | + return cachedLikes |
| 84 | + } |
| 85 | + |
| 86 | + const subjectRef = `${SUBJECT_REF_PREFIX}/${packageName}` |
| 87 | + |
| 88 | + const totalLikes = await this.constellationLikes(subjectRef) |
| 89 | + |
| 90 | + let userHasLiked = false |
| 91 | + |
| 92 | + if (usersDid) { |
| 93 | + userHasLiked = await this.constellationUserHasLiked(subjectRef, usersDid) |
| 94 | + } |
| 95 | + |
| 96 | + const packageLikes = { |
| 97 | + totalPackageLikes: totalLikes, |
| 98 | + userHasLiked, |
| 99 | + } |
| 100 | + if (userHasLiked && usersDid) { |
| 101 | + await cache.set(CACHE_USER_LIKES_KEY(packageName, usersDid), true, CACHE_MAX_AGE) |
| 102 | + } |
| 103 | + |
| 104 | + const totalLikesKey = CACHE_PACKAGE_TOTAL_KEY(packageName) |
| 105 | + await cache.set(totalLikesKey, packageLikes.totalPackageLikes, CACHE_MAX_AGE) |
| 106 | + return packageLikes |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Gets the definite answer if the user has liked a npm package. Either from the cache or the network |
| 111 | + * @param packageName |
| 112 | + * @param usersDid |
| 113 | + * @returns |
| 114 | + */ |
| 115 | + async hasTheUserLikedThePackage(packageName: string, usersDid: string) { |
| 116 | + const cache = getCacheAdatper(CACHE_PREFIX) |
| 117 | + const cached = await cache.get<boolean>(CACHE_USER_LIKES_KEY(packageName, usersDid)) |
| 118 | + if (cached !== undefined) { |
| 119 | + return cached |
| 120 | + } |
| 121 | + const userHasLiked = await this.constellationUserHasLiked( |
| 122 | + `${SUBJECT_REF_PREFIX}/${packageName}`, |
| 123 | + usersDid, |
| 124 | + ) |
| 125 | + await cache.set(CACHE_USER_LIKES_KEY(packageName, usersDid), userHasLiked, CACHE_MAX_AGE) |
| 126 | + return userHasLiked |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * It is asummed it has been checked by this point that if a user has liked a package and the new like was made as a record |
| 131 | + * to the user's atproto repostiory |
| 132 | + * @param packageName |
| 133 | + * @param usersDid |
| 134 | + */ |
| 135 | + async likeAPackageAndRetunLikes(packageName: string, usersDid: string): Promise<PackageLikes> { |
| 136 | + const cache = getCacheAdatper(CACHE_PREFIX) |
| 137 | + |
| 138 | + const totalLikesKey = CACHE_PACKAGE_TOTAL_KEY(packageName) |
| 139 | + let totalLikes = await cache.get<number>(totalLikesKey) |
| 140 | + // If a cahce entry was found for total likes increase by 1 |
| 141 | + if (totalLikes !== undefined) { |
| 142 | + await cache.set(totalLikesKey, totalLikes + 1, CACHE_MAX_AGE) |
| 143 | + } else { |
| 144 | + const subjectRef = `${SUBJECT_REF_PREFIX}/${packageName}` |
| 145 | + totalLikes = await this.constellationLikes(subjectRef) |
| 146 | + } |
| 147 | + // We already know the user has not liked the package so set in the cache |
| 148 | + await cache.set(CACHE_USER_LIKES_KEY(packageName, usersDid), true, CACHE_MAX_AGE) |
| 149 | + return { |
| 150 | + totalLikes: totalLikes, |
| 151 | + userHasLiked: true, |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments