<commit_before>import omit from 'lodash/object/omit';
import BaseAPI from './base.es6.js';
import Comment from '../models/comment.es6.js';
import Link from '../models/comment.es6.js';
import Message from '../models/message.es6.js';

const CONSTRUCTORS = {
  t1: Comment,
  t3: Link,
  t4: Message,
};

export default class Messages extends BaseAPI {
  static dataCacheConfig = undefined;

  get requestCacheRules() {
    return undefined;
  }

  put = this.notImplemented('put');
  patch = this.notImplemented('patch');
  del = this.notImplemented('del');

  formatQuery(query, method) {
    return {
      ...omit(query, 'thingId'),
      api_type: 'json',
      mark: true,
      thing_id: query.thingId,
    };
  }

  path(method, query={}) {
    if (method === 'get') { return `message/${query.view || 'inbox'}`; }
    if (!query.thingId) { return 'api/compose'; }
    return 'api/comment';
  }

  formatBody(res, req) {
    const { body } = res;

    switch(req.method) {
      case 'GET': {
        if (body) {
          return body.data.children.map(data => {
            const constructor = CONSTRUCTORS[data.kind];
            return new constructor(data.data).toJSON();
          });
        }
      }
      case 'POST': {
        if (body && body.json) {
          const message = body.json.things[0].data;
          return new Message(message).toJSON();
        } else if (body && body.json.errors.length) {
          throw body.json.errors;
        } else {
          return res;
        }
      }
    }
  }<commit_msg>Messages: enable modmail filtering by subreddit<commit_after>import omit from 'lodash/object/omit';
import BaseAPI from './base.es6.js';
import Comment from '../models/comment.es6.js';
import Link from '../models/comment.es6.js';
import Message from '../models/message.es6.js';

const CONSTRUCTORS = {
  t1: Comment,
  t3: Link,
  t4: Message,
};

export default class Messages extends BaseAPI {
  static dataCacheConfig = undefined;

  get requestCacheRules() {
    return undefined;
  }

  put = this.notImplemented('put');
  patch = this.notImplemented('patch');
  del = this.notImplemented('del');

  formatQuery(query, method) {
    return {
      ...omit(query, 'thingId'),
      api_type: 'json',
      mark: true,
      thing_id: query.thingId,
    };
  }

  path(method, query={}) {
    const { subreddit, view, thingId } = query;
    if (method === 'get') {
      const sub = subreddit ? `r/${subreddit}/` : '';
      return `${sub}message/${view || 'inbox'}`;
    }
    if (!thingId) { return 'api/compose'; }
    return 'api/comment';
  }

  formatBody(res, req) {
    const { body } = res;

    switch(req.method) {
      case 'GET': {
        if (body) {
          return body.data.children.map(data => {
            const constructor = CONSTRUCTORS[data.kind];
            return new constructor(data.data).toJSON();
          });
        }
      }
      case 'POST': {
        if (body && body.json) {
          const message = body.json.things[0].data;
          return new Message(message).toJSON();
        } else if (body && body.json.errors.length) {
          throw body.json.errors;
        } else {
          return res;
        }
      }
    }
  }
{'fraction_non_alphanumeric': 0.15324594257178528, 'fraction_numerical': 0.004993757802746567, 'mean_word_length': 2.304123711340206, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '4185339', 'n_tokens_mistral': 1028, 'n_tokens_neox': 1015, 'n_words': 234}