Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ FACEBOOK_SECRET=insert-facebook-app-secret-here
AWS_ACCESS_KEY_ID=YOUR_AWS_KEY
AWS_SECRET_ACCESS_KEY=YOUR_AWS_SECRET
AWS_PROFILE_PIC_BUCKET_NAME=bucket-name
AWS_TOPIC_BUCKET_NAME=YOUR_BUCKET_NAME

STRIPE_PUBLIC_KEY=public_key
STRIPE_SECRET_KEY=secret_key
Expand Down
3 changes: 2 additions & 1 deletion config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ const config = {
secretKey: envVars.RECAPTCHA_SECRET_KEY
},
aws: {
profilePicBucketName: envVars.AWS_PROFILE_PIC_BUCKET_NAME
profilePicBucketName: envVars.AWS_PROFILE_PIC_BUCKET_NAME,
topicBucketName: envVars.AWS_TOPIC_BUCKET_NAME,
},
email: {
fromAddress: envVars.EMAIL_FROM_ADDRESS
Expand Down
1 change: 1 addition & 0 deletions devops/.env.ci
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ ALGOLIA_POSTS_INDEX=algolia_posts_index
AWS_ACCESS_KEY_ID=YOUR_AWS_KEY
AWS_SECRET_ACCESS_KEY=YOUR_AWS_SECRET
AWS_PROFILE_PIC_BUCKET_NAME=YOUR_BUCKET_NAME
AWS_TOPIC_BUCKET_NAME=YOUR_BUCKET_NAME

SEND_GRID_KEY=send_grid_key

Expand Down
816 changes: 408 additions & 408 deletions package-lock.json

Large diffs are not rendered by default.

110 changes: 73 additions & 37 deletions server/controllers/comment.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import Comment from '../models/comment.model';
import User from '../models/user.model';
import ForumThread from '../models/forumThread.model';
import ForumNotifications from '../helpers/forumNotifications.helper';
import { subscribePostFromEntity, notifySubscribersFromEntity } from '../controllers/postSubscription.controller';
import { subscribePostFromEntity, notifyPostSubscribersFromEntity } from '../controllers/postSubscription.controller';
import { subscribeTopicPage, notifySubscribers } from '../controllers/topicPageSubscription.controller';
import { saveAndNotifyUser } from '../controllers/notification.controller';
/*
* Load comment and append to req.
Expand Down Expand Up @@ -194,51 +195,85 @@ async function update(req, res, next) {
* $ref: '#/responses/NotFound'
*/

async function subscribeAndNotifyCommenter(entityId, user, ignoreNotify) {
const post = await subscribePostFromEntity(entityId, user);

if (!post) {
return;
}

async function subscribeAndNotifyCommenter(entityId, entityType, user, ignoreNotify) {
const payload = {
notification: {
title: `New comment from @${user.name}`,
body: post.title.rendered,
data: {
user: user._id,
slug: post.slug,
thread: post.thread
}
},
type: 'comment',
entity: post._id
};

// notify all subscribers
await notifySubscribersFromEntity(entityId, user, payload, ignoreNotify);
if (entityType.toLowerCase() === 'forumthread') {
const post = await subscribePostFromEntity(entityId, user);
payload.notification.body = post.title.rendered;
payload.notification.data = {
user: user.username,
slug: post.slug,
url: `/post/${post._id}/${post.slug}`,
thread: post.thread
};
payload.entity = post._id;
// notify all subscribers
await notifyPostSubscribersFromEntity(entityId, user, payload, ignoreNotify);
}

if (entityType.toLowerCase() === 'topic') {
const topic = await subscribeTopicPage(entityId, user);
payload.notification.body = topic.name;
payload.notification.data = {
user: user.username,
slug: topic.slug,
url: `/topic/${topic.slug}`
};
payload.entity = entityId;
// notify all subscribers
await notifySubscribers(entityId, user, payload, ignoreNotify);
}
}

async function subscribeAndNotifyMentioned(entityId, mentioned, user) {
const post = await subscribePostFromEntity(entityId, mentioned);
async function subscribeAndNotifyMentioned(entityId, entityType, mentioned, user) {
if (entityType.toLowerCase() === 'forumthread') {
const post = await subscribePostFromEntity(entityId, mentioned);

const payload = {
notification: {
title: `You were mentioned by @${user.name}`,
body: post.title.rendered,
data: {
user: user.username,
mentioned: mentioned._id,
slug: post.slug,
url: `/post/${post._id}/${post.slug}`,
thread: post.thread
}
},
type: 'mention',
entity: post._id
};

const payload = {
notification: {
title: `You were mentioned by @${user.name}`,
body: post.title.rendered,
data: {
user: user._id,
mentioned: mentioned._id,
slug: post.slug,
thread: post.thread
}
},
type: 'mention',
entity: post._id
};
// just notify the mentioned user
saveAndNotifyUser(payload, mentioned._id);
}
if (entityType.toLowerCase() === 'topic') {
const topic = await subscribeTopicPage(entityId, mentioned);
const payload = {
notification: {
title: `You were mentioned by @${user.name}`,
body: topic.name,
data: {
user: user.username,
mentioned: mentioned._id,
url: `/topic/${topic.slug}`,
slug: topic.slug
}
},
type: 'mention',
entity: entityId
};

// just notify the mentioned user
saveAndNotifyUser(payload, mentioned._id);
// just notify the mentioned user
saveAndNotifyUser(payload, mentioned._id);
}
}

async function create(req, res, next) {
Expand All @@ -254,7 +289,7 @@ async function create(req, res, next) {
usersMentioned = await idsToUsers(mentions);
comment.mentions = usersMentioned;
usersMentioned.forEach((mentioned) => {
subscribeAndNotifyMentioned(entityId, mentioned, user);
subscribeAndNotifyMentioned(entityId, entityType, mentioned, user);
});
}

Expand All @@ -263,6 +298,7 @@ async function create(req, res, next) {
}

comment.rootEntity = entityId;
comment.entityType = entityType;
// If this is a child comment we need to assign it's parent
if (parentCommentId) {
comment.parentComment = parentCommentId;
Expand All @@ -273,7 +309,7 @@ async function create(req, res, next) {
.save()
.then(async (commentSaved) => {
// don't wait
subscribeAndNotifyCommenter(entityId, user, mentions);
subscribeAndNotifyCommenter(entityId, entityType, user, mentions);

// TODO: result key is not consistent with other responses, consider changing this
if (entityType && entityType.toLowerCase() === 'forumthread') {
Expand Down
2 changes: 1 addition & 1 deletion server/controllers/notification.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ async function execSendNotification(user, next) {
.exec();

notifications.forEach((n) => {
if (['comment', 'upvote', 'mention'].includes(n.type)) {
if (['comment', 'upvote', 'mention'].includes(n.type) && !n.notification.data.url) {
n.notification.data.url = `/post/${n.entity}/${n.notification.data.slug}`; // eslint-disable-line no-param-reassign
}
});
Expand Down
2 changes: 1 addition & 1 deletion server/controllers/post.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ function search(req, res, next) {
isEnd = (nextPage === reply.nbPages);
slugs = reply.hits.map(h => h.slug);

Post.list({ slugs })
Post.find({ slug: { $in: slugs } })
.then(async (posts) => {
posts = await getAdFreePostsIfSubscribed(posts, req.fullUser, next); //eslint-disable-line
res.json({ posts, isEnd, nextPage });
Expand Down
4 changes: 2 additions & 2 deletions server/controllers/postSubscription.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ async function subscribePostFromEntity(entityId, user) {
return post;
}

async function notifySubscribersFromEntity(entityId, user, payload, ignoreNotify) {
async function notifyPostSubscribersFromEntity(entityId, user, payload, ignoreNotify) {
const post = await getPostFromThread(entityId);
if (!post) return;
await notifySubscribers(post, user, payload, ignoreNotify);
Expand Down Expand Up @@ -54,7 +54,7 @@ async function notifySubscribers(post, user, payload, ignoreNotify = []) {

export default {
subscribePostFromEntity,
notifySubscribersFromEntity,
notifyPostSubscribersFromEntity,
subscribePost,
notifySubscribers
};
19 changes: 16 additions & 3 deletions server/controllers/relatedLink.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import request from 'request';
import { getMetadata } from 'page-metadata-parser';
import jsdom from 'jsdom';
import RelatedLink from '../models/relatedLink.model';
import TopicPage from '../models/topicPage.model';
import Topic from '../models/topic.model';

const { JSDOM } = jsdom;

Expand Down Expand Up @@ -75,9 +77,9 @@ function remove(req, res, next) {
* $ref: '#/responses/NotFound'
*/

function create(req, res, next) {
async function create(req, res, next) {
const { user } = req;
const { postId } = req.params;
const { postId, slug } = req.params;
const { url, type = 'link' } = req.body;
const options = {
url,
Expand All @@ -87,6 +89,15 @@ function create(req, res, next) {
}
};

let topic;
let topicPage;
let entityType = 'post';
if (slug) {
topic = await Topic.findOne({ slug }).lean();
topicPage = await TopicPage.findOne({ topic: topic._id }).lean();
entityType = 'topic';
}

request(options, (error, reply, body) => {
if (error || !body || reply.statusCode !== 200) {
return next(error);
Expand All @@ -99,7 +110,9 @@ function create(req, res, next) {
relatedLink.url = url;
relatedLink.title = metadata.title || url;
relatedLink.type = type;
relatedLink.post = postId;
relatedLink.entityType = entityType;
relatedLink.post = postId || undefined;
relatedLink.topicPage = (topicPage) ? topicPage._id : undefined;
relatedLink.author = user._id;

if (metadata.icon) {
Expand Down
32 changes: 31 additions & 1 deletion server/controllers/topic.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,30 @@ async function create(req, res) {
.catch(err => res.status(422).json(err.errmsg));
}

async function add(req, res) {
const exist = await Topic.findOne({ name: req.body.data.name });
if (exist) return res.status(400).send(`A ${req.body.data.name} Topic already exists`);
const topic = new Topic(req.body.data);
return topic
.save()
.then((topicSaved) => {
res.status(201).json(topicSaved);
})
.catch(err => res.status(400).send(err.errmsg));
}

async function get(req, res) {
const topic = await Topic.findById(req.params.topicId)
.populate('maintainer', 'name email website avatarUrl isAdmin bio');
res.send(topic);
}

async function getFull(req, res) {
const topics = await Topic.find()
.populate('maintainer', 'name email website avatarUrl isAdmin');
res.send(topics);
}

async function index(req, res) {
if (req.query.userId) {
const user = await User.findById(req.query.userId);
Expand Down Expand Up @@ -101,6 +125,7 @@ function mostPopular(req, res) {
});
}

// old, still used in mobile?
function show(req, res) {
Topic.find({ slug: req.params.slug }, async (err, topic) => {
if (err) return;
Expand Down Expand Up @@ -129,7 +154,9 @@ function show(req, res) {
}

function update(req, res) {
Topic.findByIdAndUpdate(req.params.id, { $set: req.body }, (err) => {
const data = req.body;
if (!data.maintainer) data.maintainer = null;
Topic.findByIdAndUpdate(req.params.topicId, { $set: data }, (err) => {
if (err) return;
res.send('Topic udpated.');
});
Expand Down Expand Up @@ -355,6 +382,9 @@ async function addTopicsToPost(req, res) {
*/

export default {
add,
get,
getFull,
create,
index,
mostPopular,
Expand Down
Loading