From 17fa488eb99a8eeabc6b5ce128e6da3a647b7604 Mon Sep 17 00:00:00 2001 From: Johann150 Date: Wed, 14 Sep 2022 11:50:59 +0200 Subject: [PATCH] refactor: move visibility computation to foundkey-js Changelog: Changed --- packages/client/src/components/post-form.vue | 21 +++----------------- packages/foundkey-js/src/consts.ts | 2 -- packages/foundkey-js/src/entities.ts | 4 +++- packages/foundkey-js/src/index.ts | 6 +++++- packages/foundkey-js/src/visibility.ts | 17 ++++++++++++++++ 5 files changed, 28 insertions(+), 22 deletions(-) create mode 100644 packages/foundkey-js/src/visibility.ts diff --git a/packages/client/src/components/post-form.vue b/packages/client/src/components/post-form.vue index 09be0c192..3bae9ba57 100644 --- a/packages/client/src/components/post-form.vue +++ b/packages/client/src/components/post-form.vue @@ -97,7 +97,7 @@ const props = withDefaults(defineProps<{ mention?: foundkey.entities.User; specified?: foundkey.entities.User; initialText?: string; - initialVisibility?: typeof foundkey.noteVisibilities; + initialVisibility?: foundkey.NoteVisibility; initialFiles?: foundkey.entities.DriveFile[]; initialLocalOnly?: boolean; initialVisibleUsers?: foundkey.entities.User[]; @@ -134,7 +134,7 @@ let useCw = $ref(false); let showPreview = $ref(false); let cw = $ref(null); let localOnly = $ref(props.initialLocalOnly ?? defaultStore.state.defaultNoteLocalOnly); -let visibility = $ref(props.initialVisibility ?? defaultStore.state.defaultNoteVisibility as typeof foundkey.noteVisibilities[number]); +let visibility = $ref(props.initialVisibility ?? defaultStore.state.defaultNoteVisibility as foundkey.NoteVisibility); let visibleUsers = $ref([]); if (props.initialVisibleUsers) { props.initialVisibleUsers.forEach(pushVisibleUser); @@ -144,21 +144,6 @@ let hasNotSpecifiedMentions = $ref(false); let recentHashtags = $ref(JSON.parse(localStorage.getItem('hashtags') || '[]')); let imeText = $ref(''); -// TODO: compat with misskey-js, should likely be moved into foundkey-js -const visibilityScore = (a: string): number => { - switch (a) { - case 'specified': - return 0; - case 'followers': - return 1; - case 'home': - return 2; - case 'public': default: - return 3; - } -}; -const minVisibility = (a: string, b: string): string => visibilityScore(b) > visibilityScore(a) ? a : b; - const typing = throttle(3000, () => { if (props.channel) { stream.send('typingOnChannel', { channel: props.channel.id }); @@ -271,7 +256,7 @@ if (props.channel) { } if (props.reply) { - visibility = minVisibility(props.reply.visibility, visibility); + visibility = foundkey.minVisibility(props.reply.visibility, visibility); if (visibility === 'specified') { os.api('users/show', { userIds: props.reply.visibleUserIds.filter(uid => uid !== $i.id && uid !== props.reply.userId), diff --git a/packages/foundkey-js/src/consts.ts b/packages/foundkey-js/src/consts.ts index 261ecd33f..df73e829d 100644 --- a/packages/foundkey-js/src/consts.ts +++ b/packages/foundkey-js/src/consts.ts @@ -1,7 +1,5 @@ export const notificationTypes = ['follow', 'mention', 'reply', 'renote', 'quote', 'reaction', 'pollVote', 'pollEnded', 'receiveFollowRequest', 'followRequestAccepted', 'groupInvited', 'app'] as const; -export const noteVisibilities = ['public', 'home', 'followers', 'specified'] as const; - export const mutedNoteReasons = ['word', 'manual', 'spam', 'other'] as const; export const ffVisibility = ['public', 'followers', 'private'] as const; diff --git a/packages/foundkey-js/src/entities.ts b/packages/foundkey-js/src/entities.ts index 90cee3f4d..6746d0fed 100644 --- a/packages/foundkey-js/src/entities.ts +++ b/packages/foundkey-js/src/entities.ts @@ -1,3 +1,5 @@ +import { NoteVisibility } from './visibility.js'; + export type ID = string; export type DateString = string; @@ -138,7 +140,7 @@ export type Note = { renoteId: Note['id']; files: DriveFile[]; fileIds: DriveFile['id'][]; - visibility: 'public' | 'home' | 'followers' | 'specified'; + visibility: NoteVisibility; visibleUserIds?: User['id'][]; localOnly?: boolean; myReaction?: string; diff --git a/packages/foundkey-js/src/index.ts b/packages/foundkey-js/src/index.ts index ba4d356af..61d34310b 100644 --- a/packages/foundkey-js/src/index.ts +++ b/packages/foundkey-js/src/index.ts @@ -2,10 +2,14 @@ export { Endpoints } from './api.types.js'; export { default as Stream, Connection as ChannelConnection } from './streaming.js'; export { Channels } from './streaming.types.js'; export { Acct } from './acct.js'; +export { + noteVisibilities, + NoteVisibility, + minVisibility, +} from './visibility.js'; export { permissions, notificationTypes, - noteVisibilities, mutedNoteReasons, ffVisibility, } from './consts.js'; diff --git a/packages/foundkey-js/src/visibility.ts b/packages/foundkey-js/src/visibility.ts new file mode 100644 index 000000000..3a29fe79e --- /dev/null +++ b/packages/foundkey-js/src/visibility.ts @@ -0,0 +1,17 @@ +/** + * Possible visibilities of notes. + * Ordered most public first. + */ +export const noteVisibilities = ['public', 'home', 'followers', 'specified'] as const; + +export type NoteVisibility = typeof noteVisibilities[number]; + +export function minVisibility(a: NoteVisibility, b: NoteVisibility): NoteVisibility { + return noteVisibilities[ + // larger index means more private, so pick the largest index + Math.max( + noteVisibilities.indexOf(a), + noteVisibilities.indexOf(b), + ) + ]; +}