Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

29 default likes and tips to all cache #34

Merged
merged 2 commits into from
Sep 20, 2023
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
63 changes: 53 additions & 10 deletions src/legacy/legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,15 @@ export default class ArtByCityLegacy {
slugs: LegacyMemcache<string>
profiles: LegacyMemcache<LegacyProfile>
avatars: LegacyMemcache<LegacyAvatar>
likes: LegacyMemcache<LegacyLikesFeed>
tips: LegacyMemcache<LegacyTipsFeed>
} = { /* eslint-disable indent */
publications: new LegacyMemcache<LegacyPublicationManifest>(),
slugs: new LegacyMemcache<string>(),
profiles: new LegacyMemcache<LegacyProfile>(),
avatars: new LegacyMemcache<LegacyAvatar>()
avatars: new LegacyMemcache<LegacyAvatar>(),
likes: new LegacyMemcache<LegacyLikesFeed>(),
tips: new LegacyMemcache<LegacyTipsFeed>()
} /* eslint-enable indent */

constructor(arweave: Arweave, private readonly config: ArtByCityConfig) {
Expand Down Expand Up @@ -398,9 +402,16 @@ export default class ArtByCityLegacy {
async queryLikes(
address: string,
receivedOrSent: 'received' | 'sent',
limit: number | 'all' = 100,
cursor?: string
limit: number | 'all' = 'all',
cursor?: string,
useCache: boolean = true
): Promise<LegacyLikesFeed> {
const cacheKey = `${receivedOrSent}-by-${address}`
if (this.cacheEnabled && useCache && limit === 'all') {
const cached = this.caches.likes.get(cacheKey)
if (cached) { return cached }
}

const receivedOrSentOpts = receivedOrSent === 'received'
? { to: address }
: { from: address }
Expand All @@ -414,17 +425,30 @@ export default class ArtByCityLegacy {
cursor
})

return {
const feed = {
cursor: nextCursor,
likes: transactions.map(mapLegacyLikeFromTransaction)
}

if (this.cacheEnabled && useCache && limit === 'all') {
this.caches.likes.put(cacheKey, feed)
}

return feed
}

async queryLikesForPublication(
id: string,
limit: number | 'all' = 100,
cursor?: string
limit: number | 'all' = 'all',
cursor?: string,
useCache: boolean = true
): Promise<LegacyLikesFeed> {
const cacheKey = `likes-for-${id}`
if (this.cacheEnabled && useCache && limit === 'all') {
const cached = this.caches.likes.get(cacheKey)
if (cached) { return cached }
}

const {
transactions,
cursor: nextCursor
Expand All @@ -434,18 +458,31 @@ export default class ArtByCityLegacy {
tags: [ { name: 'liked-entity', value: id } ]
})

return {
const feed = {
cursor: nextCursor,
likes: transactions.map(mapLegacyLikeFromTransaction)
}

if (this.cacheEnabled && useCache && limit === 'all') {
this.caches.likes.put(cacheKey, feed)
}

return feed
}

async queryTips(
address: string,
receivedOrSent: 'received' | 'sent',
limit: number | 'all' = 100,
cursor?: string
limit: number | 'all' = 'all',
cursor?: string,
useCache: boolean = true
): Promise<LegacyTipsFeed> {
const cacheKey = `${receivedOrSent}-by-${address}`
if (this.cacheEnabled && useCache && limit === 'all') {
const cached = this.caches.tips.get(cacheKey)
if (cached) { return cached }
}

const receivedOrSentOpts = receivedOrSent === 'received'
? { to: address }
: { from: address }
Expand All @@ -459,7 +496,7 @@ export default class ArtByCityLegacy {
cursor
})

return {
const feed = {
cursor: nextCursor,
tips: transactions.map(tx => {
return {
Expand All @@ -471,5 +508,11 @@ export default class ArtByCityLegacy {
}
})
}

if (this.cacheEnabled && useCache && limit === 'all') {
this.caches.tips.put(cacheKey, feed)
}

return feed
}
}
Loading