mirror of
https://github.com/shuaiplus/nodewarden.git
synced 2026-06-22 21:50:13 +00:00
Emit cipher update notifications for attachment changes
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import { Env, Attachment, DEFAULT_DEV_SECRET } from '../types';
|
import { Env, Attachment, Cipher, DEFAULT_DEV_SECRET } from '../types';
|
||||||
import { notifyUserVaultSync } from '../durable/notifications-hub';
|
import { notifyUserCipherUpdate, notifyUserVaultSync } from '../durable/notifications-hub';
|
||||||
import { StorageService } from '../services/storage';
|
import { StorageService } from '../services/storage';
|
||||||
import { jsonResponse, errorResponse } from '../utils/response';
|
import { jsonResponse, errorResponse } from '../utils/response';
|
||||||
import { buildDirectUploadUrl, getSafeJwtSecret, parseDirectUploadPayload } from '../utils/direct-upload';
|
import { buildDirectUploadUrl, getSafeJwtSecret, parseDirectUploadPayload } from '../utils/direct-upload';
|
||||||
@@ -31,6 +31,30 @@ function notifyVaultSyncForRequest(
|
|||||||
notifyUserVaultSync(env, userId, revisionDate, readActingDeviceIdentifier(request));
|
notifyUserVaultSync(env, userId, revisionDate, readActingDeviceIdentifier(request));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function normalizeOptionalId(value: unknown): string | null {
|
||||||
|
if (value == null) return null;
|
||||||
|
const normalized = String(value).trim();
|
||||||
|
return normalized ? normalized : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
function notifyCipherUpdateForRequest(
|
||||||
|
request: Request,
|
||||||
|
env: Env,
|
||||||
|
cipher: Cipher,
|
||||||
|
revisionDate: string
|
||||||
|
): void {
|
||||||
|
notifyUserCipherUpdate(env, {
|
||||||
|
userId: cipher.userId,
|
||||||
|
cipherId: cipher.id,
|
||||||
|
revisionDate,
|
||||||
|
organizationId: normalizeOptionalId((cipher as any).organizationId ?? null),
|
||||||
|
collectionIds: Array.isArray((cipher as any).collectionIds)
|
||||||
|
? (cipher as any).collectionIds.map((id: unknown) => String(id || '').trim()).filter(Boolean)
|
||||||
|
: null,
|
||||||
|
contextId: readActingDeviceIdentifier(request),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function contentDispositionAttachment(fileName: string | null | undefined): string {
|
function contentDispositionAttachment(fileName: string | null | undefined): string {
|
||||||
const fallback = 'attachment';
|
const fallback = 'attachment';
|
||||||
const value = String(fileName || fallback)
|
const value = String(fileName || fallback)
|
||||||
@@ -83,6 +107,7 @@ async function runWithConcurrency<T>(
|
|||||||
async function processAttachmentUpload(
|
async function processAttachmentUpload(
|
||||||
request: Request,
|
request: Request,
|
||||||
env: Env,
|
env: Env,
|
||||||
|
cipher: Cipher,
|
||||||
attachment: Attachment,
|
attachment: Attachment,
|
||||||
cipherId: string
|
cipherId: string
|
||||||
): Promise<Response> {
|
): Promise<Response> {
|
||||||
@@ -124,6 +149,7 @@ async function processAttachmentUpload(
|
|||||||
const revisionInfo = await storage.updateCipherRevisionDate(cipherId);
|
const revisionInfo = await storage.updateCipherRevisionDate(cipherId);
|
||||||
if (revisionInfo) {
|
if (revisionInfo) {
|
||||||
notifyVaultSyncForRequest(request, env, revisionInfo.userId, revisionInfo.revisionDate);
|
notifyVaultSyncForRequest(request, env, revisionInfo.userId, revisionInfo.revisionDate);
|
||||||
|
notifyCipherUpdateForRequest(request, env, cipher, revisionInfo.revisionDate);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Response(null, { status: 201 });
|
return new Response(null, { status: 201 });
|
||||||
@@ -184,6 +210,7 @@ export async function handleCreateAttachment(
|
|||||||
const revisionInfo = await storage.updateCipherRevisionDate(cipherId);
|
const revisionInfo = await storage.updateCipherRevisionDate(cipherId);
|
||||||
if (revisionInfo) {
|
if (revisionInfo) {
|
||||||
notifyVaultSyncForRequest(request, env, revisionInfo.userId, revisionInfo.revisionDate);
|
notifyVaultSyncForRequest(request, env, revisionInfo.userId, revisionInfo.revisionDate);
|
||||||
|
notifyCipherUpdateForRequest(request, env, cipher, revisionInfo.revisionDate);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get updated cipher for response
|
// Get updated cipher for response
|
||||||
@@ -227,7 +254,7 @@ export async function handleUploadAttachment(
|
|||||||
return errorResponse('Attachment not found', 404);
|
return errorResponse('Attachment not found', 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
return processAttachmentUpload(request, env, attachment, cipherId);
|
return processAttachmentUpload(request, env, cipher, attachment, cipherId);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function handlePublicUploadAttachment(
|
export async function handlePublicUploadAttachment(
|
||||||
@@ -265,7 +292,7 @@ export async function handlePublicUploadAttachment(
|
|||||||
return errorResponse('Attachment not found', 404);
|
return errorResponse('Attachment not found', 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
return processAttachmentUpload(request, env, attachment, cipherId);
|
return processAttachmentUpload(request, env, cipher, attachment, cipherId);
|
||||||
}
|
}
|
||||||
|
|
||||||
// GET /api/ciphers/{cipherId}/attachment/{attachmentId}
|
// GET /api/ciphers/{cipherId}/attachment/{attachmentId}
|
||||||
@@ -356,6 +383,7 @@ export async function handleUpdateAttachmentMetadata(
|
|||||||
const revisionInfo = await storage.updateCipherRevisionDate(cipherId);
|
const revisionInfo = await storage.updateCipherRevisionDate(cipherId);
|
||||||
if (revisionInfo) {
|
if (revisionInfo) {
|
||||||
notifyVaultSyncForRequest(request, env, revisionInfo.userId, revisionInfo.revisionDate);
|
notifyVaultSyncForRequest(request, env, revisionInfo.userId, revisionInfo.revisionDate);
|
||||||
|
notifyCipherUpdateForRequest(request, env, cipher, revisionInfo.revisionDate);
|
||||||
}
|
}
|
||||||
|
|
||||||
return jsonResponse({
|
return jsonResponse({
|
||||||
@@ -463,6 +491,7 @@ export async function handleDeleteAttachment(
|
|||||||
const revisionInfo = await storage.updateCipherRevisionDate(cipherId);
|
const revisionInfo = await storage.updateCipherRevisionDate(cipherId);
|
||||||
if (revisionInfo) {
|
if (revisionInfo) {
|
||||||
notifyVaultSyncForRequest(request, env, revisionInfo.userId, revisionInfo.revisionDate);
|
notifyVaultSyncForRequest(request, env, revisionInfo.userId, revisionInfo.revisionDate);
|
||||||
|
notifyCipherUpdateForRequest(request, env, cipher, revisionInfo.revisionDate);
|
||||||
await writeAttachmentAudit(storage, request, revisionInfo.userId, 'attachment.delete', {
|
await writeAttachmentAudit(storage, request, revisionInfo.userId, 'attachment.delete', {
|
||||||
id: attachmentId,
|
id: attachmentId,
|
||||||
cipherId,
|
cipherId,
|
||||||
|
|||||||
Reference in New Issue
Block a user