feat(chat,board): file/image attachments on chat, board threads and comments

チャット・掲示板(スレッド/コメント)へファイル/画像添付を追加。

- lib/db/migrations/002_attachments.sql: attachments テーブル + file_assets.source 列
- repositories/AttachmentRepository + services/AttachmentService: 添付紐付け(プロジェクト/アップロード者チェック)
- FileStorageService.uploadForAttachment: 通知/SSE/アクティビティを行わない添付専用アップロード(source='attachment')
- ChatService/BoardService: fileIds 受理・添付紐付け・履歴/詳細表示・削除クリーンアップ(トランザクション)
- API: POST /attachments + chat/board の fileIds 受理
- UI: AttachmentList(画像サムネイル+Lightbox/ダウンロード) + AttachmentPicker(複数アップロード・送信中は送信不可) をチャット/スレッド/コメントに統合
- 添付ファイルは既存の /api/files/[fileId]/download(権限チェック済)で配信
This commit is contained in:
Ken Yasue
2026-06-25 10:07:02 +02:00
parent c747978f3d
commit 25d800a529
31 changed files with 1640 additions and 102 deletions

View File

@ -5,8 +5,11 @@ import { UserRepository } from '@/repositories/UserRepository';
import { ProjectRepository } from '@/repositories/ProjectRepository';
import { ProjectMemberRepository } from '@/repositories/ProjectMemberRepository';
import { ChatRepository } from '@/repositories/ChatRepository';
import { FileRepository } from '@/repositories/FileRepository';
import { AttachmentRepository } from '@/repositories/AttachmentRepository';
import { NotificationRepository } from '@/repositories/NotificationRepository';
import { NotificationService } from '@/services/NotificationService';
import { AttachmentService } from '@/services/AttachmentService';
import { ChatService } from '@/services/ChatService';
import { SseHub, type SseClient } from '@/lib/sse/hub';
import { ForbiddenError, NotFoundError, ValidationError } from '@/lib/errors';
@ -56,7 +59,13 @@ describe('ChatService', () => {
members,
users,
new NotificationService(new NotificationRepository(db)),
hub
hub,
new AttachmentService(
new AttachmentRepository(db),
new FileRepository(db),
members
),
db
);
});
@ -135,4 +144,102 @@ describe('ChatService', () => {
it('throws NotFoundError for a non-existent message', () => {
expect(() => service.deleteMessage(authorId, 99999)).toThrow(NotFoundError);
});
it('sendMessage with fileIds attaches files and broadcasts them', () => {
const client = makeClient();
hub.addClient(projectId, client);
const fileRepo = new FileRepository(db);
const f1 = fileRepo.create({
projectId,
uploaderId: authorId,
filename: 'a.png',
originalName: 'a.png',
mimeType: 'image/png',
size: 1,
path: '/tmp/a.png',
source: 'attachment',
});
const f2 = fileRepo.create({
projectId,
uploaderId: authorId,
filename: 'b.png',
originalName: 'b.png',
mimeType: 'image/png',
size: 1,
path: '/tmp/b.png',
source: 'attachment',
});
const message = service.sendMessage(authorId, projectId, 'see this', [
f1.id,
f2.id,
]);
expect(message.attachments).toHaveLength(2);
expect(client.received[0]).toContain('chat.message.created');
expect(client.received[0]).toContain('"attachments"');
});
it('getHistory returns messages with their attachments', () => {
const fileRepo = new FileRepository(db);
const f1 = fileRepo.create({
projectId,
uploaderId: authorId,
filename: 'a.png',
originalName: 'a.png',
mimeType: 'image/png',
size: 1,
path: '/tmp/a.png',
source: 'attachment',
});
service.sendMessage(authorId, projectId, 'with file', [f1.id]);
service.sendMessage(authorId, projectId, 'no file');
const history = service.getHistory(authorId, projectId);
const withFile = history.items.find((m) => m.body === 'with file');
const noFile = history.items.find((m) => m.body === 'no file');
expect(withFile?.attachments).toHaveLength(1);
expect(noFile?.attachments).toEqual([]);
});
it('deleteMessage soft-deletes its attachments', () => {
const fileRepo = new FileRepository(db);
const f1 = fileRepo.create({
projectId,
uploaderId: authorId,
filename: 'a.png',
originalName: 'a.png',
mimeType: 'image/png',
size: 1,
path: '/tmp/a.png',
source: 'attachment',
});
const m = service.sendMessage(authorId, projectId, 'x', [f1.id]);
expect(m.attachments).toHaveLength(1);
service.deleteMessage(authorId, m.id);
const history = service.getHistory(authorId, projectId);
// 削除されたメッセージは履歴に無く、添付も残らない
expect(history.items.find((msg) => msg.id === m.id)).toBeUndefined();
});
it('editMessage preserves attachments in history (regression)', () => {
const fileRepo = new FileRepository(db);
const f1 = fileRepo.create({
projectId,
uploaderId: authorId,
filename: 'a.png',
originalName: 'a.png',
mimeType: 'image/png',
size: 1,
path: '/tmp/a.png',
source: 'attachment',
});
const m = service.sendMessage(authorId, projectId, 'orig', [f1.id]);
service.editMessage(authorId, m.id, 'edited');
const history = service.getHistory(authorId, projectId);
const edited = history.items.find((msg) => msg.id === m.id);
expect(edited?.body).toBe('edited');
// 編集後も添付は履歴に残る
expect(edited?.attachments).toHaveLength(1);
});
});