Skip to content

Commit a315309

Browse files
authored
Merge pull request #1114 from Tanaya555/main
Add files via upload
2 parents 5c2464d + da20bf3 commit a315309

33 files changed

+9073
-0
lines changed

Javascript/Articooz/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Articooz 🖊📝
2+
## A platform to post blogs and articles, follow each other, and even have conversations.
3+
4+
## [Live Site](https://articooz.herokuapp.com/)
5+
---
6+
## Screenshots
7+
8+
![Screenshot_1](https://user-images.githubusercontent.com/55017730/95657427-201d6600-0b32-11eb-9599-4e0aa21a26a5.png)
9+
10+
11+
![Screenshot_2](https://user-images.githubusercontent.com/55017730/95657442-2f9caf00-0b32-11eb-99f8-900465a5cb1c.png)
12+
13+
14+
![Screenshot_3](https://user-images.githubusercontent.com/55017730/95657458-375c5380-0b32-11eb-9aa1-a854f69ed824.png)

Javascript/Articooz/app.js

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
const express = require("express");
2+
const session = require("express-session");
3+
const MongoStore = require("connect-mongo")(session);
4+
const flash = require("connect-flash");
5+
const markdown = require("marked");
6+
const csrf = require("csurf");
7+
const app = express();
8+
const sanitizeHTML = require("sanitize-html");
9+
10+
app.use(express.urlencoded({ extended: false }));
11+
app.use(express.json());
12+
13+
app.use("/api", require("./router-api"));
14+
15+
let sessionOptions = session({
16+
secret: "JavaScript is sooooooooo coool",
17+
store: new MongoStore({ client: require("./db") }),
18+
resave: false,
19+
saveUninitialized: false,
20+
cookie: { maxAge: 1000 * 60 * 60 * 24, httpOnly: true },
21+
});
22+
23+
app.use(sessionOptions);
24+
app.use(flash());
25+
26+
app.use(function (req, res, next) {
27+
// make our markdown function available from within ejs templates
28+
res.locals.filterUserHTML = function (content) {
29+
return sanitizeHTML(markdown(content), {
30+
allowedTags: [
31+
"p",
32+
"br",
33+
"ul",
34+
"ol",
35+
"li",
36+
"strong",
37+
"bold",
38+
"i",
39+
"em",
40+
"h1",
41+
"h2",
42+
"h3",
43+
"h4",
44+
"h5",
45+
"h6",
46+
],
47+
allowedAttributes: {},
48+
});
49+
};
50+
51+
// make all error and success flash messages available from all templates
52+
res.locals.errors = req.flash("errors");
53+
res.locals.success = req.flash("success");
54+
55+
// make current user id available on the req object
56+
if (req.session.user) {
57+
req.visitorId = req.session.user._id;
58+
} else {
59+
req.visitorId = 0;
60+
}
61+
62+
// make user session data available from within view templates
63+
res.locals.user = req.session.user;
64+
next();
65+
});
66+
67+
const router = require("./router");
68+
69+
app.use(express.static("public"));
70+
app.set("views", "views");
71+
app.set("view engine", "ejs");
72+
73+
app.use(csrf());
74+
75+
app.use(function (req, res, next) {
76+
res.locals.csrfToken = req.csrfToken();
77+
next();
78+
});
79+
80+
app.use("/", router);
81+
82+
app.use(function (err, req, res, next) {
83+
if (err) {
84+
if (err.code == "EBADCSRFTOKEN") {
85+
req.flash("errors", "Cross site request forgery detected.");
86+
req.session.save(() => res.redirect("/"));
87+
} else {
88+
res.render("404");
89+
}
90+
}
91+
});
92+
93+
const server = require("http").createServer(app);
94+
const io = require("socket.io")(server);
95+
96+
io.use(function (socket, next) {
97+
sessionOptions(socket.request, socket.request.res, next);
98+
});
99+
100+
io.on("connection", function (socket) {
101+
if (socket.request.session.user) {
102+
let user = socket.request.session.user;
103+
104+
socket.emit("welcome", { username: user.username, avatar: user.avatar });
105+
106+
socket.on("chatMessageFromBrowser", function (data) {
107+
socket.broadcast.emit("chatMessageFromServer", {
108+
message: sanitizeHTML(data.message, {
109+
allowedTags: [],
110+
allowedAttributes: {},
111+
}),
112+
username: user.username,
113+
avatar: user.avatar,
114+
});
115+
});
116+
}
117+
});
118+
119+
module.exports = server;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const Follow = require("../models/Follow");
2+
3+
exports.addFollow = function (req, res) {
4+
let follow = new Follow(req.params.username, req.visitorId);
5+
follow
6+
.create()
7+
.then(() => {
8+
req.flash("success", `Successfully followed ${req.params.username}`);
9+
req.session.save(() => res.redirect(`/profile/${req.params.username}`));
10+
})
11+
.catch((errors) => {
12+
errors.forEach((error) => {
13+
req.flash("errors", error);
14+
});
15+
req.session.save(() => res.redirect("/"));
16+
});
17+
};
18+
19+
exports.removeFollow = function (req, res) {
20+
let follow = new Follow(req.params.username, req.visitorId);
21+
follow
22+
.delete()
23+
.then(() => {
24+
req.flash(
25+
"success",
26+
`Successfully stopped following ${req.params.username}`
27+
);
28+
req.session.save(() => res.redirect(`/profile/${req.params.username}`));
29+
})
30+
.catch((errors) => {
31+
errors.forEach((error) => {
32+
req.flash("errors", error);
33+
});
34+
req.session.save(() => res.redirect("/"));
35+
});
36+
};
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
const Post = require("../models/Post");
2+
3+
exports.viewCreateScreen = function (req, res) {
4+
res.render("create-post");
5+
};
6+
7+
exports.create = function (req, res) {
8+
let post = new Post(req.body, req.session.user._id);
9+
post
10+
.create()
11+
.then(function (newId) {
12+
req.flash("success", "New post successfully created.");
13+
req.session.save(() => res.redirect(`/post/${newId}`));
14+
})
15+
.catch(function (errors) {
16+
errors.forEach((error) => req.flash("errors", error));
17+
req.session.save(() => res.redirect("/create-post"));
18+
});
19+
};
20+
21+
exports.viewSingle = async function (req, res) {
22+
try {
23+
let post = await Post.findSingleById(req.params.id, req.visitorId);
24+
res.render("single-post-screen", { post: post, title: post.title });
25+
} catch {
26+
res.render("404");
27+
}
28+
};
29+
30+
exports.viewEditScreen = async function (req, res) {
31+
try {
32+
let post = await Post.findSingleById(req.params.id, req.visitorId);
33+
if (post.isVisitorOwner) {
34+
res.render("edit-post", { post: post });
35+
} else {
36+
req.flash("errors", "You do not have permission to perform that action.");
37+
req.session.save(() => res.redirect("/"));
38+
}
39+
} catch {
40+
res.render("404");
41+
}
42+
};
43+
44+
exports.edit = function (req, res) {
45+
let post = new Post(req.body, req.visitorId, req.params.id);
46+
post
47+
.update()
48+
.then((status) => {
49+
// the post was successfully updated in the database
50+
// or user did have permission, but there were validation errors
51+
if (status == "success") {
52+
// post was updated in db
53+
req.flash("success", "Post successfully updated.");
54+
req.session.save(function () {
55+
res.redirect(`/post/${req.params.id}/edit`);
56+
});
57+
} else {
58+
post.errors.forEach(function (error) {
59+
req.flash("errors", error);
60+
});
61+
req.session.save(function () {
62+
res.redirect(`/post/${req.params.id}/edit`);
63+
});
64+
}
65+
})
66+
.catch(() => {
67+
// a post with the requested id doesn't exist
68+
// or if the current visitor is not the owner of the requested post
69+
req.flash("errors", "You do not have permission to perform that action.");
70+
req.session.save(function () {
71+
res.redirect("/");
72+
});
73+
});
74+
};
75+
76+
exports.delete = function (req, res) {
77+
Post.delete(req.params.id, req.visitorId)
78+
.then(() => {
79+
req.flash("success", "Post successfully deleted.");
80+
req.session.save(() =>
81+
res.redirect(`/profile/${req.session.user.username}`)
82+
);
83+
})
84+
.catch(() => {
85+
req.flash("errors", "You do not have permission to perform that action.");
86+
req.session.save(() => res.redirect("/"));
87+
});
88+
};
89+
90+
exports.search = function (req, res) {
91+
Post.search(req.body.searchTerm)
92+
.then((posts) => {
93+
res.json(posts);
94+
})
95+
.catch(() => {
96+
res.json([]);
97+
});
98+
};

0 commit comments

Comments
 (0)