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:
@ -158,6 +158,7 @@ describe('Migrator', () => {
|
||||
|
||||
const expectedTables = [
|
||||
'activity_logs',
|
||||
'attachments',
|
||||
'board_comments',
|
||||
'board_threads',
|
||||
'calendar_events',
|
||||
@ -180,6 +181,7 @@ describe('Migrator', () => {
|
||||
}
|
||||
expect(migrator.getAppliedMigrations().map((m) => m.filename)).toEqual([
|
||||
'001_initial.sql',
|
||||
'002_attachments.sql',
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
@ -44,7 +44,10 @@ describe('SseHub', () => {
|
||||
|
||||
hub.broadcast(1, {
|
||||
type: 'chat.message.created',
|
||||
data: { projectId: 1, message: { id: 1 } as never },
|
||||
data: {
|
||||
projectId: 1,
|
||||
message: { id: 1, attachments: [] } as never,
|
||||
},
|
||||
});
|
||||
|
||||
expect(a1.received).toHaveLength(1);
|
||||
|
||||
152
tests/unit/repositories/AttachmentRepository.test.ts
Normal file
152
tests/unit/repositories/AttachmentRepository.test.ts
Normal file
@ -0,0 +1,152 @@
|
||||
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
||||
import { createMigratedTestDb } from '@/tests/helpers/db';
|
||||
import type { SqliteDatabase } from '@/lib/db/sqlite';
|
||||
import { UserRepository } from '@/repositories/UserRepository';
|
||||
import { ProjectRepository } from '@/repositories/ProjectRepository';
|
||||
import { ProjectMemberRepository } from '@/repositories/ProjectMemberRepository';
|
||||
import { FileRepository } from '@/repositories/FileRepository';
|
||||
import { AttachmentRepository } from '@/repositories/AttachmentRepository';
|
||||
|
||||
describe('AttachmentRepository', () => {
|
||||
let db: SqliteDatabase;
|
||||
let repo: AttachmentRepository;
|
||||
let fileRepo: FileRepository;
|
||||
let projectId: number;
|
||||
let userId: number;
|
||||
|
||||
beforeEach(() => {
|
||||
db = createMigratedTestDb();
|
||||
repo = new AttachmentRepository(db);
|
||||
fileRepo = new FileRepository(db);
|
||||
userId = new UserRepository(db).create({
|
||||
name: 'U',
|
||||
email: 'u@example.com',
|
||||
passwordHash: 'h',
|
||||
}).id;
|
||||
projectId = new ProjectRepository(db).create({
|
||||
name: 'P',
|
||||
ownerId: userId,
|
||||
}).id;
|
||||
new ProjectMemberRepository(db).add(projectId, userId, 'admin');
|
||||
});
|
||||
|
||||
afterEach(() => db.close());
|
||||
|
||||
function createFile(name: string) {
|
||||
return fileRepo.create({
|
||||
projectId,
|
||||
uploaderId: userId,
|
||||
filename: `${name}.bin`,
|
||||
originalName: name,
|
||||
mimeType: 'image/png',
|
||||
size: 10,
|
||||
path: `/tmp/${name}.bin`,
|
||||
source: 'attachment',
|
||||
});
|
||||
}
|
||||
|
||||
it('creates and finds an attachment by target', () => {
|
||||
const file = createFile('a');
|
||||
const att = repo.create({
|
||||
projectId,
|
||||
fileId: file.id,
|
||||
targetType: 'chat_message',
|
||||
targetId: 1,
|
||||
});
|
||||
expect(att.id).toBeGreaterThan(0);
|
||||
expect(att.targetType).toBe('chat_message');
|
||||
const list = repo.findByTarget('chat_message', 1);
|
||||
expect(list).toHaveLength(1);
|
||||
expect(list[0].fileId).toBe(file.id);
|
||||
});
|
||||
|
||||
it('findViewsByTargets joins file_assets and returns view fields', () => {
|
||||
const f1 = createFile('one');
|
||||
const f2 = createFile('two');
|
||||
repo.create({
|
||||
projectId,
|
||||
fileId: f1.id,
|
||||
targetType: 'board_comment',
|
||||
targetId: 10,
|
||||
});
|
||||
repo.create({
|
||||
projectId,
|
||||
fileId: f2.id,
|
||||
targetType: 'board_comment',
|
||||
targetId: 11,
|
||||
});
|
||||
const views = repo.findViewsByTargets('board_comment', [10, 11]);
|
||||
expect(views).toHaveLength(2);
|
||||
const v1 = views.find((v) => v.targetId === 10);
|
||||
expect(v1?.originalName).toBe('one');
|
||||
expect(v1?.mimeType).toBe('image/png');
|
||||
expect(v1?.fileId).toBe(f1.id);
|
||||
});
|
||||
|
||||
it('returns empty array for empty target id list', () => {
|
||||
expect(repo.findViewsByTargets('chat_message', [])).toEqual([]);
|
||||
});
|
||||
|
||||
it('isolates by target_type (same target_id, different type)', () => {
|
||||
const file = createFile('a');
|
||||
repo.create({
|
||||
projectId,
|
||||
fileId: file.id,
|
||||
targetType: 'chat_message',
|
||||
targetId: 5,
|
||||
});
|
||||
repo.create({
|
||||
projectId,
|
||||
fileId: file.id,
|
||||
targetType: 'board_thread',
|
||||
targetId: 5,
|
||||
});
|
||||
expect(repo.findByTarget('chat_message', 5)).toHaveLength(1);
|
||||
expect(repo.findByTarget('board_thread', 5)).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('excludes soft-deleted attachments and files', () => {
|
||||
const f1 = createFile('keep');
|
||||
const f2 = createFile('drop');
|
||||
repo.create({
|
||||
projectId,
|
||||
fileId: f1.id,
|
||||
targetType: 'chat_message',
|
||||
targetId: 1,
|
||||
});
|
||||
repo.create({
|
||||
projectId,
|
||||
fileId: f2.id,
|
||||
targetType: 'chat_message',
|
||||
targetId: 1,
|
||||
});
|
||||
// ファイルを論理削除するとビューから消える
|
||||
fileRepo.delete(f2.id);
|
||||
const views = repo.findViewsByTargets('chat_message', [1]);
|
||||
expect(views).toHaveLength(1);
|
||||
expect(views[0].originalName).toBe('keep');
|
||||
});
|
||||
|
||||
it('deleteByTarget soft-deletes all attachments for the target', () => {
|
||||
createFile('a');
|
||||
createFile('b');
|
||||
const f1 = createFile('one');
|
||||
const f2 = createFile('two');
|
||||
repo.create({
|
||||
projectId,
|
||||
fileId: f1.id,
|
||||
targetType: 'board_thread',
|
||||
targetId: 7,
|
||||
});
|
||||
repo.create({
|
||||
projectId,
|
||||
fileId: f2.id,
|
||||
targetType: 'board_thread',
|
||||
targetId: 7,
|
||||
});
|
||||
expect(repo.deleteByTarget('board_thread', 7)).toBe(true);
|
||||
expect(repo.findByTarget('board_thread', 7)).toHaveLength(0);
|
||||
// 既に削除済みなら false
|
||||
expect(repo.deleteByTarget('board_thread', 7)).toBe(false);
|
||||
});
|
||||
});
|
||||
@ -85,4 +85,37 @@ describe('FileRepository', () => {
|
||||
expect(repo.findFilesByProject(projectId, 1, 2).items).toHaveLength(2);
|
||||
expect(repo.findFilesByProject(projectId, 1, 2).total).toBe(5);
|
||||
});
|
||||
|
||||
it('excludes attachment-source files from the library list but findFileById still returns them', () => {
|
||||
createFile('library-file');
|
||||
repo.create({
|
||||
projectId,
|
||||
uploaderId: userId,
|
||||
filename: 'att.bin',
|
||||
originalName: 'attachment-file',
|
||||
mimeType: 'image/png',
|
||||
size: 1,
|
||||
path: '/tmp/att.bin',
|
||||
source: 'attachment',
|
||||
});
|
||||
const list = repo.findFilesByProject(projectId);
|
||||
expect(list.total).toBe(1);
|
||||
expect(list.items[0].originalName).toBe('library-file');
|
||||
// 添付ファイルも findFileById では取得可能(ダウンロード用)
|
||||
const att = repo.findFilesByProject(projectId, 1, 100).items;
|
||||
expect(att).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('defaults source to library when not specified', () => {
|
||||
const f = repo.create({
|
||||
projectId,
|
||||
uploaderId: userId,
|
||||
filename: 'd.bin',
|
||||
originalName: 'd',
|
||||
mimeType: 'text/plain',
|
||||
size: 1,
|
||||
path: '/tmp/d.bin',
|
||||
});
|
||||
expect(f.source).toBe('library');
|
||||
});
|
||||
});
|
||||
|
||||
168
tests/unit/services/AttachmentService.test.ts
Normal file
168
tests/unit/services/AttachmentService.test.ts
Normal file
@ -0,0 +1,168 @@
|
||||
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
||||
import { createMigratedTestDb } from '@/tests/helpers/db';
|
||||
import type { SqliteDatabase } from '@/lib/db/sqlite';
|
||||
import { UserRepository } from '@/repositories/UserRepository';
|
||||
import { ProjectRepository } from '@/repositories/ProjectRepository';
|
||||
import { ProjectMemberRepository } from '@/repositories/ProjectMemberRepository';
|
||||
import { FileRepository } from '@/repositories/FileRepository';
|
||||
import { AttachmentRepository } from '@/repositories/AttachmentRepository';
|
||||
import { FileStorageService } from '@/services/FileStorageService';
|
||||
import { AttachmentService } from '@/services/AttachmentService';
|
||||
import { NotificationService } from '@/services/NotificationService';
|
||||
import { NotificationRepository } from '@/repositories/NotificationRepository';
|
||||
import { ActivityLogService } from '@/services/ActivityLogService';
|
||||
import { ActivityLogRepository } from '@/repositories/ActivityLogRepository';
|
||||
import { SseHub } from '@/lib/sse/hub';
|
||||
import fs from 'node:fs';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { ForbiddenError, NotFoundError } from '@/lib/errors';
|
||||
|
||||
describe('AttachmentService', () => {
|
||||
let db: SqliteDatabase;
|
||||
let service: AttachmentService;
|
||||
let fileStorage: FileStorageService;
|
||||
let attachmentRepo: AttachmentRepository;
|
||||
let uploadsDir: string;
|
||||
let projectId: number;
|
||||
let authorId: number;
|
||||
let memberId: number;
|
||||
let outsiderId: number;
|
||||
|
||||
beforeEach(() => {
|
||||
db = createMigratedTestDb();
|
||||
uploadsDir = fs.mkdtempSync(path.join(os.tmpdir(), 'uploads-'));
|
||||
const users = new UserRepository(db);
|
||||
authorId = users.create({
|
||||
name: 'A',
|
||||
email: 'a@example.com',
|
||||
passwordHash: 'h',
|
||||
}).id;
|
||||
memberId = users.create({
|
||||
name: 'M',
|
||||
email: 'm@example.com',
|
||||
passwordHash: 'h',
|
||||
}).id;
|
||||
outsiderId = users.create({
|
||||
name: 'O',
|
||||
email: 'o@example.com',
|
||||
passwordHash: 'h',
|
||||
}).id;
|
||||
projectId = new ProjectRepository(db).create({
|
||||
name: 'P',
|
||||
ownerId: authorId,
|
||||
}).id;
|
||||
const members = new ProjectMemberRepository(db);
|
||||
members.add(projectId, authorId, 'admin');
|
||||
members.add(projectId, memberId, 'member');
|
||||
attachmentRepo = new AttachmentRepository(db);
|
||||
fileStorage = new FileStorageService(
|
||||
new FileRepository(db),
|
||||
members,
|
||||
new NotificationService(new NotificationRepository(db)),
|
||||
new ActivityLogService(new ActivityLogRepository(db)),
|
||||
new SseHub(),
|
||||
uploadsDir
|
||||
);
|
||||
service = new AttachmentService(
|
||||
attachmentRepo,
|
||||
new FileRepository(db),
|
||||
members
|
||||
);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
db.close();
|
||||
fs.rmSync(uploadsDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
function uploadAttachment(actorId: number) {
|
||||
return fileStorage.uploadForAttachment(actorId, projectId, {
|
||||
originalName: 'pic.png',
|
||||
mimeType: 'image/png',
|
||||
data: Buffer.from('img'),
|
||||
});
|
||||
}
|
||||
|
||||
it('attaches multiple files and returns their views', () => {
|
||||
const f1 = uploadAttachment(authorId);
|
||||
const f2 = uploadAttachment(authorId);
|
||||
const views = service.attach(authorId, projectId, 'chat_message', 1, [
|
||||
f1.id,
|
||||
f2.id,
|
||||
]);
|
||||
expect(views).toHaveLength(2);
|
||||
expect(views.map((v) => v.fileId).sort()).toEqual([f1.id, f2.id].sort());
|
||||
});
|
||||
|
||||
it('returns empty for no fileIds', () => {
|
||||
expect(service.attach(authorId, projectId, 'chat_message', 1, [])).toEqual(
|
||||
[]
|
||||
);
|
||||
});
|
||||
|
||||
it('deduplicates repeated fileIds', () => {
|
||||
const f1 = uploadAttachment(authorId);
|
||||
const views = service.attach(authorId, projectId, 'chat_message', 1, [
|
||||
f1.id,
|
||||
f1.id,
|
||||
]);
|
||||
expect(views).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('forbids a non-member from attaching', () => {
|
||||
const f1 = uploadAttachment(authorId);
|
||||
expect(() =>
|
||||
service.attach(outsiderId, projectId, 'chat_message', 1, [f1.id])
|
||||
).toThrow(ForbiddenError);
|
||||
});
|
||||
|
||||
it('throws NotFoundError for a missing file', () => {
|
||||
expect(() =>
|
||||
service.attach(authorId, projectId, 'chat_message', 1, [99999])
|
||||
).toThrow(NotFoundError);
|
||||
});
|
||||
|
||||
it('forbids attaching a file from another project', () => {
|
||||
const otherProject = new ProjectRepository(db).create({
|
||||
name: 'P2',
|
||||
ownerId: authorId,
|
||||
}).id;
|
||||
const members = new ProjectMemberRepository(db);
|
||||
members.add(otherProject, authorId, 'admin');
|
||||
const otherFile = fileStorage.uploadForAttachment(authorId, otherProject, {
|
||||
originalName: 'x.png',
|
||||
mimeType: 'image/png',
|
||||
data: Buffer.from('x'),
|
||||
});
|
||||
expect(() =>
|
||||
service.attach(authorId, projectId, 'chat_message', 1, [otherFile.id])
|
||||
).toThrow(ForbiddenError);
|
||||
});
|
||||
|
||||
it('forbids attaching a file uploaded by another member', () => {
|
||||
const otherMemberFile = uploadAttachment(memberId);
|
||||
expect(() =>
|
||||
service.attach(authorId, projectId, 'chat_message', 1, [
|
||||
otherMemberFile.id,
|
||||
])
|
||||
).toThrow(ForbiddenError);
|
||||
});
|
||||
|
||||
it('listViewsBatch returns views grouped by targetId', () => {
|
||||
const f1 = uploadAttachment(authorId);
|
||||
const f2 = uploadAttachment(authorId);
|
||||
service.attach(authorId, projectId, 'board_comment', 10, [f1.id]);
|
||||
service.attach(authorId, projectId, 'board_comment', 11, [f2.id]);
|
||||
const views = service.listViewsBatch('board_comment', [10, 11]);
|
||||
expect(views).toHaveLength(2);
|
||||
});
|
||||
|
||||
it('detach soft-deletes attachments for the target', () => {
|
||||
const f1 = uploadAttachment(authorId);
|
||||
service.attach(authorId, projectId, 'chat_message', 5, [f1.id]);
|
||||
expect(service.listViews('chat_message', 5)).toHaveLength(1);
|
||||
service.detach('chat_message', 5);
|
||||
expect(service.listViews('chat_message', 5)).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
@ -5,19 +5,29 @@ import { UserRepository } from '@/repositories/UserRepository';
|
||||
import { ProjectRepository } from '@/repositories/ProjectRepository';
|
||||
import { ProjectMemberRepository } from '@/repositories/ProjectMemberRepository';
|
||||
import { BoardRepository } from '@/repositories/BoardRepository';
|
||||
import { FileRepository } from '@/repositories/FileRepository';
|
||||
import { AttachmentRepository } from '@/repositories/AttachmentRepository';
|
||||
import { NotificationRepository } from '@/repositories/NotificationRepository';
|
||||
import { ActivityLogRepository } from '@/repositories/ActivityLogRepository';
|
||||
import { NotificationService } from '@/services/NotificationService';
|
||||
import { ActivityLogService } from '@/services/ActivityLogService';
|
||||
import { AttachmentService } from '@/services/AttachmentService';
|
||||
import { BoardService } from '@/services/BoardService';
|
||||
import { ForbiddenError, NotFoundError, ValidationError } from '@/lib/errors';
|
||||
|
||||
function makeService(db: SqliteDatabase) {
|
||||
const members = new ProjectMemberRepository(db);
|
||||
return new BoardService(
|
||||
new BoardRepository(db),
|
||||
new ProjectMemberRepository(db),
|
||||
members,
|
||||
new NotificationService(new NotificationRepository(db)),
|
||||
new ActivityLogService(new ActivityLogRepository(db))
|
||||
new ActivityLogService(new ActivityLogRepository(db)),
|
||||
new AttachmentService(
|
||||
new AttachmentRepository(db),
|
||||
new FileRepository(db),
|
||||
members
|
||||
),
|
||||
db
|
||||
);
|
||||
}
|
||||
|
||||
@ -167,4 +177,76 @@ describe('BoardService', () => {
|
||||
it('throws NotFoundError for a non-existent thread', () => {
|
||||
expect(() => service.getThread(memberId, 99999)).toThrow(NotFoundError);
|
||||
});
|
||||
|
||||
it('createThread with fileIds attaches files to the thread', () => {
|
||||
const fileRepo = new FileRepository(db);
|
||||
const f1 = fileRepo.create({
|
||||
projectId,
|
||||
uploaderId: memberId,
|
||||
filename: 'a.png',
|
||||
originalName: 'a.png',
|
||||
mimeType: 'image/png',
|
||||
size: 1,
|
||||
path: '/tmp/a.png',
|
||||
source: 'attachment',
|
||||
});
|
||||
const thread = service.createThread(memberId, projectId, {
|
||||
title: 'T',
|
||||
bodyMd: 'b',
|
||||
fileIds: [f1.id],
|
||||
});
|
||||
const atts = service.getAttachments(memberId, thread.id, []);
|
||||
expect(atts.thread).toHaveLength(1);
|
||||
expect(atts.thread[0].fileId).toBe(f1.id);
|
||||
});
|
||||
|
||||
it('createComment with fileIds attaches files to the comment', () => {
|
||||
const fileRepo = new FileRepository(db);
|
||||
const f1 = fileRepo.create({
|
||||
projectId,
|
||||
uploaderId: memberId,
|
||||
filename: 'c.png',
|
||||
originalName: 'c.png',
|
||||
mimeType: 'image/png',
|
||||
size: 1,
|
||||
path: '/tmp/c.png',
|
||||
source: 'attachment',
|
||||
});
|
||||
const thread = service.createThread(authorId, projectId, {
|
||||
title: 'T',
|
||||
bodyMd: 'b',
|
||||
});
|
||||
const comment = service.createComment(memberId, thread.id, 'c', [f1.id]);
|
||||
const atts = service.getAttachments(memberId, thread.id, [comment.id]);
|
||||
expect(atts.comments).toHaveLength(1);
|
||||
expect(atts.comments[0].targetId).toBe(comment.id);
|
||||
});
|
||||
|
||||
it('deleteThread and deleteComment detach attachments', () => {
|
||||
const fileRepo = new FileRepository(db);
|
||||
const f1 = fileRepo.create({
|
||||
projectId,
|
||||
uploaderId: memberId,
|
||||
filename: 'a.png',
|
||||
originalName: 'a.png',
|
||||
mimeType: 'image/png',
|
||||
size: 1,
|
||||
path: '/tmp/a.png',
|
||||
source: 'attachment',
|
||||
});
|
||||
const thread = service.createThread(memberId, projectId, {
|
||||
title: 'T',
|
||||
bodyMd: 'b',
|
||||
fileIds: [f1.id],
|
||||
});
|
||||
// 削除前は添付あり
|
||||
expect(
|
||||
new AttachmentRepository(db).findByTarget('board_thread', thread.id)
|
||||
).toHaveLength(1);
|
||||
service.deleteThread(memberId, thread.id);
|
||||
// 削除後は論理削除されて取得できない
|
||||
expect(
|
||||
new AttachmentRepository(db).findByTarget('board_thread', thread.id)
|
||||
).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
|
||||
@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
@ -157,4 +157,49 @@ describe('FileStorageService', () => {
|
||||
service.delete(authorId, file.id); // authorId is admin
|
||||
expect(() => service.getFileInfo(memberId, file.id)).toThrow();
|
||||
});
|
||||
|
||||
it('uploadForAttachment is silent: no notification/SSE/activity, source=attachment', () => {
|
||||
const file = service.uploadForAttachment(authorId, projectId, {
|
||||
originalName: 'pic.png',
|
||||
mimeType: 'image/png',
|
||||
data: Buffer.from('img'),
|
||||
});
|
||||
expect(file.source).toBe('attachment');
|
||||
expect(fs.existsSync(file.path)).toBe(true);
|
||||
// メンバーへの file_shared 通知は飛ばない
|
||||
expect(new NotificationRepository(db).countUnreadByUser(memberId)).toBe(0);
|
||||
// file_uploaded アクティビティは記録されない
|
||||
expect(
|
||||
new ActivityLogRepository(db)
|
||||
.findByProject(projectId)
|
||||
.items.some((l) => l.action === 'file_uploaded')
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it('uploadForAttachment result is excluded from the library list', () => {
|
||||
service.uploadForAttachment(authorId, projectId, {
|
||||
originalName: 'pic.png',
|
||||
mimeType: 'image/png',
|
||||
data: Buffer.from('img'),
|
||||
});
|
||||
const list = service.listFiles(authorId, projectId);
|
||||
expect(list.total).toBe(0);
|
||||
});
|
||||
|
||||
it('uploadForAttachment still enforces MIME and membership', () => {
|
||||
expect(() =>
|
||||
service.uploadForAttachment(authorId, projectId, {
|
||||
originalName: 'evil.exe',
|
||||
mimeType: 'application/x-msdownload',
|
||||
data: Buffer.from('x'),
|
||||
})
|
||||
).toThrow(ValidationError);
|
||||
expect(() =>
|
||||
service.uploadForAttachment(outsiderId, projectId, {
|
||||
originalName: 'x.png',
|
||||
mimeType: 'image/png',
|
||||
data: Buffer.from('x'),
|
||||
})
|
||||
).toThrow(ForbiddenError);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user