Skip to content

Commit

Permalink
Merge pull request #13 from CMU-313/priority
Browse files Browse the repository at this point in the history
Backend integration code for flagging priority questions for faculty issue #12 + NPM test code coverage fix
  • Loading branch information
Caryzxy authored Feb 28, 2025
2 parents 39f97d4 + 92682cd commit 7ad8e24
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/posts/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,19 @@ module.exports = function (Posts) {
if (data.toPid) {
postData.toPid = data.toPid;
}

if (data.ip && meta.config.trackIpPerPost) {
postData.ip = data.ip;
}

if (data.handle && !parseInt(uid, 10)) {
postData.handle = data.handle;
}

if (data.priorityLevel !== 0) {
postData.priorityLevel = data.priorityLevel;
}

let result = await plugins.hooks.fire('filter:post.create', { post: postData, data: data });
postData = result.post;
await db.setObject(`post:${postData.pid}`, postData);
Expand Down
2 changes: 2 additions & 0 deletions src/posts/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const pubsub = require('../pubsub');
const utils = require('../utils');
const slugify = require('../slugify');
const translator = require('../translator');
const priorityflag = require('./priorityflag');

module.exports = function (Posts) {
pubsub.on('post:edit', (pid) => {
Expand Down Expand Up @@ -195,6 +196,7 @@ module.exports = function (Posts) {
const editPostData = {
content: data.content,
editor: data.uid,
priorityLevel: priorityflag(data.content),
};

// For posts in scheduled topics, if edited before, use edit timestamp
Expand Down
23 changes: 23 additions & 0 deletions src/posts/priorityflag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

// Mapping of priority levels to numerical values
const PRIORITY_MAP = {
'low priority': 1,
'medium priority': 2,
'high priority': 3,
};

// Function to check for priority levels in the content and return the corresponding value
module.exports = function priorityflag(content) {
// Convert content to lowercase for case-insensitive matching
content = content.toLowerCase();
// Check if any priority level exists in the content
for (const [priority, value] of Object.entries(PRIORITY_MAP)) {
if (content.includes(priority)) {
return value;
}
}
// Return 0 if no priority string is found
return 0;
};

2 changes: 2 additions & 0 deletions src/topics/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const posts = require('../posts');
const privileges = require('../privileges');
const categories = require('../categories');
const translator = require('../translator');
const priorityflag = require('../posts/priorityflag');

module.exports = function (Topics) {
Topics.create = async function (data) {
Expand Down Expand Up @@ -201,6 +202,7 @@ module.exports = function (Topics) {
if (uid > 0 && settings.followTopicsOnReply) {
await Topics.follow(postData.tid, uid);
}
data.priorityLevel = priorityflag(data.content);

if (parseInt(uid, 10)) {
user.setUserField(uid, 'lastonline', Date.now());
Expand Down

0 comments on commit 7ad8e24

Please sign in to comment.