const db = require('mantra-db-models'); const md = require("markdown-it")().disable(['link', 'reference', 'reference']) const express = require('express'); const wordCounter = (totalWordCount, chunk) => { return totalWordCount + chunk.split(" ").length } const characterCounter = (totalCharacterCount, chunk) => { return totalCharacterCount + chunk.length } 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 ) } 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 }) } module.exports = function (options) { var router = express.Router(); router.route('/:id') .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 } ] }).then(artifactVersion => { if (artifactVersion) { response.display("artifact-version", { user: request.user, pageTitle: "Artifact - Mantra", artifactVersion: artifactVersion }) } else { next() } }).catch(error => { next(error) }) }) router.route('/:id/chapters/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) { response.display("chapter-form", { 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(artifactVersion => { const isString = typeof request.body.text === "string" || request.body.text instanceof String if (artifactVersion && isString) { const chunks = md.parse( request.body.text ).filter(token => token.content).map(token => token.content/*.replace(/\n/g,' ')*/) db.Chapter.create({ creatorId: request.user.id, name: chunks[0].trim(), originalText: request.body.text, index: request.body.index, artifactVersionId: artifactVersion.id, chunks: chunks.map((chunk,index) => { return { creatorId: request.user.id, text: chunk.trim(), index: index } }) }, { include: [ { association: db.Chapter.Chunks } ] }).then(chapter => { if (chapter) { response.redirect(`/v/${chapter.artifactVersionId}`) } else { response.display("chapter-form", { user: request.user, pageTitle: "Library - Mantra", artifactVersion: artifactVersion }) } }).catch(error => { next(error) }) } else { next() } }).catch(error => { next(error) }) }) 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 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, Number(chapterIndex)+Number(index), artifactVersion ) } } else { const file = request.files.chapters const chapter = await saveFile( file, request.user.id, chapterIndex, artifactVersion ) } response.redirect(`/v/${artifactVersion.id}`) } else { next() } }).catch(error => { next(error) }) }) 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, include: [ { association: db.ArtifactVersion.Artifact } ] } ] }).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, { include: [ { association: db.ArtifactVersion.Artifact, include: [ { association: db.Artifact.ArtifactApproval } ] }, { association: db.ArtifactVersion.TranslationArtifactVersions } ] }).then(artifactVersion => { if (artifactVersion) { response.display("translation-form", { 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, include: [ { association: db.Artifact.ArtifactApproval } ] }, { association: db.ArtifactVersion.TranslationArtifactVersions }, { association: db.ArtifactVersion.Chapters, include: [ { association: db.Chapter.Chunks } ] } ] }).then(async (artifactVersion) => { if (artifactVersion) { 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({ creatorId: request.user.id, name: dialect.name, artifactVersionId: artifactVersion.id, visibility: artifactVersion.artifact.visibility, dialectId: dialect.id, owner: { ownerEntities: [ { entityId: request.user.individualEntityUser.entityUser.entityId, } ] }, translationChapters: artifactVersion.chapters.map(chapter => { return { creatorId: request.user.id, chapterId: chapter.id, index: chapter.index, 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 } ] } ] }) if (translationArtifactVersion) { return response.redirect(`/translate/${translationArtifactVersion.id}`) } } } } next() }).catch(error => { next(error) }) }) 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) }) }) return router; };