<commit_before>/* globals _ */
/* eslint-disable camelcase */
import { filter, isNull, omitBy, uniqBy, isEmpty, intersection } from 'lodash/fp'
import { compact, flatten, some, uniq } from 'lodash'
import { postRoom, pushToSockets } from '../services/Websockets'
import { fulfill, unfulfill } from './post/fulfillPost'
import EnsureLoad from './mixins/EnsureLoad'
import HasGroup from './mixins/HasGroup'
import { countTotal } from '../../lib/util/knex'
import { refineMany, refineOne } from './util/relations'
import { isFollowing } from './group/queryUtils'
import html2text from '../../lib/htmlparser/html2text'
import ProjectMixin from './project/mixin'
import EventMixin from './event/mixin'

const commentersQuery = (limit, post, currentUserId) => q => {
  q.select('users.*', 'comments.user_id')
  q.join('comments', 'comments.user_id', 'users.id')

  q.where({
    'comments.post_id': post.id,
    'comments.active': true
  })

  if (currentUserId) {
    q.whereNotIn('users.id', BlockedUser.blockedFor(currentUserId))
    q.orderBy(bookshelf.knex.raw(`case when user_id = ${currentUserId} then -1 else user_id end`))
  }

  q.groupBy('users.id', 'comments.user_id')
  if (limit) q.limit(limit)
}

module.exports = bookshelf.Model.extend(Object.assign({
  // Instance Methods

  tableName: 'posts',
  requireFetch: false,

  user: function () {
    return this.belongsTo(User)
  },

  communities: function () {
    return this.belongsToMany(Community).through(PostMembership)
    .query({where: {'communities.active': true}})
  },

  networks: function () {
    return this.belongsToMany(Network).through(PostNetworkMembership)
  },

  followers: function () {
    return this.groupMembers(q => isFollowing(q))
  },

  followersWithPivots: function () {
    return this.groupMembersWithPivots().query(q => isFollowing(q))
  },

  contributions: function () {
    return this.hasMany(Contribution, 'post_id')
  },

  postMemberships: function () {
    return this.hasMany(PostMembership, 'post_id')
  },

  comments: function () {
    return this.hasMany(Comment, 'post_id')
  },

  locationObject: function () {
    return this.belongsTo(Location, 'location_id')
  },

  media: function (type) {
    const relation = this.hasMany(Media)<commit_msg>only load immediate children of posts by default when loading post comments<commit_after>/* globals _ */
/* eslint-disable camelcase */
import { filter, isNull, omitBy, uniqBy, isEmpty, intersection } from 'lodash/fp'
import { compact, flatten, some, uniq } from 'lodash'
import { postRoom, pushToSockets } from '../services/Websockets'
import { fulfill, unfulfill } from './post/fulfillPost'
import EnsureLoad from './mixins/EnsureLoad'
import HasGroup from './mixins/HasGroup'
import { countTotal } from '../../lib/util/knex'
import { refineMany, refineOne } from './util/relations'
import { isFollowing } from './group/queryUtils'
import html2text from '../../lib/htmlparser/html2text'
import ProjectMixin from './project/mixin'
import EventMixin from './event/mixin'

const commentersQuery = (limit, post, currentUserId) => q => {
  q.select('users.*', 'comments.user_id')
  q.join('comments', 'comments.user_id', 'users.id')

  q.where({
    'comments.post_id': post.id,
    'comments.active': true
  })

  if (currentUserId) {
    q.whereNotIn('users.id', BlockedUser.blockedFor(currentUserId))
    q.orderBy(bookshelf.knex.raw(`case when user_id = ${currentUserId} then -1 else user_id end`))
  }

  q.groupBy('users.id', 'comments.user_id')
  if (limit) q.limit(limit)
}

module.exports = bookshelf.Model.extend(Object.assign({
  // Instance Methods

  tableName: 'posts',
  requireFetch: false,

  user: function () {
    return this.belongsTo(User)
  },

  communities: function () {
    return this.belongsToMany(Community).through(PostMembership)
    .query({where: {'communities.active': true}})
  },

  networks: function () {
    return this.belongsToMany(Network).through(PostNetworkMembership)
  },

  followers: function () {
    return this.groupMembers(q => isFollowing(q))
  },

  followersWithPivots: function () {
    return this.groupMembersWithPivots().query(q => isFollowing(q))
  },

  contributions: function () {
    return this.hasMany(Contribution, 'post_id')
  },

  postMemberships: function () {
    return this.hasMany(PostMembership, 'post_id')
  },

  comments: function () {
    return this.hasMany(Comment, 'post_id').query({ where: {
      'comments.active': true,
      'comments.comment_id': null
    }})
  },

  locationObject: function () {
    return this.belongsTo(Location, 'location_id')
  },

  media: function (type) {
    const relation = this.hasMany(Media)
{'fraction_non_alphanumeric': 0.15023574796399486, 'fraction_numerical': 0.0012858979854264896, 'mean_word_length': 4.083877995642702, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4694453', 'n_tokens_mistral': 1530, 'n_tokens_neox': 1495, 'n_words': 327}