2020-10-17 04:12:00 -07:00
|
|
|
|
import { Component, defineAsyncComponent, markRaw, reactive, Ref, ref } from 'vue';
|
|
|
|
|
import { EventEmitter } from 'eventemitter3';
|
|
|
|
|
import Stream from '@/scripts/stream';
|
|
|
|
|
import { store } from '@/store';
|
2020-11-01 05:09:16 -08:00
|
|
|
|
import { apiUrl, debug } from '@/config';
|
2020-10-17 04:12:00 -07:00
|
|
|
|
import MkPostFormDialog from '@/components/post-form-dialog.vue';
|
2020-10-19 03:29:04 -07:00
|
|
|
|
import MkWaitingDialog from '@/components/waiting-dialog.vue';
|
2020-10-24 09:21:41 -07:00
|
|
|
|
import { resolve } from '@/router';
|
2020-10-17 04:12:00 -07:00
|
|
|
|
|
|
|
|
|
const ua = navigator.userAgent.toLowerCase();
|
|
|
|
|
export const isMobile = /mobile|iphone|ipad|android/.test(ua);
|
|
|
|
|
|
2020-10-27 02:11:41 -07:00
|
|
|
|
export const stream = markRaw(new Stream());
|
2020-10-17 04:12:00 -07:00
|
|
|
|
|
|
|
|
|
export const pendingApiRequestsCount = ref(0);
|
2020-11-01 05:43:19 -08:00
|
|
|
|
let apiRequestsCount = 0; // for debug
|
2020-11-01 05:09:16 -08:00
|
|
|
|
export const apiRequests = ref([]); // for debug
|
2020-10-17 04:12:00 -07:00
|
|
|
|
|
|
|
|
|
export const windows = new Map();
|
|
|
|
|
|
|
|
|
|
export function api(endpoint: string, data: Record<string, any> = {}, token?: string | null | undefined) {
|
|
|
|
|
pendingApiRequestsCount.value++;
|
|
|
|
|
|
|
|
|
|
const onFinally = () => {
|
|
|
|
|
pendingApiRequestsCount.value--;
|
|
|
|
|
};
|
|
|
|
|
|
2020-11-01 05:09:16 -08:00
|
|
|
|
const log = debug ? reactive({
|
2020-11-01 05:43:19 -08:00
|
|
|
|
id: ++apiRequestsCount,
|
2020-11-01 05:09:16 -08:00
|
|
|
|
endpoint,
|
2020-11-01 05:43:19 -08:00
|
|
|
|
req: markRaw(data),
|
|
|
|
|
res: null,
|
|
|
|
|
state: 'pending',
|
2020-11-01 05:09:16 -08:00
|
|
|
|
}) : null;
|
|
|
|
|
if (debug) {
|
|
|
|
|
apiRequests.value.push(log);
|
2020-11-01 05:43:19 -08:00
|
|
|
|
if (apiRequests.value.length > 128) apiRequests.value.shift();
|
2020-11-01 05:09:16 -08:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-17 04:12:00 -07:00
|
|
|
|
const promise = new Promise((resolve, reject) => {
|
|
|
|
|
// Append a credential
|
|
|
|
|
if (store.getters.isSignedIn) (data as any).i = store.state.i.token;
|
|
|
|
|
if (token !== undefined) (data as any).i = token;
|
|
|
|
|
|
|
|
|
|
// Send request
|
|
|
|
|
fetch(endpoint.indexOf('://') > -1 ? endpoint : `${apiUrl}/${endpoint}`, {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
body: JSON.stringify(data),
|
|
|
|
|
credentials: 'omit',
|
|
|
|
|
cache: 'no-cache'
|
|
|
|
|
}).then(async (res) => {
|
|
|
|
|
const body = res.status === 204 ? null : await res.json();
|
|
|
|
|
|
|
|
|
|
if (res.status === 200) {
|
|
|
|
|
resolve(body);
|
2020-11-01 05:09:16 -08:00
|
|
|
|
if (debug) {
|
2020-11-01 05:43:19 -08:00
|
|
|
|
log.res = markRaw(body);
|
2020-11-01 05:09:16 -08:00
|
|
|
|
log.state = 'success';
|
|
|
|
|
}
|
2020-10-17 04:12:00 -07:00
|
|
|
|
} else if (res.status === 204) {
|
|
|
|
|
resolve();
|
2020-11-01 05:09:16 -08:00
|
|
|
|
if (debug) {
|
|
|
|
|
log.state = 'success';
|
|
|
|
|
}
|
2020-10-17 04:12:00 -07:00
|
|
|
|
} else {
|
|
|
|
|
reject(body.error);
|
2020-11-01 05:09:16 -08:00
|
|
|
|
if (debug) {
|
2020-11-01 05:43:19 -08:00
|
|
|
|
log.res = markRaw(body.error);
|
2020-11-01 05:09:16 -08:00
|
|
|
|
log.state = 'failed';
|
|
|
|
|
}
|
2020-10-17 04:12:00 -07:00
|
|
|
|
}
|
|
|
|
|
}).catch(reject);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
promise.then(onFinally, onFinally);
|
|
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-17 18:11:34 -07:00
|
|
|
|
export function apiWithDialog(
|
|
|
|
|
endpoint: string,
|
|
|
|
|
data: Record<string, any> = {},
|
|
|
|
|
token?: string | null | undefined,
|
|
|
|
|
onSuccess?: (res: any) => void,
|
|
|
|
|
onFailure?: (e: Error) => void,
|
|
|
|
|
) {
|
|
|
|
|
const promise = api(endpoint, data, token);
|
2020-10-17 18:21:02 -07:00
|
|
|
|
promiseDialog(promise, onSuccess, onFailure ? onFailure : (e) => {
|
|
|
|
|
dialog({
|
|
|
|
|
type: 'error',
|
2020-10-28 06:47:57 -07:00
|
|
|
|
text: e.message + '\n' + (e as any).id,
|
2020-10-17 18:21:02 -07:00
|
|
|
|
});
|
|
|
|
|
});
|
2020-10-17 18:11:34 -07:00
|
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function promiseDialog<T extends Promise<any>>(
|
|
|
|
|
promise: T,
|
|
|
|
|
onSuccess?: (res: any) => void,
|
|
|
|
|
onFailure?: (e: Error) => void,
|
|
|
|
|
text?: string,
|
|
|
|
|
): T {
|
2020-10-17 04:12:00 -07:00
|
|
|
|
const showing = ref(true);
|
2020-10-17 18:11:34 -07:00
|
|
|
|
const success = ref(false);
|
2020-10-17 04:12:00 -07:00
|
|
|
|
|
|
|
|
|
promise.then(res => {
|
|
|
|
|
if (onSuccess) {
|
|
|
|
|
showing.value = false;
|
|
|
|
|
onSuccess(res);
|
|
|
|
|
} else {
|
2020-10-17 18:11:34 -07:00
|
|
|
|
success.value = true;
|
2020-10-17 04:12:00 -07:00
|
|
|
|
setTimeout(() => {
|
|
|
|
|
showing.value = false;
|
|
|
|
|
}, 1000);
|
|
|
|
|
}
|
|
|
|
|
}).catch(e => {
|
|
|
|
|
showing.value = false;
|
|
|
|
|
if (onFailure) {
|
|
|
|
|
onFailure(e);
|
|
|
|
|
} else {
|
|
|
|
|
dialog({
|
|
|
|
|
type: 'error',
|
|
|
|
|
text: e
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2020-10-19 03:29:04 -07:00
|
|
|
|
// NOTE: dynamic importすると挙動がおかしくなる(showingの変更が伝播しない)
|
|
|
|
|
popup(MkWaitingDialog, {
|
2020-10-17 18:11:34 -07:00
|
|
|
|
success: success,
|
|
|
|
|
showing: showing,
|
|
|
|
|
text: text,
|
2020-10-17 04:12:00 -07:00
|
|
|
|
}, {}, 'closed');
|
|
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isModule(x: any): x is typeof import('*.vue') {
|
|
|
|
|
return x.default != null;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-31 21:38:48 -07:00
|
|
|
|
let popupIdCount = 0;
|
2020-10-17 04:12:00 -07:00
|
|
|
|
export const popups = ref([]) as Ref<{
|
|
|
|
|
id: any;
|
|
|
|
|
component: any;
|
|
|
|
|
props: Record<string, any>;
|
|
|
|
|
}[]>;
|
|
|
|
|
|
|
|
|
|
export function popup(component: Component | typeof import('*.vue'), props: Record<string, any>, events = {}, disposeEvent?: string) {
|
|
|
|
|
if (isModule(component)) component = component.default;
|
|
|
|
|
markRaw(component);
|
|
|
|
|
|
2020-10-31 21:38:48 -07:00
|
|
|
|
const id = ++popupIdCount;
|
2020-10-17 04:12:00 -07:00
|
|
|
|
const dispose = () => {
|
|
|
|
|
if (_DEV_) console.log('os:popup close', id, component, props, events);
|
|
|
|
|
// このsetTimeoutが無いと挙動がおかしくなる(autocompleteが閉じなくなる)。Vueのバグ?
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
popups.value = popups.value.filter(popup => popup.id !== id);
|
|
|
|
|
}, 0);
|
|
|
|
|
};
|
|
|
|
|
const state = {
|
|
|
|
|
component,
|
|
|
|
|
props,
|
|
|
|
|
events: disposeEvent ? {
|
|
|
|
|
...events,
|
|
|
|
|
[disposeEvent]: dispose
|
|
|
|
|
} : events,
|
|
|
|
|
id,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (_DEV_) console.log('os:popup open', id, component, props, events);
|
|
|
|
|
popups.value.push(state);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
dispose,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-02 17:06:19 -08:00
|
|
|
|
export function pageWindow(path: string) {
|
|
|
|
|
const { component, props } = resolve(path);
|
2020-10-17 04:12:00 -07:00
|
|
|
|
popup(defineAsyncComponent(() => import('@/components/page-window.vue')), {
|
2020-11-02 17:06:19 -08:00
|
|
|
|
initialPath: path,
|
2020-10-17 04:12:00 -07:00
|
|
|
|
initialComponent: markRaw(component),
|
|
|
|
|
initialProps: props,
|
|
|
|
|
}, {}, 'closed');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function dialog(props: Record<string, any>) {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
popup(defineAsyncComponent(() => import('@/components/dialog.vue')), props, {
|
|
|
|
|
done: result => {
|
|
|
|
|
resolve(result ? result : { canceled: true });
|
|
|
|
|
},
|
|
|
|
|
}, 'closed');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function success() {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
const showing = ref(true);
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
showing.value = false;
|
|
|
|
|
}, 1000);
|
2020-10-17 18:11:34 -07:00
|
|
|
|
popup(defineAsyncComponent(() => import('@/components/waiting-dialog.vue')), {
|
|
|
|
|
success: true,
|
2020-10-17 04:12:00 -07:00
|
|
|
|
showing: showing
|
|
|
|
|
}, {
|
|
|
|
|
done: () => resolve(),
|
|
|
|
|
}, 'closed');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function waiting() {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
const showing = ref(true);
|
2020-10-17 18:11:34 -07:00
|
|
|
|
popup(defineAsyncComponent(() => import('@/components/waiting-dialog.vue')), {
|
|
|
|
|
success: false,
|
2020-10-17 04:12:00 -07:00
|
|
|
|
showing: showing
|
|
|
|
|
}, {
|
|
|
|
|
done: () => resolve(),
|
|
|
|
|
}, 'closed');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function form(title, form) {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
popup(defineAsyncComponent(() => import('@/components/form-dialog.vue')), { title, form }, {
|
|
|
|
|
done: result => {
|
|
|
|
|
resolve(result);
|
|
|
|
|
},
|
|
|
|
|
}, 'closed');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function selectUser() {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
popup(defineAsyncComponent(() => import('@/components/user-select-dialog.vue')), {}, {
|
|
|
|
|
ok: user => {
|
|
|
|
|
resolve(user);
|
|
|
|
|
},
|
|
|
|
|
}, 'closed');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function selectDriveFile(multiple: boolean) {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
popup(defineAsyncComponent(() => import('@/components/drive-window.vue')), {
|
|
|
|
|
type: 'file',
|
|
|
|
|
multiple
|
|
|
|
|
}, {
|
|
|
|
|
done: files => {
|
|
|
|
|
if (files) {
|
|
|
|
|
resolve(multiple ? files : files[0]);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
}, 'closed');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function selectDriveFolder(multiple: boolean) {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
popup(defineAsyncComponent(() => import('@/components/drive-window.vue')), {
|
|
|
|
|
type: 'folder',
|
|
|
|
|
multiple
|
|
|
|
|
}, {
|
|
|
|
|
done: folders => {
|
|
|
|
|
if (folders) {
|
|
|
|
|
resolve(multiple ? folders : folders[0]);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
}, 'closed');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function pickEmoji(src?: HTMLElement) {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
popup(defineAsyncComponent(() => import('@/components/emoji-picker.vue')), {
|
|
|
|
|
src
|
|
|
|
|
}, {
|
|
|
|
|
done: emoji => {
|
|
|
|
|
resolve(emoji);
|
|
|
|
|
},
|
|
|
|
|
}, 'closed');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function modalMenu(items: any[], src?: HTMLElement, options?: { align?: string; viaKeyboard?: boolean }) {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
const { dispose } = popup(defineAsyncComponent(() => import('@/components/ui/modal-menu.vue')), {
|
|
|
|
|
items,
|
|
|
|
|
src,
|
|
|
|
|
align: options?.align,
|
|
|
|
|
viaKeyboard: options?.viaKeyboard
|
|
|
|
|
}, {
|
|
|
|
|
closed: () => {
|
|
|
|
|
resolve();
|
|
|
|
|
dispose();
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function contextMenu(items: any[], ev: MouseEvent) {
|
|
|
|
|
ev.preventDefault();
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
const { dispose } = popup(defineAsyncComponent(() => import('@/components/ui/context-menu.vue')), {
|
|
|
|
|
items,
|
|
|
|
|
ev,
|
|
|
|
|
}, {
|
|
|
|
|
closed: () => {
|
|
|
|
|
resolve();
|
|
|
|
|
dispose();
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function post(props: Record<string, any>) {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
// NOTE: MkPostFormDialogをdynamic importするとiOSでテキストエリアに自動フォーカスできない
|
2020-10-18 22:46:32 -07:00
|
|
|
|
// NOTE: ただ、dynamic importしない場合、MkPostFormDialogインスタンスが使いまわされ、
|
|
|
|
|
// Vueが渡されたコンポーネントに内部的に__propsというプロパティを生やす影響で、
|
|
|
|
|
// 複数のpost formを開いたときに場合によってはエラーになる
|
|
|
|
|
// もちろん複数のpost formを開けること自体Misskeyサイドのバグなのだが
|
2020-10-17 04:12:00 -07:00
|
|
|
|
const { dispose } = popup(MkPostFormDialog, props, {
|
|
|
|
|
closed: () => {
|
|
|
|
|
resolve();
|
|
|
|
|
dispose();
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function sound(type: string) {
|
|
|
|
|
if (store.state.device.sfxVolume === 0) return;
|
|
|
|
|
const sound = store.state.device['sfx' + type.substr(0, 1).toUpperCase() + type.substr(1)];
|
|
|
|
|
if (sound == null) return;
|
|
|
|
|
const audio = new Audio(`/assets/sounds/${sound}.mp3`);
|
|
|
|
|
audio.volume = store.state.device.sfxVolume;
|
|
|
|
|
audio.play();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const deckGlobalEvents = new EventEmitter();
|
|
|
|
|
|
|
|
|
|
export const uploads = ref([]);
|
|
|
|
|
|
|
|
|
|
export function upload(file: File, folder?: any, name?: string) {
|
|
|
|
|
if (folder && typeof folder == 'object') folder = folder.id;
|
|
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
const id = Math.random();
|
|
|
|
|
|
|
|
|
|
const reader = new FileReader();
|
|
|
|
|
reader.onload = (e) => {
|
|
|
|
|
const ctx = reactive({
|
|
|
|
|
id: id,
|
|
|
|
|
name: name || file.name || 'untitled',
|
|
|
|
|
progressMax: undefined,
|
|
|
|
|
progressValue: undefined,
|
|
|
|
|
img: window.URL.createObjectURL(file)
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
uploads.value.push(ctx);
|
|
|
|
|
|
|
|
|
|
const data = new FormData();
|
|
|
|
|
data.append('i', store.state.i.token);
|
|
|
|
|
data.append('force', 'true');
|
|
|
|
|
data.append('file', file);
|
|
|
|
|
|
|
|
|
|
if (folder) data.append('folderId', folder);
|
|
|
|
|
if (name) data.append('name', name);
|
|
|
|
|
|
|
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
|
|
xhr.open('POST', apiUrl + '/drive/files/create', true);
|
|
|
|
|
xhr.onload = (e: any) => {
|
|
|
|
|
const driveFile = JSON.parse(e.target.response);
|
|
|
|
|
|
|
|
|
|
resolve(driveFile);
|
|
|
|
|
|
|
|
|
|
uploads.value = uploads.value.filter(x => x.id != id);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
xhr.upload.onprogress = e => {
|
|
|
|
|
if (e.lengthComputable) {
|
|
|
|
|
ctx.progressMax = e.total;
|
|
|
|
|
ctx.progressValue = e.loaded;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
xhr.send(data);
|
|
|
|
|
};
|
|
|
|
|
reader.readAsArrayBuffer(file);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
export function checkExistence(fileData: ArrayBuffer): Promise<any> {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
const data = new FormData();
|
|
|
|
|
data.append('md5', getMD5(fileData));
|
|
|
|
|
|
|
|
|
|
os.api('drive/files/find-by-hash', {
|
|
|
|
|
md5: getMD5(fileData)
|
|
|
|
|
}).then(resp => {
|
|
|
|
|
resolve(resp.length > 0 ? resp[0] : null);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}*/
|