mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-30 23:05:30 -04:00
History: Add history service and usage
Add history service to allow for CRUD operations. Use history service in controllers to: 1. Allow manipulating of history entries 2. Guaranty the correct existence of history entries Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
parent
b76fa91a3c
commit
7f1afc30c9
8 changed files with 173 additions and 118 deletions
|
@ -4,15 +4,33 @@
|
|||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import { IsBoolean, ValidateNested } from 'class-validator';
|
||||
import { NoteMetadataDto } from '../notes/note-metadata.dto';
|
||||
import { IsArray, IsBoolean, IsDate, IsString } from 'class-validator';
|
||||
|
||||
export class HistoryEntryDto {
|
||||
/**
|
||||
* Metadata of this note
|
||||
* ID or Alias of the note
|
||||
*/
|
||||
@ValidateNested()
|
||||
metadata: NoteMetadataDto;
|
||||
@IsString()
|
||||
identifier: string;
|
||||
|
||||
/**
|
||||
* Title of the note
|
||||
* Does not contain any markup but might be empty
|
||||
* @example "Shopping List"
|
||||
*/
|
||||
@IsString()
|
||||
title: string;
|
||||
|
||||
/**
|
||||
* Datestring of the last time this note was updated
|
||||
* @example "2020-12-01 12:23:34"
|
||||
*/
|
||||
@IsDate()
|
||||
lastVisited: Date;
|
||||
|
||||
@IsArray()
|
||||
@IsString({ each: true })
|
||||
tags: string[];
|
||||
|
||||
/**
|
||||
* True if this note is pinned
|
||||
|
|
|
@ -4,12 +4,7 @@
|
|||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import {
|
||||
Column,
|
||||
Entity,
|
||||
ManyToOne,
|
||||
UpdateDateColumn,
|
||||
} from 'typeorm';
|
||||
import { Column, Entity, ManyToOne, UpdateDateColumn } from 'typeorm';
|
||||
import { User } from '../users/user.entity';
|
||||
import { Note } from '../notes/note.entity';
|
||||
|
||||
|
|
|
@ -7,10 +7,19 @@
|
|||
import { Module } from '@nestjs/common';
|
||||
import { LoggerModule } from '../logger/logger.module';
|
||||
import { HistoryService } from './history.service';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { HistoryEntry } from './history-entry.entity';
|
||||
import { UsersModule } from '../users/users.module';
|
||||
import { NotesModule } from '../notes/notes.module';
|
||||
|
||||
@Module({
|
||||
providers: [HistoryService],
|
||||
exports: [HistoryService],
|
||||
imports: [LoggerModule],
|
||||
imports: [
|
||||
LoggerModule,
|
||||
TypeOrmModule.forFeature([HistoryEntry]),
|
||||
UsersModule,
|
||||
NotesModule,
|
||||
],
|
||||
})
|
||||
export class HistoryModule {}
|
||||
|
|
|
@ -8,90 +8,98 @@ import { Injectable } from '@nestjs/common';
|
|||
import { ConsoleLoggerService } from '../logger/console-logger.service';
|
||||
import { HistoryEntryUpdateDto } from './history-entry-update.dto';
|
||||
import { HistoryEntryDto } from './history-entry.dto';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { HistoryEntry } from './history-entry.enity';
|
||||
import { UsersService } from '../users/users.service';
|
||||
import { NotesService } from '../notes/notes.service';
|
||||
import { User } from '../users/user.entity';
|
||||
import { Note } from '../notes/note.entity';
|
||||
import { NotInDBError } from '../errors/errors';
|
||||
|
||||
@Injectable()
|
||||
export class HistoryService {
|
||||
constructor(private readonly logger: ConsoleLoggerService) {
|
||||
constructor(
|
||||
private readonly logger: ConsoleLoggerService,
|
||||
@InjectRepository(HistoryEntry)
|
||||
private historyEntryRepository: Repository<HistoryEntry>,
|
||||
private usersService: UsersService,
|
||||
private notesService: NotesService,
|
||||
) {
|
||||
this.logger.setContext(HistoryService.name);
|
||||
}
|
||||
|
||||
getUserHistory(username: string): HistoryEntryDto[] {
|
||||
//TODO: Use the database
|
||||
this.logger.warn('Using hardcoded data!');
|
||||
return [
|
||||
{
|
||||
metadata: {
|
||||
alias: null,
|
||||
createTime: new Date(),
|
||||
description: 'Very descriptive text.',
|
||||
editedBy: [],
|
||||
id: 'foobar-barfoo',
|
||||
permissions: {
|
||||
owner: {
|
||||
displayName: 'foo',
|
||||
userName: 'fooUser',
|
||||
email: 'foo@example.com',
|
||||
photo: '',
|
||||
},
|
||||
sharedToUsers: [],
|
||||
sharedToGroups: [],
|
||||
},
|
||||
tags: [],
|
||||
title: 'Title!',
|
||||
updateTime: new Date(),
|
||||
updateUser: {
|
||||
displayName: 'foo',
|
||||
userName: 'fooUser',
|
||||
email: 'foo@example.com',
|
||||
photo: '',
|
||||
},
|
||||
viewCount: 42,
|
||||
},
|
||||
pinStatus: false,
|
||||
},
|
||||
];
|
||||
async getEntriesByUser(user: User): Promise<HistoryEntry[]> {
|
||||
return await this.historyEntryRepository.find({
|
||||
where: { user: user },
|
||||
relations: ['note'],
|
||||
});
|
||||
}
|
||||
|
||||
updateHistoryEntry(
|
||||
noteId: string,
|
||||
updateDto: HistoryEntryUpdateDto,
|
||||
): HistoryEntryDto {
|
||||
//TODO: Use the database
|
||||
this.logger.warn('Using hardcoded data!');
|
||||
return {
|
||||
metadata: {
|
||||
alias: null,
|
||||
createTime: new Date(),
|
||||
description: 'Very descriptive text.',
|
||||
editedBy: [],
|
||||
id: 'foobar-barfoo',
|
||||
permissions: {
|
||||
owner: {
|
||||
displayName: 'foo',
|
||||
userName: 'fooUser',
|
||||
email: 'foo@example.com',
|
||||
photo: '',
|
||||
},
|
||||
sharedToUsers: [],
|
||||
sharedToGroups: [],
|
||||
},
|
||||
tags: [],
|
||||
title: 'Title!',
|
||||
updateTime: new Date(),
|
||||
updateUser: {
|
||||
displayName: 'foo',
|
||||
userName: 'fooUser',
|
||||
email: 'foo@example.com',
|
||||
photo: '',
|
||||
},
|
||||
viewCount: 42,
|
||||
private async getEntryByNoteIdOrAlias(
|
||||
noteIdOrAlias: string,
|
||||
user: User,
|
||||
): Promise<HistoryEntry> {
|
||||
const note = await this.notesService.getNoteByIdOrAlias(noteIdOrAlias);
|
||||
return await this.getEntryByNote(note, user);
|
||||
}
|
||||
|
||||
private async getEntryByNote(note: Note, user: User): Promise<HistoryEntry> {
|
||||
return await this.historyEntryRepository.findOne({
|
||||
where: {
|
||||
note: note,
|
||||
user: user,
|
||||
},
|
||||
pinStatus: updateDto.pinStatus,
|
||||
relations: ['note', 'user'],
|
||||
});
|
||||
}
|
||||
|
||||
async createOrUpdateHistoryEntry(
|
||||
note: Note,
|
||||
user: User,
|
||||
): Promise<HistoryEntry> {
|
||||
let entry = await this.getEntryByNote(note, user);
|
||||
if (!entry) {
|
||||
entry = HistoryEntry.create(user, note);
|
||||
} else {
|
||||
entry.updatedAt = new Date();
|
||||
}
|
||||
return await this.historyEntryRepository.save(entry);
|
||||
}
|
||||
|
||||
async updateHistoryEntry(
|
||||
noteIdOrAlias: string,
|
||||
user: User,
|
||||
updateDto: HistoryEntryUpdateDto,
|
||||
): Promise<HistoryEntry> {
|
||||
const entry = await this.getEntryByNoteIdOrAlias(noteIdOrAlias, user);
|
||||
if (!entry) {
|
||||
throw new NotInDBError(
|
||||
`User '${user.userName}' has no HistoryEntry for Note with id '${noteIdOrAlias}'`,
|
||||
);
|
||||
}
|
||||
entry.pinStatus = updateDto.pinStatus;
|
||||
return this.historyEntryRepository.save(entry);
|
||||
}
|
||||
|
||||
async deleteHistoryEntry(noteIdOrAlias: string, user: User): Promise<void> {
|
||||
const entry = await this.getEntryByNoteIdOrAlias(noteIdOrAlias, user);
|
||||
if (!entry) {
|
||||
throw new NotInDBError(
|
||||
`User '${user.userName}' has no HistoryEntry for Note with id '${noteIdOrAlias}'`,
|
||||
);
|
||||
}
|
||||
await this.historyEntryRepository.remove(entry);
|
||||
return;
|
||||
}
|
||||
|
||||
async toHistoryEntryDto(entry: HistoryEntry): Promise<HistoryEntryDto> {
|
||||
return {
|
||||
identifier: entry.note.alias ? entry.note.alias : entry.note.id,
|
||||
lastVisited: entry.updatedAt,
|
||||
tags: this.notesService.toTagList(entry.note),
|
||||
title: entry.note.title,
|
||||
pinStatus: entry.pinStatus,
|
||||
};
|
||||
}
|
||||
|
||||
deleteHistoryEntry(note: string) {
|
||||
//TODO: Use the database and actually do stuff
|
||||
throw new Error('Not implemented');
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue