Skip to content

Commit

Permalink
Muchas cosas jeje ❤️
Browse files Browse the repository at this point in the history
  • Loading branch information
djbautista committed May 28, 2020
1 parent 4935446 commit 1599d73
Show file tree
Hide file tree
Showing 19 changed files with 722 additions and 29 deletions.
19 changes: 15 additions & 4 deletions back-end/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const passport = require("passport");

const passportMiddleware = require("./utils/passport");

var indexRouter = require("./routes/index");
// var indexRouter = require("./routes/index");
var usersRouter = require("./routes/users");
var authRouter = require("./routes/auth");
var connectionsRounter = require("./routes/connections");
Expand All @@ -32,7 +32,7 @@ app.use(
);

app.use(cookieParser());
app.use(express.static(path.join(__dirname, "public")));
app.use(express.static(path.join(__dirname, "build")));

app.use(passport.initialize());
app.use(passport.session());
Expand All @@ -42,7 +42,7 @@ app.set("superSecret", process.env.SECRET || "youreismysecretbaby");

app.use(
cors({
origin: "http://localhost:3000", // allow to server to accept request from different origin
origin: "https://arx-speeddating-app.herokuapp.com/", // allow to server to accept request from different origin
methods: "GET,HEAD,PUT,PATCH,POST,DELETE",
credentials: true, // allow session cookie from browser to pass through
})
Expand All @@ -60,11 +60,22 @@ const authCheck = (req, res, next) => {
}
};

app.use("/meet", indexRouter);
// app.use("/meet", indexRouter);
app.use("/users", authCheck, usersRouter);
app.use("/conversations", connectionsRounter);
app.use("/auth", authRouter);

app.get("/ping", (req, res) => {
res.status(200).json({
authenticated: true,
message: "hellow from arx",
});
});

app.get("*", (req, res) => {
res.sendFile(path.join(__dirname + "/build/index.html"));
});

// if it's already login, send the profile response,
// otherwise, send a 401 response that the user is not authenticated
// authCheck before navigating to home page
Expand Down
3 changes: 3 additions & 0 deletions back-end/bin/www
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ const wsu = require("../utils/wsUtils");
const mu = require("../utils/mongoUtils")();

mu.connect().then((client) => mu.listenForChanges(client, wsu.notifyAll));
mu.connect().then((client) =>
mu.listenForUserUpdates(client, wsu.updateUserInConnections)
);
/**
* Get port from environment and store in Express.
*/
Expand Down
8 changes: 4 additions & 4 deletions back-end/routes/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const passport = require("passport");
const jwt = require("jsonwebtoken");
const bcrypt = require("bcryptjs");

const CLIENT_HOME_PAGE_URL = "http://localhost:3000";
const CLIENT_HOME_PAGE_URL = "/"; //jkasdjkadkjakjdkjakjda
/* GET home page. */
const mu = require("../utils/mongoUtils")();

Expand Down Expand Up @@ -35,7 +35,7 @@ router.get("/logout", (req, res) => {
console.log("logout intent");

req.logout();
res.redirect(CLIENT_HOME_PAGE_URL);
res.redirect(CLIENT_HOME_PAGE_URL); //jkasdjkadkjakjdkjakjda
});
router.post("/local/login", passport.authenticate("local"), function (
req,
Expand Down Expand Up @@ -187,12 +187,12 @@ router.get(
"/facebook/callback",
passport.authenticate("facebook", {
failureRedirect: "/signin",
successRedirect: CLIENT_HOME_PAGE_URL,
successRedirect: CLIENT_HOME_PAGE_URL, //jkasdjkadkjakjdkjakjda
}),
function (req, res) {
console.log("profile after auth", req.user);
// Successful authentication, redirect home.
res.redirect("/");
res.redirect("/"); //jkasdjkadkjakjdkjakjda
}
);

Expand Down
14 changes: 11 additions & 3 deletions back-end/routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ router.post("/", function (req, res) {
});
});

