Skip to content

Commit

Permalink
chore(rest): Restore auto media awaiting
Browse files Browse the repository at this point in the history
  • Loading branch information
neet committed Jul 25, 2023
1 parent 604c3d6 commit 31e20e4
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 49 deletions.
10 changes: 10 additions & 0 deletions src/builder/builder.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { snakeCase } from 'change-case';

import type { mastodon } from '..';
import type { Http, HttpMetaParams } from '../http';
import { Paginator } from '../paginator';
import { noop } from '../utils/noop';
import { inferEncoding } from './encoding';
import { waitForMediaAttachment } from './wait-for';

const get =
<T>(http: Http, context: string[]) =>
Expand Down Expand Up @@ -41,6 +43,14 @@ const apply =
return http.get(path, data, meta);
}
case 'create': {
if (path === '/api/v2/media') {
return http
.post<mastodon.v1.MediaAttachment>(path, data, meta)
.then((media) => {
return waitForMediaAttachment(http, media.id);
});
}

return http.post(path, data, meta);
}
case 'update': {
Expand Down
16 changes: 9 additions & 7 deletions src/utils/wait-for.ts → src/builder/wait-for.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import type { mastodon } from '..';
import type { Http, mastodon } from '..';
import { MastoHttpNotFoundError, MastoTimeoutError } from '../errors';
import { delay } from './delay';
import { Timeout } from './timeout';
import { delay } from '../utils/delay';
import { Timeout } from '../utils/timeout';

export const waitForMediaAttachment = async (
client: mastodon.RestAPIClient,
http: Http,
id: string,
ms = 60 * 1000,
timeoutMs = 60 * 1000,
): Promise<mastodon.v1.MediaAttachment> => {
let media: mastodon.v1.MediaAttachment | undefined;
const timeout = new Timeout(ms);
const timeout = new Timeout(timeoutMs);

while (media == undefined) {
if (timeout.signal.aborted) {
Expand All @@ -21,7 +21,9 @@ export const waitForMediaAttachment = async (
await delay(1000);

try {
const processing = await client.v1.media.select(id).fetch();
const processing = await http.get<mastodon.v1.MediaAttachment>(
`/api/v1/media/${id}`,
);

if (processing.url != undefined) {
media = processing;
Expand Down
5 changes: 3 additions & 2 deletions src/paginator.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Headers } from '@mastojs/ponyfills';

import { HttpMockImpl } from './__mocks__';
import type { HttpRequestParams } from './http';
import { Paginator } from './paginator';

describe('Paginator', () => {
Expand Down Expand Up @@ -64,10 +65,10 @@ describe('Paginator', () => {
await paginator.next();
await paginator.next();
expect(http.request).toBeCalledWith({
requestInit: { method: 'GET' },
method: 'GET',
searchParams: { max_id: '109382006402042919' },
path: '/api/v1/timelines/home',
});
} satisfies HttpRequestParams);
});

it('returns done when next link does not exist', async () => {
Expand Down
1 change: 0 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ export * from './delay';
export * from './timeout';
export * from './merge-abort-signals';
export * from './merge-headers-init';
export * from './wait-for';
export * from './exponential-backoff';
6 changes: 1 addition & 5 deletions tests/v2/media.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { fetch } from '@mastojs/ponyfills';

import { waitForMediaAttachment } from '../../src/utils';

const TRANSPARENT_1X1_PNG =
'data:image/jpeg;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==';

Expand All @@ -15,7 +13,6 @@ describe('media', () => {
it('creates a media attachment', async () => {
const file = await createFile();
let media = await admin.v2.media.create({ file });
media = await waitForMediaAttachment(admin, media.id);

media = await admin.v1.media.select(media.id).fetch();
expect(media.type).toBe('image');
Expand All @@ -29,8 +26,7 @@ describe('media', () => {
it('creates media attachment without polling', () => {
return sessions.use(async (session) => {
const file = await createFile();
let media = await session.rest.v2.media.create({ file });
media = await waitForMediaAttachment(session.rest, media.id);
const media = await session.rest.v2.media.create({ file });
expect(media.type).toBe('image');
});
});
Expand Down
70 changes: 36 additions & 34 deletions tests/websocket/timelines.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import assert from 'node:assert';

import type { mastodon } from '../../src';
import { waitForMediaAttachment } from '../../src/utils';

const TRANSPARENT_1X1_PNG =
'data:image/jpeg;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==';
Expand Down Expand Up @@ -37,37 +36,42 @@ describe('websocket', () => {
});
});

// it('streams public:media', () => {
// return sessions.use(async (session) => {
// let id!: string;
// try {
// const events = session.ws.subscribe('public:media');
// setImmediate(async () => {
// const media = await session.rest.v2.media.create(
// { file: TRANSPARENT_1X1_PNG },
// );
// await waitForMediaAttachment(session.rest, media.id);
// const status = await session.rest.v1.statuses.create({
// status: 'test',
// mediaIds: [media.id],
// visibility: 'public',
// });
// id = status.id;
// });
// const [event] = await events
// .filter((e): e is mastodon.UpdateEvent => e.event === 'update')
// .filter((e) => e.payload.id === id)
// .take(1)
// .toArray();
// assert(event?.event === 'update');
// expect(event?.payload?.id).toBe(id);
// } finally {
// session.ws.unsubscribe('public:media');
// session.ws.close();
// await session.rest.v1.statuses.select(id).remove();
// }
// });
// });
/*
it('streams public:media', () => {
return sessions.use(async (session) => {
let id!: string;
try {
const events = session.ws.subscribe('public:media');
setImmediate(async () => {
const media = await session.rest.v2.media.create({
file: TRANSPARENT_1X1_PNG,
});
const status = await session.rest.v1.statuses.create({
status: 'test',
mediaIds: [media.id],
visibility: 'public',
});
id = status.id;
});
const [event] = await events
.filter((e): e is mastodon.UpdateEvent => e.event === 'update')
.filter((e) => e.payload.id === id)
.take(1)
.toArray();
assert(event?.event === 'update');
expect(event?.payload?.id).toBe(id);
} finally {
session.ws.unsubscribe('public:media');
session.ws.close();
await session.rest.v1.statuses.select(id).remove();
}
});
});
*/

it('streams public:local', () => {
return sessions.use(async (session) => {
Expand Down Expand Up @@ -112,8 +116,6 @@ describe('websocket', () => {
file: TRANSPARENT_1X1_PNG,
});

await waitForMediaAttachment(session.rest, media.id);

const status = await session.rest.v1.statuses.create({
status: 'test',
mediaIds: [media.id],
Expand Down

0 comments on commit 31e20e4

Please sign in to comment.