From a7d3a34103f50b6bd449644c38c232b5ef9a7e85 Mon Sep 17 00:00:00 2001 From: kakkokari-gtyih <67428053+kakkokari-gtyih@users.noreply.github.com> Date: Sun, 29 Dec 2024 12:31:12 +0900 Subject: [PATCH] fix type assertion --- .../backend/test-federation/test/timeline.test.ts | 7 ++++++- packages/frontend/src/components/MkTimeline.vue | 4 ++-- packages/misskey-js/etc/misskey-js.api.md | 11 +++++++---- packages/misskey-js/src/entities.ts | 5 ++--- packages/misskey-js/src/note.ts | 6 +++++- 5 files changed, 22 insertions(+), 11 deletions(-) diff --git a/packages/backend/test-federation/test/timeline.test.ts b/packages/backend/test-federation/test/timeline.test.ts index 843a9584a5..9c8593b059 100644 --- a/packages/backend/test-federation/test/timeline.test.ts +++ b/packages/backend/test-federation/test/timeline.test.ts @@ -49,7 +49,12 @@ describe('Timeline', () => { async () => { note = (await alice.client.request('notes/create', { text, ...noteParams })).createdNote; }, - 'note', msg => (msg as Misskey.entities.Note).text === text, + 'note', (msg) => { + if (Misskey.note.isStreamNote(msg)) { + return false; + } + return msg.text === text; + }, channelParams, ); strictEqual(streamingFired, expect); diff --git a/packages/frontend/src/components/MkTimeline.vue b/packages/frontend/src/components/MkTimeline.vue index 2db4f2b51d..5b7f49368f 100644 --- a/packages/frontend/src/components/MkTimeline.vue +++ b/packages/frontend/src/components/MkTimeline.vue @@ -80,7 +80,7 @@ async function prepend(data: Misskey.entities.Note | Misskey.entities.StreamNote let note: Misskey.entities.Note & MisskeyEntity; - if ('_allowCached_' in data) { + if (Misskey.note.isStreamNote(data)) { let fullNote: Misskey.entities.Note | null = null; const { _allowCached_, ..._data } = data; @@ -91,7 +91,7 @@ async function prepend(data: Misskey.entities.Note | Misskey.entities.StreamNote credentials: 'omit', }); if (!res.ok) return; - fullNote = await res.json(); + fullNote = (await res.json()) as Misskey.entities.Note; } else { fullNote = await misskeyApi('notes/show', { noteId: data.id, diff --git a/packages/misskey-js/etc/misskey-js.api.md b/packages/misskey-js/etc/misskey-js.api.md index 9496cfd0b7..baeaff23c6 100644 --- a/packages/misskey-js/etc/misskey-js.api.md +++ b/packages/misskey-js/etc/misskey-js.api.md @@ -2359,6 +2359,9 @@ type ISigninHistoryResponse = operations['i___signin-history']['responses']['200 // @public (undocumented) function isPureRenote(note: Note): note is PureRenote; +// @public (undocumented) +function isStreamNote(note: Note | StreamNote): note is StreamNote; + // @public (undocumented) export interface IStream extends EventEmitter { // (undocumented) @@ -2652,7 +2655,8 @@ type Note = components['schemas']['Note']; declare namespace note { export { - isPureRenote + isPureRenote, + isStreamNote } } export { note } @@ -3194,14 +3198,13 @@ export type StreamEvents = { } & BroadcastEvents; // @public (undocumented) -type StreamNote = ({ +type StreamNote = { id: Note['id']; poll?: Pick, 'choices'>; reply?: Pick, 'myReaction'>; renote?: Pick, 'myReaction'>; -} & { _allowCached_: true; -}) | { +} | { id: Note['id']; _allowCached_: false; }; diff --git a/packages/misskey-js/src/entities.ts b/packages/misskey-js/src/entities.ts index 7d26a26851..750dfdb6c5 100644 --- a/packages/misskey-js/src/entities.ts +++ b/packages/misskey-js/src/entities.ts @@ -248,14 +248,13 @@ export type AnnouncementCreated = { announcement: Announcement; }; -export type StreamNote = ({ +export type StreamNote = { id: Note['id']; poll?: Pick, 'choices'>; reply?: Pick, 'myReaction'>; renote?: Pick, 'myReaction'>; -} & { _allowCached_: true; -}) | { +} | { id: Note['id']; _allowCached_: false; }; diff --git a/packages/misskey-js/src/note.ts b/packages/misskey-js/src/note.ts index 5c8298c7e4..e198a70c2e 100644 --- a/packages/misskey-js/src/note.ts +++ b/packages/misskey-js/src/note.ts @@ -1,4 +1,4 @@ -import type { Note, PureRenote } from './entities.js'; +import type { Note, StreamNote, PureRenote } from './entities.js'; export function isPureRenote(note: Note): note is PureRenote { return ( @@ -10,3 +10,7 @@ export function isPureRenote(note: Note): note is PureRenote { note.poll == null ); } + +export function isStreamNote(note: Note | StreamNote): note is StreamNote { + return '_allowCached_' in note && typeof note._allowCached_ === 'boolean'; +}