Skip to content

Commit

Permalink
Fix Moments model and fetchs to Compass (#22)
Browse files Browse the repository at this point in the history
* Fix Moments model and fetch to Compass

* Remove unnecessary Moments stuff and fix typos

* Bump package version
  • Loading branch information
nacho9900 authored May 23, 2023
1 parent 196ba64 commit c1afe63
Show file tree
Hide file tree
Showing 20 changed files with 81 additions and 185 deletions.
4 changes: 2 additions & 2 deletions examples/moments/backend/src/methods/create_moment.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable max-statements */
import { MomentsClient, createMomentInput } from '@poap-xyz/moments';
import { MomentsClient, CreateMomentInput } from '@poap-xyz/moments';
import fs from 'fs';
import mime from 'mime';

Expand All @@ -12,7 +12,7 @@ export const create_moment = async (client: MomentsClient): Promise<void> => {
return;
}

const input: createMomentInput = {
const input: CreateMomentInput = {
dropId: 110148,
/**
* The Token ID related to the moment (Optional)
Expand Down
2 changes: 1 addition & 1 deletion packages/drops/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@poap-xyz/drops",
"version": "0.0.16",
"version": "0.0.17",
"description": "Drops module for the poap.js library",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.mjs",
Expand Down
2 changes: 1 addition & 1 deletion packages/moments/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@poap-xyz/moments",
"version": "0.0.16",
"version": "0.0.17",
"description": "Moments module for the poap.js library",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.mjs",
Expand Down
47 changes: 25 additions & 22 deletions packages/moments/src/MomentsClient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PoapMomentsApi, CompassProvider } from '@poap-xyz/providers';
import { PaginatedResult, nextCursor } from '@poap-xyz/utils';
import { createMomentInput, FetchMomentsInput } from './types';
import { CreateMomentInput, FetchMomentsInput } from './types';
import { Moment } from './domain/Moment';
import {
createBetweenFilter,
Expand All @@ -9,16 +9,19 @@ import {
creatEqFilter,
filterUndefinedProperties,
} from './queries/utils';
import { MomentsQueryResponse, PAGINATED_MOMENTS_QUERY } from './queries';
import { MediaStatus } from './domain/MediaStatus';
import {
MomentResponse,
MomentsQueryResponse,
PAGINATED_MOMENTS_QUERY,
} from './queries';

export class MomentsClient {
constructor(
private PoapMomentsApi: PoapMomentsApi,
private CompassProvider: CompassProvider,
) {}

async createMoment(input: createMomentInput): Promise<Moment> {
async createMoment(input: CreateMomentInput): Promise<Moment> {
const { url, key } = await this.PoapMomentsApi.getSignedUrl();
await this.PoapMomentsApi.uploadFile(input.file, url, input.fileType);
await this.PoapMomentsApi.waitForMediaProcessing(key, input.timeOut);
Expand All @@ -33,7 +36,8 @@ export class MomentsClient {
response.author,
response.createdOn,
response.dropId,
response.media,
// We will always have a gateway because we wait for the media to be processed
response.media.gateways!,
response.tokenId,
);
}
Expand Down Expand Up @@ -75,27 +79,26 @@ export class MomentsClient {
variables,
);

const moments_response: Moment[] = response.data.moments.map((moment) => {
return new Moment(
moment.id,
moment.author,
new Date(moment.created_on),
moment.drop_id,
{
key: moment.media_key,
mimeType: moment.mime_type,
status: MediaStatus.PROCESSED,
hash: moment.media_hash,
},
moment.token_id,
);
});
const momentsResponse: Moment[] = response.data.moments.map(
this.getMomentFromMomentResponse,
);

const result = new PaginatedResult<Moment>(
moments_response,
nextCursor(moments_response.length, limit, offset),
momentsResponse,
nextCursor(momentsResponse.length, limit, offset),
);

return result;
}

private getMomentFromMomentResponse(momentResponse: MomentResponse): Moment {
return new Moment(
momentResponse.id,
momentResponse.author,
new Date(momentResponse.created_on),
momentResponse.drop_id,
momentResponse.gateways,
momentResponse.token_id,
);
}
}
29 changes: 0 additions & 29 deletions packages/moments/src/domain/Media.ts

This file was deleted.

20 changes: 0 additions & 20 deletions packages/moments/src/domain/MediaStatus.ts

This file was deleted.

17 changes: 7 additions & 10 deletions packages/moments/src/domain/Moment.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { Media } from './Media';

/**
* Represents a moment.
*/
Expand All @@ -19,12 +17,6 @@ export class Moment {
*/
public readonly createdOn: Date;

/**
* The media file associated with the moment.
* @type {Media}
*/
public readonly media: Media;

/**
* The drop ID related to the moment.
*/
Expand All @@ -35,19 +27,24 @@ export class Moment {
*/
public readonly tokenId?: number;

/**
* Indicates an array of gateways that the media file is stored on.
*/
public readonly gateways: string[];

constructor(
id: string,
author: string,
createdOn: Date,
dropId: number,
media: Media,
gateways: string[],
tokenId?: number,
) {
this.id = id;
this.author = author;
this.createdOn = createdOn;
this.dropId = dropId;
this.tokenId = tokenId;
this.media = media;
this.gateways = gateways;
}
}
2 changes: 1 addition & 1 deletion packages/moments/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { createMomentInput, FetchMomentsInput } from './types/input';
export { CreateMomentInput, FetchMomentsInput } from './types/input';
export { Moment } from './domain/Moment';
export { MomentsClient } from './MomentsClient';
10 changes: 2 additions & 8 deletions packages/moments/src/queries/PaginatedMoments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ export const PAGINATED_MOMENTS_QUERY = `
created_on
drop_id
id
location
media_gateways
media_hash
gateways
media_key
mime_type
token_id
}
}
Expand All @@ -25,11 +22,8 @@ export interface MomentResponse {
created_on: string;
drop_id: number;
id: string;
location: string;
media_gateways: string[];
media_hash: string;
gateways: string[];
media_key: string;
mime_type: string;
token_id: number;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/moments/src/types/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @property {string} timeOut - The amount of time to wait until media is processed.
* @property {string} fileType - The type of the file.
*/
export interface createMomentInput {
export interface CreateMomentInput {
dropId: number;
tokenId?: number;
file: Buffer;
Expand Down
2 changes: 1 addition & 1 deletion packages/providers/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@poap-xyz/providers",
"version": "0.0.16",
"version": "0.0.17",
"description": "Providers module for the poap.js library",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.mjs",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { InvalidMediaFileError } from './errors/InvalidMediaFileError';
import { MediaStatus } from './constants';
import { CreateMomentResponse } from '../../ports/MomentsApiProvider';
import { CreateMomentInput } from '../../ports/MomentsApiProvider';
import { MomentsApiProvider } from '../../ports';
import { AuthenticationProvider } from '../../ports';
import axios, { AxiosError } from 'axios';
import { MediaStatus } from './Types/MediaStatus';

const MOMENTS_BASE_URL = 'https://moments.poap.tech';

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export enum MediaStatus {
INVALID = 'INVALID',
PROCESSED = 'PROCESSED',
IN_PROCESS = 'IN_PROCESS',
}
20 changes: 0 additions & 20 deletions packages/providers/src/core/PoapMomentsApi/constants.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export interface CreateMomentResponse {
id: string;

/**
* The author of the moment (for now only ETH accounts).
*/
author: string;

/**
* When the moment was received on the API.
*/
createdOn: Date;

/**
* The media file associated with the moment.
*/
media: {
gateways: string[];
};

/**
* The drop ID related to the moment.
*/
dropId: number;

/**
* The token ID related to the moment (optional).
*/
tokenId?: number;
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './input';
export * from './response';
export * from './CreateMomentInput';
export * from './CreateMomentResponse';
Loading

0 comments on commit c1afe63

Please sign in to comment.