@@ -15,11 +15,11 @@ export type PackageLikes = {
1515const CACHE_PREFIX = 'atproto-likes:'
1616const CACHE_PACKAGE_TOTAL_KEY = ( packageName : string ) => `${ CACHE_PREFIX } :${ packageName } :total`
1717const CACHE_USER_LIKES_KEY = ( packageName : string , did : string ) =>
18- `${ CACHE_PREFIX } ${ packageName } users:${ did } `
18+ `${ CACHE_PREFIX } ${ packageName } : users:${ did } `
1919
2020const CACHE_MAX_AGE = CACHE_MAX_AGE_ONE_MINUTE * 5
2121
22- export class PackageLikesService {
22+ export class PackageLikesUtils {
2323 private readonly constellation : Constellation
2424 private readonly cache : CacheAdapter
2525
@@ -74,35 +74,38 @@ export class PackageLikesService {
7474 * @param usersDid
7575 * @returns
7676 */
77- async getLikes ( packageName : string , usersDid ?: string ) {
77+ async getLikes ( packageName : string , usersDid ?: string | undefined ) {
7878 //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 )
79+ const totalLikesKey = CACHE_PACKAGE_TOTAL_KEY ( packageName )
80+ const subjectRef = `${ SUBJECT_REF_PREFIX } /${ packageName } `
8081
81- const cachedLikes = await cache . get < PackageLikes > ( packageName )
82+ const cachedLikes = await this . cache . get < number > ( totalLikesKey )
83+ let totalLikes = 0
8284 if ( cachedLikes ) {
83- return cachedLikes
85+ totalLikes = cachedLikes
86+ } else {
87+ totalLikes = await this . constellationLikes ( subjectRef )
88+ await this . cache . set ( totalLikesKey , totalLikes , CACHE_MAX_AGE )
8489 }
8590
86- const subjectRef = `${ SUBJECT_REF_PREFIX } /${ packageName } `
87-
88- const totalLikes = await this . constellationLikes ( subjectRef )
89-
9091 let userHasLiked = false
91-
9292 if ( usersDid ) {
93- userHasLiked = await this . constellationUserHasLiked ( subjectRef , usersDid )
93+ const userCachedLike = await this . cache . get < boolean > (
94+ CACHE_USER_LIKES_KEY ( packageName , usersDid ) ,
95+ )
96+ if ( userCachedLike ) {
97+ userHasLiked = userCachedLike
98+ } else {
99+ userHasLiked = await this . constellationUserHasLiked ( subjectRef , usersDid )
100+ await this . cache . set ( CACHE_USER_LIKES_KEY ( packageName , usersDid ) , true , CACHE_MAX_AGE )
101+ }
94102 }
95103
96104 const packageLikes = {
97105 totalPackageLikes : totalLikes ,
98106 userHasLiked,
99107 }
100- if ( userHasLiked && usersDid ) {
101- await cache . set ( CACHE_USER_LIKES_KEY ( packageName , usersDid ) , true , CACHE_MAX_AGE )
102- }
103108
104- const totalLikesKey = CACHE_PACKAGE_TOTAL_KEY ( packageName )
105- await cache . set ( totalLikesKey , packageLikes . totalPackageLikes , CACHE_MAX_AGE )
106109 return packageLikes
107110 }
108111
@@ -113,16 +116,15 @@ export class PackageLikesService {
113116 * @returns
114117 */
115118 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 ) )
119+ const cached = await this . cache . get < boolean > ( CACHE_USER_LIKES_KEY ( packageName , usersDid ) )
118120 if ( cached !== undefined ) {
119121 return cached
120122 }
121123 const userHasLiked = await this . constellationUserHasLiked (
122124 `${ SUBJECT_REF_PREFIX } /${ packageName } ` ,
123125 usersDid ,
124126 )
125- await cache . set ( CACHE_USER_LIKES_KEY ( packageName , usersDid ) , userHasLiked , CACHE_MAX_AGE )
127+ await this . cache . set ( CACHE_USER_LIKES_KEY ( packageName , usersDid ) , userHasLiked , CACHE_MAX_AGE )
126128 return userHasLiked
127129 }
128130
@@ -133,19 +135,17 @@ export class PackageLikesService {
133135 * @param usersDid
134136 */
135137 async likeAPackageAndRetunLikes ( packageName : string , usersDid : string ) : Promise < PackageLikes > {
136- const cache = getCacheAdatper ( CACHE_PREFIX )
137-
138138 const totalLikesKey = CACHE_PACKAGE_TOTAL_KEY ( packageName )
139- let totalLikes = await cache . get < number > ( totalLikesKey )
139+ let totalLikes = await this . cache . get < number > ( totalLikesKey )
140140 // If a cahce entry was found for total likes increase by 1
141141 if ( totalLikes !== undefined ) {
142- await cache . set ( totalLikesKey , totalLikes + 1 , CACHE_MAX_AGE )
142+ await this . cache . set ( totalLikesKey , totalLikes + 1 , CACHE_MAX_AGE )
143143 } else {
144144 const subjectRef = `${ SUBJECT_REF_PREFIX } /${ packageName } `
145145 totalLikes = await this . constellationLikes ( subjectRef )
146146 }
147147 // 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 )
148+ await this . cache . set ( CACHE_USER_LIKES_KEY ( packageName , usersDid ) , true , CACHE_MAX_AGE )
149149 return {
150150 totalLikes : totalLikes ,
151151 userHasLiked : true ,
0 commit comments