Skip to content

Commit

Permalink
Merge pull request #212 from KenEucker/develop
Browse files Browse the repository at this point in the history
fix(imgur): now using the cache in deleteTag, deleteTags, and updateTag
  • Loading branch information
KenEucker authored Dec 21, 2023
2 parents 07797d0 + c40da6e commit acd689d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "biketag",
"version": "3.1.8",
"version": "3.1.9",
"description": "The Javascript client API for BikeTag Games",
"main": "./biketag.node.js",
"browser": "./biketag.js",
Expand Down
6 changes: 4 additions & 2 deletions src/imgur/deleteTag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import { deleteTagPayload } from '../common/payloads'
import { BikeTagApiResponse, ImgurImage } from '../common/types'
import { getImageHashFromImgurImage } from './helpers'
import { AvailableApis, HttpStatusCode } from '../common/enums'
import TinyCache from 'tinycache'

export async function deleteTag(
client: ImgurClient,
payload: deleteTagPayload
payload: deleteTagPayload,
cache?: typeof TinyCache
): Promise<BikeTagApiResponse<boolean[]>> {
const responses: boolean[] = []
const hashes = []
Expand All @@ -16,7 +18,7 @@ export async function deleteTag(
let success = true

if (!hasImageUrls && (payload.tagnumber || payload.slug)) {
tag = await this.getTags(payload.tagnumber ?? payload.slug)
tag = await this.getTags(payload.tagnumber ?? payload.slug, cache)
}

if (tag.foundImageUrl) {
Expand Down
7 changes: 5 additions & 2 deletions src/imgur/deleteTags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@ import { deleteTagsPayload } from '../common/payloads'
import { BikeTagApiResponse } from '../common/types'
import { getImageHashFromImgurImage } from './helpers'
import { AvailableApis, HttpStatusCode } from '../common/enums'
import TinyCache from 'tinycache'

export async function deleteTags(
client: ImgurClient,
payload: deleteTagsPayload
payload: deleteTagsPayload,
cache?: typeof TinyCache
): Promise<BikeTagApiResponse<boolean[]>> {
const responses: boolean[] = []
const deleteHashes = []
let tags = payload.tags ?? []

if (!tags.length && (payload.tagnumbers || payload.slugs)) {
const { data: tagsData } = await this.getTags(
payload.tagnumbers ?? payload.slugs
payload.tagnumbers ?? payload.slugs,
cache
)
tags = tagsData?.length ? tagsData : []
}
Expand Down
23 changes: 21 additions & 2 deletions src/imgur/updateTag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,55 @@ import { createTagObject } from '../common/data'
import { getUpdateTagPayloadFromTagData, isValidUpdatePayload } from './helpers'
import { AvailableApis, HttpStatusCode } from '../common/enums'
import { Tag } from '../common/schema'
import TinyCache from 'tinycache'

export async function updateTag(
client: ImgurClient,
payload: updateTagPayload
payload: updateTagPayload,
cache?: typeof TinyCache
): Promise<BikeTagApiResponse<Tag>> {
/// Construct imgur payloads for both mystery and found images separately
const imgurMysteryImagePayload = getUpdateTagPayloadFromTagData(payload, true)
const imgurFoundImagePayload = getUpdateTagPayloadFromTagData(payload)

return new Promise(async (resolve) => {
let success = true
let error

/// Validate payloads
if (
isValidUpdatePayload(imgurMysteryImagePayload) &&
isValidUpdatePayload(imgurFoundImagePayload)
) {
const tagExistsForBikeTagAlbum = await this.getTags(payload.tagnumber)
/// Check if tag exists
const tagExistsForBikeTagAlbum = await this.getTags(
payload.tagnumber,
cache
)
const tagExists =
tagExistsForBikeTagAlbum.success && tagExistsForBikeTagAlbum.data.length
const existingTag = tagExists ? tagExistsForBikeTagAlbum.data[0] : null

/// If the tag already exists, update the image data (title, description)
if (existingTag?.mysteryImageUrl?.length) {
const mysteryImageUpdated = (await client.updateImage({
/// Pass in the contents of the mystery image payload
...imgurMysteryImagePayload,
/// Use the image hash from the existing tag, not what was passed in
imageHash: getImageHashFromText(existingTag.mysteryImageUrl),
})) as ImgurApiResponse<boolean>

success = mysteryImageUpdated.success
} else {
/// If the tag does not exist, create the mystery image with the tag data
const mysteryImageUploaded = await this.uploadTagImage({
...imgurMysteryImagePayload,
mysteryImage: payload.mysteryImageUrl,
mysteryImageUrl: undefined,
hash: imgurMysteryImagePayload.hash ?? (payload as any).hash,
})
if (mysteryImageUploaded.success) {
/// If update was successful, update the payload with the new image url
payload.mysteryImageUrl = mysteryImageUploaded.data.mysteryImageUrl
} else {
success = false
Expand All @@ -49,20 +63,25 @@ export async function updateTag(
}
}

/// If the tag already exists, update the image data (title, description)
if (existingTag?.foundImageUrl?.length) {
/// Pass in the contents of the found image payload
const foundImageUpdated = (await client.updateImage({
...imgurFoundImagePayload,
/// Use the image hash from the existing tag, not what was passed in
imageHash: getImageHashFromText(existingTag.foundImageUrl),
})) as ImgurApiResponse<boolean>
success = success && foundImageUpdated.success
} else {
/// If the tag does not exist, create the found image with the tag data
const foundImageUploaded = await this.uploadTagImage({
...imgurFoundImagePayload,
foundImage: payload.foundImageUrl,
foundImageUrl: undefined,
hash: imgurFoundImagePayload.hash ?? (payload as any).hash,
})
if (foundImageUploaded.success) {
/// If update was successful, update the payload with the new image url
payload.foundImageUrl = foundImageUploaded.data.foundImageUrl
} else {
success = false
Expand Down

0 comments on commit acd689d

Please sign in to comment.