diff --git a/src/commands/climb.ts b/src/commands/climb.ts index 499e248..8f9c544 100644 --- a/src/commands/climb.ts +++ b/src/commands/climb.ts @@ -21,7 +21,8 @@ import { client as graphqlClient, GetClimbQuery, type GetClimbQueryResponse, - type GetClimbQueryVariables + type GetClimbQueryVariables, + getImageURL } from '../utils/openbeta'; import { getDisciplineIcon, @@ -64,7 +65,9 @@ export async function handler( const { climb } = response; // Create Embed - const image = climb.media.find(media => media.mediaType === 0); + const image = climb.media.find( + media => ['jpeg', 'png', 'webp'].indexOf(media.format) !== -1 + ); const fields: APIEmbedField[] = [ (climb.grades?.yds || climb.grades?.vscale) && { name: 'Grade', @@ -92,7 +95,7 @@ export async function handler( description: climb.content.description, url: `https://openbeta.io/climbs/${climb.uuid}`, thumbnail: image && { - url: `https://openbeta.sirv.com${image.mediaUrl}` + url: getImageURL(image.mediaUrl) }, footer: { text: climb.pathTokens.join(' > ') diff --git a/src/commands/crag.ts b/src/commands/crag.ts index 6f8113a..8547259 100644 --- a/src/commands/crag.ts +++ b/src/commands/crag.ts @@ -20,7 +20,8 @@ import { client as graphqlClient, GetAreaQuery, type GetAreaQueryResponse, - type GetAreaQueryVariables + type GetAreaQueryVariables, + getImageURL } from '../utils/openbeta'; import { chunk } from 'lodash'; @@ -61,7 +62,9 @@ export async function handler( // Create Embed const maxClimbsOrAreas = 50; const maxClimbsOrAreasPerField = 5; - const image = area.media.find(media => media.mediaType === 0); + const image = area.media.find( + media => ['jpeg', 'png', 'webp'].indexOf(media.format) !== -1 + ); const areaChunks = chunk(area.children, maxClimbsOrAreasPerField); const climbChunks = chunk(area.climbs, maxClimbsOrAreasPerField); const hasMoreAreas = @@ -106,7 +109,7 @@ export async function handler( description: area.content.description, url: `https://openbeta.io/crag/${area.uuid}`, thumbnail: image && { - url: `https://openbeta.sirv.com${image.mediaUrl}` + url: getImageURL(image.mediaUrl) }, footer: { text: area.pathTokens.join(' > ') diff --git a/src/utils/openbeta.ts b/src/utils/openbeta.ts index b1d45b2..9512b80 100644 --- a/src/utils/openbeta.ts +++ b/src/utils/openbeta.ts @@ -5,12 +5,18 @@ import type { IClimbContent, IClimbProps } from './openbeta/climb-types'; -import type { MediaType } from './openbeta/media-types'; +import type { MediaObject } from './openbeta/media-types'; export const client = new GraphQLClient( process.env.OPEN_BETA_GRAPHQL_ENDPOINT! ); +export const BASE_URL_IMAGE = 'https://storage.googleapis.com/openbeta-prod'; + +export function getImageURL(imagePath: string) { + return `${BASE_URL_IMAGE}${imagePath}?format=jpg&w=600&q=90`; +} + export type GetClimbQueryVariables = { uuid: string; }; @@ -19,7 +25,7 @@ export type GetClimbQueryResponse = { climb: Pick & { uuid: string; content: IClimbContent; - media: Array>; + media: Array>; metadata: { lat: number; lng: number; @@ -49,7 +55,7 @@ export const GetClimbQuery = gql` protection } media { - mediaType + format mediaUrl } metadata { @@ -89,7 +95,7 @@ export type GetAreaQueryResponse = { area: Pick & { uuid: string; content: IAreaContent; - media: Array>; + media: Array>; metadata: { lat: number; lng: number; @@ -120,7 +126,7 @@ export const GetAreaQuery = gql` description } media { - mediaType + format mediaUrl } totalClimbs diff --git a/src/utils/openbeta/media-types.ts b/src/utils/openbeta/media-types.ts index f170cbe..37aa9b4 100644 --- a/src/utils/openbeta/media-types.ts +++ b/src/utils/openbeta/media-types.ts @@ -1,64 +1,25 @@ // https://github.com/OpenBeta/openbeta-graphql/blob/develop/src/db/MediaTypes.ts +import type { Point } from '@turf/helpers'; +export type ImageFormatType = 'jpeg' | 'png' | 'webp' | 'avif'; -import type { AreaType } from './area-types'; -import type { ClimbType } from './climb-types'; - -// Type for 'Media' collection schema -export interface MediaType { - _id?: string; - mediaUuid: string; - mediaUrl: string; - mediaType: number; // 0: image, 1: video - destinationId: string; // reference to a climb or area - destType: number; // 0: climb, 1: area - onModel: RefModelType; -} - -export enum RefModelType { - climbs = 'climbs', - areas = 'areas' -} - -export interface MediaListByAuthorType { +export interface MediaObject { _id: string; - tagList: MediaType[]; -} - -export interface MediaInputType { - mediaUuid: string; + userUuid: string; mediaUrl: string; - mediaType: number; - destinationId: string; - destType: number; + width: number; + height: number; + format: ImageFormatType; + createdAt: Date; + size: number; + entityTags: EntityTag[]; } -interface BaseTagType { +export interface EntityTag { _id: string; - mediaUuid: string; - mediaUrl: string; - mediaType: number; - destType: number; - onModel: RefModelType; -} - -export interface AreaTagType extends BaseTagType { - area: AreaType; -} - -export interface ClimbTagType extends BaseTagType { - climb: ClimbType; -} - -export type TagEntryResultType = AreaTagType | ClimbTagType; - -export interface DeleteTagResult { - id: string; - mediaUuid: string; - destType: number; - destinationId: string; -} - -export interface TagsLeaderboardType { - userUuid: string; - total: number; + targetId: string; + type: number; + ancestors: string; + climbName?: string; + areaName: string; + lnglat: Point; }