Sharkey/packages/frontend/src/scripts/use-note-capture.ts

153 lines
3.9 KiB
TypeScript
Raw Normal View History

/*
* SPDX-FileCopyrightText: syuilo and other misskey contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { onUnmounted, Ref } from 'vue';
import * as Misskey from 'misskey-js';
2023-09-19 09:37:43 +02:00
import { useStream } from '@/stream.js';
import { $i } from '@/account.js';
2023-09-22 23:44:07 +02:00
import * as os from '@/os.js';
export function useNoteCapture(props: {
rootEl: Ref<HTMLElement>;
note: Ref<Misskey.entities.Note>;
pureNote: Ref<Misskey.entities.Note>;
isDeletedRef: Ref<boolean>;
onReplyCallback: (replyNote: Misskey.entities.Note) => void | undefined;
onDeleteCallback: (id: Misskey.entities.Note['id']) => void | undefined;
}) {
const note = props.note;
2023-10-31 19:33:24 +01:00
const pureNote = props.pureNote !== undefined ? props.pureNote : props.note;
const connection = $i ? useStream() : null;
2023-09-22 23:44:07 +02:00
async function onStreamNoteUpdated(noteData): void {
const { type, id, body } = noteData;
if ((id !== note.value.id) && (id !== pureNote.value.id)) return;
switch (type) {
case 'replied': {
if (!props.onReplyCallback) break;
const replyNote = await os.api("notes/show", {
noteId: body.id,
});
await props.onReplyCallback(replyNote);
break;
}
case 'reacted': {
const reaction = body.reaction;
if (body.emoji && !(body.emoji.name in note.value.reactionEmojis)) {
note.value.reactionEmojis[body.emoji.name] = body.emoji.url;
}
// TODO: reactionsプロパティがない場合ってあったっけ なければ || {} は消せる
const currentCount = (note.value.reactions || {})[reaction] || 0;
note.value.reactions[reaction] = currentCount + 1;
if ($i && (body.userId === $i.id)) {
note.value.myReaction = reaction;
}
break;
}
case 'unreacted': {
const reaction = body.reaction;
// TODO: reactionsプロパティがない場合ってあったっけ なければ || {} は消せる
const currentCount = (note.value.reactions || {})[reaction] || 0;
note.value.reactions[reaction] = Math.max(0, currentCount - 1);
if (note.value.reactions[reaction] === 0) delete note.value.reactions[reaction];
if ($i && (body.userId === $i.id)) {
note.value.myReaction = null;
}
break;
}
case 'pollVoted': {
const choice = body.choice;
const choices = [...note.value.poll.choices];
choices[choice] = {
...choices[choice],
votes: choices[choice].votes + 1,
...($i && (body.userId === $i.id) ? {
isVoted: true,
} : {}),
};
note.value.poll.choices = choices;
break;
}
case 'deleted': {
props.isDeletedRef.value = true;
if (props.onDeleteCallback) await props.onDeleteCallback(id);
break;
}
2023-09-22 23:44:07 +02:00
2023-10-04 02:24:20 +02:00
case 'updated': {
2023-09-22 23:44:07 +02:00
const editedNote = await os.api("notes/show", {
noteId: id,
});
const keys = new Set<string>();
Object.keys(editedNote)
.concat(Object.keys(note.value))
.forEach((key) => keys.add(key));
keys.forEach((key) => {
note.value[key] = editedNote[key];
});
break;
}
}
}
function capture(withHandler = false): void {
if (connection) {
// TODO: このノートがストリーミング経由で流れてきた場合のみ sr する
connection.send(document.body.contains(props.rootEl.value) ? 'sr' : 's', { id: note.value.id });
if (pureNote.value.id !== note.value.id) connection.send('s', { id: pureNote.value.id });
if (withHandler) connection.on('noteUpdated', onStreamNoteUpdated);
}
}
function decapture(withHandler = false): void {
if (connection) {
connection.send('un', {
id: note.value.id,
});
if (pureNote.value.id !== note.value.id) {
connection.send('un', {
id: pureNote.value.id,
});
}
if (withHandler) connection.off('noteUpdated', onStreamNoteUpdated);
}
}
function onStreamConnected() {
capture(false);
}
capture(true);
if (connection) {
connection.on('_connected_', onStreamConnected);
}
onUnmounted(() => {
decapture(true);
if (connection) {
connection.off('_connected_', onStreamConnected);
}
});
}