<commit_before>/*
 Copyright 2017 <NAME>

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

'use strict';

const dbThread = require('../db/connectors/forumThread');
const { dbConfig } = require('../config/defaults/config');
const errorCreator = require('../error/errorCreator');
const authenticator = require('../helpers/authenticator');
const forumManager = require('./forums');
const managerHelper = require('../helpers/manager');

/**
 * Get forum thread by Id.
 * @param {Object} params Parameters.
 * @param {string} [params.threadId] Id of forum thread to retrieve.
 * @param {string} params.token jwt.
 * @param {Function} params.callback Callback.
 */
function getThreadById({
  threadId,
  token,
  internalCallUser,
  needsAccess,
  callback,
}) {
  managerHelper.getObjectById({
    token,
    internalCallUser,
    callback,
    needsAccess,
    objectId: threadId,
    objectType: 'thread',
    objectIdType: 'threadId',
    dbCallFunc: dbThread.getThreadById,
    commandName: dbConfig.apiCommands.GetForumThread.name,
  });
}

/**
 * Create new forum thread
 * @param {Object} params Parameters
 * @param {Object} params.thread Forum thread to create
 * @param {Function} params.callback Callback
 * @param {string} params.token jwt
 * @param {Object} params.io Socket.io.
 */
function createThread({
  thread,
  callback,
  token,
  io,
  socket,
}) {
  authenticator.isUserAllowed({
    token,
    commandName: dbConfig.apiCommands.CreateForumThread.name,
    callback: ({ error, data }) => {
      if (error) {
        callback({ error });

        return;
      }

      const { user: authUser } = data;

      forumManager.getForumById({
        internalCallUser: authUser,
        forumId: thread.forumId,
        needsAccess: true,
        callback: ({ error: forumError }) => {
          if (forumError) {
            callback({ error: forumError });

            return;
          }

          const threadToCreate = thread;
          threadToCreate.ownerId = authUser.objectId;

          if (threadToCreate.ownerAliasId && !authUser.aliases.includes(threadToCreate.ownerAliasId)) {
            callback({ error: new errorCreator.NotAllowed({ name: `create thread with alias ${threadToCreate.ownerAliasId}` }) });

            return;<commit_msg>Threads inherit isPublic from parent forum<commit_after>/*
 Copyright 2017 <NAME>

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

'use strict';

const dbThread = require('../db/connectors/forumThread');
const { dbConfig } = require('../config/defaults/config');
const errorCreator = require('../error/errorCreator');
const authenticator = require('../helpers/authenticator');
const forumManager = require('./forums');
const managerHelper = require('../helpers/manager');

/**
 * Get forum thread by Id.
 * @param {Object} params Parameters.
 * @param {string} [params.threadId] Id of forum thread to retrieve.
 * @param {string} params.token jwt.
 * @param {Function} params.callback Callback.
 */
function getThreadById({
  threadId,
  token,
  internalCallUser,
  needsAccess,
  callback,
}) {
  managerHelper.getObjectById({
    token,
    internalCallUser,
    callback,
    needsAccess,
    objectId: threadId,
    objectType: 'thread',
    objectIdType: 'threadId',
    dbCallFunc: dbThread.getThreadById,
    commandName: dbConfig.apiCommands.GetForumThread.name,
  });
}

/**
 * Create new forum thread
 * @param {Object} params Parameters
 * @param {Object} params.thread Forum thread to create
 * @param {Function} params.callback Callback
 * @param {string} params.token jwt
 * @param {Object} params.io Socket.io.
 */
function createThread({
  thread,
  callback,
  token,
  io,
  socket,
}) {
  authenticator.isUserAllowed({
    token,
    commandName: dbConfig.apiCommands.CreateForumThread.name,
    callback: ({ error, data }) => {
      if (error) {
        callback({ error });

        return;
      }

      const { user: authUser } = data;

      forumManager.getForumById({
        internalCallUser: authUser,
        forumId: thread.forumId,
        needsAccess: true,
        callback: ({ error: forumError, data: forumData }) => {
          if (forumError) {
            callback({ error: forumError });

            return;
          }

          const { forum } = forumData;
          const threadToCreate = thread;
          threadToCreate.ownerId = authUser.objectId;
          threadToCreate.isPublic = forum.isPublic;

          if (threadToCreate.ownerAliasId && !authUser.aliases.includes(threadToCreate.ownerAliasId)) {
            callback({ error: new errorCreator.NotAllowed({ name: `create thread with alias ${threadToCreate.ownerAliasId}` }) });

            return;
{'fraction_non_alphanumeric': 0.11191783247742164, 'fraction_numerical': 0.0028333628475296618, 'mean_word_length': 3.2562170308967597, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2849643', 'n_tokens_mistral': 1575, 'n_tokens_neox': 1549, 'n_words': 481}