Skip to content

Commit

Permalink
fix pageviews and db, bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
faisalnjs committed Oct 9, 2023
1 parent ee98233 commit 45d82ac
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 38 deletions.
25 changes: 13 additions & 12 deletions frontend/pages/newspaper.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -39,33 +39,33 @@
<p>Unable to display PDF file, you may have Javascript disabled. No worries, you can <a href="<%- vars.asset_prefix %><%= newspaper.pdf.path %>">download the PDF version</a> instead!</p>
<% if (newspaper.articles.length != 0) { %>
<div class="content">
<% for (article of newspaper.articles) { article = cms.articles.find(a => a._id === article._id); %>
<% for (article of newspaper.articles) { article = cms.articles.find(a => a._id === article._id); if (article) { %>
<h2><%= article.title %></h2>
<%- article.content.replaceAll('/.spaces/', vars.asset_url) %>
<br />
<% }%>
<% } } %>
</div>
<% }%>
<% } %>
</object>
<a href="<%- vars.asset_prefix %><%= newspaper.pdf.path %>" class="mobileOnly">Click here to open the PDF file.</a>
<% if (newspaper.articles.length != 0) { %>
<div class="content mobileOnly">
<% for (article of newspaper.articles) { article = cms.articles.find(a => a._id === article._id); %>
<% for (article of newspaper.articles) { article = cms.articles.find(a => a._id === article._id); if (article) { %>
<h2><%= article.title %></h2>
<%- article.content.replaceAll('/.spaces/', vars.asset_url) %>
<br />
<% }%>
<% } } %>
</div>
<% }%>
<% } %>
<% } else if (newspaper.articles.length != 0) { %>
<div class="content">
<% for (article of newspaper.articles) { article = cms.articles.find(a => a._id === article._id); %>
<% for (article of newspaper.articles) { article = cms.articles.find(a => a._id === article._id); if (article) { %>
<h2><%= article.title %></h2>
<%- article.content.replaceAll('/.spaces/', vars.asset_url) %>
<br />
<% }%>
<% } } %>
</div>
<% }%>
<% } %>
<div class="comments">
<h1>Comments</h1>
<form action="" method="post" class="new">
Expand All @@ -76,12 +76,13 @@
<input name="content" type="text" placeholder="Your Comment">
<button>Post Comment</button>
</form>
<% if (comments.length > 0) { %><hr><% } %>
<% if (comments.length > 0) { %>
<hr><% } %>
<% for (comment of comments.reverse()) { %>
<div id="<%= comment.id %>" class="comment">
<h4><%= comment.author_name %>
<h5><% var date = new Date(comment.date); if (date.isToday()) { %>Today<% } else if (ifYesterday(date)) { %>Yesterday<% } else { %><%= date.toLocaleDateString('en-US', { month: 'long', day: 'numeric', year: 'numeric' }) %><% } %></h5>
<p><%= comment.content %></p>
<h5><% var date = new Date(comment.date); if (date.isToday()) { %>Today<% } else if (ifYesterday(date)) { %>Yesterday<% } else { %><%= date.toLocaleDateString('en-US', { month: 'long', day: 'numeric', year: 'numeric' }) %><% } %></h5>
<p><%= comment.content %></p>
</div>
<% } %>
</div>
Expand Down
1 change: 1 addition & 0 deletions frontend/partials/footer.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<div class="bottom">
<%- include('socials.ejs') %>
<p>&copy; <%= cms.siteDetails[0].title %> <%= cms.siteDetails[0].est %> - <%= new Date().getFullYear() %> | <a href="<%- vars.domain %>/admin">Admin<a href="https://dangoweb.com/?from=<%= cms.siteDetails[0].title.toLowerCase().replaceAll(' ', '-') %>" style="display: none;"> (Host)</a></a> | <%- vars.environment.replace(/\w\S*/g, function(txt) { return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase() }) %> Version | <%= pageviews %> Views</p>
<p>We use cookies for analytics and security.</p>
</div>
</div>
</footer>
Expand Down
7 changes: 5 additions & 2 deletions frontend/public/css/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
}

.hero .newspaper {
height: 25vh;
}

.hero .newspaper:first-of-type {
height: 100%;
}

.hero .side {
width: 100%;
height: 100%;
}

.articles .grid {
Expand Down Expand Up @@ -751,7 +754,6 @@ footer .inner .bottom {
display: flex;
flex-direction: column;
align-items: center;
gap: 10px;
width: 75%;
color: var(--text);
font-size: 12px;
Expand All @@ -775,6 +777,7 @@ footer .inner .bottom a {
justify-content: center;
opacity: 0;
transition: opacity 0.2s ease-in-out;
z-index: 100;
}

.toTop.visible {
Expand Down
44 changes: 20 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ app.set('trust proxy', true);
app.use(cookieParser());
app.use(rateLimit({
windowMs: 60 * 1000,
max: 15
max: 15,
skip: (req, res) => {
return req.ip !== undefined;
}
}));
app.use((req, res, next) => {
if (req.method === 'GET') {
Expand All @@ -54,7 +57,7 @@ app.use((req, res, next) => {
body: new URLSearchParams(pageviewData),
})
.then(() => {
db.query('INSERT INTO pageviews (url, count) VALUES (?, 1) ON DUPLICATE KEY UPDATE count = count + 1', [req.originalUrl], (err, results) => {
db.query('INSERT INTO pageviews (url, count) VALUES (?, 1) ON DUPLICATE KEY UPDATE count = IF(TIMESTAMPDIFF(HOUR, timestamp, NOW()) <= 1, count, count + 1), timestamp = IF(TIMESTAMPDIFF(HOUR, timestamp, NOW()) <= 1, timestamp, NOW());', [req.originalUrl], (err, results) => {
if (err) {
console.error('Error tracking pageview:', err);
};
Expand Down Expand Up @@ -184,13 +187,11 @@ async function startApp() {
await allRoutes(req, res);
var newspaper = cms.newspapers.find(newspaper => newspaper.slug === req.params.newspaper);
if (newspaper && req.body.id && (req.body.name.length > 0) && (req.body.email.includes('.')) && (req.body.email.length > 0) && (req.body.content.length > 0)) {
db.prepare("INSERT INTO comments (author_name, author_email, post_id, content) VALUES (?, ?, ?, ?)", (err, statement) => {
statement.execute([req.body.name, req.body.email, req.body.id, req.body.content], function (err, results, fields) {
if (err) {
console.log(err);
};
res.redirect(`/newspapers/${req.params.newspaper}#${results.insertId}`);
});
db.query("INSERT INTO comments (author_name, author_email, post_id, content) VALUES (?, ?, ?, ?)", [req.body.name, req.body.email, req.body.id, req.body.content], function (err, results, fields) {
if (err) {
console.log(err);
};
res.redirect(`/newspapers/${req.params.newspaper}#${results.insertId}`);
});
} else {
res.redirect('/');
Expand Down Expand Up @@ -220,13 +221,11 @@ async function startApp() {
await allRoutes(req, res);
var article = cms.articles.find(article => article.slug === req.params.article);
if (article && req.body.id && (req.body.name.length > 0) && (req.body.email.includes('.')) && (req.body.email.length > 0) && (req.body.content.length > 0)) {
db.prepare("INSERT INTO comments (author_name, author_email, post_id, content) VALUES (?, ?, ?, ?)", (err, statement) => {
statement.execute([req.body.name, req.body.email, req.body.id, req.body.content], function (err, results, fields) {
if (err) {
console.log(err);
};
res.redirect(`/articles/${req.params.article}#${results.insertId}`);
});
db.query("INSERT INTO comments (author_name, author_email, post_id, content) VALUES (?, ?, ?, ?)", [req.body.name, req.body.email, req.body.id, req.body.content], function (err, results, fields) {
if (err) {
console.log(err);
};
res.redirect(`/articles/${req.params.article}#${results.insertId}`);
});
} else {
res.redirect('/');
Expand Down Expand Up @@ -258,7 +257,6 @@ async function startApp() {
voteId = uuid.v4();
res.cookie('voteId', voteId);
};
console.log(voteId);
var poll = cms.polls.find(poll => poll.slug === req.params.poll);
if (poll) {
db.query(`SELECT * FROM poll_responses WHERE poll_id = '${poll._id}'`,
Expand Down Expand Up @@ -308,13 +306,11 @@ async function startApp() {
}
);
} else if (poll && req.body.id && (req.body.name.length > 0) && (req.body.email.includes('.')) && (req.body.email.length > 0) && (req.body.content.length > 0)) {
db.prepare("INSERT INTO comments (author_name, author_email, post_id, content) VALUES (?, ?, ?, ?)", (err, statement) => {
statement.execute([req.body.name, req.body.email, req.body.id, req.body.content], function (err, results, fields) {
if (err) {
console.log(err);
};
res.redirect(`/polls/${req.params.poll}#${results.insertId}`);
});
db.query("INSERT INTO comments (author_name, author_email, post_id, content) VALUES (?, ?, ?, ?)", [req.body.name, req.body.email, req.body.id, req.body.content], function (err, results, fields) {
if (err) {
console.log(err);
};
res.redirect(`/polls/${req.params.poll}#${results.insertId}`);
});
} else {
res.redirect('/');
Expand Down

0 comments on commit 45d82ac

Please sign in to comment.