Files
mantra.press/server/router/versions/index.js

778 lines
33 KiB
JavaScript
Raw Normal View History

2022-06-19 02:17:55 +02:00
const db = require('mantra-db-models');
2023-01-15 03:55:14 +02:00
const md = require("markdown-it")().disable(['link', 'reference', 'reference'])
2021-12-25 01:35:32 +02:00
const express = require('express');
2022-02-26 11:18:50 +02:00
2022-06-19 02:17:55 +02:00
const wordCounter = (totalWordCount, chunk) => {
return totalWordCount + chunk.split(" ").length
}
2021-12-25 01:35:32 +02:00
2022-06-19 02:17:55 +02:00
const characterCounter = (totalCharacterCount, chunk) => {
return totalCharacterCount + chunk.length
}
2022-06-19 03:00:48 +02:00
const saveFile = async (file, userId, chapterIndex, artifactVersion) => {
const fileText = file.data.toString()
// if (Object.hasOwnProperty.call(file, index)) {
// const file = request.files[index]
// }
return await saveChapter(
userId,
file.name,
fileText,
chapterIndex,
artifactVersion
)
}
2022-06-19 02:17:55 +02:00
const saveChapter = async (userId, chapterName, text, chapterIndex, artifactVersion) => {
const chunks = md.parse(
text
).filter(token => token.content).map(token => token.content/*.replace(/\n/g,' ')*/)
return db.Chapter.create({
creatorId: userId,
name: chapterName,
originalText: text,
wordCount: chunks.reduce(wordCounter, 0),// TODO: Count words...
characterCount: chunks.reduce(characterCounter, 0),// TODO: Count words...
index: chapterIndex,
artifactVersionId: artifactVersion.id,
chunks: chunks.map((chunk,index) => {
return {
creatorId: userId,
text: chunk.trim(),
index: index,
wordCount: chunk.split(" ").length,
characterCount: chunk.length
}
})
}, {
include: [
{
association: db.Chapter.Chunks
}
]
}).catch(error => {
console.error("Chapter Upload Error: ", error)
return null
})
}
2021-12-25 01:35:32 +02:00
module.exports = function (options) {
var router = express.Router();
router.route('/:id')
.get(function(request, response, next) {
db.ArtifactVersion.findByPk(request.params.id, {
2021-12-25 01:35:32 +02:00
include: [
{
association: db.ArtifactVersion.Artifact,
2022-01-09 01:44:02 +02:00
// required: true,
2021-12-25 01:35:32 +02:00
include: [
{
association: db.Artifact.ArtifactApproval
2022-01-09 01:44:02 +02:00
},
{
association: db.Artifact.Owner,
// required: false,
include: [
{
association: db.Owner.OwnerEntities,
// required: true,
include: [
{
association: db.OwnerEntity.Entity,
// required: false,
include: [
{
association: db.Entity.EntityUsers,
required: false,
where: {
2022-01-09 03:22:40 +02:00
userId: request.user?.id ?? null
2022-01-09 01:44:02 +02:00
}
}
]
}
]
}
]
2021-12-25 01:35:32 +02:00
}
]
},
{
association: db.ArtifactVersion.Chapters
2021-12-25 01:35:32 +02:00
},
{
2022-01-08 03:34:51 +02:00
association: db.ArtifactVersion.TranslationArtifactVersions,
where: {
2022-01-08 20:22:23 +02:00
backTranslationFromId: null,
forkedFromId: null
2022-01-08 03:34:51 +02:00
},
required: false
2022-01-16 04:10:24 +02:00
},
{
association: db.ArtifactVersion.ArtifactVersionApproval
2021-12-25 01:35:32 +02:00
}
]
}).then(artifactVersion => {
if (artifactVersion) {
response.display("artifact-version", {
2021-12-25 01:35:32 +02:00
user: request.user,
pageTitle: "Artifact - Mantra",
artifactVersion: artifactVersion
2021-12-25 01:35:32 +02:00
})
} else {
next()
}
}).catch(error => {
next(error)
})
})
router.route('/:id/chapters/add')
.get(function(request, response, next) {
db.ArtifactVersion.findByPk(request.params.id, {
2021-12-25 01:35:32 +02:00
include: [
{
association: db.ArtifactVersion.Artifact,
2022-01-09 01:44:02 +02:00
required: true,
2021-12-25 01:35:32 +02:00
include: [
{
2022-01-09 01:44:02 +02:00
association: db.Artifact.ArtifactApproval,
},
{
association: db.Artifact.Owner,
required: true,
include: [
{
association: db.Owner.OwnerEntities,
required: true,
include: [
{
association: db.OwnerEntity.Entity,
required: true,
include: [
{
association: db.Entity.EntityUsers,
required: true,
where: {
2022-01-09 03:22:40 +02:00
userId: request.user?.id ?? null
2022-01-09 01:44:02 +02:00
}
}
]
}
]
}
]
2021-12-25 01:35:32 +02:00
}
]
},
{
association: db.ArtifactVersion.Chapters
2021-12-25 01:35:32 +02:00
}
]
}).then(artifactVersion => {
if (artifactVersion) {
2021-12-25 01:35:32 +02:00
response.display("chapter-form", {
user: request.user,
pageTitle: "Library - Mantra",
artifactVersion: artifactVersion
2021-12-25 01:35:32 +02:00
})
} else {
next()
}
}).catch(error => {
next(error)
})
})
.post(function(request, response, next) {
db.ArtifactVersion.findByPk(request.params.id, {
2021-12-25 01:35:32 +02:00
include: [
{
association: db.ArtifactVersion.Artifact,
2022-01-09 01:44:02 +02:00
required: true,
2021-12-25 01:35:32 +02:00
include: [
{
2022-01-09 01:44:02 +02:00
association: db.Artifact.ArtifactApproval,
},
{
association: db.Artifact.Owner,
required: true,
include: [
{
association: db.Owner.OwnerEntities,
required: true,
include: [
{
association: db.OwnerEntity.Entity,
required: true,
include: [
{
association: db.Entity.EntityUsers,
required: true,
where: {
2022-01-09 03:22:40 +02:00
userId: request.user?.id ?? null
2022-01-09 01:44:02 +02:00
}
}
]
}
]
}
]
2021-12-25 01:35:32 +02:00
}
]
},
{
association: db.ArtifactVersion.Chapters
2021-12-25 01:35:32 +02:00
}
]
}).then(artifactVersion => {
2021-12-25 01:35:32 +02:00
const isString = typeof request.body.text === "string" || request.body.text instanceof String
if (artifactVersion && isString) {
2022-02-26 11:18:50 +02:00
const chunks = md.parse(
request.body.text
).filter(token => token.content).map(token => token.content/*.replace(/\n/g,' ')*/)
2021-12-25 01:35:32 +02:00
db.Chapter.create({
2022-01-07 00:27:14 +02:00
creatorId: request.user.id,
2021-12-25 01:35:32 +02:00
name: chunks[0].trim(),
originalText: request.body.text,
2022-02-10 23:43:31 +02:00
index: request.body.index,
artifactVersionId: artifactVersion.id,
2021-12-25 01:35:32 +02:00
chunks: chunks.map((chunk,index) => {
return {
2022-01-07 02:15:44 +02:00
creatorId: request.user.id,
text: chunk.trim(),
2021-12-25 01:35:32 +02:00
index: index
}
})
}, {
include: [
{
association: db.Chapter.Chunks
}
]
}).then(chapter => {
if (chapter) {
response.redirect(`/v/${chapter.artifactVersionId}`)
2021-12-25 01:35:32 +02:00
} else {
response.display("chapter-form", {
user: request.user,
pageTitle: "Library - Mantra",
artifactVersion: artifactVersion
2021-12-25 01:35:32 +02:00
})
}
}).catch(error => {
next(error)
})
} else {
next()
}
}).catch(error => {
next(error)
})
2022-06-19 02:17:55 +02:00
})
router.route('/:id/chapters/upload')
.get(function(request, response, next) {
db.ArtifactVersion.findByPk(request.params.id, {
include: [
{
association: db.ArtifactVersion.Artifact,
required: true,
include: [
{
association: db.Artifact.ArtifactApproval,
},
{
association: db.Artifact.Owner,
required: true,
include: [
{
association: db.Owner.OwnerEntities,
required: true,
include: [
{
association: db.OwnerEntity.Entity,
required: true,
include: [
{
association: db.Entity.EntityUsers,
required: true,
where: {
userId: request.user?.id ?? null
}
}
]
}
]
}
]
}
]
},
{
association: db.ArtifactVersion.Chapters
}
]
}).then(artifactVersion => {
if (artifactVersion) {
response.display("chapters-upload", {
user: request.user,
pageTitle: "Library - Mantra",
artifactVersion: artifactVersion
})
} else {
next()
}
}).catch(error => {
next(error)
})
})
.post(function(request, response, next) {
db.ArtifactVersion.findByPk(request.params.id, {
include: [
{
association: db.ArtifactVersion.Artifact,
required: true,
include: [
{
association: db.Artifact.ArtifactApproval,
},
{
association: db.Artifact.Owner,
required: true,
include: [
{
association: db.Owner.OwnerEntities,
required: true,
include: [
{
association: db.OwnerEntity.Entity,
required: true,
include: [
{
association: db.Entity.EntityUsers,
required: true,
where: {
userId: request.user?.id ?? null
}
}
]
}
]
}
]
}
]
},
{
association: db.ArtifactVersion.Chapters
}
]
}).then(async (artifactVersion) => {
if (artifactVersion && request.files) {
// TODO: Parse files and store their texts...
const chapterIndex = artifactVersion.chapters.length + 1
2022-06-19 03:00:48 +02:00
if (request.files.chapters.length > 0) {
for (const index in request.files.chapters) {
const file = request.files.chapters[index] ?? request.files.chapters
const fileText = file.data.toString()
// if (Object.hasOwnProperty.call(file, index)) {
// const file = request.files[index]
// }
const chapter = await saveFile(
file,
request.user.id,
2022-06-22 03:04:53 +02:00
Number(chapterIndex)+Number(index),
2022-06-19 03:00:48 +02:00
artifactVersion
)
}
} else {
const file = request.files.chapters
const chapter = await saveFile(
file,
2022-06-19 02:17:55 +02:00
request.user.id,
2022-06-19 03:00:48 +02:00
chapterIndex,
2022-06-19 02:17:55 +02:00
artifactVersion
)
}
2022-06-19 03:00:48 +02:00
2022-06-19 02:17:55 +02:00
response.redirect(`/v/${artifactVersion.id}`)
} else {
next()
}
}).catch(error => {
next(error)
})
2021-12-25 01:35:32 +02:00
})
router.route('/:id/chapters/:chapterId')
.get(function(request, response, next) {
db.Chapter.findByPk(request.params.chapterId, {
include: [
{
association: db.Chapter.Chunks
},
{
association: db.Chapter.ArtifactVersion,
2021-12-25 01:35:32 +02:00
include: [
{
association: db.ArtifactVersion.Artifact
2021-12-25 01:35:32 +02:00
}
]
}
]
}).then(chapter => {
if (chapter) {
response.display("chapter", {
user: request.user,
pageTitle: "Chapter - Mantra",
chapter: chapter
})
} else {
next()
}
}).catch(error => {
next(error)
})
})
router.route('/:id/translations/add')
.get(function(request, response, next) {
db.ArtifactVersion.findByPk(request.params.id, {
2021-12-25 01:35:32 +02:00
include: [
{
association: db.ArtifactVersion.Artifact,
2021-12-25 01:35:32 +02:00
include: [
{
association: db.Artifact.ArtifactApproval
2021-12-25 01:35:32 +02:00
}
]
},
{
association: db.ArtifactVersion.TranslationArtifactVersions
2021-12-25 01:35:32 +02:00
}
]
}).then(artifactVersion => {
if (artifactVersion) {
2021-12-25 01:35:32 +02:00
response.display("translation-form", {
user: request.user,
pageTitle: "Library - Mantra",
artifactVersion: artifactVersion
2021-12-25 01:35:32 +02:00
})
} else {
next()
}
}).catch(error => {
next(error)
})
})
.post(function(request, response, next) {
db.ArtifactVersion.findByPk(request.params.id, {
2021-12-25 01:35:32 +02:00
include: [
{
association: db.ArtifactVersion.Artifact,
2021-12-25 01:35:32 +02:00
include: [
{
association: db.Artifact.ArtifactApproval
2021-12-25 01:35:32 +02:00
}
]
},
{
association: db.ArtifactVersion.TranslationArtifactVersions
2022-01-08 00:59:36 +02:00
},
{
association: db.ArtifactVersion.Chapters,
include: [
{
association: db.Chapter.Chunks
}
]
2021-12-25 01:35:32 +02:00
}
]
}).then(async (artifactVersion) => {
if (artifactVersion) {
2021-12-25 01:35:32 +02:00
const dialectTokens = request.body.dialect?.split(":")
if (dialectTokens?.length == 2) {
const countryId = dialectTokens[0].split("-")[0]
const languageId = dialectTokens[0].split("-")[1]
const dialect = await db.Dialect.findOne({
where: {
countryId: countryId,
languageId: languageId
}
})
if (dialect) {
const translationArtifactVersion = await db.TranslationArtifactVersion.create({
2022-01-07 00:27:14 +02:00
creatorId: request.user.id,
2021-12-25 01:35:32 +02:00
name: dialect.name,
artifactVersionId: artifactVersion.id,
2022-06-17 22:55:52 +02:00
visibility: artifactVersion.artifact.visibility,
dialectId: dialect.id,
owner: {
ownerEntities: [
{
entityId: request.user.individualEntityUser.entityUser.entityId,
}
]
},
2022-01-08 00:59:36 +02:00
translationChapters: artifactVersion.chapters.map(chapter => {
return {
creatorId: request.user.id,
chapterId: chapter.id,
2022-01-15 00:59:30 +02:00
index: chapter.index,
2022-01-08 00:59:36 +02:00
translationChunks: chapter.chunks.map(chunk => {
return {
creatorId: request.user.id,
chunkId: chunk.id,
text: chunk.text,
index: chunk.index
}
})
}
})
}, {
include: [
{
association: db.TranslationArtifactVersion.TranslationChapters,
include: [
{
association: db.TranslationChapter.TranslationChunks
}
]
},
{
association: db.TranslationArtifactVersionPledge.Owner,
include: [
{
association: db.Owner.OwnerEntities
}
]
2022-01-08 00:59:36 +02:00
}
]
2021-12-25 01:35:32 +02:00
})
if (translationArtifactVersion) {
2022-01-09 02:33:41 +02:00
return response.redirect(`/translate/${translationArtifactVersion.id}`)
2021-12-25 01:35:32 +02:00
}
}
}
}
next()
}).catch(error => {
next(error)
})
})
2021-12-27 02:25:03 +02:00
2022-01-16 04:10:24 +02:00
router.route('/:id/approval')
.get(function(request, response, next) {
db.ArtifactVersion.findByPk(request.params.id, {
include: [
{
association: db.ArtifactVersion.Artifact,
// required: true,
include: [
{
association: db.Artifact.ArtifactApproval
},
{
association: db.Artifact.Owner,
// required: false,
include: [
{
association: db.Owner.OwnerEntities,
// required: true,
include: [
{
association: db.OwnerEntity.Entity,
// required: false,
include: [
{
association: db.Entity.EntityUsers,
required: false,
where: {
userId: request.user?.id ?? null
}
}
]
}
]
}
]
}
]
},
{
association: db.ArtifactVersion.Chapters
},
{
association: db.ArtifactVersion.TranslationArtifactVersions,
where: {
backTranslationFromId: null,
forkedFromId: null
},
required: false
},
{
association: db.ArtifactVersion.ArtifactVersionApproval,
required:true
}
]
}).then(artifactVersion => {
if (artifactVersion) {
response.display("artifact-version-approval", {
user: request.user,
pageTitle: "Artifact - Mantra",
artifactVersion: artifactVersion
})
} else {
next()
}
}).catch(error => {
next(error)
})
})
router.route('/:id/approval/add')
.get(function(request, response, next) {
db.ArtifactVersion.findByPk(request.params.id, {
include: [
{
association: db.ArtifactVersion.Artifact,
required: true,
include: [
{
association: db.Artifact.ArtifactApproval,
},
{
association: db.Artifact.Owner,
required: true,
include: [
{
association: db.Owner.OwnerEntities,
required: true,
include: [
{
association: db.OwnerEntity.Entity,
required: true,
include: [
{
association: db.Entity.EntityUsers,
required: true,
where: {
userId: request.user?.id ?? null
}
}
]
}
]
}
]
}
]
},
{
association: db.ArtifactVersion.Chapters
}
]
}).then(artifactVersion => {
if (artifactVersion) {
if (artifactVersion.artifactVersionApproval) {
return response.redirect(`/v/${artifactVersion.id}/approval`)
}
response.display("artifact-version-approval-form", {
user: request.user,
pageTitle: "Approval - Mantra",
artifactVersion: artifactVersion,
artifactVersionApproval: {
name: artifactVersion.artifact.artifactApproval?.name,
description: artifactVersion.artifact.artifactApproval?.description,
conditions: artifactVersion.artifact.artifactApproval?.conditions,
satoshis: artifactVersion.artifact.artifactApproval?.satoshis,
approvedAt: artifactVersion.artifact.artifactApproval?.approvedAt
}
})
} else {
next()
}
}).catch(error => {
next(error)
})
})
.post(function(request, response, next) {
db.ArtifactVersion.findByPk(request.params.id, {
include: [
{
association: db.ArtifactVersion.Artifact,
required: true,
include: [
{
association: db.Artifact.ArtifactApproval,
},
{
association: db.Artifact.Owner,
required: true,
include: [
{
association: db.Owner.OwnerEntities,
required: true,
include: [
{
association: db.OwnerEntity.Entity,
required: true,
include: [
{
association: db.Entity.EntityUsers,
required: true,
where: {
userId: request.user?.id ?? null
}
}
]
}
]
}
]
}
]
},
{
association: db.ArtifactVersion.Chapters
}
]
}).then(async (artifactVersion) => {
if (artifactVersion) {
if (artifactVersion.artifactVersionApproval == null) {
// Create artifactVersionApproval
const satoshis = Number(request.body.satoshis) ? Number(request.body.satoshis) : null
await db.ArtifactVersionApproval.create({
name: request.body.name,
type: 'umbrella',
artifactVersionId: artifactVersion.id,
description: request.body.description,
conditions: request.body.conditions,
satoshis: satoshis,
creatorId: request.user.id,
approvedAt: request.body.isApproved == 'on' ? new Date() : null
})
}
response.redirect(`/v/${artifactVersion.id}/approval`)
} else {
next()
}
}).catch(error => {
next(error)
})
})
2021-12-25 01:35:32 +02:00
return router;
};