Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored code in src/api/posts.js by adding a helper function #105

Open
wants to merge 1 commit into
base: f24
Choose a base branch
from
Open
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
25 changes: 15 additions & 10 deletions src/api/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,8 @@ postsAPI.getRaw = async (caller, { pid }) => {
return result.postData.content;
};

postsAPI.edit = async function (caller, data) {
if (!data || !data.pid || (meta.config.minimumPostLength !== 0 && !data.content)) {
throw new Error('[[error:invalid-data]]');
}
if (!caller.uid) {
throw new Error('[[error:not-logged-in]]');
}
// Trim and remove HTML (latter for composers that send in HTML, like redactor)
const contentLen = utils.stripHTMLTags(data.content).trim().length;

// Added code to lower cognitive complexity below, Procos12
async function validatePost(data, meta, contentLen, caller) {
if (data.title && data.title.length < meta.config.minimumTitleLength) {
throw new Error(`[[error:title-too-short, ${meta.config.minimumTitleLength}]]`);
} else if (data.title && data.title.length > meta.config.maximumTitleLength) {
Expand All @@ -104,6 +96,19 @@ postsAPI.edit = async function (caller, data) {
} else if (!await posts.canUserPostContentWithLinks(caller.uid, data.content)) {
throw new Error(`[[error:not-enough-reputation-to-post-links, ${meta.config['min:rep:post-links']}]]`);
}
}

postsAPI.edit = async function (caller, data) {
if (!data || !data.pid || (meta.config.minimumPostLength !== 0 && !data.content)) {
throw new Error('[[error:invalid-data]]');
}
if (!caller.uid) {
throw new Error('[[error:not-logged-in]]');
}
// Trim and remove HTML (latter for composers that send in HTML, like redactor)
const contentLen = utils.stripHTMLTags(data.content).trim().length;

await validatePost(data, meta, contentLen, caller);

data.uid = caller.uid;
data.req = apiHelpers.buildReqObject(caller);
Expand Down