refactor: move visibility computation to foundkey-js

Changelog: Changed
This commit is contained in:
Johann150 2022-09-14 11:50:59 +02:00 committed by Francis Dinh
parent d434343a09
commit 17fa488eb9
Signed by untrusted user: norm
GPG key ID: 7123E30E441E80DE
5 changed files with 28 additions and 22 deletions

View file

@ -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<string | null>(null);
let localOnly = $ref<boolean>(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),

View file

@ -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;

View file

@ -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;

View file

@ -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';

View file

@ -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),
)
];
}