-
Notifications
You must be signed in to change notification settings - Fork 0
/
fonctions.js
210 lines (208 loc) · 5.74 KB
/
fonctions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//const redis=require("redis");
//const client = redis.createClient();
const users=[];
const kick=[];
const mongoose=require("mongoose");
mongoose.connect('mongodb://localhost/my_database', {
useNewUrlParser: true,
useUnifiedTopology: true
});
mongoose.connection.once("open",()=>{
console.log("connection done")
}).on("error",(err)=>{
console.log(err)
})
var userSchema = mongoose.Schema({
_id: String,
username: String,
lastname: String,
email: String,
room: String,
img:String
});
var users2 = mongoose.model('user', userSchema);
const nodemailer=require("nodemailer");
const smtpTransport = require('nodemailer-smtp-transport');
let mysql=require('mysql');
const moment=require("moment")
let now=moment();
var dbconfig = require('./database');
const { assign } = require("nodemailer/lib/shared");
var connection = mysql.createConnection(dbconfig.connection);
connection.query('USE ' + dbconfig.database);
//sending email to a user
async function sendEmail(email,text){
let testAccount= await nodemailer.createTestAccount();
let transporter=nodemailer.createTransport(smtpTransport({
service:'gmail',
host:"smtp.gmail.com",
auth:{
user:"[email protected]",
pass:'djallilMCA10'
}
}))
//send the email
let mailOptions={
from:"[email protected]",
to:`${email}`,
subject:"Sending email with nodemailer",
text:`${text}`
};
transporter.sendMail(mailOptions,function(error,info){
if(error) console.log(error);
else console.log("Email sent"+info.response);
})
}
//sending verification email fonction
async function main(email,key){
let testAccount= await nodemailer.createTestAccount();
let transporter=nodemailer.createTransport(smtpTransport({
service:'gmail',
host:"smtp.gmail.com",
auth:{
user:"[email protected]",
pass:'djallilMCA10'
}
}))
//send the email
let mailOptions={
from:"[email protected]",
to:`${email}`,
subject:"Sending email with nodemailer",
text:`http://localhost:8080/activation/${email}/${key}`
};
transporter.sendMail(mailOptions,function(error,info){
if(error) console.log(error);
else console.log("Email sent"+info.response);
})
}
//delete everything(dev only)
const dede=function() {
users2.find(function(err, result){
if(err) throw err;
else{
for (let index = 0; index < result.length; index++) {
users2.remove({_id:`${result[index]._id}`},function (err,res) {
if(err) throw err;
else console.log(res,'well done')
})
}
}
})
}
//users of the room
const usersRoom=function (room,cb) {
users2.find(function(err, result){
if (err){
console.log(err)
}
else{
var t=[];
for (let index = 0; index < result.length; index++) {
if(result[index].room==room){
t.push(result[index]);
}
}
}
cb(t)
})
}
//user leaves the chat
const userLeave=function (id,cb) {
getUserId(id,cb)
users2.remove({_id: `${id}`}, function(err, piscine){
if (err){
console.log(err)
}
else{console.log(`${id} left the chat`)}
});
}
// get user with id
const getUserId=function (id,cb) {
users2.findById(`${id}`,(err,result)=>{
if(err) console.log(err)
else{
cb(result)
}
})
}
//join the chat
const joinChat=function (id,username,lastname,email,room,img,cb) {
//const user={id,username,lastname,email,room};
//users.push(user);
var user2=new users2();
user2._id=id;
user2.username=username;
user2.lastname=lastname;
user2.email=email;
user2.room=room;
user2.img=img;
user2.save(function(err,product){
if(err){
console.log(err);
}
cb(product)
})
//return user;
}
//give the user's message a shape
const formatMessage=function(id,email,username,lastname,text){
return{
id,
email,
username,
lastname,
text,
time:moment().format("h:mm a"),
date:moment().format("DD-MM-YYYY")
}
}
//bring old messages
function oldMsg(socket,room){
connection.query(`SELECT* FROM ${room}`,async (err,result)=>{
if(err) throw err;
// let yoyo=ress[0]
//let houhou=ress[1]
socket.emit("afficherMsg",result);
/* await client.LRANGE(room,0,-1, function(err,ress){
if(err) throw err;
let yoyo=ress[0]
let houhou=ress[1]
socket.emit("afficherMsg",result,yoyo,houhou);
})*/
})
}
//store message into DB
function storeMsg(room,email,username,lastname,message,img){
connection.query(`INSERT INTO ${room} SET pdp=?,email=?,username=?,lastname=?,message=?,time=?,date=?`,[img,email,username,lastname,message,new Date().toLocaleTimeString(),new Date().toLocaleDateString()],(err,result)=>{
if(err) throw err;
})
}
//role of the user
function role(email,cb){
connection.query("SELECT role FROM users WHERE email=?",[email],(err,res)=>{
if(err) throw err;
cb(res[0].role);
})
}
//find user with his email
function trouver(email,cb) {
var verif=false
users2.find((err,res)=>{
if(err) console.log(err)
for (let index = 0; index < res.length; index++) {
if(res[index].email==email){verif=true}
}
cb(verif,res)
})
}
function cons(email){return(users.find(user=>user.email===email))}
//find user with his username
function usernameFind(username,lastname,room) {
var toto=0
for (let index = 0; index < users.length; index++) {
if(users[index].username==username && users[index].lastname==lastname && users[index].room==room){toto++}
}
return toto
}
module.exports={sendEmail,main,usersRoom,userLeave,getUserId,joinChat,formatMessage,oldMsg,storeMsg,role,cons,usernameFind,dede,trouver};