2023-01-13 13:40:33 +09:00
|
|
|
|
import { onUnmounted, Ref } from "vue";
|
|
|
|
|
import * as misskey from "calckey-js";
|
|
|
|
|
import { stream } from "@/stream";
|
|
|
|
|
import { $i } from "@/account";
|
2022-01-14 10:25:51 +09:00
|
|
|
|
|
|
|
|
|
export function useNoteCapture(props: {
|
|
|
|
|
rootEl: Ref<HTMLElement>;
|
2022-02-11 21:35:28 +09:00
|
|
|
|
note: Ref<misskey.entities.Note>;
|
|
|
|
|
isDeletedRef: Ref<boolean>;
|
2022-01-14 10:25:51 +09:00
|
|
|
|
}) {
|
2022-02-11 21:35:28 +09:00
|
|
|
|
const note = props.note;
|
2022-01-14 10:25:51 +09:00
|
|
|
|
const connection = $i ? stream : null;
|
|
|
|
|
|
2022-05-07 14:19:15 +09:00
|
|
|
|
function onStreamNoteUpdated(noteData): void {
|
|
|
|
|
const { type, id, body } = noteData;
|
2022-01-14 10:25:51 +09:00
|
|
|
|
|
2022-02-11 21:35:28 +09:00
|
|
|
|
if (id !== note.value.id) return;
|
2022-01-14 10:25:51 +09:00
|
|
|
|
|
|
|
|
|
switch (type) {
|
2023-01-13 13:40:33 +09:00
|
|
|
|
case "reacted": {
|
2022-01-14 10:25:51 +09:00
|
|
|
|
const reaction = body.reaction;
|
|
|
|
|
|
|
|
|
|
if (body.emoji) {
|
2022-02-11 21:35:28 +09:00
|
|
|
|
const emojis = note.value.emojis || [];
|
2022-01-14 10:25:51 +09:00
|
|
|
|
if (!emojis.includes(body.emoji)) {
|
2022-02-11 21:35:28 +09:00
|
|
|
|
note.value.emojis = [...emojis, body.emoji];
|
2022-01-14 10:25:51 +09:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: reactionsプロパティがない場合ってあったっけ? なければ || {} は消せる
|
2023-01-13 13:40:33 +09:00
|
|
|
|
const currentCount = note.value.reactions?.[reaction] || 0;
|
2022-01-14 10:25:51 +09:00
|
|
|
|
|
2022-02-11 21:35:28 +09:00
|
|
|
|
note.value.reactions[reaction] = currentCount + 1;
|
2022-01-14 10:25:51 +09:00
|
|
|
|
|
2023-01-13 13:40:33 +09:00
|
|
|
|
if ($i && body.userId === $i.id) {
|
2022-02-11 21:35:28 +09:00
|
|
|
|
note.value.myReaction = reaction;
|
2022-01-14 10:25:51 +09:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-13 13:40:33 +09:00
|
|
|
|
case "unreacted": {
|
2022-01-14 10:25:51 +09:00
|
|
|
|
const reaction = body.reaction;
|
|
|
|
|
|
|
|
|
|
// TODO: reactionsプロパティがない場合ってあったっけ? なければ || {} は消せる
|
2023-01-13 13:40:33 +09:00
|
|
|
|
const currentCount = note.value.reactions?.[reaction] || 0;
|
2022-01-14 10:25:51 +09:00
|
|
|
|
|
2022-02-11 21:35:28 +09:00
|
|
|
|
note.value.reactions[reaction] = Math.max(0, currentCount - 1);
|
2022-01-14 10:25:51 +09:00
|
|
|
|
|
2023-01-13 13:40:33 +09:00
|
|
|
|
if ($i && body.userId === $i.id) {
|
2022-02-11 21:35:28 +09:00
|
|
|
|
note.value.myReaction = null;
|
2022-01-14 10:25:51 +09:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-13 13:40:33 +09:00
|
|
|
|
case "pollVoted": {
|
2022-01-14 10:25:51 +09:00
|
|
|
|
const choice = body.choice;
|
|
|
|
|
|
2022-02-11 21:35:28 +09:00
|
|
|
|
const choices = [...note.value.poll.choices];
|
2022-01-14 10:25:51 +09:00
|
|
|
|
choices[choice] = {
|
|
|
|
|
...choices[choice],
|
|
|
|
|
votes: choices[choice].votes + 1,
|
2023-01-13 13:40:33 +09:00
|
|
|
|
...($i && body.userId === $i.id
|
|
|
|
|
? {
|
|
|
|
|
isVoted: true,
|
|
|
|
|
}
|
|
|
|
|
: {}),
|
2022-01-14 10:25:51 +09:00
|
|
|
|
};
|
|
|
|
|
|
2022-02-11 21:35:28 +09:00
|
|
|
|
note.value.poll.choices = choices;
|
2022-01-14 10:25:51 +09:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-13 13:40:33 +09:00
|
|
|
|
case "deleted": {
|
2022-02-11 21:35:28 +09:00
|
|
|
|
props.isDeletedRef.value = true;
|
2022-01-14 10:25:51 +09:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function capture(withHandler = false): void {
|
|
|
|
|
if (connection) {
|
|
|
|
|
// TODO: このノートがストリーミング経由で流れてきた場合のみ sr する
|
2023-01-13 13:40:33 +09:00
|
|
|
|
connection.send(document.body.contains(props.rootEl.value) ? "sr" : "s", {
|
|
|
|
|
id: note.value.id,
|
|
|
|
|
});
|
|
|
|
|
if (withHandler) connection.on("noteUpdated", onStreamNoteUpdated);
|
2022-01-14 10:25:51 +09:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function decapture(withHandler = false): void {
|
|
|
|
|
if (connection) {
|
2023-01-13 13:40:33 +09:00
|
|
|
|
connection.send("un", {
|
2022-02-11 21:35:28 +09:00
|
|
|
|
id: note.value.id,
|
2022-01-14 10:25:51 +09:00
|
|
|
|
});
|
2023-01-13 13:40:33 +09:00
|
|
|
|
if (withHandler) connection.off("noteUpdated", onStreamNoteUpdated);
|
2022-01-14 10:25:51 +09:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function onStreamConnected() {
|
|
|
|
|
capture(false);
|
|
|
|
|
}
|
2023-01-13 13:40:33 +09:00
|
|
|
|
|
2022-01-14 10:25:51 +09:00
|
|
|
|
capture(true);
|
|
|
|
|
if (connection) {
|
2023-01-13 13:40:33 +09:00
|
|
|
|
connection.on("_connected_", onStreamConnected);
|
2022-01-14 10:25:51 +09:00
|
|
|
|
}
|
2023-01-13 13:40:33 +09:00
|
|
|
|
|
2022-01-14 10:25:51 +09:00
|
|
|
|
onUnmounted(() => {
|
|
|
|
|
decapture(true);
|
|
|
|
|
if (connection) {
|
2023-01-13 13:40:33 +09:00
|
|
|
|
connection.off("_connected_", onStreamConnected);
|
2022-01-14 10:25:51 +09:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|