From 28c11ca7afb7e47c3fe55180eda3ebd8c083992b Mon Sep 17 00:00:00 2001 From: Johann150 Date: Sat, 4 Feb 2023 15:58:13 +0100 Subject: [PATCH] refactor isPureRenote to foundkey-js --- packages/backend/src/misc/renote.ts | 12 ------------ .../remote/activitypub/renderer/note-or-renote.ts | 4 ++-- packages/backend/src/server/activitypub/outbox.ts | 1 - .../backend/src/server/api/endpoints/notes/create.ts | 7 +++---- packages/backend/src/services/note/delete.ts | 4 ++-- packages/client/src/components/note-detailed.vue | 7 +------ packages/client/src/components/note.vue | 8 +------- packages/client/src/scripts/get-note-menu.ts | 11 +++-------- packages/foundkey-js/src/entities.ts | 11 +++++++++++ 9 files changed, 23 insertions(+), 42 deletions(-) delete mode 100644 packages/backend/src/misc/renote.ts diff --git a/packages/backend/src/misc/renote.ts b/packages/backend/src/misc/renote.ts deleted file mode 100644 index 5b2657633..000000000 --- a/packages/backend/src/misc/renote.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { Note } from '@/models/entities/note.js'; - -export function isPureRenote(note: Note): note is Note & { renoteId: string, text: null, cw: null, fileIds: null | never[], hasPoll: false } { - return note.renoteId != null - && note.text == null - && note.cw == null - && ( - note.fileIds == null - || note.fileIds.length === 0 - ) - && !note.hasPoll; -} diff --git a/packages/backend/src/remote/activitypub/renderer/note-or-renote.ts b/packages/backend/src/remote/activitypub/renderer/note-or-renote.ts index cb013140c..0857cd5b2 100644 --- a/packages/backend/src/remote/activitypub/renderer/note-or-renote.ts +++ b/packages/backend/src/remote/activitypub/renderer/note-or-renote.ts @@ -1,14 +1,14 @@ +import * as foundkey from 'foundkey-js'; import config from '@/config/index.js'; import { Notes } from '@/models/index.js'; import { Note } from '@/models/entities/note.js'; -import { isPureRenote } from '@/misc/renote.js'; import { IActivity } from '@/remote/activitypub/types.js'; import renderNote from '@/remote/activitypub/renderer/note.js'; import renderCreate from '@/remote/activitypub/renderer/create.js'; import renderAnnounce from '@/remote/activitypub/renderer/announce.js'; export async function renderNoteOrRenoteActivity(note: Note): Promise { - if (isPureRenote(note)) { + if (foundkey.entities.isPureRenote(note)) { const renote = await Notes.findOneByOrFail({ id: note.renoteId }); return renderAnnounce(renote.uri ?? `${config.url}/notes/${renote.id}`, note); } else { diff --git a/packages/backend/src/server/activitypub/outbox.ts b/packages/backend/src/server/activitypub/outbox.ts index 7b8fefb3b..a0a6af011 100644 --- a/packages/backend/src/server/activitypub/outbox.ts +++ b/packages/backend/src/server/activitypub/outbox.ts @@ -12,7 +12,6 @@ import { countIf } from '@/prelude/array.js'; import * as url from '@/prelude/url.js'; import { Users, Notes } from '@/models/index.js'; import { Note } from '@/models/entities/note.js'; -import { isPureRenote } from '@/misc/renote.js'; import { makePaginationQuery } from '../api/common/make-pagination-query.js'; import { setResponseType } from '../activitypub.js'; diff --git a/packages/backend/src/server/api/endpoints/notes/create.ts b/packages/backend/src/server/api/endpoints/notes/create.ts index 1b272e9dc..d10ba4df0 100644 --- a/packages/backend/src/server/api/endpoints/notes/create.ts +++ b/packages/backend/src/server/api/endpoints/notes/create.ts @@ -1,5 +1,5 @@ import { In } from 'typeorm'; -import { noteVisibilities } from 'foundkey-js'; +import { noteVisibilities, entities } from 'foundkey-js'; import create from '@/services/note/create.js'; import { User } from '@/models/entities/user.js'; import { Users, DriveFiles, Notes, Channels, Blockings } from '@/models/index.js'; @@ -7,7 +7,6 @@ import { DriveFile } from '@/models/entities/drive-file.js'; import { Note } from '@/models/entities/note.js'; import { Channel } from '@/models/entities/channel.js'; import { HOUR } from '@/const.js'; -import { isPureRenote } from '@/misc/renote.js'; import config from '@/config/index.js'; import { ApiError } from '../../error.js'; import define from '../../define.js'; @@ -160,7 +159,7 @@ export default define(meta, paramDef, async (ps, user) => { throw e; }); - if (isPureRenote(renote)) throw new ApiError('PURE_RENOTE', 'Cannot renote a pure renote.'); + if (entities.isPureRenote(renote)) throw new ApiError('PURE_RENOTE', 'Cannot renote a pure renote.'); // check that the visibility is not less restrictive if (noteVisibilities.indexOf(renote.visibility) > noteVisibilities.indexOf(ps.visibility)) { @@ -185,7 +184,7 @@ export default define(meta, paramDef, async (ps, user) => { throw e; }); - if (isPureRenote(reply)) throw new ApiError('PURE_RENOTE', 'Cannot reply to a pure renote.'); + if (entities.isPureRenote(reply)) throw new ApiError('PURE_RENOTE', 'Cannot reply to a pure renote.'); // check that the visibility is not less restrictive if (noteVisibilities.indexOf(reply.visibility) > noteVisibilities.indexOf(ps.visibility)) { diff --git a/packages/backend/src/services/note/delete.ts b/packages/backend/src/services/note/delete.ts index 6075f15b1..1df08d5ea 100644 --- a/packages/backend/src/services/note/delete.ts +++ b/packages/backend/src/services/note/delete.ts @@ -1,4 +1,5 @@ import { FindOptionsWhere, In, IsNull, Not } from 'typeorm'; +import * as foundkey from 'foundkey-js'; import { publishNoteStream } from '@/services/stream.js'; import renderDelete from '@/remote/activitypub/renderer/delete.js'; import renderAnnounce from '@/remote/activitypub/renderer/announce.js'; @@ -12,7 +13,6 @@ import { Notes, Users, Instances } from '@/models/index.js'; import { notesChart, perUserNotesChart, instanceChart } from '@/services/chart/index.js'; import { DeliverManager } from '@/remote/activitypub/deliver-manager.js'; import { countSameRenotes } from '@/misc/count-same-renotes.js'; -import { isPureRenote } from '@/misc/renote.js'; import { registerOrFetchInstanceDoc } from '../register-or-fetch-instance-doc.js'; import { deliverToRelays } from '../relay.js'; @@ -42,7 +42,7 @@ export default async function(user: { id: User['id']; uri: User['uri']; host: Us let renote: Note | null = null; // if deleted note is renote - if (isPureRenote(note)) { + if (foundkey.entities.isPureRenote(note)) { renote = await Notes.findOneBy({ id: note.renoteId }); } diff --git a/packages/client/src/components/note-detailed.vue b/packages/client/src/components/note-detailed.vue index e64e13375..3549ef953 100644 --- a/packages/client/src/components/note-detailed.vue +++ b/packages/client/src/components/note-detailed.vue @@ -159,12 +159,7 @@ if (noteViewInterruptors.length > 0) { }); } -const isRenote = ( - note.renote != null && - note.text == null && - note.fileIds.length === 0 && - note.poll == null -); +const isRenote = foundkey.entities.isPureRenote(note); const el = ref(); const menuButton = ref(); diff --git a/packages/client/src/components/note.vue b/packages/client/src/components/note.vue index e8758329a..b0938b3c0 100644 --- a/packages/client/src/components/note.vue +++ b/packages/client/src/components/note.vue @@ -148,13 +148,7 @@ if (noteViewInterruptors.length > 0) { }); } -const isRenote = ( - note.renote != null && - note.text == null && - note.cw == null && - note.fileIds.length === 0 && - note.poll == null -); +const isRenote = foundkey.entities.isPureRenote(note); const el = ref(); const menuButton = ref(); diff --git a/packages/client/src/scripts/get-note-menu.ts b/packages/client/src/scripts/get-note-menu.ts index 57445a49a..1afab0071 100644 --- a/packages/client/src/scripts/get-note-menu.ts +++ b/packages/client/src/scripts/get-note-menu.ts @@ -16,14 +16,9 @@ export function getNoteMenu(props: { isDeleted: Ref; currentClipPage?: Ref; }) { - const isRenote = ( - props.note.renote != null && - props.note.text == null && - props.note.fileIds.length === 0 && - props.note.poll == null - ); - - const appearNote = isRenote ? props.note.renote as foundkey.entities.Note : props.note; + const appearNote = foundkey.entities.isPureRenote(props.note) + ? props.note.renote as foundkey.entities.Note + : props.note; function del(): void { os.confirm({ diff --git a/packages/foundkey-js/src/entities.ts b/packages/foundkey-js/src/entities.ts index 3d2404b47..30dd90582 100644 --- a/packages/foundkey-js/src/entities.ts +++ b/packages/foundkey-js/src/entities.ts @@ -471,3 +471,14 @@ export type UserSorting = | '+updatedAt' | '-updatedAt'; export type OriginType = 'combined' | 'local' | 'remote'; + +export function isPureRenote(note: Note): boolean { + return note.renoteId != null + && note.text == null + && note.cw == null + && ( + note.fileIds == null + || note.fileIds.length === 0 + ) + && note.poll == null; +}