Skip to content

Commit

Permalink
Added support for user information and follow
Browse files Browse the repository at this point in the history
  • Loading branch information
am1t committed Jul 20, 2018
1 parent d8a84e8 commit 083040f
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 42 deletions.
119 changes: 104 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"license": "MIT",
"dependencies": {
"bcryptjs": "^2.4.3",
"body-parser": "^1.18.3",
"cheerio": "^1.0.0-rc.2",
"connect-flash": "^0.1.1",
"crypto-js": "^3.1.9-1",
Expand All @@ -17,6 +18,7 @@
"express-session": "^1.15.6",
"helmet": "^3.12.1",
"linkscrape": "^1.0.0",
"marked": "^0.4.0",
"method-override": "^2.3.10",
"mongoose": "^5.0.6",
"passport": "^0.4.0",
Expand Down
25 changes: 20 additions & 5 deletions public/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ body{
font-family: "Libre Franklin";
font-size: 16px;
}

img {
max-width: 100%;
max-height: 100%;
}

.site-title{
font-size: 35px;
color: #000;
Expand All @@ -18,6 +24,10 @@ body{
text-transform: uppercase;
}

.author-bio{
font-size:18px;
}

.thread_banner{
border: 1px solid #24292e;
font-size: 18px;
Expand Down Expand Up @@ -73,14 +83,19 @@ body{
a.edit_rec { color: deepskyblue}
a:hover.edit_rec { color: #fff}

.follow_btn {
.follow-btn {
float: right;
font-size: 20px
margin-right: 40px;
font-size: 18px;
margin: 10px 0px;
padding: 5px 20px;
}

.follow_btn a { padding: 5px 20px;color:white}
.follow_btn a:hover {color: red}
a .follow-btn { padding: 5px 20px;color:white}
a:hover .follow-btn {color: red}

.follow-link {padding:5px 0;font-size:20px;}
.profile-link {font-size: 20px; color:darkgray;padding-left:2px;}
a:hover.profile-link {color: red; text-decoration: none;}

.recos_context{
color: #000;
Expand Down
43 changes: 39 additions & 4 deletions routes/discover.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const express = require('express');
const mongoose = require('mongoose');
var request = require('request');
const request = require('request');
const passport = require('passport');
const {ensureAuthenticated} = require('../helper/auth');
const router = express.Router();
var CryptoJS = require("crypto-js");
const CryptoJS = require("crypto-js");
const marked = require('marked');

const { fetch_stream, fetch_discover, fetch_following, fetch_user_information,
fetch_users_from_stream, fetch_user_from_discover } = require('../helper/utils');
Expand Down Expand Up @@ -94,8 +95,8 @@ const fetch_posts_by_type = function(items){
var at_identifier = "<p><a href=\"https://micro.blog/";

if(content.startsWith(at_identifier)){
interactions.push(content);
} else { original.push(content); }
if(interactions.length < 10) interactions.push({content:content, url: item.url}) ;
} else { if(original.length < 10) original.push({content:content, url: item.url}); }
});
posts = {interactions : interactions, originals : original}
resolve(posts);
Expand All @@ -116,6 +117,7 @@ router.get('/user/:userid', ensureAuthenticated, async (req, res, next) => {
const posts = await fetch_posts_by_type(user_info.items);

author.userid = req.params.userid;
author.bio = marked(user_info._microblog.bio);

res.render('discover/user_info', {
author: author,
Expand All @@ -132,4 +134,37 @@ router.get('/user/:userid', ensureAuthenticated, async (req, res, next) => {
}
});

//Follow user route
router.post('/user/follow', ensureAuthenticated, async (req, res) => {
let errors = [];
let follow_api = 'http://micro.blog/users/follow';
var app_token = CryptoJS.AES.decrypt(req.user.token, appconfig.seckey).toString(CryptoJS.enc.Utf8);
const error_string = "Error while processing the request.";

try {
var userid = req.body.userid;
request.post(
{
url:follow_api,
headers: {'Authorization': 'Token ' + app_token},
form:{username:userid}
}, function(error, response, body){
if(body.indexOf(error_string) != -1){
console.error("Failed to follow the user");
errors.push({text:"Failed to follow the user"});
req.flash('error_msg', 'Failed to follow the user');
res.redirect('/discover/user');
} else {
req.flash('success_msg', 'Successfully followed ' + userid);
res.redirect('/discover/user');
}
});
} catch (error) {
console.error(error);
errors.push({text:"Failed to follow the user"});
req.flash('error_msg', 'Failed to follow the user');
res.redirect('/discover/user');
}
});

module.exports = router;
10 changes: 7 additions & 3 deletions routes/threads.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ router.get('/refresh/:id', ensureAuthenticated, (req, res) => {
post_link = 'http://micro.blog/posts/conversation?id=' + thread.postId;
}

console.log(post_link);
request.get({
url: post_link,
headers: {'Authorization': 'Token DEB996A63C13C04E8387'}
Expand Down Expand Up @@ -142,7 +141,12 @@ router.get('/refresh/:id', ensureAuthenticated, (req, res) => {
req.flash('success_msg', 'Thread Refreshed Successfully');
res.redirect('/micro/threads/' + thread.id + '/recommendations');
}
});
})
.catch(function(error){
console.warn("Some links details cannot be fetched, possible reason - no title.");
req.flash('info_msg', 'Thread Refreshed Successfully, with some failures');
res.redirect('/micro/threads/' + thread.id + '/recommendations');
});;

});
});
Expand All @@ -152,7 +156,7 @@ router.get('/refresh/:id', ensureAuthenticated, (req, res) => {
});
})
} catch (error) {
console.log(error);
console.error(error);
req.flash('info_msg', 'Thread Refreshed Successfully, with some failures');
res.redirect('/micro/threads/' + thread.id + '/recommendations');
}
Expand Down
4 changes: 1 addition & 3 deletions views/discover/user_discovery.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
<span style="font-size:20px;color:darkgray;padding-left:20px;">
{{this}}
</span>
<button type="button" class="follow_btn btn btn-primary">
<a href="https://micro.blog/{{@key}}" target="_blank">Follow</a>
</button>
<a class="follow-btn btn btn-primary" href="/discover/user/{{@key}}">Know More</a>
</div>
</li>
{{else}}
Expand Down
Loading

0 comments on commit 083040f

Please sign in to comment.