feat(notes): Use publicId in notes service

Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
Philip Molares 2021-05-16 22:44:43 +02:00
parent 11b0946f1d
commit 430e75ef44

View file

@ -32,6 +32,8 @@ import { NoteGroupPermission } from '../permissions/note-group-permission.entity
import { GroupsService } from '../groups/groups.service'; import { GroupsService } from '../groups/groups.service';
import { checkArrayForDuplicates } from '../utils/arrayDuplicatCheck'; import { checkArrayForDuplicates } from '../utils/arrayDuplicatCheck';
import appConfiguration, { AppConfig } from '../config/app.config'; import appConfiguration, { AppConfig } from '../config/app.config';
import base32Encode from 'base32-encode';
import { randomBytes } from 'crypto';
@Injectable() @Injectable()
export class NotesService { export class NotesService {
@ -92,6 +94,7 @@ export class NotesService {
newNote.revisions = Promise.resolve([ newNote.revisions = Promise.resolve([
Revision.create(noteContent, noteContent), Revision.create(noteContent, noteContent),
]); ]);
newNote.publicId = this.generatePublicId();
if (alias) { if (alias) {
newNote.alias = alias; newNote.alias = alias;
this.checkNoteIdOrAlias(alias); this.checkNoteIdOrAlias(alias);
@ -164,7 +167,7 @@ export class NotesService {
const note = await this.noteRepository.findOne({ const note = await this.noteRepository.findOne({
where: [ where: [
{ {
id: noteIdOrAlias, publicId: noteIdOrAlias,
}, },
{ {
alias: noteIdOrAlias, alias: noteIdOrAlias,
@ -210,6 +213,15 @@ export class NotesService {
} }
} }
/**
* Generate publicId for a note.
* This is a randomly generated 128-bit value encoded with base32-encode using the crockford variant and converted to lowercase.
*/
generatePublicId(): string {
const randomId = randomBytes(128);
return base32Encode(randomId, 'Crockford').toLowerCase();
}
/** /**
* @async * @async
* Delete a note * Delete a note
@ -358,8 +370,7 @@ export class NotesService {
async toNoteMetadataDto(note: Note): Promise<NoteMetadataDto> { async toNoteMetadataDto(note: Note): Promise<NoteMetadataDto> {
const updateUser = await this.calculateUpdateUser(note); const updateUser = await this.calculateUpdateUser(note);
return { return {
// TODO: Convert DB UUID to base64 id: note.publicId,
id: note.id,
alias: note.alias ?? null, alias: note.alias ?? null,
title: note.title ?? '', title: note.title ?? '',
createTime: (await this.getFirstRevision(note)).createdAt, createTime: (await this.getFirstRevision(note)).createdAt,