2023-07-27 08:31:52 +03:00
|
|
|
|
/*
|
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
*/
|
|
|
|
|
|
2022-01-14 03:25:51 +02:00
|
|
|
|
import { onUnmounted, Ref } from 'vue';
|
2023-09-04 07:33:38 +03:00
|
|
|
|
import * as Misskey from 'misskey-js';
|
2023-09-19 10:37:43 +03:00
|
|
|
|
import { useStream } from '@/stream.js';
|
|
|
|
|
import { $i } from '@/account.js';
|
2023-09-23 00:44:07 +03:00
|
|
|
|
import * as os from '@/os.js';
|
2022-01-14 03:25:51 +02:00
|
|
|
|
|
|
|
|
|
export function useNoteCapture(props: {
|
|
|
|
|
rootEl: Ref<HTMLElement>;
|
2023-09-04 07:33:38 +03:00
|
|
|
|
note: Ref<Misskey.entities.Note>;
|
2022-02-11 14:35:28 +02:00
|
|
|
|
isDeletedRef: Ref<boolean>;
|
2022-01-14 03:25:51 +02:00
|
|
|
|
}) {
|
2022-02-11 14:35:28 +02:00
|
|
|
|
const note = props.note;
|
2023-05-15 13:08:46 +03:00
|
|
|
|
const connection = $i ? useStream() : null;
|
2022-01-14 03:25:51 +02:00
|
|
|
|
|
2023-09-23 00:44:07 +03:00
|
|
|
|
async function onStreamNoteUpdated(noteData): void {
|
2022-05-07 08:19:15 +03:00
|
|
|
|
const { type, id, body } = noteData;
|
2022-01-14 03:25:51 +02:00
|
|
|
|
|
2022-02-11 14:35:28 +02:00
|
|
|
|
if (id !== note.value.id) return;
|
2022-01-14 03:25:51 +02:00
|
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
|
case 'reacted': {
|
|
|
|
|
const reaction = body.reaction;
|
|
|
|
|
|
2023-01-26 10:46:21 +02:00
|
|
|
|
if (body.emoji && !(body.emoji.name in note.value.reactionEmojis)) {
|
|
|
|
|
note.value.reactionEmojis[body.emoji.name] = body.emoji.url;
|
2022-01-14 03:25:51 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: reactionsプロパティがない場合ってあったっけ? なければ || {} は消せる
|
2022-02-11 14:35:28 +02:00
|
|
|
|
const currentCount = (note.value.reactions || {})[reaction] || 0;
|
2022-01-14 03:25:51 +02:00
|
|
|
|
|
2022-02-11 14:35:28 +02:00
|
|
|
|
note.value.reactions[reaction] = currentCount + 1;
|
2022-01-14 03:25:51 +02:00
|
|
|
|
|
|
|
|
|
if ($i && (body.userId === $i.id)) {
|
2022-02-11 14:35:28 +02:00
|
|
|
|
note.value.myReaction = reaction;
|
2022-01-14 03:25:51 +02:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case 'unreacted': {
|
|
|
|
|
const reaction = body.reaction;
|
|
|
|
|
|
|
|
|
|
// TODO: reactionsプロパティがない場合ってあったっけ? なければ || {} は消せる
|
2022-02-11 14:35:28 +02:00
|
|
|
|
const currentCount = (note.value.reactions || {})[reaction] || 0;
|
2022-01-14 03:25:51 +02:00
|
|
|
|
|
2022-02-11 14:35:28 +02:00
|
|
|
|
note.value.reactions[reaction] = Math.max(0, currentCount - 1);
|
2022-12-30 12:01:01 +02:00
|
|
|
|
if (note.value.reactions[reaction] === 0) delete note.value.reactions[reaction];
|
2022-01-14 03:25:51 +02:00
|
|
|
|
|
|
|
|
|
if ($i && (body.userId === $i.id)) {
|
2022-02-11 14:35:28 +02:00
|
|
|
|
note.value.myReaction = null;
|
2022-01-14 03:25:51 +02:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case 'pollVoted': {
|
|
|
|
|
const choice = body.choice;
|
|
|
|
|
|
2022-02-11 14:35:28 +02:00
|
|
|
|
const choices = [...note.value.poll.choices];
|
2022-01-14 03:25:51 +02:00
|
|
|
|
choices[choice] = {
|
|
|
|
|
...choices[choice],
|
|
|
|
|
votes: choices[choice].votes + 1,
|
|
|
|
|
...($i && (body.userId === $i.id) ? {
|
2022-12-22 09:01:59 +02:00
|
|
|
|
isVoted: true,
|
|
|
|
|
} : {}),
|
2022-01-14 03:25:51 +02:00
|
|
|
|
};
|
|
|
|
|
|
2022-02-11 14:35:28 +02:00
|
|
|
|
note.value.poll.choices = choices;
|
2022-01-14 03:25:51 +02:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-28 11:21:16 +03:00
|
|
|
|
case 'updated': {
|
2023-09-28 15:06:14 +03:00
|
|
|
|
note.value.updatedAt = new Date().toISOString();
|
2023-09-28 11:21:16 +03:00
|
|
|
|
note.value.cw = body.cw;
|
|
|
|
|
note.value.text = body.text;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-14 03:25:51 +02:00
|
|
|
|
case 'deleted': {
|
2022-02-11 14:35:28 +02:00
|
|
|
|
props.isDeletedRef.value = true;
|
2022-01-14 03:25:51 +02:00
|
|
|
|
break;
|
|
|
|
|
}
|
2023-09-23 00:44:07 +03:00
|
|
|
|
|
|
|
|
|
case "updated": {
|
|
|
|
|
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;
|
|
|
|
|
}
|
2022-01-14 03:25:51 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function capture(withHandler = false): void {
|
|
|
|
|
if (connection) {
|
|
|
|
|
// TODO: このノートがストリーミング経由で流れてきた場合のみ sr する
|
2022-02-11 14:35:28 +02:00
|
|
|
|
connection.send(document.body.contains(props.rootEl.value) ? 'sr' : 's', { id: note.value.id });
|
2022-01-14 03:25:51 +02:00
|
|
|
|
if (withHandler) connection.on('noteUpdated', onStreamNoteUpdated);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function decapture(withHandler = false): void {
|
|
|
|
|
if (connection) {
|
|
|
|
|
connection.send('un', {
|
2022-02-11 14:35:28 +02:00
|
|
|
|
id: note.value.id,
|
2022-01-14 03:25:51 +02:00
|
|
|
|
});
|
|
|
|
|
if (withHandler) connection.off('noteUpdated', onStreamNoteUpdated);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function onStreamConnected() {
|
|
|
|
|
capture(false);
|
|
|
|
|
}
|
2023-07-08 01:08:16 +03:00
|
|
|
|
|
2022-01-14 03:25:51 +02:00
|
|
|
|
capture(true);
|
|
|
|
|
if (connection) {
|
|
|
|
|
connection.on('_connected_', onStreamConnected);
|
|
|
|
|
}
|
2023-07-08 01:08:16 +03:00
|
|
|
|
|
2022-01-14 03:25:51 +02:00
|
|
|
|
onUnmounted(() => {
|
|
|
|
|
decapture(true);
|
|
|
|
|
if (connection) {
|
|
|
|
|
connection.off('_connected_', onStreamConnected);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|