v3: allow users to edit dump title
All checks were successful
Build and Publish Docker Image / build-and-push (push) Successful in 2m54s
All checks were successful
Build and Publish Docker Image / build-and-push (push) Successful in 2m54s
This commit is contained in:
@@ -109,6 +109,7 @@ export async function createFileDump(
|
||||
comment: string | undefined,
|
||||
userId: string,
|
||||
isPrivate = false,
|
||||
title?: string,
|
||||
): Promise<Dump> {
|
||||
if (!isAllowedMime(file.type)) {
|
||||
throw new APIException(
|
||||
@@ -127,7 +128,8 @@ export async function createFileDump(
|
||||
|
||||
const dumpId = crypto.randomUUID();
|
||||
const createdAt = new Date();
|
||||
const slug = makeSlug(file.name, dumpId);
|
||||
const finalTitle = title?.trim() || file.name;
|
||||
const slug = makeSlug(finalTitle, dumpId);
|
||||
|
||||
await Deno.mkdir(DUMPS_DIR, { recursive: true });
|
||||
const data = new Uint8Array(await file.arrayBuffer());
|
||||
@@ -141,7 +143,7 @@ export async function createFileDump(
|
||||
).run(
|
||||
dumpId,
|
||||
"file",
|
||||
file.name,
|
||||
finalTitle,
|
||||
slug,
|
||||
comment ?? null,
|
||||
userId,
|
||||
@@ -160,7 +162,7 @@ export async function createFileDump(
|
||||
const dump: Dump = {
|
||||
id: dumpId,
|
||||
kind: "file",
|
||||
title: file.name,
|
||||
title: finalTitle,
|
||||
slug,
|
||||
comment,
|
||||
userId,
|
||||
@@ -174,10 +176,10 @@ export async function createFileDump(
|
||||
};
|
||||
if (!isPrivate) {
|
||||
broadcastNewDump(dump);
|
||||
notifyUserFollowersNewDump(userId, dumpId, file.name);
|
||||
notifyUserFollowersNewDump(userId, dumpId, finalTitle);
|
||||
}
|
||||
if (comment) {
|
||||
notifyMentions(userId, comment, "dump", dumpId, file.name);
|
||||
notifyMentions(userId, comment, "dump", dumpId, finalTitle);
|
||||
linkAttachments(comment, dumpId);
|
||||
}
|
||||
return dump;
|
||||
@@ -285,10 +287,14 @@ export async function updateDump(
|
||||
|
||||
const now = new Date();
|
||||
|
||||
// File dumps: only comment and isPrivate are editable
|
||||
// File dumps: title, comment and isPrivate are editable
|
||||
if (dump.kind === "file") {
|
||||
const title = request.title?.trim() || dump.title;
|
||||
const slug = title !== dump.title ? makeSlug(title, dumpId) : dump.slug;
|
||||
const updatedDump: Dump = {
|
||||
...dump,
|
||||
title,
|
||||
slug,
|
||||
comment: "comment" in request
|
||||
? (request.comment ?? undefined)
|
||||
: dump.comment,
|
||||
@@ -298,8 +304,10 @@ export async function updateDump(
|
||||
updatedAt: now,
|
||||
};
|
||||
db.prepare(
|
||||
`UPDATE dumps SET comment = ?, is_private = ?, updated_at = ? WHERE id = ?;`,
|
||||
`UPDATE dumps SET title = ?, slug = ?, comment = ?, is_private = ?, updated_at = ? WHERE id = ?;`,
|
||||
).run(
|
||||
updatedDump.title,
|
||||
updatedDump.slug ?? null,
|
||||
updatedDump.comment ?? null,
|
||||
updatedDump.isPrivate ? 1 : 0,
|
||||
now.toISOString(),
|
||||
@@ -334,7 +342,11 @@ export async function updateDump(
|
||||
title = richContent?.title ?? titleFromUrl(newUrl);
|
||||
}
|
||||
|
||||
const newSlug = makeSlug(title, dumpId);
|
||||
if (request.title?.trim()) {
|
||||
title = request.title.trim();
|
||||
}
|
||||
|
||||
const newSlug = title !== dump.title ? makeSlug(title, dumpId) : dump.slug;
|
||||
const updatedDump: Dump = {
|
||||
...dump,
|
||||
title,
|
||||
@@ -387,6 +399,7 @@ export async function replaceFileDump(
|
||||
dumpId: string,
|
||||
file: File,
|
||||
comment: string | undefined,
|
||||
title?: string,
|
||||
): Promise<Dump> {
|
||||
if (!isAllowedMime(file.type)) {
|
||||
throw new APIException(
|
||||
@@ -415,13 +428,16 @@ export async function replaceFileDump(
|
||||
await Deno.writeFile(filePath, data);
|
||||
|
||||
const now = new Date();
|
||||
const newSlug = makeSlug(file.name, dumpId);
|
||||
const finalTitle = title?.trim() || dump.title;
|
||||
const newSlug = finalTitle !== dump.title
|
||||
? makeSlug(finalTitle, dumpId)
|
||||
: dump.slug;
|
||||
try {
|
||||
db.prepare(
|
||||
`UPDATE dumps SET title = ?, slug = ?, file_name = ?, file_mime = ?, file_size = ?, comment = ?, updated_at = ? WHERE id = ?;`,
|
||||
).run(
|
||||
file.name,
|
||||
newSlug,
|
||||
finalTitle,
|
||||
newSlug ?? null,
|
||||
file.name,
|
||||
file.type,
|
||||
file.size,
|
||||
@@ -437,12 +453,12 @@ export async function replaceFileDump(
|
||||
}
|
||||
|
||||
if (comment) {
|
||||
notifyMentions(dump.userId, comment, "dump", dumpId, file.name);
|
||||
notifyMentions(dump.userId, comment, "dump", dumpId, finalTitle);
|
||||
linkAttachments(comment, dumpId);
|
||||
}
|
||||
return {
|
||||
...dump,
|
||||
title: file.name,
|
||||
title: finalTitle,
|
||||
slug: newSlug,
|
||||
fileName: file.name,
|
||||
fileMime: file.type,
|
||||
|
||||
Reference in New Issue
Block a user