2023-07-27 00:31:52 -05:00
|
|
|
|
/*
|
2024-02-13 09:59:27 -06:00
|
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 00:31:52 -05:00
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
*/
|
|
|
|
|
|
2024-01-30 04:53:53 -06:00
|
|
|
|
import { onUnmounted, Ref, ShallowRef } from 'vue';
|
2023-09-03 23:33:38 -05:00
|
|
|
|
import * as Misskey from 'misskey-js';
|
2024-11-09 21:30:44 -06:00
|
|
|
|
import { misskeyApi } from './misskey-api.js';
|
2023-09-19 02:37:43 -05:00
|
|
|
|
import { useStream } from '@/stream.js';
|
|
|
|
|
import { $i } from '@/account.js';
|
2024-11-09 21:30:44 -06:00
|
|
|
|
import * as os from '@/os.js';
|
2022-01-13 19:25:51 -06:00
|
|
|
|
|
|
|
|
|
export function useNoteCapture(props: {
|
2024-01-30 04:53:53 -06:00
|
|
|
|
rootEl: ShallowRef<HTMLElement | undefined>;
|
2023-09-03 23:33:38 -05:00
|
|
|
|
note: Ref<Misskey.entities.Note>;
|
2024-11-09 21:30:44 -06:00
|
|
|
|
pureNote?: Ref<Misskey.entities.Note>;
|
2022-02-11 06:35:28 -06:00
|
|
|
|
isDeletedRef: Ref<boolean>;
|
2024-11-09 21:30:44 -06:00
|
|
|
|
onReplyCallback?: (replyNote: Misskey.entities.Note) => void | Promise<void>;
|
|
|
|
|
onDeleteCallback?: (id: Misskey.entities.Note['id']) => void | Promise<void>;
|
2022-01-13 19:25:51 -06:00
|
|
|
|
}) {
|
2022-02-11 06:35:28 -06:00
|
|
|
|
const note = props.note;
|
2024-11-09 21:41:15 -06:00
|
|
|
|
const pureNote = props.pureNote !== undefined ? props.pureNote : props.note;
|
2023-05-15 05:08:46 -05:00
|
|
|
|
const connection = $i ? useStream() : null;
|
2022-01-13 19:25:51 -06:00
|
|
|
|
|
2024-11-09 21:30:44 -06:00
|
|
|
|
async function onStreamNoteUpdated(noteData): Promise<void> {
|
2022-05-07 00:19:15 -05:00
|
|
|
|
const { type, id, body } = noteData;
|
2022-01-13 19:25:51 -06:00
|
|
|
|
|
2023-10-19 05:36:18 -05:00
|
|
|
|
if ((id !== note.value.id) && (id !== pureNote.value.id)) return;
|
2022-01-13 19:25:51 -06:00
|
|
|
|
|
|
|
|
|
switch (type) {
|
2024-11-09 21:30:44 -06:00
|
|
|
|
case 'replied': {
|
|
|
|
|
if (!props.onReplyCallback) break;
|
|
|
|
|
|
|
|
|
|
// notes/show may throw if the current user can't see the note
|
|
|
|
|
try {
|
|
|
|
|
const replyNote = await misskeyApi('notes/show', {
|
|
|
|
|
noteId: body.id,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await props.onReplyCallback(replyNote);
|
|
|
|
|
} catch { /* empty */ }
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-13 19:25:51 -06:00
|
|
|
|
case 'reacted': {
|
|
|
|
|
const reaction = body.reaction;
|
|
|
|
|
|
2023-01-26 02:46:21 -06:00
|
|
|
|
if (body.emoji && !(body.emoji.name in note.value.reactionEmojis)) {
|
|
|
|
|
note.value.reactionEmojis[body.emoji.name] = body.emoji.url;
|
2022-01-13 19:25:51 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: reactionsプロパティがない場合ってあったっけ? なければ || {} は消せる
|
2022-02-11 06:35:28 -06:00
|
|
|
|
const currentCount = (note.value.reactions || {})[reaction] || 0;
|
2022-01-13 19:25:51 -06:00
|
|
|
|
|
2022-02-11 06:35:28 -06:00
|
|
|
|
note.value.reactions[reaction] = currentCount + 1;
|
2024-03-06 06:08:42 -06:00
|
|
|
|
note.value.reactionCount += 1;
|
2022-01-13 19:25:51 -06:00
|
|
|
|
|
|
|
|
|
if ($i && (body.userId === $i.id)) {
|
2022-02-11 06:35:28 -06:00
|
|
|
|
note.value.myReaction = reaction;
|
2022-01-13 19:25:51 -06:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case 'unreacted': {
|
|
|
|
|
const reaction = body.reaction;
|
|
|
|
|
|
|
|
|
|
// TODO: reactionsプロパティがない場合ってあったっけ? なければ || {} は消せる
|
2022-02-11 06:35:28 -06:00
|
|
|
|
const currentCount = (note.value.reactions || {})[reaction] || 0;
|
2022-01-13 19:25:51 -06:00
|
|
|
|
|
2022-02-11 06:35:28 -06:00
|
|
|
|
note.value.reactions[reaction] = Math.max(0, currentCount - 1);
|
2024-03-06 06:08:42 -06:00
|
|
|
|
note.value.reactionCount = Math.max(0, note.value.reactionCount - 1);
|
2022-12-30 04:01:01 -06:00
|
|
|
|
if (note.value.reactions[reaction] === 0) delete note.value.reactions[reaction];
|
2022-01-13 19:25:51 -06:00
|
|
|
|
|
|
|
|
|
if ($i && (body.userId === $i.id)) {
|
2022-02-11 06:35:28 -06:00
|
|
|
|
note.value.myReaction = null;
|
2022-01-13 19:25:51 -06:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case 'pollVoted': {
|
|
|
|
|
const choice = body.choice;
|
|
|
|
|
|
2022-02-11 06:35:28 -06:00
|
|
|
|
const choices = [...note.value.poll.choices];
|
2022-01-13 19:25:51 -06:00
|
|
|
|
choices[choice] = {
|
|
|
|
|
...choices[choice],
|
|
|
|
|
votes: choices[choice].votes + 1,
|
|
|
|
|
...($i && (body.userId === $i.id) ? {
|
2022-12-22 01:01:59 -06:00
|
|
|
|
isVoted: true,
|
|
|
|
|
} : {}),
|
2022-01-13 19:25:51 -06:00
|
|
|
|
};
|
|
|
|
|
|
2022-02-11 06:35:28 -06:00
|
|
|
|
note.value.poll.choices = choices;
|
2022-01-13 19:25:51 -06:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-24 23:44:44 -06:00
|
|
|
|
case 'updated': {
|
2024-01-26 23:43:47 -06:00
|
|
|
|
note.value.history = [
|
2024-04-01 08:47:02 -05:00
|
|
|
|
...(note.value.history || []),
|
2024-01-26 23:43:47 -06:00
|
|
|
|
{
|
|
|
|
|
createdAt: note.value.updatedAt,
|
|
|
|
|
cw: note.value.cw,
|
|
|
|
|
text: note.value.text,
|
|
|
|
|
},
|
|
|
|
|
];
|
2024-01-26 10:43:07 -06:00
|
|
|
|
note.value.updatedAt = body.updatedAt;
|
2024-01-24 23:44:44 -06:00
|
|
|
|
note.value.cw = body.cw;
|
|
|
|
|
note.value.text = body.text;
|
2024-07-30 09:12:36 -05:00
|
|
|
|
note.value.tags = body.tags;
|
|
|
|
|
note.value.emojis = body.emojis;
|
2024-07-31 06:16:05 -05:00
|
|
|
|
note.value.fileIds = body.fileIds;
|
|
|
|
|
note.value.files = body.files;
|
2024-01-24 23:44:44 -06:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-13 19:25:51 -06:00
|
|
|
|
case 'deleted': {
|
2022-02-11 06:35:28 -06:00
|
|
|
|
props.isDeletedRef.value = true;
|
2024-11-09 21:30:44 -06:00
|
|
|
|
|
|
|
|
|
if (props.onDeleteCallback) await props.onDeleteCallback(id);
|
2022-01-13 19:25:51 -06:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function capture(withHandler = false): void {
|
|
|
|
|
if (connection) {
|
|
|
|
|
// TODO: このノートがストリーミング経由で流れてきた場合のみ sr する
|
2024-01-30 04:53:53 -06:00
|
|
|
|
connection.send(document.body.contains(props.rootEl.value ?? null as Node | null) ? 'sr' : 's', { id: note.value.id });
|
2023-10-19 05:36:18 -05:00
|
|
|
|
if (pureNote.value.id !== note.value.id) connection.send('s', { id: pureNote.value.id });
|
2022-01-13 19:25:51 -06:00
|
|
|
|
if (withHandler) connection.on('noteUpdated', onStreamNoteUpdated);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function decapture(withHandler = false): void {
|
|
|
|
|
if (connection) {
|
|
|
|
|
connection.send('un', {
|
2022-02-11 06:35:28 -06:00
|
|
|
|
id: note.value.id,
|
2022-01-13 19:25:51 -06:00
|
|
|
|
});
|
2023-10-19 05:36:18 -05:00
|
|
|
|
if (pureNote.value.id !== note.value.id) {
|
|
|
|
|
connection.send('un', {
|
|
|
|
|
id: pureNote.value.id,
|
|
|
|
|
});
|
|
|
|
|
}
|
2022-01-13 19:25:51 -06:00
|
|
|
|
if (withHandler) connection.off('noteUpdated', onStreamNoteUpdated);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function onStreamConnected() {
|
|
|
|
|
capture(false);
|
|
|
|
|
}
|
2023-07-07 17:08:16 -05:00
|
|
|
|
|
2022-01-13 19:25:51 -06:00
|
|
|
|
capture(true);
|
|
|
|
|
if (connection) {
|
|
|
|
|
connection.on('_connected_', onStreamConnected);
|
|
|
|
|
}
|
2023-07-07 17:08:16 -05:00
|
|
|
|
|
2022-01-13 19:25:51 -06:00
|
|
|
|
onUnmounted(() => {
|
|
|
|
|
decapture(true);
|
|
|
|
|
if (connection) {
|
|
|
|
|
connection.off('_connected_', onStreamConnected);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|