2022-01-14 01:25:51 +00:00
|
|
|
|
import { onUnmounted, Ref } from 'vue';
|
|
|
|
|
import * as misskey from 'misskey-js';
|
|
|
|
|
import { stream } from '@/stream';
|
|
|
|
|
import { $i } from '@/account';
|
|
|
|
|
|
|
|
|
|
export function useNoteCapture(props: {
|
|
|
|
|
rootEl: Ref<HTMLElement>;
|
|
|
|
|
appearNote: Ref<misskey.entities.Note>;
|
|
|
|
|
}) {
|
|
|
|
|
const appearNote = props.appearNote;
|
|
|
|
|
const connection = $i ? stream : null;
|
|
|
|
|
|
|
|
|
|
function onStreamNoteUpdated(data): void {
|
|
|
|
|
const { type, id, body } = data;
|
|
|
|
|
|
|
|
|
|
if (id !== appearNote.value.id) return;
|
|
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
|
case 'reacted': {
|
|
|
|
|
const reaction = body.reaction;
|
|
|
|
|
|
|
|
|
|
if (body.emoji) {
|
|
|
|
|
const emojis = appearNote.value.emojis || [];
|
|
|
|
|
if (!emojis.includes(body.emoji)) {
|
2022-01-27 16:05:31 +00:00
|
|
|
|
appearNote.value.emojis = [...emojis, body.emoji];
|
2022-01-14 01:25:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: reactionsプロパティがない場合ってあったっけ? なければ || {} は消せる
|
|
|
|
|
const currentCount = (appearNote.value.reactions || {})[reaction] || 0;
|
|
|
|
|
|
2022-01-27 16:05:31 +00:00
|
|
|
|
appearNote.value.reactions[reaction] = currentCount + 1;
|
2022-01-14 01:25:51 +00:00
|
|
|
|
|
|
|
|
|
if ($i && (body.userId === $i.id)) {
|
2022-01-27 16:05:31 +00:00
|
|
|
|
appearNote.value.myReaction = reaction;
|
2022-01-14 01:25:51 +00:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case 'unreacted': {
|
|
|
|
|
const reaction = body.reaction;
|
|
|
|
|
|
|
|
|
|
// TODO: reactionsプロパティがない場合ってあったっけ? なければ || {} は消せる
|
|
|
|
|
const currentCount = (appearNote.value.reactions || {})[reaction] || 0;
|
|
|
|
|
|
2022-01-27 16:05:31 +00:00
|
|
|
|
appearNote.value.reactions[reaction] = Math.max(0, currentCount - 1);
|
2022-01-14 01:25:51 +00:00
|
|
|
|
|
|
|
|
|
if ($i && (body.userId === $i.id)) {
|
2022-01-27 16:05:31 +00:00
|
|
|
|
appearNote.value.myReaction = null;
|
2022-01-14 01:25:51 +00:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case 'pollVoted': {
|
|
|
|
|
const choice = body.choice;
|
|
|
|
|
|
|
|
|
|
const choices = [...appearNote.value.poll.choices];
|
|
|
|
|
choices[choice] = {
|
|
|
|
|
...choices[choice],
|
|
|
|
|
votes: choices[choice].votes + 1,
|
|
|
|
|
...($i && (body.userId === $i.id) ? {
|
|
|
|
|
isVoted: true
|
|
|
|
|
} : {})
|
|
|
|
|
};
|
|
|
|
|
|
2022-01-27 16:05:31 +00:00
|
|
|
|
appearNote.value.poll.choices = choices;
|
2022-01-14 01:25:51 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case 'deleted': {
|
2022-01-27 16:05:31 +00:00
|
|
|
|
appearNote.value.deletedAt = new Date();
|
2022-01-14 01:25:51 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function capture(withHandler = false): void {
|
|
|
|
|
if (connection) {
|
|
|
|
|
// TODO: このノートがストリーミング経由で流れてきた場合のみ sr する
|
|
|
|
|
connection.send(document.body.contains(props.rootEl.value) ? 'sr' : 's', { id: appearNote.value.id });
|
|
|
|
|
if (withHandler) connection.on('noteUpdated', onStreamNoteUpdated);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function decapture(withHandler = false): void {
|
|
|
|
|
if (connection) {
|
|
|
|
|
connection.send('un', {
|
|
|
|
|
id: appearNote.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);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|