Skip to content

Commit

Permalink
Merge pull request #256 from KenEucker/develop
Browse files Browse the repository at this point in the history
Adding method for deleting all images in the queue
  • Loading branch information
KenEucker authored Jan 15, 2025
2 parents a885de0 + e641fa2 commit c449d93
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 57 deletions.
50 changes: 39 additions & 11 deletions functions/autopost-clear.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export const autoClearQueue = async (event): Promise<BackgroundProcessResults> =
}

let errors = false
const forceNotify = event.queryStringParameters.force === 'true'
const forceClear = event.queryStringParameters.force === 'true'
const clearAll = event.queryStringParameters.all === 'true'
let results: any = []
const nonAdminBiketagOpts = getBikeTagClientOpts(event, true)
const nonAdminBiketag = new BikeTagClient(nonAdminBiketagOpts)
Expand All @@ -33,7 +34,7 @@ export const autoClearQueue = async (event): Promise<BackgroundProcessResults> =
const { data: mostRecentTag } = await adminBiketag.getTag(undefined, { source: 'imgur' })
const twentyFourHoursAgo = new Date().getTime() - 60 * 60 * 24 * 1000

if (twentyFourHoursAgo > mostRecentTag.mysteryTime * 1000 && !forceNotify) {
if (twentyFourHoursAgo > mostRecentTag.mysteryTime * 1000 && !forceClear) {
const errorMessage =
'Most recent tag was created more than 24 hours ago. Please clear the queue manually.'
console.log(errorMessage)
Expand All @@ -43,17 +44,44 @@ export const autoClearQueue = async (event): Promise<BackgroundProcessResults> =
}
}

