Skip to content

Commit 7dbf15e

Browse files
authored
fix: Fix v2.media.create nor working with $raw (#1376)
1 parent 9cac4ec commit 7dbf15e

2 files changed

Lines changed: 57 additions & 5 deletions

File tree

src/adapters/action/dispatcher-http-hook-mastodon.spec.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { httpGet, HttpMockImpl, httpPost } from "../../__mocks__/index.js";
2+
import { type HttpResponse } from "../../interfaces/http.js";
23
import { MastoHttpError, MastoTimeoutError } from "../errors/index.js";
34
import { HttpActionDispatcher } from "./dispatcher-http.js";
45
import { HttpActionDispatcherHookMastodon } from "./dispatcher-http-hook-mastodon.js";
@@ -38,6 +39,7 @@ describe("DispatcherHttp", () => {
3839
path: "/api/v2/media",
3940
data: undefined,
4041
meta: {},
42+
raw: false,
4143
});
4244

4345
expect(media).toHaveProperty("id", "1");
@@ -65,6 +67,7 @@ describe("DispatcherHttp", () => {
6567
path: "/api/v2/media",
6668
data: undefined,
6769
meta: {},
70+
raw: false,
6871
});
6972

7073
await expect(promise).rejects.toBeInstanceOf(MastoTimeoutError);
@@ -91,6 +94,7 @@ describe("DispatcherHttp", () => {
9194
path: "/api/v2/media",
9295
data: undefined,
9396
meta: {},
97+
raw: false,
9498
});
9599

96100
await expect(promise).rejects.toBeInstanceOf(MastoTimeoutError);
@@ -114,6 +118,7 @@ describe("DispatcherHttp", () => {
114118
path: "/api/v2/media",
115119
data: undefined,
116120
meta: {},
121+
raw: false,
117122
});
118123

119124
await expect(promise).rejects.toBeInstanceOf(Error);
@@ -138,9 +143,41 @@ describe("DispatcherHttp", () => {
138143
skipPolling: true,
139144
},
140145
meta: {},
146+
raw: false,
141147
});
142148

143149
expect(media).toHaveProperty("id", "1");
144150
expect(httpGet).toHaveBeenCalledTimes(0);
145151
});
152+
153+
it("waits for media attachment to be created ($raw)", async () => {
154+
const http = new HttpMockImpl();
155+
const dispatcher = new HttpActionDispatcher(
156+
http,
157+
new HttpActionDispatcherHookMastodon(http),
158+
);
159+
const postHeaders = new Headers();
160+
161+
httpPost.mockResolvedValueOnce({
162+
data: { id: "1" },
163+
headers: postHeaders,
164+
});
165+
166+
httpGet.mockResolvedValueOnce({
167+
data: { id: "1", url: "https://example.com" },
168+
headers: new Headers(),
169+
});
170+
171+
const { data, headers } = await dispatcher.dispatch<HttpResponse<unknown>>({
172+
type: "create",
173+
path: "/api/v2/media",
174+
data: undefined,
175+
meta: {},
176+
raw: true,
177+
});
178+
179+
expect(data).toHaveProperty("id", "1");
180+
expect(data).toHaveProperty("url", "https://example.com");
181+
expect(headers).toBe(postHeaders);
182+
});
146183
});

src/adapters/action/dispatcher-http-hook-mastodon.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
type Encoding,
77
type Http,
88
type HttpMetaParams,
9+
type HttpResponse,
910
} from "../../interfaces/index.js";
1011
import { type mastodon } from "../../mastodon/index.js";
1112
import { isRecord, sleep } from "../../utils/index.js";
@@ -116,15 +117,29 @@ export class HttpActionDispatcherHookMastodon
116117
return false;
117118
}
118119

119-
afterDispatch(action: AnyAction, result: unknown): unknown {
120+
afterDispatch(action: AnyAction, _result: unknown): unknown {
120121
if (action.type === "create" && action.path === "/api/v2/media") {
121-
const media = result as mastodon.v1.MediaAttachment;
122122
if (isRecord(action.data) && action.data?.skipPolling === true) {
123-
return media;
123+
return _result;
124+
}
125+
126+
if (action.raw) {
127+
const result = _result as HttpResponse<mastodon.v1.MediaAttachment>;
128+
129+
return waitForMediaAttachment(
130+
result.data.id,
131+
this.mediaTimeout,
132+
this.http,
133+
).then((data) => ({
134+
headers: result.headers,
135+
data,
136+
}));
137+
} else {
138+
const result = _result as mastodon.v1.MediaAttachment;
139+
return waitForMediaAttachment(result.id, this.mediaTimeout, this.http);
124140
}
125-
return waitForMediaAttachment(media.id, this.mediaTimeout, this.http);
126141
}
127142

128-
return result;
143+
return _result;
129144
}
130145
}

0 commit comments

Comments
 (0)