Compare commits

..

2 commits

Author SHA1 Message Date
syuilo
f43aac7291 チャット復活 2025-03-26 08:58:05 +09:00
syuilo
6368dde285 Update package.json 2025-03-26 08:57:34 +09:00
9 changed files with 149 additions and 80 deletions

View file

@ -11,7 +11,7 @@
"dependencies": {
"@types/chalk": "2.2.0",
"@types/lokijs": "1.5.14",
"@types/node": "20.11.5",
"@types/node": "22.13.13",
"@types/promise-retry": "1.1.6",
"@types/random-seed": "0.3.5",
"@types/seedrandom": "3.0.8",
@ -30,12 +30,12 @@
"random-seed": "0.3.0",
"reconnecting-websocket": "4.4.0",
"seedrandom": "3.0.5",
"ts-patch": "3.1.2",
"ts-patch": "3.3.0",
"twemoji-parser": "14.0.0",
"typescript": "5.3.3",
"typescript-transform-paths": "3.4.6",
"typescript": "5.8.2",
"typescript-transform-paths": "3.5.5",
"uuid": "9.0.1",
"ws": "8.16.0"
"ws": "8.18.1"
},
"devDependencies": {
},

View file

@ -54,6 +54,7 @@ export default class 藍 {
private meta: loki.Collection<Meta>;
private contexts: loki.Collection<{
isChat: boolean;
noteId?: string;
userId?: string;
module: string;
@ -146,7 +147,7 @@ export default class 藍 {
if (data.text && data.text.startsWith('@' + this.account.username)) {
// Misskeyのバグで投稿が非公開扱いになる
if (data.text == null) data = await this.api('notes/show', { noteId: data.id });
this.onReceiveMessage(new Message(this, data));
this.onReceiveMessage(new Message(this, data, false));
}
});
@ -156,7 +157,7 @@ export default class 藍 {
if (data.text && data.text.startsWith('@' + this.account.username)) return;
// Misskeyのバグで投稿が非公開扱いになる
if (data.text == null) data = await this.api('notes/show', { noteId: data.id });
this.onReceiveMessage(new Message(this, data));
this.onReceiveMessage(new Message(this, data, false));
});
// Renoteされたとき
@ -171,16 +172,48 @@ export default class 藍 {
});
});
// メッセージ
mainStream.on('messagingMessage', data => {
if (data.userId == this.account.id) return; // 自分は弾く
this.onReceiveMessage(new Message(this, data));
});
// 通知
mainStream.on('notification', data => {
this.onNotification(data);
});
// チャット
mainStream.on('newChatMessage', data => {
const fromUser = data.fromUser;
if (data.fromUserId == this.account.id) return; // 自分は弾く
this.onReceiveMessage(new Message(this, data, true));
// 一定期間 chatUser / chatRoom のストリームに接続して今後のやり取りに備える
if (data.fromUserId) {
const chatStream = this.connection.connectToChannel('chatUser', {
otherId: data.fromUserId,
});
let timer;
function setTimer() {
if (timer) clearTimeout(timer);
timer = setTimeout(() => {
chatStream.dispose();
}, 1000 * 60 * 2);
}
setTimer();
chatStream.on('message', (data) => {
if (data.fromUserId == this.account.id) return; // 自分は弾く
chatStream.send('read', {
id: data.id,
});
this.onReceiveMessage(new Message(this, {
...data,
// fromUserは省略されてくるため
fromUser: fromUser,
}, true));
setTimer();
});
} else {
// TODO: room
}
});
//#endregion
// Install modules
@ -218,10 +251,14 @@ export default class 藍 {
return;
}
const isNoContext = msg.replyId == null;
const isNoContext = !msg.isChat && msg.replyId == null;
// Look up the context
const context = isNoContext ? null : this.contexts.findOne({
const context = isNoContext ? null : this.contexts.findOne(msg.isChat ? {
isChat: true,
userId: msg.userId
} : {
isChat: false,
noteId: msg.replyId
});
@ -266,6 +303,9 @@ export default class 藍 {
await sleep(1000);
}
if (msg.isChat) {
// TODO: リアクション?
} else {
// リアクションする
if (reaction) {
this.api('notes/reactions/create', {
@ -274,6 +314,7 @@ export default class 藍 {
});
}
}
}
@bindThis
private onNotification(notification: any) {
@ -366,13 +407,12 @@ export default class 藍 {
}
/**
*
*
*/
@bindThis
public sendMessage(userId: any, param: any) {
return this.post(Object.assign({
visibility: 'specified',
visibleUserIds: [userId],
return this.api('chat/messages/create-to-user', Object.assign({
toUserId: userId,
}, param));
}
@ -393,12 +433,20 @@ export default class 藍 {
*
* @param module
* @param key
* @param id ID稿ID
* @param isChat
* @param id ID稿ID
* @param data
*/
@bindThis
public subscribeReply(module: Module, key: string | null, id: string, data?: any) {
this.contexts.insertOne({
public subscribeReply(module: Module, key: string | null, isChat: boolean, id: string, data?: any) {
this.contexts.insertOne(isChat ? {
isChat: true,
userId: id,
module: module.name,
key: key,
data: data
} : {
isChat: false,
noteId: id,
module: module.name,
key: key,

View file

@ -11,30 +11,36 @@ import { sleep } from '@/utils/sleep.js';
export default class Message {
private ai: ;
private note: any;
private chatMessage: { id: string; fromUser: any; fromUserId: string; text: string; } | null;
private note: { id: string; user: any; userId: string; text: string; renoteId: string; replyId: string; } | null;
public isChat: boolean;
public get id(): string {
return this.note.id;
return this.chatMessage ? this.chatMessage.id : this.note.id;
}
public get user(): User {
return this.note.user;
return this.chatMessage ? this.chatMessage.fromUser : this.note.user;
}
public get userId(): string {
return this.note.userId;
return this.chatMessage ? this.chatMessage.fromUserId : this.note.userId;
}
public get text(): string {
return this.note.text;
return this.chatMessage ? this.chatMessage.text : this.note.text;
}
public get quoteId(): string | null {
return this.note.renoteId;
return this.chatMessage ? null : this.note.renoteId;
}
public get visibility(): string {
return this.note.visibility;
public get replyId(): string | null {
return this.chatMessage ? null : this.note.replyId;
}
public get visibility(): string | null {
return this.chatMessage ? null : this.note.visibility;
}
/**
@ -48,15 +54,13 @@ export default class Message {
.trim();
}
public get replyId(): string {
return this.note.replyId;
}
public friend: Friend;
constructor(ai: , note: any) {
constructor(ai: , chatMessageOrNote: any, isChat: boolean) {
this.ai = ai;
this.note = note;
this.chatMessage = isChat ? chatMessageOrNote : null;
this.note = isChat ? null : chatMessageOrNote;
this.isChat = isChat;
this.friend = new Friend(ai, { user: this.user });
@ -83,19 +87,19 @@ export default class Message {
await sleep(2000);
}
const postData = {
if (this.chatMessage) {
return await this.ai.sendMessage(this.chatMessage.fromUserId, {
text: text,
fileId: opts?.file?.id
});
} else {
return await this.ai.post({
replyId: this.note.id,
text: text,
fileIds: opts?.file ? [opts?.file.id] : undefined,
cw: opts?.cw,
renoteId: opts?.renote
};
// DM以外は普通に返信し、DMの場合はDMで返信する
if (this.note.visibility != 'specified') {
return await this.ai.post(postData);
} else {
return await this.ai.sendMessage(this.userId, postData);
});
}
}

View file

@ -32,12 +32,13 @@ export default abstract class Module {
/**
*
* @param key
* @param id ID稿ID
* @param isChat
* @param id ID稿ID
* @param data
*/
@bindThis
protected subscribeReply(key: string | null, id: string, data?: any) {
this.ai.subscribeReply(this, key, id, data);
protected subscribeReply(key: string | null, isChat: boolean, id: string, data?: any) {
this.ai.subscribeReply(this, key, isChat, id, data);
}
/**

View file

@ -85,7 +85,7 @@ export default class extends Module {
msg.reply(serifs.core.setNameOk(name));
} else {
msg.reply(serifs.core.san).then(reply => {
this.subscribeReply(msg.userId, reply.id, {
this.subscribeReply(msg.userId, msg.isChat, msg.isChat ? msg.userId : reply.id, {
name: name
});
});
@ -143,7 +143,7 @@ export default class extends Module {
done();
} else {
msg.reply(serifs.core.yesOrNo).then(reply => {
this.subscribeReply(msg.userId, reply.id, data);
this.subscribeReply(msg.userId, msg.isChat, reply.id, data);
});
}
}

View file

@ -49,7 +49,7 @@ export default class extends Module {
});
msg.reply(serifs.guessingGame.started).then(reply => {
this.subscribeReply(msg.userId, reply.id);
this.subscribeReply(msg.userId, msg.isChat, msg.isChat ? msg.userId : reply.id);
});
return true;
@ -83,7 +83,7 @@ export default class extends Module {
if (guess == null) {
msg.reply(serifs.guessingGame.nan).then(reply => {
this.subscribeReply(msg.userId, reply.id);
this.subscribeReply(msg.userId, msg.isChat, reply.id);
});
return;
}
@ -121,7 +121,7 @@ export default class extends Module {
msg.reply(text).then(reply => {
if (!end) {
this.subscribeReply(msg.userId, reply.id);
this.subscribeReply(msg.userId, msg.isChat, reply.id);
}
});
}

View file

@ -75,7 +75,7 @@ export default class extends Module {
postId: post.id
});
this.subscribeReply(null, post.id);
this.subscribeReply(null, false, post.id);
this.log('New kazutori game started');

View file

@ -14,6 +14,7 @@ export default class extends Module {
private reminds: loki.Collection<{
userId: string;
id: string;
isChat: boolean;
thing: string | null;
quoteId: string | null;
times: number; // 催促した回数(使うのか?)
@ -69,6 +70,7 @@ export default class extends Module {
const remind = this.reminds.insertOne({
id: msg.id,
userId: msg.userId,
isChat: msg.isChat,
thing: thing === '' ? null : thing,
quoteId: msg.quoteId,
times: 0,
@ -76,13 +78,13 @@ export default class extends Module {
});
// メンションをsubscribe
this.subscribeReply(remind!.id, msg.id, {
this.subscribeReply(remind!.id, msg.isChat, msg.isChat ? msg.userId : msg.id, {
id: remind!.id
});
if (msg.quoteId) {
// 引用元をsubscribe
this.subscribeReply(remind!.id, msg.quoteId, {
this.subscribeReply(remind!.id, false, msg.quoteId, {
id: remind!.id
});
}
@ -124,6 +126,7 @@ export default class extends Module {
msg.reply(serifs.reminder.doneFromInvalidUser);
return;
} else {
if (msg.isChat) this.unsubscribeReply(key);
return false;
}
}
@ -142,6 +145,11 @@ export default class extends Module {
if (friend == null) return; // 処理の流れ上、実際にnullになることは無さそうだけど一応
let reply;
if (remind.isChat) {
this.ai.sendMessage(friend.userId, {
text: serifs.reminder.notifyWithThing(remind.thing, friend.name)
});
} else {
try {
reply = await this.ai.post({
renoteId: remind.thing == null && remind.quoteId ? remind.quoteId : remind.id,
@ -156,8 +164,9 @@ export default class extends Module {
}
return;
}
}
this.subscribeReply(remind.id, reply.id, {
this.subscribeReply(remind.id, remind.isChat, remind.isChat ? remind.userId : reply.id, {
id: remind.id
});

View file

@ -47,6 +47,7 @@ export default class extends Module {
// タイマーセット
this.setTimeoutWithPersistence(time, {
isChat: msg.isChat,
msgId: msg.id,
userId: msg.friend.userId,
time: str
@ -60,9 +61,15 @@ export default class extends Module {
const friend = this.ai.lookupFriend(data.userId);
if (friend == null) return; // 処理の流れ上、実際にnullになることは無さそうだけど一応
const text = serifs.timer.notify(data.time, friend.name);
if (data.isChat) {
this.ai.sendMessage(friend.userId, {
text: text
});
} else {
this.ai.post({
replyId: data.msgId,
text: text
});
}
}
}