<commit_before>import { compact, flatten, uniq, filter, merge, union, omit } from 'lodash'
import { debug } from '../util/logging'
import {
  CREATE_COMMUNITY,
  FETCH_POSTS,
  FETCH_COMMUNITY,
  FETCH_COMMUNITY_FOR_INVITATION,
  FETCH_COMMUNITY_SETTINGS,
  FETCH_COMMUNITY_MODERATORS,
  FETCH_COMMUNITIES,
  FETCH_PERSON,
  FETCH_POST,
  FETCH_CURRENT_USER,
  UPDATE_COMMUNITY_SETTINGS,
  UPDATE_COMMUNITY_SETTINGS_PENDING,
  ADD_COMMUNITY_MODERATOR,
  ADD_COMMUNITY_MODERATOR_PENDING,
  REMOVE_COMMUNITY_MODERATOR,
  REMOVE_COMMUNITY_MODERATOR_PENDING,
  TYPEAHEAD,
  USE_INVITATION
} from '../actions'
import { mergeList } from './util'

export default function (state = {}, action) {
  let { error, type, payload, meta } = action
  if (error) {
    switch (type) {
      case UPDATE_COMMUNITY_SETTINGS:
      case ADD_COMMUNITY_MODERATOR:
      case REMOVE_COMMUNITY_MODERATOR:
        return {
          ...state,
          [meta.slug]: {...state[meta.slug], ...meta.prevProps}
        }
      default:
        return state
    }
  }

  let moderators

  switch (type) {
    case FETCH_COMMUNITY:
    case FETCH_COMMUNITY_SETTINGS:
      let slug = payload.slug || meta.cache.id
      community = {...state[slug], ...payload}
      debug('caching community:', community.slug)
      return {...state, [slug]: community}
    case FETCH_COMMUNITY_FOR_INVITATION:
      return {...state, [meta.token]: payload}
    case USE_INVITATION:
      return {...state, [meta.token]: payload.community}
    case FETCH_COMMUNITY_MODERATORS:
      community = {...state[meta.cache.id], moderators: payload}
      return {...state, [meta.cache.id]: community}
    case FETCH_POSTS:
      let communities = uniq(flatten(payload.posts.map(p => p.communities)), c => c.id)
      return mergeList(state, communities, 'slug')
    case FETCH_POST:
      return mergeList(state, payload.communities, 'slug')
    case FETCH_PERSON:
      const newPosts = compact([payload.recent_request, payload.recent_offer])
      return mergeList(state, newPosts.map(p => p.communities), 'slug')<commit_msg>fix handling of community for recent request/offer on profile<commit_after>import { map, uniqBy } from 'lodash/fp'
import { compact, flatten, filter, merge, union, omit } from 'lodash'
import { debug } from '../util/logging'
import {
  CREATE_COMMUNITY,
  FETCH_POSTS,
  FETCH_COMMUNITY,
  FETCH_COMMUNITY_FOR_INVITATION,
  FETCH_COMMUNITY_SETTINGS,
  FETCH_COMMUNITY_MODERATORS,
  FETCH_COMMUNITIES,
  FETCH_PERSON,
  FETCH_POST,
  FETCH_CURRENT_USER,
  UPDATE_COMMUNITY_SETTINGS,
  UPDATE_COMMUNITY_SETTINGS_PENDING,
  ADD_COMMUNITY_MODERATOR,
  ADD_COMMUNITY_MODERATOR_PENDING,
  REMOVE_COMMUNITY_MODERATOR,
  REMOVE_COMMUNITY_MODERATOR_PENDING,
  TYPEAHEAD,
  USE_INVITATION
} from '../actions'
import { mergeList } from './util'

const getPostCommunities = posts =>
  uniqBy('id', flatten(map(p => p.communities, posts)))

export default function (state = {}, action) {
  let { error, type, payload, meta } = action
  if (error) {
    switch (type) {
      case UPDATE_COMMUNITY_SETTINGS:
      case ADD_COMMUNITY_MODERATOR:
      case REMOVE_COMMUNITY_MODERATOR:
        return {
          ...state,
          [meta.slug]: {...state[meta.slug], ...meta.prevProps}
        }
      default:
        return state
    }
  }

  let moderators

  switch (type) {
    case FETCH_COMMUNITY:
    case FETCH_COMMUNITY_SETTINGS:
      let slug = payload.slug || meta.cache.id
      community = {...state[slug], ...payload}
      debug('caching community:', community.slug)
      return {...state, [slug]: community}
    case FETCH_COMMUNITY_FOR_INVITATION:
      return {...state, [meta.token]: payload}
    case USE_INVITATION:
      return {...state, [meta.token]: payload.community}
    case FETCH_COMMUNITY_MODERATORS:
      community = {...state[meta.cache.id], moderators: payload}
      return {...state, [meta.cache.id]: community}
    case FETCH_POSTS:
      return mergeList(state, getPostCommunities(payload.posts), 'slug')
    case FETCH_POST:
      return mergeList(state, payload.communities, 'slug')
    case FETCH_PERSON:
      const newPosts = compact([payload.recent_request, payload.recent_offer])
      return mergeList(state, getPostCommunities(newPosts), 'slug')
{'fraction_non_alphanumeric': 0.11262638137785093, 'fraction_numerical': 0.0, 'mean_word_length': 3.511134676564157, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, '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': '1333246', 'n_tokens_mistral': 1512, 'n_tokens_neox': 1484, 'n_words': 293}