-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a82e074
commit 21bd8a7
Showing
12 changed files
with
237 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const { response } = require('express'); | ||
const Mensaje = require('../models/mensaje'); | ||
|
||
|
||
const obtenerChat = async (request, response = response) => { | ||
|
||
const miId = request.uid; | ||
const mensajeDe = request.params.de; | ||
|
||
|
||
const last30 = await Mensaje.find({ | ||
$or: [{de: miId, para: mensajeDe}, {de: mensajeDe, para: miId}] | ||
}) | ||
.sort({createdAt: 'desc'}) | ||
.limit(30); | ||
|
||
|
||
return response.json({ | ||
ok: true, | ||
mensajes: last30 | ||
}); | ||
|
||
} | ||
|
||
|
||
module.exports = { | ||
obtenerChat | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
const Usuario = require('../models/usuario'); | ||
const Mensaje = require('../models/mensaje'); | ||
|
||
|
||
const usuarioConectado = (uid = '') => { | ||
return usuarioEstado(uid, true); | ||
} | ||
|
||
const usuarioDesconectado = (uid = '') => { | ||
return usuarioEstado(uid); | ||
} | ||
|
||
const usuarioEstado = async ( uid = '', estado = false) => { | ||
const usuario = await Usuario.findById(uid); | ||
|
||
usuario.online = estado; | ||
await usuario.save(); | ||
|
||
return usuario; | ||
} | ||
|
||
const grabarMensaje = async (payload) => { | ||
/* | ||
payload = { | ||
de: '', | ||
para: '', | ||
mensaje: '', | ||
} | ||
*/ | ||
try { | ||
const mensaje = new Mensaje(payload); | ||
await mensaje.save(); | ||
return true; | ||
} catch (error) { | ||
return false; | ||
} | ||
} | ||
|
||
|
||
|
||
module.exports = { | ||
usuarioConectado, | ||
usuarioDesconectado, | ||
grabarMensaje | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const { response } = require('express'); | ||
const Usuario = require('../models/usuario'); | ||
|
||
|
||
const getUsuarios = async (request, response = response) => { | ||
|
||
// console.log(request); | ||
|
||
// se agrega en la url mediante ?desde=12 | ||
const desde = Number( request.query.desde ) || 0; | ||
|
||
|
||
const usuarios = await Usuario | ||
.find({ _id: { $ne: request.uid } }) | ||
.sort('-online') | ||
.skip(desde) | ||
.limit(20) | ||
; | ||
|
||
return response.json({ | ||
ok: true, | ||
usuarios, | ||
desde | ||
}); | ||
|
||
} | ||
|
||
|
||
module.exports = { | ||
getUsuarios | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const { Schema, model } = require('mongoose'); | ||
|
||
const MensajeSchema = Schema({ | ||
de: { | ||
type: Schema.Types.ObjectId, // existe en las coleccion de datos | ||
ref: 'Usuario', | ||
required: true, | ||
}, | ||
para: { | ||
type: Schema.Types.ObjectId, // existe en las coleccion de datos | ||
ref: 'Usuario', | ||
required: true, | ||
}, | ||
mensaje: { | ||
type: String, | ||
required: true | ||
}, | ||
|
||
}, { | ||
timestamps: true | ||
}); | ||
|
||
MensajeSchema.method('toJSON', function() { | ||
const { __v, _id, ...object } = this.toObject(); | ||
object.uid = _id; | ||
return object; | ||
}); | ||
|
||
|
||
// por defecto mongoose agrega una s al final de cada modelo creado | ||
// por eso solo se define el modelo como Mensaje | ||
// pero en la base de datos se creara el modelo Mensajes | ||
module.exports = model('Mensaje', MensajeSchema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// path: api/mensajes | ||
|
||
const {Router} = require('express'); | ||
|
||
const { validarJWT } = require('../middlewares/validar-jwt'); | ||
const { obtenerChat } = require('../controller/mensajes'); | ||
|
||
const router = Router(); | ||
|
||
|
||
router.get('/:de', [validarJWT], obtenerChat); | ||
|
||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// path: api/usuarios | ||
|
||
const {Router} = require('express'); | ||
|
||
const { getUsuarios } = require('../controller/usuarios'); | ||
const { validarJWT } = require('../middlewares/validar-jwt'); | ||
|
||
const router = Router(); | ||
|
||
|
||
router.get('/', [validarJWT], getUsuarios); | ||
|
||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters