Skip to content

Commit

Permalink
Updated GraphQL Schema / Image handling after move to google image st…
Browse files Browse the repository at this point in the history
…orage
  • Loading branch information
jlaramie committed May 11, 2023
1 parent dd5a1eb commit d162791
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 67 deletions.
9 changes: 6 additions & 3 deletions src/commands/climb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import {
client as graphqlClient,
GetClimbQuery,
type GetClimbQueryResponse,
type GetClimbQueryVariables
type GetClimbQueryVariables,
getImageURL
} from '../utils/openbeta';
import {
getDisciplineIcon,
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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(' > ')
Expand Down
9 changes: 6 additions & 3 deletions src/commands/crag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import {
client as graphqlClient,
GetAreaQuery,
type GetAreaQueryResponse,
type GetAreaQueryVariables
type GetAreaQueryVariables,
getImageURL
} from '../utils/openbeta';
import { chunk } from 'lodash';

Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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(' > ')
Expand Down
16 changes: 11 additions & 5 deletions src/utils/openbeta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand All @@ -19,7 +25,7 @@ export type GetClimbQueryResponse = {
climb: Pick<IClimbProps, 'grades' | 'fa' | 'name' | 'safety' | 'type'> & {
uuid: string;
content: IClimbContent;
media: Array<Pick<MediaType, 'mediaType' | 'mediaUrl'>>;
media: Array<Pick<MediaObject, 'format' | 'mediaUrl'>>;
metadata: {
lat: number;
lng: number;
Expand Down Expand Up @@ -49,7 +55,7 @@ export const GetClimbQuery = gql`
protection
}
media {
mediaType
format
mediaUrl
}
metadata {
Expand Down Expand Up @@ -89,7 +95,7 @@ export type GetAreaQueryResponse = {
area: Pick<IAreaProps, 'area_name' | 'totalClimbs' | 'pathTokens'> & {
uuid: string;
content: IAreaContent;
media: Array<Pick<MediaType, 'mediaType' | 'mediaUrl'>>;
media: Array<Pick<MediaObject, 'format' | 'mediaUrl'>>;
metadata: {
lat: number;
lng: number;
Expand Down Expand Up @@ -120,7 +126,7 @@ export const GetAreaQuery = gql`
description
}
media {
mediaType
format
mediaUrl
}
totalClimbs
Expand Down
73 changes: 17 additions & 56 deletions src/utils/openbeta/media-types.ts
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit d162791

Please sign in to comment.