Skip to content

Commit

Permalink
Merge pull request #28 from CMU-313/shanting/project1changes
Browse files Browse the repository at this point in the history
Shanting - Project 1 changes
  • Loading branch information
shantinghou authored Sep 11, 2024
2 parents c043ef9 + 509fe61 commit 64e9003
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions src/topics/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,24 @@ module.exports = function (Topics) {
return topicData.tid;
};

async function validateContent(uid, content) {
Topics.checkContent(content);
if (!await posts.canUserPostContentWithLinks(uid, content)) {
throw new Error(`[[error:not-enough-reputation-to-post-links, ${meta.config['min:rep:post-links']}]]`);
}
}

async function handleFollowAndNotifications(uid, settings, postData, topics) {
if (uid > 0 && settings.followTopicsOnCreate) {
await Topics.follow(postData.tid, uid);
}
if (parseInt(uid, 10) && !topics[0].scheduled) {
user.notifications.sendTopicNotificationToFollowers(uid, topics[0], postData);
Topics.notifyTagFollowers(postData, uid);
categories.notifyCategoryFollowers(postData, uid);
}
}

Topics.post = async function (data) {
data = await plugins.hooks.fire('filter:topic.post', data);
const { uid } = data;
Expand All @@ -98,10 +116,7 @@ module.exports = function (Topics) {
await Topics.validateTags(data.tags, data.cid, uid);
data.tags = await Topics.filterTags(data.tags, data.cid);
if (!data.fromQueue && !isAdmin) {
Topics.checkContent(data.content);
if (!await posts.canUserPostContentWithLinks(uid, data.content)) {
throw new Error(`[[error:not-enough-reputation-to-post-links, ${meta.config['min:rep:post-links']}]]`);
}
await validateContent(uid, data.content);
}

if (!categoryExists) {
Expand Down Expand Up @@ -135,9 +150,7 @@ module.exports = function (Topics) {
throw new Error('[[error:no-topic]]');
}

if (uid > 0 && settings.followTopicsOnCreate) {
await Topics.follow(postData.tid, uid);
}
handleFollowAndNotifications(uid, settings, postData, topics);
const topicData = topics[0];
topicData.unreplied = true;
topicData.mainPost = postData;
Expand All @@ -151,12 +164,6 @@ module.exports = function (Topics) {
analytics.increment(['topics', `topics:byCid:${topicData.cid}`]);
plugins.hooks.fire('action:topic.post', { topic: topicData, post: postData, data: data });

if (parseInt(uid, 10) && !topicData.scheduled) {
user.notifications.sendTopicNotificationToFollowers(uid, topicData, postData);
Topics.notifyTagFollowers(postData, uid);
categories.notifyCategoryFollowers(postData, uid);
}

return {
topicData: topicData,
postData: postData,
Expand Down

0 comments on commit 64e9003

Please sign in to comment.