Skip to content

Commit

Permalink
Merge branch 'c3' into matias
Browse files Browse the repository at this point in the history
  • Loading branch information
MatiasBais committed Apr 8, 2024
2 parents d9abd4f + 8915daf commit 0dc20ae
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 112 deletions.
3 changes: 2 additions & 1 deletion api/v1/ia.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ async function moderar(post) {
try {
const response = await fetch(`${base_url}/chat/completions`, requestOptions);
const responseData = await response.json();
return JSON.parse(responseData.choices[0].message.content);
const contenido=responseData.choices[0].message.content;
return JSON.parse(contenido);
} catch (error) {
throw error;
}
Expand Down
4 changes: 4 additions & 0 deletions api/v1/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ router.get('/reporte', function (req, res) {
attributes: ['cuerpo', 'fecha'],
required: true,
as: 'reportado',
// TODO Refactor: DRY sobre las cuestiones de eliminar cosas
where:{
eliminadorDNI:null
},
include: [
{
model: Usuario
Expand Down
117 changes: 52 additions & 65 deletions api/v1/pregunta.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ router.get("/", (req, res) => {
});
});

function editarPregunta(req, res, respuestaIA = null) {
}

router.patch("/", function (req, res) {
Pregunta.findByPk(req.body.ID, {
include: [
Expand All @@ -61,60 +64,22 @@ router.patch("/", function (req, res) {
} else {
// TODO Refactor: DRY en este if
let modera = getModera();
if (modera == 1) {
moderarWithRetry(req.body.titulo + " " + req.body.cuerpo, 50).then(
(respuesta) => {
let esperarA = []
let rechazaPost = getRechazaPost();
let reportaPost = getReportaPost();
if (respuesta.apropiado < rechazaPost) {
res
.status(400)
.send("Texto rechazo por moderación automática. Razón: " + respuesta.motivo);
return;
} else if (respuesta.apropiado < reportaPost) {
//Crear reporte
esperarA.push(ReportePost.create({
tipoID: 1,
reportadoID: pregunta.ID,
}))

}
//si pasa el filtro
pregunta.post.cuerpo = req.body.cuerpo;
pregunta.titulo = req.body.titulo;
const etiquetasIDs = Array.isArray(req.body.etiquetas) ? req.body.etiquetas : [req.body.etiquetas];
// !no se porque pero asi anda pregunta.save() no
esperarA.push(
pregunta.post.save()
.then(() =>
pregunta.setEtiquetas([])
)
.then(pre => pre.save())
.then(() =>
Promise.all(etiquetasIDs.map(
(ID) => EtiquetasPregunta.create({ etiquetumID: ID, preguntumID: pregunta.post.ID })
))
)
// .then(ep => pregunta.setEtiquetas(req.body.etiquetas.map(
// (ID) =>({ preguntumID : pregunta.post.ID , etiquetumID: ID })
// )))
)
Promise.all(esperarA)
.then(() =>
res.status(200).send(req.body.ID + "")
)

}
);
} else {
let esperarA = []
let esperarA = []
const editarPregunta = (motivo = null) => {
pregunta.post.cuerpo = req.body.cuerpo;
pregunta.titulo = req.body.titulo;


const etiquetas = Array.isArray(req.body.etiquetas) ? req.body.etiquetas : [req.body.etiquetas];
// !no se porque pero asi anda pregunta.save() no
//TODO Refactor: Usar setEtiquetas. etiquetas vienen los id en array
// pregunta.setEtiquetas(
// req.body.etiquetasIDs.map(
// (ID) => new EtiquetasPregunta({ etiquetumID: ID })
// )
// );
// .then(ep => pregunta.setEtiquetas(req.body.etiquetas.map(
// (ID) =>({ preguntumID : pregunta.post.ID , etiquetumID: ID })
// )))
esperarA.push(
pregunta.post.save()
.then(() =>
Expand All @@ -126,24 +91,40 @@ router.patch("/", function (req, res) {
(ID) => EtiquetasPregunta.create({ etiquetumID: ID, preguntumID: pregunta.post.ID })
))
)
// .then(ep => pregunta.setEtiquetas(req.body.etiquetas.map(
// (ID) =>({ preguntumID : pregunta.post.ID , etiquetumID: ID })
// )))
)
Promise.all(esperarA)
.then(() =>
res.status(200).send(req.body.ID + "")
res.status(200).json({ ID: req.body.ID, motivo })
)
//etiquetas vienen los id en array
// pregunta.setEtiquetas(
// req.body.etiquetasIDs.map(
// (ID) => new EtiquetasPregunta({ etiquetumID: ID })
// )
// );
//no se porque pero asi anda pregunta.save() no
// pregunta.post.save();
// res.status(200).send("Pregunta actualizada exitosamente");
}

if (modera == 1) {
moderarWithRetry(req.body.titulo + " " + req.body.cuerpo, 50).then(
(respuesta) => {
let rechazaPost = getRechazaPost();
if (respuesta.apropiado < rechazaPost) {
res
.status(400)
.send("Texto rechazo por moderación automática. Razón: " + respuesta.motivo);
return;
}

if (respuesta.apropiado < getReportaPost()) {
// * Crear reporte
esperarA.push(ReportePost.create({
tipoID: 1,
reportadoID: req.body.ID
}))

editarPregunta(respuesta.motivo);
return;
}

// TODO Refactor: DRY. No se si es posible igual, dejar comentario en todo caso.
editarPregunta();
}
);
} else editarPregunta();
}
}
})
Expand Down Expand Up @@ -263,9 +244,10 @@ router.put('/:ID', function (req, res) {
{
model: Post, include: [
{ model: Usuario, as: 'eliminador' }
, { model: ReportePost, include: { model: TipoReporte, as: 'tipo', where: { ID: 2 }, attributes: [] } } // ! 2 es pregunta repetida
// ,{model:ReportePost,include:{model:TipoReporte, as:'tipo',attributes:[]}}
]
},
{ model: SuscripcionesPregunta, as: 'suscripciones' },
{ model: Respuesta, as: 'respuestas', include: Post },
// TODO Refactor: Algún ENUM de tipos de reportes
]
Expand All @@ -278,15 +260,20 @@ router.put('/:ID', function (req, res) {
}

// TODO Refactor: Ver si es posible simplificar
let esperarA = [], preguntaReemplazoID = req.body.duplicadaID;
let esperarA = []
, preguntaReemplazoID = req.body.duplicadaID;

if (pre.respuestas.length) {
esperarA.push(...pre.respuestas.map(resp => resp.setPregunta(preguntaReemplazoID).then(r => r.save())));
}

esperarA.push(pre.post.setEliminador(usuarioActual.DNI).then(p => p.save()));

esperarA.push(pre.post.getReportePosts().then(reportes => Promise.all(reportes.map(reporte => reporte.destroy()))));
// TODO Feature: Test this.
esperarA.push(pre.getSuscripciones().then(suscripciones => Promise.all(suscripciones.map(suscripcion => suscripcion.setPregunta(preguntaReemplazoID).then(s => s.save())))));

// TODO Feature: ¿Pasar reportes de otras índoles?
// esperarA.push(pre.post.getReportePosts().then(reportes=>Promise.all(reportes.map(reporte => reporte.tipoID==2?/* ! 2 es pregunta repetida */reporte.destroy()))));

Promise.all(esperarA).then(() => {
res.send();
Expand Down
13 changes: 9 additions & 4 deletions api/v1/respuesta.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ router.patch("/", function (req, res) {
res.status(403).send(mensajeError403);
return;
} else {
const editarRespuesta = () => {
const editarRespuesta = (motivo=null) => {
respuesta.post.cuerpo = req.body.cuerpo;
respuesta.post.save();
res.status(200).send(req.body.IDPregunta + "");
res.status(200).json({ID:req.body.IDPregunta,motivo});
}

let modera = getModera();
Expand All @@ -129,18 +129,23 @@ router.patch("/", function (req, res) {
res.status(400).send("Texto rechazo por moderación automática. Razón: " + respuesta.motivo);
return;
}

if (resp.apropiado < reportaPost) {
//Crear reporte
// * Crear reporte
ReportePost.create({
tipoID: 1,
reportadoID: respuesta.ID,
});

editarRespuesta(resp.motivo);
}

editarRespuesta();
});
} else {
}else{
editarRespuesta();
}

}
}
})
Expand Down
32 changes: 19 additions & 13 deletions frontend/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,20 +136,25 @@ router.get("/pregunta/:id?", async (req, res) => {
model: EtiquetaDAO,
include: { model: Categoria, as: "categoria" },
}
},
// TODO Refactor: Agregar la condición de suscripciones solo si req.session.usuario.DNI está definido. No hace falta traer todas, sino solo la que nos interesa. Ver voto como ejemplo.
{
model: UsuarioDAO
, as: 'usuariosSuscriptos',
through: {
model: SuscripcionesPreguntaDAO,
where: {
fecha_baja: null // Condición para que la fecha de baja sea nula
}
}
}
];

// TODO Refactor: Evitar chequear antes y después.
if(req.session.usuario){
include.push(
// TODO Refactor: DRY, esto se repite en Pregunta.pagina
{
model:SuscripcionesPreguntaDAO
,as:'suscripciones'
, include: { model: UsuarioDAO, as: 'suscripto', where: { DNI: req.session.usuario.DNI }, attributes: [] }
, where: {
fecha_baja: null // * Vigentes
}
,required:false
}
)
}

const p = await PreguntaDAO.findByPk(req.params.id, { include });

if (!p) {
Expand All @@ -162,6 +167,7 @@ router.get("/pregunta/:id?", async (req, res) => {
res.redirect('/');
return;
}

NotificacionDAO.findAll({
include: [
{
Expand Down Expand Up @@ -189,14 +195,14 @@ router.get("/pregunta/:id?", async (req, res) => {


} else if (p.post.eliminadorDNI) {
// No está logueado y la pregunta esta eliminada
// * No está logueado y la pregunta esta eliminada
res.redirect('/');
return;
}

// ! No se puede traer votos Y un resumen, por eso lo calculamos acá. Los votos los traemos solo para ver si el usuario actual votó.

//Ordenar respuestas por valoracion
// * Ordenar respuestas por valoracion
function calculateSumValoracion(respuesta) {
return respuesta.post.votos.reduce(
(total, voto) => total + voto.valoracion,
Expand Down
34 changes: 16 additions & 18 deletions frontend/static/pantallas/editar-pregunta.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,27 @@ function crearPagina(ruta,sesion, pregunta, categorias){
'editando-pregunta'
,'/api/pregunta'
,[
// TODO Refactor: Mandar patch a /pregunta/:ID, no ?ID=
{name:'ID',textoEtiqueta:'ID',value:pregunta.ID, type:'hidden'}
,{name:'titulo',textoEtiqueta:'Título',value:pregunta.titulo}
,{name:'cuerpo',textoEtiqueta:'Detalles',type:'textarea',value:pregunta.cuerpo}
// TODO refactor: llevar el map a Formulario y mandar extra: categorias
,{name:'etiquetas',textoEtiqueta:'Etiquetas',type:'lista-etiquetas',value:pregunta.etiquetas , extra:categorias.map(cat => cat.etiquetas.map(eti => `<option value=${eti.ID} data-color="${cat.color}" data-categoria="${cat.descripcion}" ${pregunta.etiquetas.some(({etiquetum: {ID}})=>ID==eti.ID) ? 'selected' : ''}>${cat.descripcion} - ${eti.descripcion}</option>`)).flat().join('')}
]
,(res)=>{
console.log(res)
setTimeout(function() {
window.location.replace('/pregunta/'+res);
}, 1000);

,(res,{ok,codigo})=>{
if(ok){
// ! No se puede acceder a las variables de alrededor.
respuesta=JSON.parse(res);
const preguntaID=+respuesta.ID
const recargar=()=>window.location.replace('/pregunta/'+preguntaID);
// TODO Refactor: trim??
if(respuesta.motivo){
Swal.redirigirEn(10,`La edición se va a publicar, pero fue automáticamente reportada por el siguiente motivo:<br><br><i>${respuesta.motivo}</i>`)
.then(recargar);
}else recargar();
}else{
Swal.error(`Error ${codigo}: ${res}`);
}
}
,{
textoEnviar:'Editar Pregunta', verbo: 'PATCH', clasesBoton:'is-link is-rounded mt-3'
Expand All @@ -32,15 +41,4 @@ function crearPagina(ruta,sesion, pregunta, categorias){
return pagina;
}

export {crearPagina as PantallaEditarPregunta};
// for(let eti of cat.etiquetas){
// optionsEtiquetas.push(['OPTION',{
// value:eti.ID
// ,dataset:{
// categoriaID:cat.ID
// }
// ,innerText:`${cat.descripcion} - ${eti.descripcion}`
// }]);

// htmlStyle+=`, .tag.is-rounded[data-value="${eti.ID}"]`;
// }
export {crearPagina as PantallaEditarPregunta};
24 changes: 17 additions & 7 deletions frontend/static/pantallas/editar-respuesta.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,26 @@ function crearPagina(ruta, sesion, respuesta) {
, '/api/respuesta/'
, [
{ name: 'ID', textoEtiqueta: 'ID', value: respuesta.ID, type: 'hidden' },
// TODO Refactor: ¿Por qué respuesta.pregunta puede no tener ID?? No tiene sentido según el dominio.
// TODO Refactor: Also, en algún punto de la vida, quizá meter los endpoints de respuestas en los de preguntas. Ejemplo: POST /api/pregunta/:ID/respuesta
{ name: 'IDPregunta', textoEtiqueta: 'ID', value: respuesta.pregunta?.ID, type: 'hidden' }
, { name: 'cuerpo', textoEtiqueta: 'Respuesta', type: 'textarea', value: respuesta.cuerpo }
]
, (res) => {
console.log(res)
setTimeout(function () {
// TODO refactor: redirigir a pregunta asociada a esa respuesta
window.location.replace('/pregunta/' + res);
}, 1000);

, (res,{ok,codigo}) => {
// TODO Refactor: DRY en esto al editar o crear pregunta o respuesta.
if(ok){
// ! No se puede acceder a las variables de alrededor.
respuesta=JSON.parse(res);
const preguntaID=+respuesta.ID
const recargar=()=>window.location.replace('/pregunta/'+preguntaID);
// TODO Refactor: trim??
if(respuesta.motivo){
Swal.redirigirEn(10,`La edición se va a publicar, pero fue automáticamente reportada por el siguiente motivo:<br><br><i>${respuesta.motivo}</i>`)
.then(recargar);
}else recargar();
}else{
Swal.error(`Error ${codigo}: ${res}`);
}
}
, {
textoEnviar: 'Editar Respuesta', verbo: 'PATCH', clasesBoton: 'is-link is-rounded mt-3'
Expand Down
2 changes: 1 addition & 1 deletion frontend/static/pantallas/nueva-pregunta.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function crearPagina(ruta,sesion,categorias){
const preguntaID=+respuesta.ID
const irAPregunta=()=>window.location.replace('/pregunta/'+preguntaID);
if(respuesta.motivo){
Swal.redirigirEn(10,`La respuesta se va a publicar, pero fue automáticamente reportada por el siguiente motivo:<br><br><i>${respuesta.motivo}</i>`)
Swal.redirigirEn(10,`La pregunta se va a publicar, pero fue automáticamente reportada por el siguiente motivo:<br><br><i>${respuesta.motivo}</i>`)
.then(irAPregunta);
}else irAPregunta();
}else Swal.error(`Error ${info.codigo}: ${respuesta}`);
Expand Down
2 changes: 1 addition & 1 deletion frontend/static/pantallas/pregunta.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function PaginaPregunta(ruta, sesion, pregunta){
if(ok){
const reload=()=>window.location.reload();
if(respuesta.motivo){
Swal.redirigirEn(10,`La pregunta se va a publicar, pero fue automáticamente reportada por el siguiente motivo:<br><br><i>${respuesta.motivo}</i>`)
Swal.redirigirEn(10,`La respuesta se va a publicar, pero fue automáticamente reportada por el siguiente motivo:<br><br><i>${respuesta.motivo}</i>`)
.then(reload);
}else reload();
}else{
Expand Down
Loading

0 comments on commit 0dc20ae

Please sign in to comment.