Skip to content

Commit

Permalink
wip change area.media type to match the new common media type
Browse files Browse the repository at this point in the history
  • Loading branch information
viet nguyen committed Apr 30, 2023
1 parent 1bc2ee4 commit 9e9be63
Show file tree
Hide file tree
Showing 12 changed files with 207 additions and 102 deletions.
4 changes: 2 additions & 2 deletions src/db/MediaMetaType.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export type ImageFormatType = 'jpeg' | 'png' | 'webp' | 'avif'
export interface MediaMetaType {
name: string
export interface MediaObjectType {
mediaUrl: string
width: number
height: number
format: ImageFormatType
Expand Down
8 changes: 4 additions & 4 deletions src/db/MediaObjectSchema.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import mongoose from 'mongoose'

import { MediaMetaType } from './MediaMetaType.js'
import { MediaObjectType } from './MediaMetaType.js'

const { Schema } = mongoose

const schema = new Schema<MediaMetaType>({
name: { type: Schema.Types.String, unique: true },
const schema = new Schema<MediaObjectType>({
mediaUrl: { type: Schema.Types.String, unique: true },
width: { type: Schema.Types.Number, required: true },
height: { type: Schema.Types.Number, required: true },
size: { type: Schema.Types.Number, required: true },
Expand All @@ -14,6 +14,6 @@ const schema = new Schema<MediaMetaType>({
mtime: { type: Schema.Types.Date, required: true }
}, { _id: true, timestamps: true })

export const getMediaObjectModel = (): mongoose.Model<MediaMetaType> => {
export const getMediaObjectModel = (): mongoose.Model<MediaObjectType> => {
return mongoose.model('media_objects', schema)
}
7 changes: 4 additions & 3 deletions src/db/MediaTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { MUUID } from 'uuid-mongodb'

import { AreaType } from './AreaTypes.js'
import { ClimbType } from './ClimbTypes.js'
import { MediaMetaType } from './MediaMetaType.js'
import { MediaObjectType } from './MediaMetaType.js'

// Type for 'Media' collection schema
// This is a misnomer. Should have been called Tag.
Expand All @@ -26,7 +26,7 @@ export enum RefModelType {
/**
* A tag with media metadata
*/
export type BaseTagType = MediaType & MediaMetaType
export type BaseTagType = MediaType & MediaObjectType

export interface CompleteAreaTag extends BaseTagType {
area: AreaType
Expand All @@ -46,8 +46,9 @@ export interface MediaListByAuthorType {
export interface SimpleTag {
id: MUUID
name: string
type: number
}
export interface UserMediaWithTags extends MediaMetaType {
export interface UserMediaWithTags extends MediaObjectType {
climbTags: SimpleTag[]
areaTags: SimpleTag[]
}
Expand Down
6 changes: 3 additions & 3 deletions src/db/utils/jobs/CreateMediaMetaCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import sharp from 'sharp'
import { glob } from 'glob'
import { connectDB, gracefulExit } from '../../index.js'
import { logger } from '../../../logger.js'
import { MediaMetaType } from '../../MediaMetaType.js'
import { MediaObjectType } from '../../MediaMetaType.js'
import { getMediaObjectModel } from '../../MediaObjectSchema.js'

const onConnected = async (): Promise<void> => {
Expand All @@ -21,8 +21,8 @@ const onConnected = async (): Promise<void> => {
if (width == null || height == null || image.size == null) continue
if ((format !== 'avif' && format !== 'jpeg' && format !== 'png' && format !== 'webp')) continue

const meta: MediaMetaType = {
name: `/u/${image.parent?.name ?? ''}/${image.name}`,
const meta: MediaObjectType = {
mediaUrl: `/u/${image.parent?.name ?? ''}/${image.name}`,
mtime: new Date(Math.round(image?.mtimeMs ?? 0)),
birthTime: new Date(Math.round(image?.birthtimeMs ?? 0)),
size: image.size,
Expand Down
6 changes: 5 additions & 1 deletion src/graphql/media/MediaResolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,12 @@ const MediaResolvers = {
}
},

MediaWithTags: {
username: async (node: BaseTagType) => await getUserNickFromMediaDir(node.mediaUrl.substring(3, 39))
},

SimpleTag: {
id: async (node: SimpleTag) => node.id.toUUID().toString()
id: (node: SimpleTag) => node.id.toUUID().toString()
},

MediaListByAuthorType: {
Expand Down
2 changes: 1 addition & 1 deletion src/graphql/media/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const MediaQueries = {
*/
getUserMedia: async (_, { userUuid, limit = 1000 }: { limit: number | undefined, userUuid: string }, { dataSources }): Promise<UserMediaWithTags[]> => {
const { media }: DataSourcesType = dataSources
return await media.getUserPhotos(userUuid, limit)
return await media.getUserMedia(userUuid, limit)
},

getTagsLeaderboard: async (_, { limit = 30 }: { limit: number }, { dataSources }): Promise<TagsLeaderboardType[]> => {
Expand Down
7 changes: 4 additions & 3 deletions src/graphql/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import fs from 'fs'

import { gql } from 'apollo-server'
import { DocumentNode } from 'graphql'
import MediaDataSource from '../model/MediaDataSource.js'

/**
* It takes a file name as an argument, reads the file, and returns a GraphQL DocumentNode.
Expand Down Expand Up @@ -164,9 +165,9 @@ const resolvers = {

ancestors: (node: ClimbGQLQueryType) => node.ancestors.split(','),

media: async (node: any, args: any, { dataSources }) => {
const { areas }: { areas: AreaDataSource } = dataSources
return await areas.findMediaByClimbId(node._id)
media: async (node: ClimbType, args: any, { dataSources }) => {
const { media }: { media: MediaDataSource } = dataSources
return await media.findMediaByClimbId(node._id, node.name)
},

content: (node: ClimbGQLQueryType) => node.content == null
Expand Down
2 changes: 1 addition & 1 deletion src/graphql/schema/Area.gql
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ type Area {
"The total number of climbs in this area"
totalClimbs: Int!
"Media associated with this area, or its child climbs"
media: [BaseTag]
media: [MediaWithTags]
createdAt: Date
createdBy: ID
updatedAt: Date
Expand Down
2 changes: 1 addition & 1 deletion src/graphql/schema/Climb.gql
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type Climb {
ancestors: [String!]!

"Media associated with this climb"
media: [BaseTag]
media: [MediaWithTags]

yds: String @deprecated(reason: "Migrating to 'grades' field")

Expand Down
6 changes: 4 additions & 2 deletions src/graphql/schema/Media.gql
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Query {
}

type Query {
getUserMedia(userUuid: ID!): [MediaWithTags]
getUserMedia(userUuid: ID!, limit: Int): [MediaWithTags]
}

type Query {
Expand Down Expand Up @@ -115,10 +115,12 @@ type MediaListByAuthorType {
type SimpleTag {
id: ID!
name: String!
type: Int!
}

type MediaWithTags implements IMediaMetadata {
name: String!
username: String
mediaUrl: String!
width: Int!
height: Int!
format: String!
Expand Down
Loading

0 comments on commit 9e9be63

Please sign in to comment.