summaryrefslogtreecommitdiff
path: root/packages/backend/src
diff options
context:
space:
mode:
authorMar0xy <marie@kaifa.ch>2023-10-22 13:16:30 +0200
committerMar0xy <marie@kaifa.ch>2023-10-22 13:16:30 +0200
commitd50e81e475b96343db230db97bbc80571cfd7690 (patch)
treec2ae18b6d883ef2b218b846d8ebc674528e22499 /packages/backend/src
parentchore: remove debug from versions menu (diff)
downloadsharkey-d50e81e475b96343db230db97bbc80571cfd7690.tar.gz
sharkey-d50e81e475b96343db230db97bbc80571cfd7690.tar.bz2
sharkey-d50e81e475b96343db230db97bbc80571cfd7690.zip
upd: improve note edit table & improve previous version view
Closes transfem-org/Sharkey#105
Diffstat (limited to 'packages/backend/src')
-rw-r--r--packages/backend/src/core/NoteEditService.ts177
-rw-r--r--packages/backend/src/models/NoteEdit.ts7
-rw-r--r--packages/backend/src/models/json-schema/note-edit.ts7
-rw-r--r--packages/backend/src/server/api/endpoints/notes/versions.ts2
4 files changed, 104 insertions, 89 deletions
diff --git a/packages/backend/src/core/NoteEditService.ts b/packages/backend/src/core/NoteEditService.ts
index 1cbf0ee7f0..7b8aab7f3e 100644
--- a/packages/backend/src/core/NoteEditService.ts
+++ b/packages/backend/src/core/NoteEditService.ts
@@ -387,104 +387,109 @@ export class NoteEditService implements OnApplicationShutdown {
update.hasPoll = !!data.poll;
}
- await this.noteEditRepository.insert({
- id: this.idService.gen(),
- noteId: oldnote.id,
- text: data.text || undefined,
- cw: data.cw,
- fileIds: undefined,
- updatedAt: new Date(),
- });
+ if (Object.keys(update).length > 0) {
+ await this.noteEditRepository.insert({
+ id: this.idService.gen(),
+ noteId: oldnote.id,
+ oldText: update.text ? oldnote.text : undefined,
+ newText: update.text || undefined,
+ cw: update.cw || undefined,
+ fileIds: undefined,
+ updatedAt: new Date(),
+ });
- const note = new MiNote({
- id: oldnote.id,
- updatedAt: data.updatedAt ? data.updatedAt : new Date(),
- fileIds: data.files ? data.files.map(file => file.id) : [],
- replyId: data.reply ? data.reply.id : null,
- renoteId: data.renote ? data.renote.id : null,
- channelId: data.channel ? data.channel.id : null,
- threadId: data.reply
- ? data.reply.threadId
+ const note = new MiNote({
+ id: oldnote.id,
+ updatedAt: data.updatedAt ? data.updatedAt : new Date(),
+ fileIds: data.files ? data.files.map(file => file.id) : [],
+ replyId: data.reply ? data.reply.id : null,
+ renoteId: data.renote ? data.renote.id : null,
+ channelId: data.channel ? data.channel.id : null,
+ threadId: data.reply
? data.reply.threadId
- : data.reply.id
- : null,
- name: data.name,
- text: data.text,
- hasPoll: data.poll != null,
- cw: data.cw ?? null,
- tags: tags.map(tag => normalizeForSearch(tag)),
- emojis,
- reactions: oldnote.reactions,
- userId: user.id,
- localOnly: data.localOnly!,
- reactionAcceptance: data.reactionAcceptance,
- visibility: data.visibility as any,
- visibleUserIds: data.visibility === 'specified'
- ? data.visibleUsers
- ? data.visibleUsers.map(u => u.id)
- : []
- : [],
+ ? data.reply.threadId
+ : data.reply.id
+ : null,
+ name: data.name,
+ text: data.text,
+ hasPoll: data.poll != null,
+ cw: data.cw ?? null,
+ tags: tags.map(tag => normalizeForSearch(tag)),
+ emojis,
+ reactions: oldnote.reactions,
+ userId: user.id,
+ localOnly: data.localOnly!,
+ reactionAcceptance: data.reactionAcceptance,
+ visibility: data.visibility as any,
+ visibleUserIds: data.visibility === 'specified'
+ ? data.visibleUsers
+ ? data.visibleUsers.map(u => u.id)
+ : []
+ : [],
- attachedFileTypes: data.files ? data.files.map(file => file.type) : [],
+ attachedFileTypes: data.files ? data.files.map(file => file.type) : [],
- // 以下非正規化データ
- replyUserId: data.reply ? data.reply.userId : null,
- replyUserHost: data.reply ? data.reply.userHost : null,
- renoteUserId: data.renote ? data.renote.userId : null,
- renoteUserHost: data.renote ? data.renote.userHost : null,
- userHost: user.host,
- });
+ // 以下非正規化データ
+ replyUserId: data.reply ? data.reply.userId : null,
+ replyUserHost: data.reply ? data.reply.userHost : null,
+ renoteUserId: data.renote ? data.renote.userId : null,
+ renoteUserHost: data.renote ? data.renote.userHost : null,
+ userHost: user.host,
+ });
- if (data.uri != null) note.uri = data.uri;
- if (data.url != null) note.url = data.url;
+ if (data.uri != null) note.uri = data.uri;
+ if (data.url != null) note.url = data.url;
- if (mentionedUsers.length > 0) {
- note.mentions = mentionedUsers.map(u => u.id);
- const profiles = await this.userProfilesRepository.findBy({ userId: In(note.mentions) });
- note.mentionedRemoteUsers = JSON.stringify(mentionedUsers.filter(u => this.userEntityService.isRemoteUser(u)).map(u => {
- const profile = profiles.find(p => p.userId === u.id);
- const url = profile != null ? profile.url : null;
- return {
- uri: u.uri,
- url: url ?? undefined,
- username: u.username,
- host: u.host,
- } as IMentionedRemoteUsers[0];
- }));
- }
+ if (mentionedUsers.length > 0) {
+ note.mentions = mentionedUsers.map(u => u.id);
+ const profiles = await this.userProfilesRepository.findBy({ userId: In(note.mentions) });
+ note.mentionedRemoteUsers = JSON.stringify(mentionedUsers.filter(u => this.userEntityService.isRemoteUser(u)).map(u => {
+ const profile = profiles.find(p => p.userId === u.id);
+ const url = profile != null ? profile.url : null;
+ return {
+ uri: u.uri,
+ url: url ?? undefined,
+ username: u.username,
+ host: u.host,
+ } as IMentionedRemoteUsers[0];
+ }));
+ }
- if (data.poll != null) {
- // Start transaction
- await this.db.transaction(async transactionalEntityManager => {
- await transactionalEntityManager.update(MiNote, oldnote.id, note);
+ if (data.poll != null) {
+ // Start transaction
+ await this.db.transaction(async transactionalEntityManager => {
+ await transactionalEntityManager.update(MiNote, oldnote.id, note);
+
+ const poll = new MiPoll({
+ noteId: note.id,
+ choices: data.poll!.choices,
+ expiresAt: data.poll!.expiresAt,
+ multiple: data.poll!.multiple,
+ votes: new Array(data.poll!.choices.length).fill(0),
+ noteVisibility: note.visibility,
+ userId: user.id,
+ userHost: user.host,
+ });
- const poll = new MiPoll({
- noteId: note.id,
- choices: data.poll!.choices,
- expiresAt: data.poll!.expiresAt,
- multiple: data.poll!.multiple,
- votes: new Array(data.poll!.choices.length).fill(0),
- noteVisibility: note.visibility,
- userId: user.id,
- userHost: user.host,
+ if (!oldnote.hasPoll) {
+ await transactionalEntityManager.insert(MiPoll, poll);
+ } else {
+ await transactionalEntityManager.update(MiPoll, oldnote.id, poll);
+ }
});
+ } else {
+ await this.notesRepository.update(oldnote.id, note);
+ }
- if (!oldnote.hasPoll) {
- await transactionalEntityManager.insert(MiPoll, poll);
- } else {
- await transactionalEntityManager.update(MiPoll, oldnote.id, poll);
- }
- });
+ setImmediate('post edited', { signal: this.#shutdownController.signal }).then(
+ () => this.postNoteEdited(note, user, data, silent, tags!, mentionedUsers!),
+ () => { /* aborted, ignore this */ },
+ );
+
+ return note;
} else {
- await this.notesRepository.update(oldnote.id, note);
+ return oldnote;
}
-
- setImmediate('post edited', { signal: this.#shutdownController.signal }).then(
- () => this.postNoteEdited(note, user, data, silent, tags!, mentionedUsers!),
- () => { /* aborted, ignore this */ },
- );
-
- return note;
}
@bindThis
diff --git a/packages/backend/src/models/NoteEdit.ts b/packages/backend/src/models/NoteEdit.ts
index 547b135e56..440f9b8208 100644
--- a/packages/backend/src/models/NoteEdit.ts
+++ b/packages/backend/src/models/NoteEdit.ts
@@ -24,7 +24,12 @@ export class NoteEdit {
@Column("text", {
nullable: true,
})
- public text: string | null;
+ public oldText: string | null;
+
+ @Column("text", {
+ nullable: true,
+ })
+ public newText: string | null;
@Column("varchar", {
length: 512,
diff --git a/packages/backend/src/models/json-schema/note-edit.ts b/packages/backend/src/models/json-schema/note-edit.ts
index e877f3f946..a58e2aa1de 100644
--- a/packages/backend/src/models/json-schema/note-edit.ts
+++ b/packages/backend/src/models/json-schema/note-edit.ts
@@ -26,7 +26,12 @@ export const packedNoteEdit = {
nullable: false,
format: "id",
},
- text: {
+ oldText: {
+ type: "string",
+ optional: true,
+ nullable: true,
+ },
+ newText: {
type: "string",
optional: true,
nullable: true,
diff --git a/packages/backend/src/server/api/endpoints/notes/versions.ts b/packages/backend/src/server/api/endpoints/notes/versions.ts
index 9733d781a4..e6831f3208 100644
--- a/packages/backend/src/server/api/endpoints/notes/versions.ts
+++ b/packages/backend/src/server/api/endpoints/notes/versions.ts
@@ -51,7 +51,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
for (const edit of edits) {
editArray.push({
updatedAt: new Date(edit.updatedAt).toLocaleString('UTC', { hour: 'numeric', minute: 'numeric', second: 'numeric', year: 'numeric', month: 'short', day: 'numeric' }),
- text: edit.text,
+ text: edit.oldText,
});
}