617 lines
27 KiB
JavaScript
617 lines
27 KiB
JavaScript
const express = require('express')
|
|
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.TranslationChapters,
|
|
limit: 1,
|
|
required: true
|
|
}
|
|
// {
|
|
// 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.translationChapters[0].id}`)
|
|
} else {
|
|
next()
|
|
}
|
|
})
|
|
})
|
|
|
|
router.route('/:id/chapter/:chapterId')
|
|
.get(function(request, response, next) {
|
|
db.TranslationChapter.findByPk(request.params.chapterId, {
|
|
include: [
|
|
{
|
|
association: db.TranslationChapter.TranslationChunks,
|
|
include: [
|
|
{
|
|
association: db.TranslationChunk.Translation
|
|
}
|
|
]
|
|
},
|
|
{
|
|
association: db.TranslationChapter.TranslationArtifactVersion,
|
|
include: [
|
|
{
|
|
association: db.TranslationArtifactVersion.ArtifactVersion,
|
|
include: [
|
|
{
|
|
association: db.ArtifactVersion.Artifact,
|
|
include: [
|
|
{
|
|
association: db.Artifact.Dialect
|
|
}
|
|
]
|
|
}
|
|
]
|
|
}
|
|
]
|
|
}
|
|
]
|
|
}).then(translationChapter => {
|
|
if (translationChapter) {
|
|
response.display("translate-chapter", {
|
|
user: request.user,
|
|
pageTitle: `Translate Chapter ${translationChapter.name}`,
|
|
translationChapter: translationChapter
|
|
})
|
|
} else {
|
|
next()
|
|
}
|
|
}).catch(error => {
|
|
next(error)
|
|
})
|
|
})
|
|
|
|
router.route('/:id/chapter/:chapterId/view')
|
|
.get(function(request, response, next) {
|
|
db.TranslationChapter.findByPk(request.params.chapterId, {
|
|
include: [
|
|
{
|
|
association: db.TranslationChapter.TranslationChunks,
|
|
include: [
|
|
{
|
|
association: db.TranslationChunk.Translation
|
|
}
|
|
]
|
|
},
|
|
{
|
|
association: db.TranslationChapter.TranslationArtifactVersion,
|
|
include: [
|
|
{
|
|
association: db.TranslationArtifactVersion.ArtifactVersion,
|
|
include: [
|
|
{
|
|
association: db.ArtifactVersion.Artifact,
|
|
include: [
|
|
{
|
|
association: db.Artifact.Dialect
|
|
}
|
|
]
|
|
}
|
|
]
|
|
}
|
|
]
|
|
}
|
|
]
|
|
}).then(translationChapter => {
|
|
if (translationChapter) {
|
|
response.display("translate-chapter-view", {
|
|
user: request.user,
|
|
pageTitle: `Translate Chapter ${translationChapter.name}`,
|
|
translationChapter: translationChapter
|
|
})
|
|
} else {
|
|
next()
|
|
}
|
|
}).catch(error => {
|
|
next(error)
|
|
})
|
|
})
|
|
|
|
router.route('/:id/chapter/:chapterId/t/:chunkIndex')
|
|
.get(function(request, response, next) {
|
|
const previousIndex = Number(request.params.chunkIndex)-1
|
|
const nextIndex = Number(request.params.chunkIndex)+1
|
|
db.TranslationChunk.findAll({
|
|
where: {
|
|
translationChapterId: request.params.chapterId,
|
|
index: {
|
|
[Op.between]: [
|
|
previousIndex,
|
|
nextIndex
|
|
],
|
|
}
|
|
},
|
|
limit: 3,
|
|
include: [
|
|
{
|
|
association: db.TranslationChunk.Translation
|
|
},
|
|
{
|
|
association: db.TranslationChunk.TranslationChapter,
|
|
required: true,
|
|
include: [
|
|
{
|
|
association: db.TranslationChapter.TranslationArtifactVersion,
|
|
required: true,
|
|
include: [
|
|
{
|
|
association: db.TranslationArtifactVersion.ArtifactVersion,
|
|
include: [
|
|
{
|
|
association: db.ArtifactVersion.Artifact,
|
|
include: [
|
|
{
|
|
association: db.Artifact.Dialect
|
|
}
|
|
]
|
|
}
|
|
]
|
|
}
|
|
]
|
|
}
|
|
]
|
|
}
|
|
]
|
|
}).then(translationChunks => {
|
|
if (translationChunks.length > 0) {
|
|
const translationChunk = translationChunks.find(translationChunk => translationChunk.index == Number(request.params.chunkIndex))
|
|
if (translationChunk) {
|
|
response.display("translate-chunk", {
|
|
user: request.user,
|
|
pageTitle: `Translate ${translationChunk.translationChapter.translationArtifactVersion.artifactVersion.artifact.name}`,
|
|
translationChunk: translationChunk,
|
|
previousTranslationChunk: translationChunks.find(translationChunk => translationChunk.index == previousIndex),
|
|
nextTranslationChunk: translationChunks.find(translationChunk => translationChunk.index == nextIndex)
|
|
})
|
|
} else {
|
|
response.redirect(`/translate/${request.params.id}/chapter/${request.params.chapterId}`)
|
|
}
|
|
|
|
} else {
|
|
next()
|
|
}
|
|
}).catch(error => {
|
|
next(error)
|
|
})
|
|
})
|
|
.post(function(request, response, next) {
|
|
db.TranslationChunk.findOne({
|
|
where: {
|
|
translationChapterId: request.params.chapterId,
|
|
index: request.params.chunkIndex
|
|
},
|
|
include: [
|
|
{
|
|
association: db.TranslationChunk.Translation
|
|
},
|
|
{
|
|
association: db.TranslationChunk.TranslationChapter,
|
|
required: true,
|
|
include: [
|
|
{
|
|
association: db.TranslationChapter.TranslationArtifactVersion,
|
|
|
|
include: [
|
|
{
|
|
association: db.TranslationArtifactVersion.ArtifactVersion,
|
|
include: [
|
|
{
|
|
association: db.ArtifactVersion.Artifact,
|
|
include: [
|
|
{
|
|
association: db.Artifact.Dialect
|
|
}
|
|
]
|
|
}
|
|
]
|
|
}
|
|
]
|
|
}
|
|
]
|
|
}
|
|
]
|
|
}).then(async (translationChunk) => {
|
|
if (translationChunk) {
|
|
if (translationChunk.translation) {
|
|
translationChunk.translation.text = request.body.translatedText
|
|
await translationChunk.translation.save()
|
|
} else {
|
|
translation = await db.Translation.create({
|
|
creatorId: request.user.id,
|
|
translationChunkId: translationChunk.id,
|
|
text: request.body.translatedText,
|
|
translationArtifactVersionId: request.params.id
|
|
})
|
|
}
|
|
response.redirect(`/translate/${request.params.id}/chapter/${request.params.chapterId}/t/${Number(request.params.chunkIndex)+1}`)
|
|
} else {
|
|
next()
|
|
}
|
|
}).catch(error => {
|
|
next(error)
|
|
})
|
|
})
|
|
|
|
router.route('/:id/back')
|
|
.post(function(request, response, next) {
|
|
db.TranslationArtifactVersion.findByPk(request.params.id, {
|
|
include: [
|
|
{
|
|
association: db.TranslationArtifactVersion.ArtifactVersion,
|
|
required: true,
|
|
include: [
|
|
{
|
|
association: db.ArtifactVersion.Chapters,
|
|
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,
|
|
include: [
|
|
{
|
|
association: db.Artifact.Dialect
|
|
}
|
|
]
|
|
}
|
|
]
|
|
}
|
|
]
|
|
}).then((translationArtifactVersion) => {
|
|
if (translationArtifactVersion) {
|
|
// TODO: Create a backTranslation...
|
|
backTranslatedArtifact = {}
|
|
return db.BackTranslation.create({
|
|
creatorId: request.user.id,
|
|
translationArtifactVersionId: translationArtifactVersion.id,
|
|
artifact: {
|
|
creatorId: request.user.id,
|
|
name: translationArtifactVersion.name + translationArtifactVersion.artifactVersion.artifact.name,
|
|
url: `/translate/${request.params.id}`,
|
|
dialectId: translationArtifactVersion.dialectId,
|
|
licenseId: "copyright", // Should be closed because it's a back translation not meant to be published
|
|
artifactVersions: [
|
|
{
|
|
tag: translationArtifactVersion.artifactVersion.tag,
|
|
chapters: translationArtifactVersion.artifactVersion.chapters.map(chapter => {
|
|
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: [
|
|
{
|
|
dialectId: translationArtifactVersion.artifactVersion.artifact.dialect.id,
|
|
name: translationArtifactVersion.artifactVersion.artifact.dialect.name
|
|
}
|
|
]
|
|
}
|
|
]
|
|
}
|
|
}, {
|
|
include: [
|
|
{
|
|
association: db.BackTranslation.Artifact,
|
|
include: [
|
|
{
|
|
association: db.Artifact.ArtifactVersions,
|
|
include: [
|
|
{
|
|
association: db.ArtifactVersion.Chapters,
|
|
include: [
|
|
{
|
|
association: db.Chapter.Chunks
|
|
}
|
|
]
|
|
},
|
|
{
|
|
association: db.ArtifactVersion.TranslationArtifactVersions
|
|
}
|
|
]
|
|
}
|
|
]
|
|
}
|
|
]
|
|
})
|
|
} else {
|
|
return null
|
|
}
|
|
}).then(backTranslation => {
|
|
if (backTranslation) {
|
|
response.redirect(`/library/${backTranslation.artifactId}`)
|
|
} else {
|
|
// TODO: Let user know we cannot back translate
|
|
response.redirect(`/translate/${request.params.id}`)
|
|
}
|
|
}).catch(error => {
|
|
next(error)
|
|
})
|
|
})
|
|
|
|
router.route('/:id/campaigns')
|
|
.get(function(request, response, next) {
|
|
db.TranslationArtifactVersion.findByPk(request.params.id, {
|
|
include: [
|
|
{
|
|
association: db.TranslationArtifactVersion.TranslationArtifactVersionCampaigns,
|
|
required: true,
|
|
include: [
|
|
{
|
|
association: db.TranslationArtifactVersionCampaign.Campaign,
|
|
required: true
|
|
}
|
|
]
|
|
}
|
|
]
|
|
}).then(translationArtifactVersion => {
|
|
if (translationArtifactVersion) {
|
|
if (translationArtifactVersion.translationArtifactVersionCampaigns.length == 1) {
|
|
response.redirect(`/campaigns/${translationArtifactVersion.translationArtifactVersionCampaigns[0].campaignId}`)
|
|
} else {
|
|
// TODO: Display translation artifact campaign...
|
|
next()
|
|
}
|
|
} else {
|
|
response.redirect(`/translate/${request.params.id}/campaigns/create`)
|
|
}
|
|
})
|
|
})
|
|
|
|
router.route('/:id/campaigns/create')
|
|
.get(function(request, response, next) {
|
|
db.TranslationArtifactVersion.findByPk(request.params.id, {
|
|
include: [
|
|
{
|
|
association: db.TranslationArtifactVersion.ArtifactVersion,
|
|
include: [
|
|
{
|
|
association: db.ArtifactVersion.Artifact
|
|
}
|
|
]
|
|
},
|
|
{
|
|
association: db.TranslationArtifactVersion.TranslationArtifactVersionCampaigns,
|
|
required: false,
|
|
include: [
|
|
{
|
|
association: db.TranslationArtifactVersionCampaign.Campaign,
|
|
required: false
|
|
}
|
|
]
|
|
}
|
|
]
|
|
}).then(translationArtifactVersion => {
|
|
if (translationArtifactVersion) {
|
|
response.display("campaign-form", {
|
|
user: request.user,
|
|
pageTitle: "Campaign - Mantra",
|
|
campaign: {
|
|
name: `Campaign for ${translationArtifactVersion.name} translation of ${translationArtifactVersion.artifactVersion.artifact.name}`
|
|
},
|
|
translationArtifactVersions: [translationArtifactVersion]
|
|
})
|
|
} else {
|
|
next()
|
|
}
|
|
}).catch(error => {
|
|
next(error)
|
|
})
|
|
})
|
|
.post(function(request, response, next) {
|
|
db.TranslationArtifactVersion.findByPk(request.params.id, {
|
|
include: [
|
|
{
|
|
association: db.TranslationArtifactVersion.ArtifactVersion,
|
|
include: [
|
|
{
|
|
association: db.ArtifactVersion.Artifact
|
|
}
|
|
]
|
|
},
|
|
{
|
|
association: db.TranslationArtifactVersion.TranslationArtifactVersionCampaigns,
|
|
required: false,
|
|
include: [
|
|
{
|
|
association: db.TranslationArtifactVersionCampaign.Campaign,
|
|
required: false
|
|
}
|
|
]
|
|
}
|
|
]
|
|
}).then(async (translationArtifactVersion) => {
|
|
if (translationArtifactVersion) {
|
|
// TODO: Create campaign...
|
|
const campaign = await db.Campaign.create({
|
|
creatorId: request.user.id,
|
|
name: request.body.name,
|
|
description: request.body.description,
|
|
// defaultSatoshis: [request.body.satoshis].flat()[0],
|
|
defaultSatoshis: request.body.satoshis,
|
|
entityId: request.user.individualEntityUser.entityUser.entityId,
|
|
translationArtifactVersionCampaigns: [
|
|
{
|
|
creatorId: request.user.id,
|
|
translationArtifactVersionId: translationArtifactVersion.id,
|
|
entityId: request.user.individualEntityUser.entityUser.entityId,
|
|
satoshis: request.body.satoshis
|
|
}
|
|
]
|
|
}, {
|
|
include: [
|
|
{
|
|
association: db.Campaign.TranslationArtifactVersionCampaigns
|
|
}
|
|
]
|
|
})
|
|
|
|
response.redirect(`/campaigns/${campaign.id}`)
|
|
} else {
|
|
next()
|
|
}
|
|
}).catch(error => {
|
|
next(error)
|
|
})
|
|
})
|
|
|
|
router.route('/:id/pledges')
|
|
.get(function(request, response, next) {
|
|
db.TranslationArtifactVersion.findByPk(request.params.id, {
|
|
include: [
|
|
{
|
|
association: db.TranslationArtifactVersion.TranslationArtifactVersionPledges,
|
|
required: true,
|
|
include: [
|
|
{
|
|
association: db.TranslationArtifactVersionPledge.Pledge,
|
|
required: true
|
|
}
|
|
]
|
|
}
|
|
]
|
|
}).then(translationArtifactVersion => {
|
|
if (translationArtifactVersion) {
|
|
if (translationArtifactVersion.translationArtifactVersionPledges.length == 1) {
|
|
response.redirect(`/pledges/${translationArtifactVersion.translationArtifactVersionPledges[0].campaignId}`)
|
|
} else {
|
|
// TODO: Display translation artifact campaign...
|
|
next()
|
|
}
|
|
} else {
|
|
response.redirect(`/translate/${request.params.id}/pledges/create`)
|
|
}
|
|
})
|
|
})
|
|
|
|
router.route('/:id/pledges/create')
|
|
.get(function(request, response, next) {
|
|
db.TranslationArtifactVersion.findByPk(request.params.id, {
|
|
include: [
|
|
{
|
|
association: db.TranslationArtifactVersion.ArtifactVersion,
|
|
include: [
|
|
{
|
|
association: db.ArtifactVersion.Artifact
|
|
}
|
|
]
|
|
},
|
|
{
|
|
association: db.TranslationArtifactVersion.TranslationArtifactVersionPledges,
|
|
required: false,
|
|
include: [
|
|
{
|
|
association: db.TranslationArtifactVersionPledge.Pledge,
|
|
required: false
|
|
}
|
|
]
|
|
}
|
|
]
|
|
}).then(translationArtifactVersion => {
|
|
if (translationArtifactVersion) {
|
|
response.display("pledge-form", {
|
|
user: request.user,
|
|
pageTitle: "Pledge - Mantra",
|
|
campaign: {
|
|
name: `Pledge for ${translationArtifactVersion.name} translation of ${translationArtifactVersion.artifactVersion.artifact.name}`,
|
|
},
|
|
translationArtifactVersions: [translationArtifactVersion]
|
|
})
|
|
} else {
|
|
next()
|
|
}
|
|
}).catch(error => {
|
|
next(error)
|
|
})
|
|
})
|
|
.post(function(request, response, next) {
|
|
db.TranslationArtifactVersion.findByPk(request.params.id, {
|
|
include: [
|
|
{
|
|
association: db.TranslationArtifactVersion.ArtifactVersion,
|
|
include: [
|
|
{
|
|
association: db.ArtifactVersion.Artifact
|
|
}
|
|
]
|
|
},
|
|
{
|
|
association: db.TranslationArtifactVersion.TranslationArtifactVersionPledges,
|
|
required: false,
|
|
include: [
|
|
{
|
|
association: db.TranslationArtifactVersionPledge.Pledge,
|
|
required: false
|
|
}
|
|
]
|
|
}
|
|
]
|
|
}).then(async (translationArtifactVersion) => {
|
|
if (translationArtifactVersion) {
|
|
// TODO: Create campaign...
|
|
const pledge = await db.Pledge.create({
|
|
creatorId: request.user.id,
|
|
message: request.body.message,
|
|
entityId: request.user.individualEntityUser.entityUser.entityId,
|
|
translationArtifactVersionPledges: [
|
|
{
|
|
creatorId: request.user.id,
|
|
translationArtifactVersionId: translationArtifactVersion.id,
|
|
entityId: request.user.individualEntityUser.entityUser.entityId,
|
|
satoshis: request.body.satoshis
|
|
}
|
|
]
|
|
}, {
|
|
include: [
|
|
{
|
|
association: db.Pledge.TranslationArtifactVersionPledges
|
|
}
|
|
]
|
|
})
|
|
|
|
response.redirect(`/pledges/${pledge.id}`)
|
|
} else {
|
|
next()
|
|
}
|
|
}).catch(error => {
|
|
next(error)
|
|
})
|
|
})
|
|
return router;
|
|
}; |