mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-09 13:51:57 -04:00
refactor(notes-service): use default-access-level & cleanup createNote
Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
parent
cdc9ebd352
commit
9e78776412
1 changed files with 33 additions and 18 deletions
|
@ -9,7 +9,7 @@ import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||||
import { InjectRepository } from '@nestjs/typeorm';
|
import { InjectRepository } from '@nestjs/typeorm';
|
||||||
import { Repository } from 'typeorm';
|
import { Repository } from 'typeorm';
|
||||||
|
|
||||||
import { DefaultAccessPermission } from '../config/default-access-permission.enum';
|
import { DefaultAccessLevel } from '../config/default-access-level.enum';
|
||||||
import noteConfiguration, { NoteConfig } from '../config/note.config';
|
import noteConfiguration, { NoteConfig } from '../config/note.config';
|
||||||
import {
|
import {
|
||||||
AlreadyInDBError,
|
AlreadyInDBError,
|
||||||
|
@ -92,37 +92,51 @@ export class NotesService {
|
||||||
owner: User | null,
|
owner: User | null,
|
||||||
alias?: string,
|
alias?: string,
|
||||||
): Promise<Note> {
|
): Promise<Note> {
|
||||||
|
// Check if new note doesn't violate application constraints
|
||||||
if (alias) {
|
if (alias) {
|
||||||
this.checkNoteIdOrAlias(alias);
|
this.checkNoteIdOrAlias(alias);
|
||||||
}
|
}
|
||||||
const newNote = Note.create(owner, alias);
|
|
||||||
if (noteContent.length > this.noteConfig.maxDocumentLength) {
|
if (noteContent.length > this.noteConfig.maxDocumentLength) {
|
||||||
throw new MaximumDocumentLengthExceededError();
|
throw new MaximumDocumentLengthExceededError();
|
||||||
}
|
}
|
||||||
//TODO: Calculate patch
|
|
||||||
|
// We cast to a note early to keep the later code clean
|
||||||
|
const newNote = Note.create(owner, alias) as Note;
|
||||||
newNote.revisions = Promise.resolve([
|
newNote.revisions = Promise.resolve([
|
||||||
Revision.create(noteContent, noteContent, newNote as Note) as Revision,
|
//TODO: Calculate patch
|
||||||
|
Revision.create(noteContent, noteContent, newNote) as Revision,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
let everyoneAccessLevel;
|
||||||
|
|
||||||
if (owner) {
|
if (owner) {
|
||||||
|
// When we know an owner, an initial history entry is created
|
||||||
newNote.historyEntries = Promise.resolve([
|
newNote.historyEntries = Promise.resolve([
|
||||||
HistoryEntry.create(owner, newNote as Note) as HistoryEntry,
|
HistoryEntry.create(owner, newNote) as HistoryEntry,
|
||||||
]);
|
]);
|
||||||
|
// Use the default access level from the config
|
||||||
|
everyoneAccessLevel = this.noteConfig.permissions.default.everyone;
|
||||||
|
} else {
|
||||||
|
// If there is no owner, this is an anonymous note
|
||||||
|
// Anonymous notes are always writeable by everyone
|
||||||
|
everyoneAccessLevel = DefaultAccessLevel.WRITE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create permission object for the 'everyone' group
|
||||||
const everyonePermission = this.createGroupPermission(
|
const everyonePermission = this.createGroupPermission(
|
||||||
newNote as Note,
|
newNote,
|
||||||
await this.groupsService.getEveryoneGroup(),
|
await this.groupsService.getEveryoneGroup(),
|
||||||
owner === null
|
everyoneAccessLevel,
|
||||||
? DefaultAccessPermission.WRITE
|
|
||||||
: this.noteConfig.permissions.default.everyone,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Create permission object for logged-in users
|
||||||
const loggedInPermission = this.createGroupPermission(
|
const loggedInPermission = this.createGroupPermission(
|
||||||
newNote as Note,
|
newNote,
|
||||||
await this.groupsService.getLoggedInGroup(),
|
await this.groupsService.getLoggedInGroup(),
|
||||||
this.noteConfig.permissions.default.loggedIn,
|
this.noteConfig.permissions.default.loggedIn,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Merge into permissions array
|
||||||
newNote.groupPermissions = Promise.resolve([
|
newNote.groupPermissions = Promise.resolve([
|
||||||
...Optional.ofNullable(everyonePermission).wrapInArray(),
|
...Optional.ofNullable(everyonePermission).wrapInArray(),
|
||||||
...Optional.ofNullable(loggedInPermission).wrapInArray(),
|
...Optional.ofNullable(loggedInPermission).wrapInArray(),
|
||||||
|
@ -148,15 +162,16 @@ export class NotesService {
|
||||||
private createGroupPermission(
|
private createGroupPermission(
|
||||||
note: Note,
|
note: Note,
|
||||||
group: Group,
|
group: Group,
|
||||||
permission: DefaultAccessPermission,
|
accessLevel: DefaultAccessLevel,
|
||||||
): NoteGroupPermission | null {
|
): NoteGroupPermission | null {
|
||||||
return permission === DefaultAccessPermission.NONE
|
if (accessLevel === DefaultAccessLevel.NONE) {
|
||||||
? null
|
return null;
|
||||||
: NoteGroupPermission.create(
|
}
|
||||||
group,
|
return NoteGroupPermission.create(
|
||||||
note,
|
group,
|
||||||
permission === DefaultAccessPermission.WRITE,
|
note,
|
||||||
);
|
accessLevel === DefaultAccessLevel.WRITE,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue