22 |
60 |
61 | <% if(!isAuth) { %>
62 | You must be
logged in to leave a comment
63 | <% } else { %>
64 |
69 | <% }; %>
70 |
71 |
72 |
73 |
82 | <% if (comments != undefined) {
83 | comments.forEach((comment) => { %>
84 |
85 |
111 | <% });%>
112 | <% } else { %>
113 | There are no comments.
114 | <% } %>
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
--------------------------------------------------------------------------------
/public/js/post.js:
--------------------------------------------------------------------------------
1 | $("document").ready(function () {
2 | autosize($('.comment-text'))
3 |
4 | $(".edit-post").click(function () {
5 | let query = $(this).closest('article')
6 | let ref = query.data('ref')
7 |
8 | let body = query.find('.post-body').text()
9 | let options = query.find('.post-options')
10 | query.find('.post-body').html(`
`)
11 | query.find('.post-body').append("
");
12 | autosize(query.find('.post-text'))
13 |
14 | options.hide();
15 |
16 | $("button.edit_cancel").click(function () {
17 | let text = body;
18 |
19 | query.find(".post-text").remove();
20 | query.find(".post-body").text(text)
21 |
22 | options.show();
23 | })
24 |
25 | $("button.edit_submit").click(function () {
26 | let new_text = query.find('.post-text').val();
27 |
28 | $.ajax({
29 | type: "put",
30 | url: `/edit/post/${ref}`,
31 | data: {
32 | text: new_text
33 | }
34 | }).done(function (res) {
35 | query.find(".post-text").remove();
36 | query.find(".post-body").text(new_text)
37 | options.show();
38 | })
39 | })
40 | return false;
41 | })
42 |
43 | $(".delete-post").click(function () {
44 | let query = $(this).closest('article')
45 | let ref = query.data('ref')
46 |
47 | if (confirm("Are you sure you want to delete?")) {
48 | $.ajax({
49 | type: "delete",
50 | url: `/delete/post/${ref}`,
51 | }).done(function (res) {
52 | query.remove();
53 | })
54 | }
55 | return false;
56 | })
57 |
58 | $(".save-post").click(function () {
59 | let query = $(this).closest('article')
60 | let ref = query.data('ref')
61 | let that = $(this)
62 |
63 | if ($(this).text() == "save") {
64 | $.ajax({
65 | type: "put",
66 | url: `/save/post/${ref}`
67 | }).done(function (res) {
68 | if (res == "success") {
69 | that.text('unsave');
70 | return false;
71 | }
72 | })
73 | } else if ($(this).text() == "unsave") {
74 | $.ajax({
75 | type: "put",
76 | url: `/unsave/post/${ref}`,
77 | }).done(function (res) {
78 | if (res == "success") {
79 | that.text('save');
80 | return false;
81 | }
82 | })
83 | }
84 | return false;
85 | })
86 |
87 | $(".upvote-post").click(function () {
88 | let query = $(this).closest('article')
89 | let ref = query.data('ref')
90 |
91 | let votes = query.find('.post-votes')
92 | let down_arrow = query.find(".downvote-post")
93 | let post_user = query.find('.post-user').text()
94 | let counter;
95 |
96 | // if upvote is already toggled and user presses it again,
97 | // toggle off the upvote button and decrement vote.
98 | if ($(this).hasClass("up-enabled")) {
99 | counter = votes.text();
100 | votes.text(--counter);
101 | $(this).removeClass("up-enabled");
102 |
103 | $.ajax({
104 | type: "put",
105 | url: `/vote/post/${ref}`,
106 | data: {
107 | vote: counter,
108 | state: "neutral",
109 | action: "decrement",
110 | user: post_user
111 | },
112 | success: function (res) {}
113 | });
114 | return false;
115 | }
116 |
117 | // if downvote is already toggled while upvote is pressed
118 | // toggle off downvote and increment vote
119 | if (down_arrow.hasClass('down-enabled')) {
120 | down_arrow.removeClass("down-enabled");
121 | counter = votes.text();
122 | votes.text(++counter);
123 |
124 | $.ajax({
125 | type: "put",
126 | data: {
127 | vote: counter,
128 | state: "neutral",
129 | action: "increment",
130 | user: post_user
131 | },
132 | url: `/vote/post/${ref}`,
133 | success: function (res) {}
134 | });
135 | }
136 |
137 | // if upvote isnt toggled while upvote is pressed,
138 | // toggle upvote and increment vote.
139 | else if (!$(this).hasClass("up-enabled")) {
140 | counter = votes.text();
141 | votes.text(++counter);
142 | $(this).addClass("up-enabled");
143 |
144 | $.ajax({
145 | type: "put",
146 | data: {
147 | vote: counter,
148 | state: "up",
149 | action: "increment",
150 | user: post_user
151 | },
152 | url: `/vote/post/${ref}`,
153 | success: function (res) {}
154 | });
155 | }
156 | return false;
157 | });
158 |
159 | $(".downvote-post").click(function () {
160 | let query = $(this).closest('article')
161 | let ref = query.data('ref')
162 |
163 | let votes = query.find('.post-votes')
164 | let up_arrow = query.find(".upvote-post")
165 | let post_user = query.find('.post-user').text()
166 | let counter;
167 |
168 | // if downvote is already toggled and user presses it again,
169 | // toggle off the downvote button and increment vote.
170 | if ($(this).hasClass("down-enabled")) {
171 | counter = votes.text();
172 | votes.text(++counter);
173 | $(this).removeClass("down-enabled");
174 |
175 | $.ajax({
176 | type: "put",
177 | data: {
178 | vote: counter,
179 | state: "neutral",
180 | action: "increment",
181 | user: post_user
182 | },
183 | url: `/vote/post/${ref}`,
184 | success: function (res) {}
185 | });
186 | return false;
187 | }
188 |
189 | // if upvote is already toggled while downvote is pressed
190 | // toggle off upvote and decrement vote
191 | if (up_arrow.hasClass('up-enabled')) {
192 | up_arrow.removeClass("up-enabled");
193 | counter = votes.text();
194 | votes.text(--counter);
195 |
196 | $.ajax({
197 | type: "put",
198 | data: {
199 | vote: counter,
200 | state: "neutral",
201 | action: "decrement",
202 | user: post_user
203 | },
204 | url: `/vote/post/${ref}`,
205 | success: function (res) {}
206 | });
207 |
208 | // if downvote isnt toggled while downvote is pressed,
209 | // toggle downvote and decrement vote.
210 | } else if (!$(this).hasClass("down-enabled")) {
211 | counter = votes.text();
212 | votes.text(--counter);
213 | $(this).addClass("down-enabled");
214 |
215 | $.ajax({
216 | type: "put",
217 | data: {
218 | vote: counter,
219 | state: "down",
220 | action: "decrement",
221 | user: post_user
222 | },
223 | url: `/vote/post/${ref}`,
224 | success: function (res) {}
225 | });
226 | }
227 | return false;
228 | });
229 | });
--------------------------------------------------------------------------------
/public/js/comment.js:
--------------------------------------------------------------------------------
1 | $("document").ready(function () {
2 |
3 | // event handler for editing a comment
4 | $(".edit-comment").click(function () {
5 | let query = $(this).closest('article')
6 | let ref = query.data('ref')
7 | let body = query.find(".comment-body").text()
8 | let options = query.find(".comment-options")
9 |
10 | // display a text area with the comment body and hide comment options
11 | query.find(".comment-body").html(``)
12 | query.find(".comment-body").append(`
`);
13 | autosize(query.find('.comment-text'))
14 |
15 | options.hide();
16 |
17 |
18 | // when user clicks on cancel, remove textarea and show comment options
19 | $(`button.edit-comment-cancel[data-ref="${ref}"]`).click(function () {
20 | let text = body;
21 |
22 | query.find(".comment-text").remove();
23 | query.find(".comment-body").text(text)
24 |
25 | options.show();
26 | })
27 |
28 |
29 | // when user clicks on submit, upate comment in database, remove textarea and show comment options
30 | $(`button.edit-comment-submit[data-ref="${ref}"]`).click(function () {
31 | let new_text = query.find('.comment-text').val();
32 |
33 | $.ajax({
34 | type: "put",
35 | url: `/edit/comment/${ref}`,
36 | data: {
37 | text: new_text
38 | }
39 | }).done(function (res) {
40 | query.find(".comment-text").remove();
41 | query.find(".comment-body").text(new_text)
42 | options.show();
43 | })
44 | })
45 | return false;
46 | })
47 |
48 |
49 | // event handler for deleting a comment
50 | $(".delete-comment").click(function () {
51 | let query = $(this).closest('article')
52 | let ref = query.data('ref')
53 |
54 | // only when user clicks on okay, remove comment from database,then remove from page
55 | if (confirm("Are you sure you want to delete?")) {
56 | $.ajax({
57 | type: "delete",
58 | url: `/delete/comment/${ref}`,
59 | }).done(function (res) {
60 | query.remove();
61 | })
62 | }
63 | return false;
64 | })
65 |
66 |
67 | // event handler for saving a comment
68 | $(".save-comment").click(function () {
69 | let query = $(this).closest('article')
70 | let ref = query.data('ref')
71 | let that = $(this)
72 |
73 | if ($(this).text() == "save") {
74 | alert('saved')
75 | $.ajax({
76 | type: "put",
77 | url: `/save/comment/${ref}`
78 | }).done(function (res) {
79 | if (res == "success") {
80 | that.text('unsave');
81 | return false;
82 | }
83 | })
84 | } else if ($(this).text() == "unsave") {
85 | alert('unsaved')
86 | $.ajax({
87 | type: "put",
88 | url: `/unsave/comment/${ref}`,
89 | }).done(function (res) {
90 | if (res == "success") {
91 | that.text('save');
92 | return false;
93 | }
94 | })
95 | }
96 | return false;
97 | })
98 |
99 | // event handler for upvoting a comment
100 | $(".upvote-comment").click(function () {
101 | let down_arrow = $(this).parent().find(".downvote-comment")
102 | let query = $(this).closest('article')
103 |
104 | let ref = query.data('ref')
105 | let votes = query.find('.comment-votes')
106 | let comment_user = query.find('.comment-user').text()
107 | let counter;
108 |
109 | // if upvote is already toggled and user presses it again,
110 | // toggle off the upvote button and decrement vote.
111 | if ($(this).hasClass("up-enabled")) {
112 | counter = votes.text();
113 | votes.text(--counter);
114 | $(this).removeClass("up-enabled");
115 |
116 | $.ajax({
117 | type: "put",
118 | url: `/vote/comment/${ref}`,
119 | data: {
120 | vote: counter,
121 | state: "neutral",
122 | action: "decrement",
123 | user: comment_user
124 | }
125 | });
126 | return false;
127 | }
128 |
129 | // if downvote is already toggled while upvote is pressed
130 | // toggle off downvote and increment vote
131 | if (down_arrow.hasClass('down-enabled')) {
132 | down_arrow.removeClass("down-enabled");
133 | counter = votes.text();
134 | votes.text(++counter);
135 |
136 | $.ajax({
137 | type: "put",
138 | url: `/vote/comment/${ref}`,
139 | data: {
140 | vote: counter,
141 | state: "neutral",
142 | action: "increment",
143 | user: comment_user
144 | }
145 | });
146 | }
147 |
148 | // if upvote isnt toggled while upvote is pressed,
149 | // toggle upvote and increment vote.
150 | else if (!$(this).hasClass("up-enabled")) {
151 | counter = votes.text();
152 | votes.text(++counter);
153 | $(this).addClass("up-enabled");
154 |
155 | $.ajax({
156 | type: "put",
157 | url: `/vote/comment/${ref}`,
158 | data: {
159 | vote: counter,
160 | state: "up",
161 | action: "increment",
162 | user: comment_user
163 | }
164 | });
165 | }
166 | return false;
167 | })
168 |
169 | // event handler for downvoting a comment
170 | $(".downvote-comment").click(function () {
171 | let up_arrow = $(this).parent().find(".upvote-comment")
172 | let query = $(this).closest('article')
173 |
174 | let ref = query.data('ref')
175 | let votes = query.find('.comment-votes')
176 | let comment_user = query.find('.comment-user').text()
177 | let counter;
178 |
179 | // if downvote is already toggled and user presses it again,
180 | // toggle off the downvote button and increment vote.
181 | if ($(this).hasClass("down-enabled")) {
182 | counter = votes.text();
183 | votes.text(++counter);
184 | $(this).removeClass("down-enabled");
185 |
186 | $.ajax({
187 | type: "put",
188 | url: `/vote/comment/${ref}`,
189 | data: {
190 | vote: counter,
191 | state: "neutral",
192 | action: "increment",
193 | user: comment_user
194 | }
195 | });
196 | return false;
197 | }
198 |
199 | // if upvote is already toggled while downvote is pressed
200 | // toggle off upvote and decrement vote
201 | if (up_arrow.hasClass('up-enabled')) {
202 | up_arrow.removeClass("up-enabled");
203 | counter = votes.text();
204 | votes.text(--counter);
205 |
206 | $.ajax({
207 | type: "put",
208 | url: `/vote/comment/${ref}`,
209 | data: {
210 | vote: counter,
211 | state: "neutral",
212 | action: "decrement",
213 | user: comment_user
214 | }
215 | });
216 |
217 | // if downvote isnt toggled while downvote is pressed,
218 | // toggle downvote and decrement vote.
219 | } else if (!$(this).hasClass("down-enabled")) {
220 | counter = votes.text();
221 | votes.text(--counter);
222 | $(this).addClass("down-enabled");
223 |
224 | $.ajax({
225 | type: "put",
226 | url: `/vote/comment/${ref}`,
227 | data: {
228 | vote: counter,
229 | state: "down",
230 | action: "decrement",
231 | user: comment_user
232 | }
233 | });
234 | }
235 | return false;
236 | });
237 | });
--------------------------------------------------------------------------------
/controllers/profile_controller.js:
--------------------------------------------------------------------------------
1 | const mongoose = require("mongoose");
2 | mongoose.Promise = global.Promise;
3 |
4 | let Post = require("../models/post");
5 | let Comment = require("../models/comment");
6 | let Profile = require("../models/profile");
7 | let Account = require("../models/account")
8 |
9 | exports.posts = function (req, res) {
10 | let subscribed = undefined;
11 | let posts = undefined;
12 | let created = undefined;
13 | let karma = 0
14 |
15 | let sort = undefined;
16 |
17 | switch (req.query.sort) {
18 | case "top":
19 | sort = {
20 | votes: -1
21 | }
22 | break;
23 | case "new":
24 | sort = {
25 | time: -1
26 | }
27 | break;
28 | case "old":
29 | sort = {
30 | time: 1
31 | }
32 | break;
33 | default:
34 | sort = {
35 | votes: -1
36 | }
37 | }
38 |
39 | Profile.find({
40 | username: req.params.user
41 | }, function (err, result) {
42 | if (err) throw err;
43 |
44 | if (result.length) {
45 | karma = result[0]['karma_post'] + result[0]['karma_comment']
46 | }
47 | })
48 |
49 | Account.find({
50 | username: req.params.user
51 | }, function (err, result) {
52 | if (err) throw err;
53 |
54 | if (result.length) {
55 | var d = new Date(result[0]['created'])
56 | created = d.toLocaleDateString().replace(/\//g, '-')
57 | } else {
58 | res.render("./error")
59 | }
60 | }).then(function () {
61 | Profile.find({
62 | username: req.params.user
63 | }, function (err, result) {
64 | if (err) throw err;
65 |
66 | if (result.length) {
67 | subscribed = result[0]['subscribed'];
68 | }
69 | }).then(function () {
70 | Post.find({
71 | username: req.params.user
72 | })
73 | .sort(sort).exec(function (err, result) {
74 | if (err) throw err;
75 |
76 | if (result.length) {
77 | posts = result
78 | }
79 | console.log(`[Profile] fetching posts from ${req.params.user} !`)
80 | res.render("./profile/profile_posts", {
81 | profile_user: req.params.user,
82 | posts: posts,
83 | karma: karma,
84 | subscribed: subscribed,
85 | created: created,
86 | isAuth: req.isAuthenticated()
87 | })
88 | })
89 | })
90 | })
91 | }
92 |
93 | exports.comments = function (req, res) {
94 | let subscribed = undefined;
95 | let comments = undefined;
96 | let created = undefined;
97 | let karma = 0
98 |
99 | let sort = undefined;
100 |
101 | switch (req.query.sort) {
102 | case "top":
103 | sort = {
104 | votes: -1
105 | }
106 | break;
107 | case "new":
108 | sort = {
109 | time: -1
110 | }
111 | break;
112 | case "old":
113 | sort = {
114 | time: 1
115 | }
116 | break;
117 | default:
118 | sort = {
119 | votes: -1
120 | }
121 | }
122 |
123 | Profile.find({
124 | username: req.params.user
125 | }, function (err, result) {
126 | if (err) throw err;
127 |
128 | if (result.length) {
129 | karma = result[0]['karma_post'] + result[0]['karma_comment']
130 | }
131 | })
132 |
133 | Account.find({
134 | username: req.params.user
135 | }, function (err, result) {
136 | if (err) throw err;
137 |
138 | if (result.length) {
139 | var d = new Date(result[0]['created'])
140 | created = d.toLocaleDateString().replace(/\//g, '-')
141 | }
142 | }).then(function () {
143 | Profile.find({
144 | username: req.params.user
145 | }, function (err, result) {
146 | if (err) throw err;
147 |
148 | if (result.length) {
149 | subscribed = result[0]['subscribed'];
150 | }
151 | }).then(function () {
152 | Comment.aggregate([{
153 | $match: {
154 | username: req.params.user
155 | }
156 | },
157 | {
158 | $sort: sort
159 | },
160 | {
161 | $lookup: {
162 | from: "posts",
163 | localField: "ref", // field in the orders collection
164 | foreignField: "_id", // field in the items collection
165 | as: "parent"
166 | }
167 | }
168 | ]).exec(function (err, result) {
169 | if (err) throw err;
170 |
171 | if (result.length) {
172 | comments = result
173 | }
174 | console.log(`[Profile] fetching comments from ${req.params.user} !`)
175 | res.render("./profile/profile_comments", {
176 | profile_user: req.params.user,
177 | comments: comments,
178 | karma: karma,
179 | created: created,
180 | subscribed: subscribed,
181 | isAuth: req.isAuthenticated()
182 | })
183 | });
184 | });
185 | });
186 | }
187 |
188 | exports.saved_posts = function (req, res) {
189 | let created = undefined
190 | let subscribed = undefined
191 | let karma = 0
192 |
193 | let sort = undefined;
194 |
195 | switch (req.query.sort) {
196 | case "top":
197 | sort = {
198 | votes: -1
199 | }
200 | break;
201 | case "new":
202 | sort = {
203 | time: -1
204 | }
205 | break;
206 | case "old":
207 | sort = {
208 | time: 1
209 | }
210 | break;
211 | default:
212 | sort = {
213 | votes: -1
214 | }
215 | }
216 |
217 | Profile.find({
218 | username: req.params.user
219 | }, function (err, result) {
220 | if (err) throw err;
221 |
222 | if (result.length) {
223 | subscribed = result[0]['subscribed']
224 | karma = result[0]['karma_post'] + result[0]['karma_comment']
225 | }
226 | })
227 |
228 | Account.find({
229 | username: req.params.user
230 | }).exec().then((result) => {
231 | created = new Date(result[0]['created']).toLocaleDateString().replace(/\//g, '-')
232 |
233 | return Profile.find({
234 | username: req.params.user
235 | })
236 | }).then((result) => {
237 | console.log(result)
238 | return Post.find({
239 | _id: {
240 | $in: result[0].saved_posts
241 | }
242 | }).sort(sort)
243 | }).then((result) => {
244 | res.render("./profile/profile_saved_posts", {
245 | profile_user: req.params.user,
246 | posts: result,
247 | karma: karma,
248 | created: created,
249 | subscribed: subscribed,
250 | isAuth: req.isAuthenticated()
251 | })
252 | }).catch((err) => {
253 | console.log(err)
254 | })
255 | }
256 |
257 | exports.saved_comments = function (req, res) {
258 | let created = undefined
259 | let subscribed = undefined
260 | let karma = 0
261 |
262 | let sort = undefined;
263 |
264 | switch (req.query.sort) {
265 | case "top":
266 | sort = {
267 | votes: -1
268 | }
269 | break;
270 | case "new":
271 | sort = {
272 | time: -1
273 | }
274 | break;
275 | case "old":
276 | sort = {
277 | time: 1
278 | }
279 | break;
280 | default:
281 | sort = {
282 | votes: -1
283 | }
284 | }
285 |
286 | Profile.find({
287 | username: req.params.user
288 | }, function (err, result) {
289 | if (err) throw err;
290 |
291 | if (result.length) {
292 | subscribed = result[0]['subscribed']
293 | karma = result[0]['karma_post'] + result[0]['karma_comment']
294 | }
295 | })
296 |
297 | Account.find({
298 | username: req.params.user
299 | }).exec().then((result) => {
300 | created = new Date(result[0]['created']).toLocaleDateString().replace(/\//g, '-')
301 |
302 | return Profile.find({
303 | username: req.params.user
304 | })
305 | }).then((result) => {
306 | let casted_saved_comments = result[0].saved_comments.map(function (el) {
307 | return mongoose.Types.ObjectId(el)
308 | })
309 | return Comment.aggregate([{
310 | $match: {
311 | _id: {
312 | $in: casted_saved_comments
313 | }
314 | }
315 | },
316 | {
317 | $sort: sort
318 | },
319 | {
320 | $lookup: {
321 | from: "posts",
322 | localField: "ref", // field in the orders collection
323 | foreignField: "_id", // field in the items collection
324 | as: "parent"
325 | }
326 | }
327 | ])
328 | }).then((result) => {
329 | res.render("./profile/profile_saved_comments", {
330 | profile_user: req.params.user,
331 | comments: result,
332 | karma: karma,
333 | created: created,
334 | subscribed: subscribed,
335 | isAuth: req.isAuthenticated()
336 | })
337 | }).catch((err) => {
338 | console.log(err)
339 | })
340 |
341 | }
--------------------------------------------------------------------------------
/controllers/submit_controller.js:
--------------------------------------------------------------------------------
1 | let Subreddit = require("../models/subreddit");
2 | let Post = require("../models/post");
3 | let Profile = require("../models/profile");
4 |
5 | exports.subreddit_post_view = function (req, res) {
6 | let subscribed = false
7 | let karma = 0
8 |
9 | Profile.find({
10 | username: req.session.user
11 | }, function (err, result) {
12 | if (err) throw err;
13 |
14 | if (result.length) {
15 | karma = result[0]['karma_post'] + result[0]['karma_comment']
16 | }
17 | });
18 |
19 | Profile.find({
20 | username: req.session.user,
21 | subscribed: req.params.subreddit,
22 | }, function (err, doc) {
23 | if (err) throw err;
24 |
25 | if (!doc.length) {
26 | // res.send("Unable to find subreddit state")
27 | return;
28 | } else {
29 | subscribed = true
30 | }
31 | }).then(function () {
32 | Subreddit.find({
33 | name: req.params.subreddit
34 | }, function (err, doc) {
35 | if (err) throw err
36 |
37 | if (doc.length) {
38 | res.render('./subreddit/subreddit_post', {
39 | info: doc[0],
40 | karma: karma,
41 | state: subscribed,
42 | isAuth: req.isAuthenticated(),
43 | })
44 | }
45 | })
46 | })
47 | }
48 | exports.subreddit_post = function (req, res) {
49 | Post({
50 | title: req.body.title,
51 | body: req.body.body,
52 | username: req.session.user,
53 | type: "post",
54 | subreddit: req.params.subreddit,
55 | }).save(function (err, doc) {
56 | if (err) throw err;
57 |
58 | console.log(`[${req.params.subreddit}] post submitted!`)
59 | res.redirect(`/r/${req.params.subreddit}`)
60 | })
61 | }
62 | exports.subreddit_link_view = function (req, res) {
63 | let subscribed = false;
64 | let karma = 0
65 |
66 | Profile.find({
67 | username: req.session.user
68 | }, function (err, result) {
69 | if (err) throw err;
70 |
71 | if (result.length) {
72 | karma = result[0]['karma_post'] + result[0]['karma_comment']
73 | }
74 | });
75 |
76 |
77 | Profile.find({
78 | username: req.session.user,
79 | subscribed: req.params.subreddit,
80 | }, function (err, doc) {
81 | if (err) throw err;
82 |
83 | if (!doc.length) {
84 | // res.send("Unable to find subreddit state")
85 | return;
86 | } else {
87 | subscribed = true
88 | }
89 | }).then(function () {
90 | Subreddit.find({
91 | name: req.params.subreddit
92 | }, function (err, doc) {
93 | if (err) throw err
94 |
95 | if (doc.length) {
96 | res.render('./subreddit/subreddit_link', {
97 | info: doc[0],
98 | karma: karma,
99 | state: subscribed,
100 | isAuth: req.isAuthenticated(),
101 | })
102 | }
103 | })
104 | })
105 | }
106 | exports.subreddit_link = function (req, res) {
107 | let type = "link"
108 |
109 | function checkURL(url) {
110 | return (url.match(/\.(jpeg|jpg|gif|png)$/) != null);
111 | }
112 |
113 | if (checkURL(req.body.link)) {
114 | type = "img"
115 | }
116 |
117 | Post({
118 | title: req.body.title,
119 | body: req.body.body,
120 | username: req.session.user,
121 | type: type,
122 | link: req.body.link,
123 | subreddit: req.params.subreddit,
124 | }).save(function (err, doc) {
125 | if (err) throw error;
126 |
127 | console.log(`[${req.params.subreddit}] link submitted!`)
128 | res.redirect(`/r/${req.params.subreddit}`)
129 | })
130 | }
131 |
132 | exports.subreddit_search = function (req, res) {
133 | let subreddit = undefined
134 | let posts = undefined
135 | let subscribed = false
136 | let karma = 0
137 |
138 | Profile.find({
139 | username: req.session.user
140 | }, function (err, result) {
141 | if (err) throw err;
142 |
143 | if (result.length) {
144 | karma = result[0]['karma_post'] + result[0]['karma_comment']
145 | }
146 | });
147 |
148 | Subreddit.find({
149 | name: req.params.subreddit
150 | }, function (err, doc) {
151 | if (err) throw err
152 |
153 | if (doc.length) {
154 | subreddit = doc[0]
155 | }
156 | }).then(function () {
157 | Profile.find({
158 | username: req.session.user,
159 | subscribed: req.params.subreddit,
160 | }, function (err, doc) {
161 | if (err) throw err;
162 |
163 | if (!doc.length) {
164 | // res.send("Unable to find subreddit state")
165 | return;
166 | } else {
167 | subscribed = true
168 | }
169 | }).then(function () {
170 | Post.find({
171 | $and: [{
172 | subreddit: req.params.subreddit
173 | },
174 | {
175 | title: {
176 | $regex: '.*' + req.body.query + '.*',
177 | $options: 'i'
178 | }
179 | }
180 | ]
181 | }).sort({
182 | votes: '-1'
183 | }).exec(function (err, result) {
184 | if (err) throw err;
185 | if (result.length) {
186 | posts = result
187 | }
188 |
189 | console.log(`[${req.params.subreddit}] searching for posts which contain '{${req.body.query}}'`)
190 | res.render("./subreddit/subreddit_search", {
191 | info: subreddit,
192 | posts: result,
193 | karma: karma,
194 | state: subscribed,
195 | query: req.body.query,
196 | isAuth: req.isAuthenticated(),
197 | })
198 | })
199 | })
200 | })
201 | }
202 |
203 |
204 | // SUBMITING A POST
205 | exports.front_post = function (req, res) {
206 | Post({
207 | title: req.body.title,
208 | body: req.body.text,
209 | username: req.session.user,
210 | type: "post",
211 | subreddit: req.body.subreddit,
212 | }).save(function (err, doc) {
213 | if (err) throw err;
214 |
215 | console.log(`[Frontpage] post submitted to [${req.body.subreddit}]`)
216 | res.redirect(`/r/${req.body.subreddit}/${doc._id}/comments`);
217 | });
218 | }
219 |
220 | // SUBMITING A LINK
221 | exports.front_link = function (req, res) {
222 | let type = "link"
223 |
224 | function checkURL(url) {
225 | return (url.match(/\.(jpeg|jpg|gif|png)$/) != null);
226 | }
227 |
228 | if (checkURL(req.body.link)) {
229 | type = "img"
230 | }
231 |
232 | Post({
233 | title: req.body.title,
234 | link: req.body.link,
235 | username: req.session.user,
236 | type: type,
237 | subreddit: req.body.subreddit,
238 | }).save(function (err, doc) {
239 | if (err) throw err;
240 |
241 | console.log(`[Frontpage] link submitted to [${req.body.subreddit}]`)
242 | res.redirect(`/r/${req.body.subreddit}/${doc._id}/comments`);
243 | });
244 | }
245 |
246 |
247 | // SUBMITING A SUBREDDIT
248 | exports.subreddit = function (req, res) {
249 | Profile.update({
250 | username: req.session.user
251 | }, {
252 | $push: {
253 | owned: req.body.subreddit
254 | }
255 | },
256 | function (err, doc) {
257 | if (err) throw err;
258 |
259 | }).then(function () {
260 | Subreddit({
261 | name: req.body.subreddit,
262 | description: req.body.description
263 | }).save(function (err, doc) {
264 | if (err) throw err
265 |
266 | console.log(`[Frontpage] ${req.body.subreddit} subreddit created`)
267 | res.redirect(`/r/${req.body.subreddit}`);
268 | });
269 | });
270 | }
271 |
272 | // SEARCHING FOR A POST
273 | exports.front_search = function (req, res) {
274 | let subscribed = undefined;
275 | let subreddits = undefined;
276 | let posts = undefined;
277 | let karma = 0;
278 |
279 | Profile.find({
280 | username: req.session.user
281 | }, function (err, result) {
282 | if (err) throw err;
283 | if (result.length) {
284 | subscribed = result[0]['subscribed'];
285 | karma = result[0]['karma_post'] + result[0]['karma_comment']
286 | }
287 | })
288 | .then(function () {
289 | Subreddit.find({}, function (err, doc) {
290 | if (err) throw err;
291 |
292 | if (doc.length) {
293 | subreddits = doc
294 | }
295 | })
296 | .then(function () {
297 | Post.find({
298 | title: {
299 | $regex: '.*' + req.body.query + '.*',
300 | $options: 'i'
301 | }
302 | })
303 | .sort({
304 | votes: '-1'
305 | })
306 | .exec(function (err, result) {
307 | if (err) throw err;
308 | if (result.length) {
309 | posts = result
310 | }
311 |
312 | console.log(`[Frontpage] searching for posts which contain '{${req.body.query}}'`)
313 | res.render("./front/front_search", {
314 | posts: result,
315 | subreddits: subreddits,
316 | subscribed: subscribed,
317 | karma: karma,
318 | query: req.body.query,
319 | isAuth: req.isAuthenticated()
320 | })
321 | });
322 | });
323 | });
324 | }
325 |
326 | exports.front_post_view = function (req, res) {
327 | let subscribed = undefined;
328 | let karma = 0;
329 |
330 | Profile.find({
331 | username: req.session.user
332 | }, function (err, result) {
333 | if (err) throw err;
334 |
335 | if (result.length) {
336 | subscribed = result[0]['subscribed']
337 | karma = result[0]['karma_post'] + result[0]['karma_comment']
338 |
339 | }
340 |
341 | res.render("./front/front_post", {
342 | isAuth: req.isAuthenticated(),
343 | subscribed: subscribed,
344 | karma: karma
345 | });
346 | })
347 | }
348 |
349 | exports.front_post_view = function (req, res) {
350 | let subscribed = undefined;
351 | let karma = 0;
352 |
353 | Profile.find({
354 | username: req.session.user
355 | }, function (err, result) {
356 | if (err) throw err;
357 |
358 | if (result.length) {
359 | subscribed = result[0]['subscribed']
360 | karma = result[0]['karma_post'] + result[0]['karma_comment']
361 |
362 | }
363 |
364 | res.render("./front/front_post", {
365 | isAuth: req.isAuthenticated(),
366 | subscribed: subscribed,
367 | karma: karma
368 | });
369 | })
370 | }
371 | exports.front_link_view = function (req, res) {
372 | let subscribed = undefined;
373 | let karma = 0;
374 |
375 | Profile.find({
376 | username: req.session.user
377 | }, function (err, result) {
378 | if (err) throw err;
379 |
380 | if (result.length) {
381 | subscribed = result[0]['subscribed']
382 | karma = result[0]['karma_post'] + result[0]['karma_comment']
383 | }
384 |
385 | res.render("./front/front_link", {
386 | isAuth: req.isAuthenticated(),
387 | karma: karma,
388 | subscribed: subscribed
389 | });
390 | })
391 | }
392 | exports.subreddit_view = function (req, res) {
393 | let subscribed = undefined;
394 | let karma = 0;
395 |
396 | Profile.find({
397 | username: req.session.user
398 | }, function (err, result) {
399 | if (err) throw err;
400 |
401 | if (result.length) {
402 | subscribed = result[0]['subscribed']
403 | karma = result[0]['karma_post'] + result[0]['karma_comment']
404 | }
405 |
406 | res.render("./front/front_subreddit", {
407 | isAuth: req.isAuthenticated(),
408 | karma: karma,
409 | subscribed: result[0]['subscribed']
410 | });
411 | })
412 | }
--------------------------------------------------------------------------------
65 |
66 |- <%= comment.username %>
•
67 | - <%= comment.votes %> points
•
68 |
69 |
70 |72 |-
73 |
74 |
75 |
76 |
77 | - save
78 |
79 | <% if(comment.username == locals.user) { %>
80 | - edit
81 | - delete
82 | <% } %>
83 |
84 |