Compare commits
No commits in common. "b015f3a97e263ea8311415cc84d6acf7e7313a2b" and "1cb6c731d8527ae22b0793f3caaedccbe3d455d3" have entirely different histories.
b015f3a97e
...
1cb6c731d8
@ -6,5 +6,4 @@ MYSQL_ROOT_PASSWORD=
|
|||||||
#
|
#
|
||||||
## can be generated with `openssl rand -base64 32`
|
## can be generated with `openssl rand -base64 32`
|
||||||
MYSQL_PASSWORD=
|
MYSQL_PASSWORD=
|
||||||
# password should be the same as mysql_password
|
PASSWORD=${MYSQL_PASSWORD}
|
||||||
PASSWORD=
|
|
||||||
|
|||||||
19
README.md
19
README.md
@ -1,19 +0,0 @@
|
|||||||
# SimpleNotes, a simple markdown note taking website
|
|
||||||
|
|
||||||
## Requirements
|
|
||||||
|
|
||||||
- Docker
|
|
||||||
- docker-compose
|
|
||||||
|
|
||||||
## How to run
|
|
||||||
|
|
||||||
- Copy the docker-compose.yml somewhere
|
|
||||||
- In the same directory, copy the *.env.dist* file and rename it to *.env*
|
|
||||||
- Edit the variables inside *.env* (see below)
|
|
||||||
- Run it with `docker-compose up -d`
|
|
||||||
|
|
||||||
## Configuration
|
|
||||||
|
|
||||||
The app is configured with environments variables.
|
|
||||||
If no match is found within the env, a default value is read from a properties file in /app/src/main/resources/application.properties.
|
|
||||||
Don't use the default values for secrets ! Every value inside *.env.dist* should be changed.
|
|
||||||
@ -68,28 +68,15 @@ class NoteController(
|
|||||||
fun note(request: Request, jwtPayload: JwtPayload): Response {
|
fun note(request: Request, jwtPayload: JwtPayload): Response {
|
||||||
val noteUuid = request.uuidPath() ?: return Response(NOT_FOUND)
|
val noteUuid = request.uuidPath() ?: return Response(NOT_FOUND)
|
||||||
|
|
||||||
if (request.method == Method.POST) {
|
if (request.method == Method.POST && request.form("delete") != null) {
|
||||||
if (request.form("delete") != null) {
|
|
||||||
return if (noteService.trash(jwtPayload.userId, noteUuid))
|
return if (noteService.trash(jwtPayload.userId, noteUuid))
|
||||||
Response.redirect("/notes") // TODO: flash cookie to show success ?
|
Response.redirect("/notes") // TODO: flash cookie to show success ?
|
||||||
else
|
else
|
||||||
Response(NOT_FOUND) // TODO: show an error
|
Response(NOT_FOUND) // TODO: show an error
|
||||||
}
|
}
|
||||||
if (request.form("public") != null) {
|
|
||||||
if (!noteService.makePublic(jwtPayload.userId, noteUuid)) return Response(NOT_FOUND)
|
|
||||||
} else if (request.form("private") != null) {
|
|
||||||
if (!noteService.makePrivate(jwtPayload.userId, noteUuid)) return Response(NOT_FOUND)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
val note = noteService.find(jwtPayload.userId, noteUuid) ?: return Response(NOT_FOUND)
|
val note = noteService.find(jwtPayload.userId, noteUuid) ?: return Response(NOT_FOUND)
|
||||||
return Response(OK).html(view.renderedNote(jwtPayload, note, shared = false))
|
return Response(OK).html(view.renderedNote(jwtPayload, note))
|
||||||
}
|
|
||||||
|
|
||||||
fun public(request: Request, jwtPayload: JwtPayload?): Response {
|
|
||||||
val noteUuid = request.uuidPath() ?: return Response(NOT_FOUND)
|
|
||||||
val note = noteService.findPublic(noteUuid) ?: return Response(NOT_FOUND)
|
|
||||||
return Response(OK).html(view.renderedNote(jwtPayload, note, shared = true))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun edit(request: Request, jwtPayload: JwtPayload): Response {
|
fun edit(request: Request, jwtPayload: JwtPayload): Response {
|
||||||
|
|||||||
@ -42,7 +42,6 @@ class Router(
|
|||||||
"/login" bind GET public userController::login,
|
"/login" bind GET public userController::login,
|
||||||
"/login" bind POST public userController::login,
|
"/login" bind POST public userController::login,
|
||||||
"/logout" bind POST to userController::logout,
|
"/logout" bind POST to userController::logout,
|
||||||
"/notes/public/{uuid}" bind GET public noteController::public,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
val protectedRoutes = routes(
|
val protectedRoutes = routes(
|
||||||
|
|||||||
@ -116,21 +116,12 @@ class NoteView(staticFileResolver: StaticFileResolver) : View(staticFileResolver
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun renderedNote(jwtPayload: JwtPayload?, note: PersistedNote, shared: Boolean) = renderPage(
|
fun renderedNote(jwtPayload: JwtPayload, note: PersistedNote) = renderPage(
|
||||||
note.meta.title,
|
note.meta.title,
|
||||||
jwtPayload = jwtPayload,
|
jwtPayload = jwtPayload,
|
||||||
scripts = listOf("/highlight.10.1.2.js", "/init-highlight.0.0.1.js")
|
scripts = listOf("/highlight.10.1.2.js", "/init-highlight.0.0.1.js")
|
||||||
) {
|
) {
|
||||||
div("container mx-auto p-4") {
|
div("container mx-auto p-4") {
|
||||||
|
|
||||||
if (shared) {
|
|
||||||
p("p-4 bg-gray-800") {
|
|
||||||
+"You are viewing a public note "
|
|
||||||
}
|
|
||||||
|
|
||||||
hr { }
|
|
||||||
}
|
|
||||||
|
|
||||||
div("flex items-center justify-between mb-4") {
|
div("flex items-center justify-between mb-4") {
|
||||||
h1("text-3xl fond-bold underline") { +note.meta.title }
|
h1("text-3xl fond-bold underline") { +note.meta.title }
|
||||||
span("space-x-2") {
|
span("space-x-2") {
|
||||||
@ -141,31 +132,6 @@ class NoteView(staticFileResolver: StaticFileResolver) : View(staticFileResolver
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!shared) {
|
|
||||||
noteActionForm(note)
|
|
||||||
publicPrivateForm(note)
|
|
||||||
|
|
||||||
if (note.public) {
|
|
||||||
p("my-4") {
|
|
||||||
+"You can share this link : "
|
|
||||||
a(href = "/notes/public/${note.uuid}", classes = "text-blue-300 underline") {
|
|
||||||
+"/notes/public/${note.uuid}"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
hr { }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
div {
|
|
||||||
attributes["id"] = "note"
|
|
||||||
unsafe {
|
|
||||||
+note.html
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun DIV.noteActionForm(note: PersistedNote) {
|
|
||||||
span("flex space-x-2 justify-end mb-4") {
|
span("flex space-x-2 justify-end mb-4") {
|
||||||
a(
|
a(
|
||||||
href = "/notes/${note.uuid}/edit",
|
href = "/notes/${note.uuid}/edit",
|
||||||
@ -179,21 +145,10 @@ class NoteView(staticFileResolver: StaticFileResolver) : View(staticFileResolver
|
|||||||
) { +"Delete" }
|
) { +"Delete" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
div {
|
||||||
|
attributes["id"] = "note"
|
||||||
private fun DIV.publicPrivateForm(note: PersistedNote) {
|
unsafe {
|
||||||
span("flex space-x-2 justify-end mb-4") {
|
+note.html
|
||||||
|
|
||||||
form(method = FormMethod.post, classes = "ml-auto ") {
|
|
||||||
button(
|
|
||||||
type = ButtonType.submit,
|
|
||||||
name = if (note.public) "private" else "public",
|
|
||||||
classes = "btn btn-teal"
|
|
||||||
) {
|
|
||||||
if (note.public)
|
|
||||||
+"This note is public, do you want to make it private ?"
|
|
||||||
else
|
|
||||||
+"This note is private, do you want to make it public ?"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -29,7 +29,6 @@ data class PersistedNote(
|
|||||||
val html: String,
|
val html: String,
|
||||||
val updatedAt: LocalDateTime,
|
val updatedAt: LocalDateTime,
|
||||||
val uuid: UUID,
|
val uuid: UUID,
|
||||||
val public: Boolean,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
|
|||||||
@ -88,10 +88,6 @@ class NoteService(
|
|||||||
fun search(userId: Int, searchTerms: SearchTerms) = searcher.search(userId, searchTerms)
|
fun search(userId: Int, searchTerms: SearchTerms) = searcher.search(userId, searchTerms)
|
||||||
|
|
||||||
fun dropAllIndexes() = searcher.dropAll()
|
fun dropAllIndexes() = searcher.dropAll()
|
||||||
|
|
||||||
fun makePublic(userId: Int, uuid: UUID) = noteRepository.makePublic(userId, uuid)
|
|
||||||
fun makePrivate(userId: Int, uuid: UUID) = noteRepository.makePrivate(userId, uuid)
|
|
||||||
fun findPublic(uuid: UUID) = noteRepository.findPublic(uuid)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
data class PaginatedNotes(val pages: Int, val notes: List<PersistedNoteMetadata>)
|
data class PaginatedNotes(val pages: Int, val notes: List<PersistedNoteMetadata>)
|
||||||
|
|||||||
@ -28,8 +28,4 @@ interface NoteRepository {
|
|||||||
fun update(userId: Int, uuid: UUID, note: Note): PersistedNote?
|
fun update(userId: Int, uuid: UUID, note: Note): PersistedNote?
|
||||||
fun export(userId: Int): List<ExportedNote>
|
fun export(userId: Int): List<ExportedNote>
|
||||||
fun findAllDetails(userId: Int): List<PersistedNote>
|
fun findAllDetails(userId: Int): List<PersistedNote>
|
||||||
|
|
||||||
fun makePublic(userId: Int, uuid: UUID): Boolean
|
|
||||||
fun makePrivate(userId: Int, uuid: UUID): Boolean
|
|
||||||
fun findPublic(uuid: UUID): PersistedNote?
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -121,8 +121,7 @@ internal class NoteRepositoryImpl(private val db: Database) : NoteRepository {
|
|||||||
markdown = note.markdown,
|
markdown = note.markdown,
|
||||||
html = note.html,
|
html = note.html,
|
||||||
updatedAt = now,
|
updatedAt = now,
|
||||||
uuid = uuid,
|
uuid = uuid
|
||||||
public = false, // TODO
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -200,32 +199,6 @@ internal class NoteRepositoryImpl(private val db: Database) : NoteRepository {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun makePublic(userId: Int, uuid: UUID) = db.update(Notes) {
|
|
||||||
it.public to true
|
|
||||||
where { Notes.userId eq userId and (Notes.uuid eq uuid) and (it.deleted eq false) }
|
|
||||||
} == 1
|
|
||||||
|
|
||||||
override fun makePrivate(userId: Int, uuid: UUID) = db.update(Notes) {
|
|
||||||
it.public to false
|
|
||||||
where { Notes.userId eq userId and (Notes.uuid eq uuid) and (it.deleted eq false) }
|
|
||||||
} == 1
|
|
||||||
|
|
||||||
override fun findPublic(uuid: UUID): PersistedNote? {
|
|
||||||
val note = db.notes
|
|
||||||
.filterColumns { it.columns - it.userId }
|
|
||||||
.filter { it.uuid eq uuid }
|
|
||||||
.filter { it.public eq true }
|
|
||||||
.find { it.deleted eq false }
|
|
||||||
?: return null
|
|
||||||
|
|
||||||
val tags = db.from(Tags)
|
|
||||||
.select(Tags.name)
|
|
||||||
.where { Tags.noteUuid eq uuid }
|
|
||||||
.map { it[Tags.name]!! }
|
|
||||||
|
|
||||||
return note.toPersistedNote(tags)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun List<NoteEntity>.tagsByUuid(): Map<UUID, List<String>> {
|
private fun List<NoteEntity>.tagsByUuid(): Map<UUID, List<String>> {
|
||||||
return if (isEmpty()) emptyMap()
|
return if (isEmpty()) emptyMap()
|
||||||
else db.tags
|
else db.tags
|
||||||
|
|||||||
@ -27,7 +27,6 @@ internal open class Notes(alias: String?) : Table<NoteEntity>("Notes", alias) {
|
|||||||
val userId = int("user_id").references(Users) { it.user }
|
val userId = int("user_id").references(Users) { it.user }
|
||||||
val updatedAt = datetime("updated_at").bindTo { it.updatedAt }
|
val updatedAt = datetime("updated_at").bindTo { it.updatedAt }
|
||||||
val deleted = boolean("deleted").bindTo { it.deleted }
|
val deleted = boolean("deleted").bindTo { it.deleted }
|
||||||
val public = boolean("public").bindTo { it.public }
|
|
||||||
val user get() = userId.referenceTable as Users
|
val user get() = userId.referenceTable as Users
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,7 +41,6 @@ internal interface NoteEntity : Entity<NoteEntity> {
|
|||||||
var html: String
|
var html: String
|
||||||
var updatedAt: LocalDateTime
|
var updatedAt: LocalDateTime
|
||||||
var deleted: Boolean
|
var deleted: Boolean
|
||||||
var public: Boolean
|
|
||||||
|
|
||||||
var user: UserEntity
|
var user: UserEntity
|
||||||
}
|
}
|
||||||
@ -50,7 +48,7 @@ internal interface NoteEntity : Entity<NoteEntity> {
|
|||||||
internal fun NoteEntity.toPersistedMetadata(tags: List<String>) = PersistedNoteMetadata(title, tags, updatedAt, uuid)
|
internal fun NoteEntity.toPersistedMetadata(tags: List<String>) = PersistedNoteMetadata(title, tags, updatedAt, uuid)
|
||||||
|
|
||||||
internal fun NoteEntity.toPersistedNote(tags: List<String>) =
|
internal fun NoteEntity.toPersistedNote(tags: List<String>) =
|
||||||
PersistedNote(NoteMetadata(title, tags), markdown, html, updatedAt, uuid, public)
|
PersistedNote(NoteMetadata(title, tags), markdown, html, updatedAt, uuid)
|
||||||
|
|
||||||
internal fun Note.toEntity(uuid: UUID, userId: Int): NoteEntity {
|
internal fun Note.toEntity(uuid: UUID, userId: Int): NoteEntity {
|
||||||
val note = this
|
val note = this
|
||||||
@ -60,7 +58,6 @@ internal fun Note.toEntity(uuid: UUID, userId: Int): NoteEntity {
|
|||||||
this.html = note.html
|
this.html = note.html
|
||||||
this.uuid = uuid
|
this.uuid = uuid
|
||||||
this.deleted = false
|
this.deleted = false
|
||||||
this.public = false
|
|
||||||
this.user["id"] = userId
|
this.user["id"] = userId
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,2 +0,0 @@
|
|||||||
alter table Notes
|
|
||||||
add column public bool not null default false
|
|
||||||
@ -1,2 +0,0 @@
|
|||||||
alter table Notes
|
|
||||||
add column public bool not null default false
|
|
||||||
@ -94,13 +94,13 @@ internal class NoteRepositoryImplTest {
|
|||||||
val note = Note(NoteMetadata("title", emptyList()), "md", "html")
|
val note = Note(NoteMetadata("title", emptyList()), "md", "html")
|
||||||
|
|
||||||
assertThat(noteRepo.create(user1.id, note))
|
assertThat(noteRepo.create(user1.id, note))
|
||||||
.isEqualToIgnoringGivenFields(note, "uuid", "updatedAt", "public")
|
.isEqualToIgnoringGivenFields(note, "uuid", "updatedAt")
|
||||||
.hasNoNullFieldsOrProperties()
|
.hasNoNullFieldsOrProperties()
|
||||||
|
|
||||||
assertThat(db.notes.toList())
|
assertThat(db.notes.toList())
|
||||||
.hasSize(1)
|
.hasSize(1)
|
||||||
.first()
|
.first()
|
||||||
.isEqualToIgnoringGivenFields(note, "uuid", "updatedAt", "public")
|
.isEqualToIgnoringGivenFields(note, "uuid", "updatedAt")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -25,12 +25,7 @@ internal class NoteSearcherImplTest {
|
|||||||
content: String = "",
|
content: String = "",
|
||||||
uuid: UUID = UUID.randomUUID(),
|
uuid: UUID = UUID.randomUUID(),
|
||||||
): PersistedNote {
|
): PersistedNote {
|
||||||
val note = PersistedNote(NoteMetadata(title, tags),
|
val note = PersistedNote(NoteMetadata(title, tags), markdown = content, html = "", LocalDateTime.MIN, uuid)
|
||||||
markdown = content,
|
|
||||||
html = "",
|
|
||||||
LocalDateTime.MIN,
|
|
||||||
uuid,
|
|
||||||
public = false)
|
|
||||||
searcher.indexNote(1, note)
|
searcher.indexNote(1, note)
|
||||||
return note
|
return note
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user