Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Updated homepage layout. Markdown parser working. Upvote + Downvoting…

… threads works
  • Loading branch information...
commit f2ad542b1af19017484f5016033453af89ba64f4 1 parent 9dce830
@SlashmanX authored
View
101 app.js
@@ -24,7 +24,6 @@ var RedditStrategy = require('passport-reddit').Strategy;
var crypto = require('crypto');
var mongoose = require('mongoose');
var marked = require('marked');
-var expose = require('express-expose');
var User = require('./models/User.js');
@@ -135,10 +134,19 @@ app.use(express.session({
}));
app.use(passport.initialize());
app.use(passport.session());
+
+app.locals.moment = require('moment');
+app.locals.md = function(text) {
+ if(text)
+ return require('marked')(text);
+ return text;
+}
app.use(app.router);
+
+
app.use(require('less-middleware')({ src: __dirname + '/public', compress: true, optimization: 2 }));
app.use(express.static(path.join(__dirname, 'public')));
-app.expose(marked, 'md');
+
// development only
if ('development' == app.get('env')) {
@@ -146,15 +154,18 @@ if ('development' == app.get('env')) {
}
app.get('/', function(req, res) {
- /*if(req.user) {
- getUserSubreddits({user: req.user}, function(err, data) {
- console.log(data);
+ if(req.user) {
+ getUserHomePage({user: req.user}, function(err, data) {
+ res.render('home', {title: 'Home', threads: data, user: req.user})
})
- }*/
- getActiveThreads({limit: 10, time: ACTIVE_THREAD_TIME_LIMIT}, function(err, data) {
+ }
+ else {
+ res.render('home', {title: 'Home', threads: [], user: req.user});
+ }
+ /*getActiveThreads({limit: 10, time: ACTIVE_THREAD_TIME_LIMIT}, function(err, data) {
var active = data.data.data;
res.render('home', {title: 'Home', activeThreads: active, user: req.user });
- })
+ })*/
});
app.get('/r/:subreddit/', function(req, res) {
res.render('stream', { title: req.params.subreddit, subreddit: req.params.subreddit, user: req.user });
@@ -166,7 +177,7 @@ app.get('/r/:subreddit/comments/:topicid/:topicname?/*', function(req, res) {
else
{
getThreadAndComments({id: req.params.topicid}, function(err, topic) {
- res.render('stream', { title: req.params.subreddit, topic: topic, user: req.user, marked: marked });
+ res.render('stream', { title: req.params.subreddit, topic: topic, user: req.user });
})
}
});
@@ -202,7 +213,26 @@ app.get('/logout/', function(req, res){
res.redirect('/');
});
-var parser = JSONStream.parse() //emit parts that match this path (any element of the rows array)
+app.post('/upvote/thread/:threadid/', function(req, res){
+
+ voteThing({id: 't3_'+ req.params.threadid, dir: 1, user: req.user}, function(err, status) {
+ if(!err)
+ res.send('ok', 200);
+ else
+ res.send(err, 403);
+ })
+})
+
+app.post('/downvote/thread/:threadid/', function(req, res){
+
+ voteThing({id: 't3_'+ req.params.threadid, dir: -1, user: req.user}, function(err, status) {
+ if(!err)
+ res.send('ok', 200);
+ else
+ res.send(err, 403);
+ })
+})
+/*var parser = JSONStream.parse() //emit parts that match this path (any element of the rows array)
var req = request({url: 'http://stream.redditanalytics.com'})
var logger = es.mapSync(function (data) {
io.sockets.in(data.subreddit).emit('comment', {comment: data});
@@ -211,6 +241,8 @@ var logger = es.mapSync(function (data) {
req.pipe(parser)
parser.pipe(logger);
+*/
+
server.listen(app.get('port'));
@@ -269,4 +301,53 @@ function getUserSubreddits(data, callback) {
callback(null, user_subreddits);
}
})
+}
+
+function getUserHomePage(data, callback) {
+ var url = 'https://oauth.reddit.com/.json';
+ var oauth = { consumer_key: REDDIT_CONSUMER_KEY,
+ consumer_secret: REDDIT_CONSUMER_SECRET,
+ token: data.user.token,
+ token_secret: data.user.token_secret
+ }
+
+ request({url: url, headers: {'User-Agent': 'reddit-live/0.1 by SlashmanX', 'Authorization': 'bearer '+data.user.token}}, function(error, res, body) {
+ if(error) {
+ callback(error, null);
+ }
+ else {
+ var data = JSON.parse(body);
+ var links = data.data.children;
+ var user_links = [];
+ for(var link in links) {
+ var tmp = links[link];
+ user_links.push(tmp.data);
+ }
+ callback(null, user_links);
+ }
+ })
+}
+
+function voteThing(data, callback) {
+ var url = 'https://oauth.reddit.com/api/vote';
+
+ request.post(url, {
+ form:
+ {
+ dir: data.dir,
+ id: data.id
+ },
+ headers:
+ {
+ 'User-Agent': 'reddit-live/0.1 by SlashmanX',
+ 'Authorization': 'bearer '+data.user.token
+ }
+ }, function(error, res, body) {
+ if(!error && res.statusCode == 200) {
+ callback(null, 'ok');
+ }
+ else {
+ callback(error, 'error');
+ }
+ })
}
View
0  models/Subreddit.js
No changes.
View
5 models/User.js
@@ -1,6 +1,7 @@
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;
+var Subreddit = require('./Subreddit');
var userSchema = new Schema({
username: String,
@@ -8,7 +9,9 @@ var userSchema = new Schema({
token_secret: String,
profile: {
id: String
- }
+ },
+ subscribedTo: [{type: ObjectId, ref: 'Subreddit'}],
+ lastUpdated: {type: Date, default: Date.now}
});
module.exports = mongoose.model('User', userSchema);
View
26 public/javascripts/app.js
@@ -15,4 +15,30 @@ socket.on('comment', function(data) {
$(newComment).hide().prependTo('.comments-space').fadeIn("fast");
}
+})
+
+$('span.upvote').on('click', function() {
+ var parent_row = $($(this).closest('.row[id]'))[0];
+ var thread_id = ($(parent_row).attr('id'));
+
+ $.ajax({
+ type: "POST",
+ url: "/upvote/thread/"+ thread_id +"/",
+ success: function() {
+ console.log('upvoted');
+ }
+ })
+})
+
+$('span.downvote').on('click', function() {
+ var parent_row = $($(this).closest('.row[id]'))[0];
+ var thread_id = ($(parent_row).attr('id'));
+
+ $.ajax({
+ type: "POST",
+ url: "/downvote/thread/"+ thread_id +"/",
+ success: function() {
+ console.log('downvoted');
+ }
+ })
})
View
10 public/stylesheets/style.css
@@ -835,3 +835,13 @@ th.hidden-lg,td.hidden-lg{display:table-cell !important}
th.visible-print,td.visible-print{display:none !important}
@media print{.visible-print{display:block !important}tr.visible-print{display:table-row !important} th.visible-print,td.visible-print{display:table-cell !important} .hidden-print{display:none !important}tr.hidden-print{display:none !important} th.hidden-print,td.hidden-print{display:none !important}}body{padding-top:70px}
.navbar{margin-bottom:30px}
+.vote{position:relative;top:1px;display:inline-block;font-family:'Glyphicons Halflings';font-style:normal;font-weight:normal;line-height:1;-webkit-font-smoothing:antialiased;font-size:30px;color:#D0D0D0;cursor:pointer}.vote.liked{color:#ff8b60}
+.vote.disliked{color:#9494ff}
+.upvote{position:relative;top:1px;display:inline-block;font-family:'Glyphicons Halflings';font-style:normal;font-weight:normal;line-height:1;-webkit-font-smoothing:antialiased;font-size:30px;color:#D0D0D0;cursor:pointer}.upvote.liked{color:#ff8b60}
+.upvote.disliked{color:#9494ff}
+.upvote:before{content:"\e113"}
+.upvote:hover{color:#ff8b60}
+.downvote{position:relative;top:1px;display:inline-block;font-family:'Glyphicons Halflings';font-style:normal;font-weight:normal;line-height:1;-webkit-font-smoothing:antialiased;font-size:30px;color:#D0D0D0;cursor:pointer}.downvote.liked{color:#ff8b60}
+.downvote.disliked{color:#9494ff}
+.downvote:before{content:"\e114"}
+.downvote:hover{color:#9494ff}
View
34 public/stylesheets/style.less
@@ -1,9 +1,43 @@
@import "./lib/bootstrap.less";
+@upvote_color: #FF8B60;
+@downvote_color: #9494FF;
+
+
body {
padding-top: 70px;
}
.navbar {
margin-bottom: 30px;
+}
+
+.vote {
+ .glyphicon;
+ font-size: 30px;
+ color: #D0D0D0;
+ cursor: pointer;
+
+ &.liked {
+ color: @upvote_color;
+ }
+
+ &.disliked {
+ color: @downvote_color;
+ }
+}
+.upvote {
+ .vote;
+ .glyphicon-chevron-up;
+ &:hover {
+ color: @upvote_color;
+ }
+}
+
+.downvote {
+ .vote;
+ .glyphicon-chevron-down;
+ &:hover {
+ color: @downvote_color;
+ }
}
View
7 views/home.jade
@@ -1,7 +1,8 @@
extends layout
block content
- h3 Active Threads
+ h3 Your Homepage
.active-threads
- - each thread in activeThreads
- include partials/thread.jade
+ - each thread in threads
+ - thread.score = thread.ups - thread.downs
+ include partials/homepage_thread.jade
View
4 views/layout.jade
@@ -22,9 +22,9 @@ body
.navbar-collapse.collapse
ul.nav.navbar-nav
li.active
- a(href='#') Link
+ a(href='#') Home
li
- a(href='#') Link
+ a(href='#') Active Threads
li
a(href='#') Link
li.dropdown
View
0  views/partials/thread.jade → views/partials/active_thread.jade
File renamed without changes
View
2  views/partials/comment.jade
@@ -1,3 +1,3 @@
.panel.panel-default(id=comment.id)
.panel-heading <strong>#{comment.author}</strong> in <a href = "http://reddit.com/#{topic.info.id}">#{topic.info.title}</a> (<a href="http://reddit.com/r/#{topic.info.subreddit}">#{topic.info.subreddit}</a>) #{comment.score} pts <a class = 'pull-right' target='_blank' href = 'http://reddit.com#{topic.info.permalink}#{comment.id}'><span class = 'glyphicon glyphicon-comment'></span></a>
- .panel-body=comment.body
+ .panel-body!= md(comment.body)
View
0  views/partials/homepage_thread.jade
No changes.
Please sign in to comment.
Something went wrong with that request. Please try again.