router.post("/update", function (req, res) {
router.post("/update", function (req, res, next) {
let user = {};
console.log("a ver prro", req.user, req.isAuthenticated());
if (req.body.name) user["name"] = req.body.name;
Expand All @@ -61,13 +61,21 @@ router.post("/update", function (req, res) {
.then((client) => mu.updateUser(client, req.user._id, user))
.then((resp) => {
console.log("response", resp);
if (resp)
if (resp) {
if (resp.value) {
req.login(resp.value, function (err) {
if (err) {
return next(err);
}
});
}
res.status(200).json({
success: true,
msg: "User updated successfully",
data: resp,
});
else
res.end();
} else
res.status(200).json({
success: false,
msg: "User not updated",
Expand Down
13 changes: 13 additions & 0 deletions back-end/utils/mongoUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ const MongoUtils = () => {
$setOnInsert: doc || query,
},
{
returnOriginal: false,
upsert: true, // insert the document if it does not exist
new: true, // return new doc if one is upserted
}
Expand Down Expand Up @@ -153,6 +154,18 @@ const MongoUtils = () => {
});
};

mu.listenForUserUpdates = (client, callback) => {
console.log("listening for changes in users collection");

const cursor = handler(client).watch({ fullDocument: "updateLookup" });

cursor.on("change", (user) => {
console.log("change user", user);

callback(user);
});
};

//
// Conversation functions
//
Expand Down
4 changes: 3 additions & 1 deletion back-end/utils/passport.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ module.exports = function (passport) {
})
);

//hola
passport.use(
new FacebookStrategy(
{
clientID: process.env.FACEBOOK_APP_ID,
clientSecret: process.env.FACEBOOK_APP_SECRET,
callbackURL: "http://localhost:3001/auth/facebook/callback",
callbackURL:
"https://arx-speeddating-app.herokuapp.com/auth/facebook/callback", //jkasdjkadkjakjdkjakjda
profileFields: ["id", "displayName", "email"],
},
function (accessToken, refreshToken, profile, cb) {
Expand Down
108 changes: 100 additions & 8 deletions back-end/utils/wsUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const mu = require("./mongoUtils")();
let connections = [];
let conversations = [];

const MAX_TIME = 20 * 60 * 1000;
const MAX_TIME = 90 * 1000;

const endConversation = (
user1id,
Expand Down Expand Up @@ -119,9 +119,9 @@ const wsUtils = () => {
return [c.socketId, c.dbId, c.active, c.state].join(" *-* ");
})
);
console.log("conversations");
console.log(conversations);
}, 5000);
// console.log("conversations");
// console.log(conversations);
}, 2000);

wsu.notify = (userid, data) => {
const conn = connections.find(
Expand All @@ -133,6 +133,30 @@ const wsUtils = () => {
else console.log("no se pudo notificar", userid);
};

wsu.updateUserInConnections = (userData) => {
console.log("change user in ws", userData);
if (userData.fullDocument) {
const user = userData.fullDocument;

console.log("user a meter", user);

let conn = connections.find((c) => {
console.log(
"conn id y userid",
c.dbId.toString(),
user._id.toString(),
c.dbId.toString() == user._id.toString()
);
return c.dbId.toString() == user._id.toString();
});

if (conn) {
console.log("cambiando connection");
conn.userInfo = user;
}
}
};

wsu.notifyAll = (users_ids, data) => {
console.log("usuarios a notificar", users_ids);

Expand All @@ -159,15 +183,15 @@ const wsUtils = () => {

ws.on("pong", function heartbeat() {
const conn = connections.find((c) => c.socketId === id);
if (conn) conn["active"] = true;
if (conn) conn["time_inactive"] = 0;

const conversation = conversations.find(
(c) => c.user1 === id || c.user2 === id
);

if (conversation) {
let millsLeft = Math.max(
-5000,
-15000,
conversation.startTime + MAX_TIME - Date.now()
);
const payload = {
Expand All @@ -184,7 +208,7 @@ const wsUtils = () => {
if (conversation.likes.size >= 2) {
console.log("termina con Match: ", millsLeft);
endConversation(id, killedId, conversation, false);
} else if (millsLeft <= -5000) {
} else if (millsLeft <= -15000) {
console.log("termina forzado: ", millsLeft);
endConversation(id, killedId, false, conversation);
}
Expand All @@ -196,7 +220,31 @@ const wsUtils = () => {

setInterval(() => {
const conn = connections.find((c) => c.socketId === id);
if (conn) conn["active"] = false;
if (conn) {
conn["time_inactive"] += 1;
if (conn["time_inactive"] >= 10) {
conn["active"] = false;
console.log("**** eliminando por inactividad", conn.dbId);

//avisa los usuarios eliminados
connections
.filter((c) => c.active === true && c.state === 1)
.forEach((c) => {
c.client.send(
JSON.stringify({
state: 3,
data: {
noUser: conn.dbId,
},
})
);
});

connections = connections.filter(
(c) => c.socketId !== conn.socketId
);
}
}
ws.ping(() => {});
}, 1000);

Expand Down Expand Up @@ -329,10 +377,54 @@ const wsUtils = () => {
socketId: id,
client: ws,
state: 1,
time_inactive: 0,
active: true,
dbId: jsonMessage.message,
userInfo: resp[0],
});

ws.send(
JSON.stringify({
state: 4,
confirm: true,
})
);

let uss = {};

connections.forEach((c) => {
if (c.userInfo) {
uss[c.dbId] = {
id: c.dbId,
name:
(c.userInfo.name || "someone").split(" ")[0] +
" is online",
};
}
});

let users = [];

for (const val in uss) {
users.push(uss[val]);
}

console.log("users", users);

connections
.filter((c) => c.active === true && c.state === 1)
.forEach((c) => {
c.client.send(
JSON.stringify({
state: 3,
data: {
users: [...users],
distanceFactor: 150,
},
})
);
});

console.log(
"on push",
connections.map((c) => {
Expand Down
17 changes: 17 additions & 0 deletions front-end/d3Normal/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<div
class="particles"
id="particles"
style="width: 800px; height: 600px;"
></div>
</body>
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="./index.js"></script>
</html>
Loading

0 comments on commit 1599d73

Please sign in to comment.