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

392 lines
17 KiB
JavaScript
Raw Normal View History

const express = require('express')
2021-12-23 00:47:06 +02:00
const { Op } = require("sequelize")
module.exports = function (options) {
const db = options.db;
var router = express.Router();
router.route('/:id')
.get(function(request, response, next) {
db.TranslationArtifactVersion.findByPk(request.params.id, {
include: [
{
association: db.TranslationArtifactVersion.ArtifactVersion,
required: true,
include: [
{
association: db.ArtifactVersion.Chapters,
required: true,
limit: 1,
// TODO: Order by chapter index
}
]
}
]
}).then(translationArtifactVersion => {
if (translationArtifactVersion) {
response.redirect(`/translate/${translationArtifactVersion.id}/chapter/${translationArtifactVersion.artifactVersion.chapters[0].id}`)
} else {
next()
}
})
})
router.route('/:id/chapter/:chapterId')
.get(function(request, response, next) {
db.Chapter.findByPk(request.params.chapterId, {
include: [
{
association: db.Chapter.Chunks,
include: [
{
association: db.Chunk.Translation
}
]
},
{
association: db.Chapter.ArtifactVersion,
required: true,
include: [
{
association: db.ArtifactVersion.TranslationArtifactVersions,
required: true,
where: {
id: request.params.id
}
},
{
association: db.ArtifactVersion.Artifact,
2021-12-25 01:35:32 +02:00
include: [
{
association: db.Artifact.Dialect
2021-12-25 01:35:32 +02:00
}
]
}
]
}
]
}).then(chapter => {
if (chapter) {
response.display("translate-chapter", {
user: request.user,
pageTitle: `Translate Chapter ${chapter.name}`,
chapter: chapter
})
} else {
next()
}
}).catch(error => {
next(error)
})
})
2021-12-22 22:34:58 +02:00
2021-12-23 00:47:06 +02:00
router.route('/:id/chapter/:chapterId/t/:chunkIndex')
2021-12-22 22:34:58 +02:00
.get(function(request, response, next) {
2021-12-23 00:47:06 +02:00
const previousIndex = Number(request.params.chunkIndex)-1
const nextIndex = Number(request.params.chunkIndex)+1
db.Chunk.findAll({
where: {
chapterId: request.params.chapterId,
index: {
[Op.between]: [
previousIndex,
nextIndex
],
}
},
limit: 3,
2021-12-22 22:34:58 +02:00
include: [
{
association: db.Chunk.Translation
},
{
association: db.Chunk.Chapter,
2021-12-23 00:47:06 +02:00
required: true,
2021-12-22 22:34:58 +02:00
include: [
{
association: db.Chapter.ArtifactVersion,
2021-12-22 22:34:58 +02:00
required: true,
include: [
{
association: db.ArtifactVersion.TranslationArtifactVersions,
2021-12-22 22:34:58 +02:00
required: true,
where: {
id: request.params.id
}
},
{
association: db.ArtifactVersion.Artifact,
2021-12-25 01:35:32 +02:00
include: [
{
association: db.Artifact.Dialect
2021-12-25 01:35:32 +02:00
}
]
2021-12-22 22:34:58 +02:00
}
]
}
]
}
]
2021-12-23 00:47:06 +02:00
}).then(chunks => {
if (chunks.length > 0) {
const chunk = chunks.find(chunk => chunk.index == Number(request.params.chunkIndex))
2021-12-27 00:14:48 +02:00
if (chunk) {
response.display("translate-chunk", {
user: request.user,
pageTitle: `Translate ${chunk.chapter.artifactVersion.artifact.name}`,
2021-12-27 00:14:48 +02:00
chunk: chunk,
previousChunk: chunks.find(chunk => chunk.index == previousIndex),
nextChunk: chunks.find(chunk => chunk.index == nextIndex)
})
} else {
response.redirect(`/translate/${request.params.id}/chapter/${request.params.chapterId}`)
}
2021-12-22 22:34:58 +02:00
} else {
next()
}
}).catch(error => {
next(error)
})
})
2021-12-25 02:03:56 +02:00
.post(function(request, response, next) {
db.Chunk.findOne({
where: {
chapterId: request.params.chapterId,
index: request.params.chunkIndex
},
include: [
{
association: db.Chunk.Translation
},
{
association: db.Chunk.Chapter,
required: true,
include: [
{
association: db.Chapter.ArtifactVersion,
2021-12-25 02:03:56 +02:00
required: true,
include: [
{
association: db.ArtifactVersion.TranslationArtifactVersions,
2021-12-25 02:03:56 +02:00
required: true,
where: {
id: request.params.id
}
},
{
association: db.ArtifactVersion.Artifact,
2021-12-25 02:03:56 +02:00
include: [
{
association: db.Artifact.Dialect
2021-12-25 02:03:56 +02:00
}
]
}
]
}
]
}
]
}).then(async (chunk) => {
if (chunk) {
if (chunk.translation) {
chunk.translation.text = request.body.translatedText
await chunk.translation.save()
} else {
translation = await db.Translation.create({
chunkId: chunk.id,
text: request.body.translatedText,
translationArtifactVersionId: request.params.id
2021-12-25 02:03:56 +02:00
})
}
2021-12-27 00:14:48 +02:00
response.redirect(`/translate/${request.params.id}/chapter/${request.params.chapterId}/t/${Number(request.params.chunkIndex)+1}`)
2021-12-25 02:03:56 +02:00
} else {
next()
}
}).catch(error => {
next(error)
})
})
2021-12-27 00:00:36 +02:00
router.route('/:id/back')
.post(function(request, response, next) {
db.TranslationArtifactVersion.findByPk(request.params.id, {
2021-12-27 00:00:36 +02:00
include: [
{
association: db.TranslationArtifactVersion.ArtifactVersion,
2021-12-27 00:00:36 +02:00
required: true,
include: [
{
association: db.ArtifactVersion.Chapters,
2021-12-27 00:00:36 +02:00
required: true,
include: [
{
association: db.Chapter.Chunks,
required: true,
include: [
{
association: db.Chunk.Translation,
required: true,
// TODO: Where text is not null
}
]
}
]
},
{
association: db.ArtifactVersion.Artifact,
2021-12-27 00:00:36 +02:00
include: [
{
association: db.Artifact.Dialect
2021-12-27 00:00:36 +02:00
}
]
}
]
}
]
}).then((translationArtifactVersion) => {
if (translationArtifactVersion) {
2021-12-27 00:00:36 +02:00
// TODO: Create a backTranslation...
backTranslatedArtifact = {}
2021-12-27 00:00:36 +02:00
return db.BackTranslation.create({
translationArtifactVersionId: translationArtifactVersion.id,
artifact: {
name: translationArtifactVersion.name + translationArtifactVersion.artifactVersion.artifact.name,
2021-12-27 00:00:36 +02:00
url: `/translate/${request.params.id}`,
dialectId: translationArtifactVersion.dialectId,
2021-12-27 00:00:36 +02:00
licenseId: "copyright", // Should be closed because it's a back translation not meant to be published
artifactVersions: [
2021-12-27 00:00:36 +02:00
{
tag: translationArtifactVersion.artifactVersion.tag,
chapters: translationArtifactVersion.artifactVersion.chapters.map(chapter => {
2021-12-27 00:00:36 +02:00
return {
name: chapter.chunks[0].translation.text, // TODO: This isn't important...
chunks: chapter.chunks.map(chunk => {
return {
text: chunk.translation.text,
index: chunk.index
}
})
}
}),
translationArtifactVersions: [
2021-12-27 00:00:36 +02:00
{
dialectId: translationArtifactVersion.artifactVersion.artifact.dialect.id,
name: translationArtifactVersion.artifactVersion.artifact.dialect.name
2021-12-27 00:00:36 +02:00
}
]
}
]
}
}, {
include: [
{
association: db.BackTranslation.Artifact,
2021-12-27 00:00:36 +02:00
include: [
{
association: db.Artifact.ArtifactVersions,
2021-12-27 00:00:36 +02:00
include: [
{
association: db.ArtifactVersion.Chapters,
2021-12-27 00:00:36 +02:00
include: [
{
association: db.Chapter.Chunks
}
]
},
{
association: db.ArtifactVersion.TranslationArtifactVersions
2021-12-27 00:00:36 +02:00
}
]
}
]
}
]
})
} else {
return null
}
}).then(backTranslation => {
if (backTranslation) {
response.redirect(`/library/${backTranslation.artifactId}`)
2021-12-27 00:00:36 +02:00
} else {
// TODO: Let user know we cannot back translate
response.redirect(`/translate/${request.params.id}`)
}
}).catch(error => {
next(error)
})
})
2021-12-27 02:25:03 +02:00
router.route('/:id/campaign')
.get(function(request, response, next) {
db.TranslationArtifactVersion.findByPk(request.params.id, {
2021-12-27 02:25:03 +02:00
include: [
{
association: db.TranslationArtifactVersion.TranslationArtifactVersionCampaigns,
2021-12-27 02:25:03 +02:00
required: true,
include: [
{
association: db.TranslationArtifactVersionCampaign.Campaign,
2021-12-27 02:25:03 +02:00
required: true
}
]
}
]
}).then(translationArtifactVersion => {
if (translationArtifactVersion) {
if (translationArtifactVersion.translationArtifactVersionCampaings.length == 1) {
response.redirect(`/campaigns/${translationArtifactVersion.translationArtifactVersionCampaings[0].campaignId}`)
2021-12-27 02:25:03 +02:00
} else {
// TODO: Display translation artifact campaign...
2021-12-27 02:25:03 +02:00
next()
}
} else {
response.redirect(`/translate/${request.params.id}/campaign/create`)
}
})
})
router.route('/:id/campaign/create')
.get(function(request, response, next) {
db.TranslationArtifactVersion.findByPk(request.params.id, {
2021-12-27 02:25:03 +02:00
include: [
{
association: db.TranslationArtifactVersion.ArtifactVersion,
2021-12-27 02:25:03 +02:00
include: [
{
association: db.ArtifactVersion.Artifact
2021-12-27 02:25:03 +02:00
}
]
},
{
association: db.TranslationArtifactVersion.TranslationArtifactVersionCampaigns,
2021-12-27 02:25:03 +02:00
required: false,
include: [
{
association: db.TranslationArtifactVersionCampaign.Campaign,
2021-12-27 02:25:03 +02:00
required: false
}
]
}
]
}).then(translationArtifactVersion => {
if (translationArtifactVersion) {
2021-12-27 02:25:03 +02:00
response.display("campaign-form", {
user: request.user,
pageTitle: "Campaign - Mantra",
campaign: {
name: `Campaign for ${translationArtifactVersion.name} translation of ${translationArtifactVersion.artifactVersion.artifact.name}`
2021-12-27 02:25:03 +02:00
},
translationArtifactVersions: [translationArtifactVersion]
2021-12-27 02:25:03 +02:00
})
} else {
next()
}
}).catch(error => {
next(error)
})
})
return router;
};