const { queuedTags } = await getActiveQueueForGame(game, adminBiketag)
if (clearAll) {
const allTags = (await nonAdminBiketag.getQueue({ game: adminBiketagOpts.game })).data

if (queuedTags.length) {
console.log('non-winning tag(s) found', { game, queuedTags })
const archiveAndClearQueueResults = await archiveAndClearQueue(queuedTags)
results = archiveAndClearQueueResults.results
errors = archiveAndClearQueueResults.errors
if (allTags.length) {
console.log('all tags found', { game, allTags })
const archiveAndClearQueueResults = await archiveAndClearQueue(
allTags,
game,
adminBiketag,
undefined,
true,
)
results = archiveAndClearQueueResults.results
errors = archiveAndClearQueueResults.errors
} else {
const nothingToDoMessage = 'no tags found'
console.log(nothingToDoMessage)
results.push(nothingToDoMessage)
}
} else {
const nothingToDoMessage = 'no non-winning tags found'
console.log(nothingToDoMessage)
results.push(nothingToDoMessage)
const { queuedTags } = await getActiveQueueForGame(game, adminBiketag)

if (queuedTags.length) {
console.log('non-winning tag(s) found', { game, queuedTags })
const archiveAndClearQueueResults = await archiveAndClearQueue(
queuedTags,
game,
adminBiketag,
undefined,
forceClear,
)
results = archiveAndClearQueueResults.results
errors = archiveAndClearQueueResults.errors
} else {
const nothingToDoMessage = 'no non-winning tags found'
console.log(nothingToDoMessage)
results.push(nothingToDoMessage)
}
}

return {
Expand Down
111 changes: 69 additions & 42 deletions functions/common/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ export const getPayloadAuthorization = async (event: any): Promise<any> => {
const basic = 'Basic '
const bearer = 'Bearer '
const client = 'Client-ID '
let authProfile

const authorizationType: string | null =
authorizationString?.indexOf(basic) === 0
Expand Down Expand Up @@ -469,30 +470,39 @@ export const getPayloadAuthorization = async (event: any): Promise<any> => {
}
}

/// DEBUG: uncomment to check incoming authorization credentials
// console.log({ orign: event.headers.authorization, authorizationType, authorizationString })

switch (authorizationType) {
case 'basic':
authorizationString = authorizationString.substring(basic.length)
return getBasicAuthProfile(authorizationString)
authProfile = await getBasicAuthProfile(authorizationString)
break
case 'netlify':
authorizationString = authorizationString.substring(client.length)
return getNetlifyAuthProfile(authorizationString)
authProfile = await getNetlifyAuthProfile(authorizationString)
break
case 'client':
authorizationString = authorizationString.substring(client.length)
return getAuth0AuthProfile(authorizationString)
authProfile = await getAuth0AuthProfile(authorizationString)
break
case 'bearer':
authorizationString = authorizationString.substring(bearer.length)
return getAuth0AuthProfile(authorizationString)
authProfile = await getAuth0AuthProfile(authorizationString)
break
default:
authorizationString = authorizationString?.length
? 'authorization type not supported'
: authorizationString
authProfile = authorizationString?.length ? 'authorization type not supported' : null
break
}

return null
/// DEBUG: uncomment to check incoming authorization credentials
if (process.env.DEBUG_A) {
console.log({
orign: event.headers.authorization,
authorizationType,
authorizationString,
authProfile,
})
}

return authProfile
}

export const defaultLogo = '/images/BikeTag.svg'
Expand Down Expand Up @@ -729,6 +739,7 @@ export const archiveAndClearQueue = async (
game?: Game | null,
adminBiketag?: BikeTagClient,
nonAdminBikeTag?: BikeTagClient,
noArchive = false,
): Promise<BackgroundProcessResults> => {
const results: any = []
let errors = false
Expand All @@ -747,7 +758,6 @@ export const archiveAndClearQueue = async (
if (queuedTags.length && game) {
const nonAdminBikeTagOpts = getBikeTagClientOpts(undefined, true)
const gameName = game.name.toLocaleLowerCase()
console.log('archiving remaining queued tags', { game: gameName, queuedTags })
nonAdminBikeTagOpts.game = gameName
nonAdminBikeTagOpts.imgur.hash = game.queuehash

Expand All @@ -757,50 +767,67 @@ export const archiveAndClearQueue = async (
nonAdminBikeTag.config(nonAdminBikeTagOpts, false)
}

const currentBikeTag = (await adminBiketag.getTag({ limit: 1 })).data
for (const nonWinningTag of queuedTags) {
/// If there are remnants of tags from the currently posted biketag, don't archive them
if (
nonWinningTag.mysteryPlayer !== currentBikeTag?.mysteryPlayer &&
nonWinningTag.foundPlayer !== currentBikeTag?.mysteryPlayer
) {
/* Archive using ambassador credentials (mainhash and archivehash are both ambassador albums) */
const archiveTagResult = await adminBiketag.archiveTag({
...nonWinningTag,
archivehash: game.archivehash,
})
if (archiveTagResult.success) {
if (!noArchive) {
console.log('archiving remaining queued tags', { game: gameName, queuedTags })

const currentBikeTag = (await adminBiketag.getTag({ limit: 1 })).data
for (const nonWinningTag of queuedTags) {
/// If there are remnants of tags from the currently posted biketag, don't archive them
if (
nonWinningTag.mysteryPlayer !== currentBikeTag?.mysteryPlayer &&
nonWinningTag.foundPlayer !== currentBikeTag?.mysteryPlayer
) {
/* Archive using ambassador credentials (mainhash and archivehash are both ambassador albums) */
const archiveTagResult = await adminBiketag.archiveTag({
...nonWinningTag,
archivehash: game.archivehash,
})
if (archiveTagResult.success) {
results.push({
message: 'non-winning found image archived',
game: gameName,
tag: nonWinningTag,
})
} else {
// console.log({ archiveTagResult })
results.push({
message: 'error archiving non-winning found image',
game: gameName,
tag: nonWinningTag,
})
errors = true
}
}
/* delete using player credentials (queuehash is player album) */
const deleteArchivedTagFromQueueResult = await nonAdminBikeTag.deleteTag(nonWinningTag)
if (deleteArchivedTagFromQueueResult.success) {
results.push({
message: 'non-winning found image archived',
message: 'non-winning tag deleted from queue',
game: gameName,
tag: nonWinningTag,
})
} else {
// console.log({ archiveTagResult })
// console.log({ deleteArchivedTagFromQueueResult })
results.push({
message: 'error archiving non-winning found image',
message: 'error deleting non-winning tag from the queue',
game: gameName,
tag: nonWinningTag,
})
errors = true
/// No error here?
}
}
/* delete using player credentials (queuehash is player album) */
const deleteArchivedTagFromQueueResult = await nonAdminBikeTag.deleteTag(nonWinningTag)
if (deleteArchivedTagFromQueueResult.success) {
results.push({
message: 'non-winning tag deleted from queue',
game: gameName,
tag: nonWinningTag,
})
} else {
// console.log({ deleteArchivedTagFromQueueResult })
} else {
// Just remove all tags from the queue
for (const queuedTag of queuedTags) {
const deletedTagResult = await nonAdminBikeTag.deleteTag(queuedTag)

results.push({
message: 'error deleting non-winning tag from the queue',
message: deletedTagResult.success
? 'tag deleted from queue'
: 'error deleting tag from the queue',
game: gameName,
tag: nonWinningTag,
tag: queuedTag,
})
/// No error here?
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ created()
.spacer-bottom {
margin-bottom: 50px;
}
.spacer-less {
height: 0;
}
Expand Down
7 changes: 4 additions & 3 deletions src/common/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ export const ordinalSuffixOf = (n: number) => {
}
export const getBikeTagHash = (val: string): string => md5(`${val}${process.env.HOST_KEY}`)

export const getImgurImageSized = (imgurUrl = '', size = 'm') =>
imgurUrl
.replace('.jpeg', `${size}.jpg`)
export const getImgurImageSized = (imgurUrl = '', size = 'm') => {
return imgurUrl
.replace('.jpg', `${size}.jpg`)
.replace('.jpeg', `${size}.jpg`)
.replace('.gif', `${size}.gif`)
.replace('.png', `${size}.png`)
.replace('.webp', `${size}.webp`)
.replace('.mp4', `${size}.mp4`)
}

export const getDomainInfo = (req: any): DomainInfo => {
const nonSubdomainHosts = [
Expand Down
2 changes: 1 addition & 1 deletion src/views/Round.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</div>
</template>

<script setup name="QueueBikeTagView">
<script setup name="RoundView">
import { BiketagQueueFormSteps } from '@/common/types'
import { useBikeTagStore } from '@/store/index'
import { computed, onMounted, ref } from 'vue'
Expand Down

0 comments on commit c449d93

Please sign in to comment.