feat(client): Plugin:register_note_post_interruptor API

This commit is contained in:
syuilo 2020-07-29 23:37:50 +09:00
parent 60d81d74e3
commit e7de5f6051
3 changed files with 26 additions and 4 deletions

View file

@ -69,6 +69,7 @@ import getAcct from '../../misc/acct/render';
import { formatTimeString } from '../../misc/format-time-string'; import { formatTimeString } from '../../misc/format-time-string';
import { selectDriveFile } from '../scripts/select-drive-file'; import { selectDriveFile } from '../scripts/select-drive-file';
import { noteVisibilities } from '../../types'; import { noteVisibilities } from '../../types';
import { utils } from '@syuilo/aiscript';
export default Vue.extend({ export default Vue.extend({
components: { components: {
@ -533,9 +534,8 @@ export default Vue.extend({
localStorage.setItem('drafts', JSON.stringify(data)); localStorage.setItem('drafts', JSON.stringify(data));
}, },
post() { async post() {
this.posting = true; let data = {
this.$root.api('notes/create', {
text: this.text == '' ? undefined : this.text, text: this.text == '' ? undefined : this.text,
fileIds: this.files.length > 0 ? this.files.map(f => f.id) : undefined, fileIds: this.files.length > 0 ? this.files.map(f => f.id) : undefined,
replyId: this.reply ? this.reply.id : undefined, replyId: this.reply ? this.reply.id : undefined,
@ -546,7 +546,17 @@ export default Vue.extend({
visibility: this.visibility, visibility: this.visibility,
visibleUserIds: this.visibility == 'specified' ? this.visibleUsers.map(u => u.id) : undefined, visibleUserIds: this.visibility == 'specified' ? this.visibleUsers.map(u => u.id) : undefined,
viaMobile: this.$root.isMobile viaMobile: this.$root.isMobile
}).then(data => { };
// plugin
if (this.$store.state.notePostInterruptors.length > 0) {
for (const interruptor of this.$store.state.notePostInterruptors) {
data = utils.valToJs(await interruptor.handler(JSON.parse(JSON.stringify(data))));
}
}
this.posting = true;
this.$root.api('notes/create', data).then(() => {
this.clear(); this.clear();
this.deleteDraft(); this.deleteDraft();
this.$emit('posted'); this.$emit('posted');

View file

@ -73,6 +73,9 @@ export function createPluginEnv(vm, opts) {
'Plugin:register_note_view_interruptor': values.FN_NATIVE(([handler]) => { 'Plugin:register_note_view_interruptor': values.FN_NATIVE(([handler]) => {
vm.$store.commit('registerNoteViewInterruptor', { pluginId: opts.plugin.id, handler }); vm.$store.commit('registerNoteViewInterruptor', { pluginId: opts.plugin.id, handler });
}), }),
'Plugin:register_note_post_interruptor': values.FN_NATIVE(([handler]) => {
vm.$store.commit('registerNotePostInterruptor', { pluginId: opts.plugin.id, handler });
}),
'Plugin:open_url': values.FN_NATIVE(([url]) => { 'Plugin:open_url': values.FN_NATIVE(([url]) => {
window.open(url.value, '_blank'); window.open(url.value, '_blank');
}), }),

View file

@ -112,6 +112,7 @@ export default () => new Vuex.Store({
userActions: [], userActions: [],
noteActions: [], noteActions: [],
noteViewInterruptors: [], noteViewInterruptors: [],
notePostInterruptors: [],
}, },
getters: { getters: {
@ -283,6 +284,14 @@ export default () => new Vuex.Store({
} }
}); });
}, },
registerNotePostInterruptor(state, { pluginId, handler }) {
state.notePostInterruptors.push({
handler: (note) => {
return state.pluginContexts.get(pluginId).execFn(handler, [utils.jsToVal(note)]);
}
});
},
}, },
actions: { actions: {