<commit_before>/*
 Copyright 2016-2017 Resin.io

 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.
 */

import * as Promise from 'bluebird';
import * as _ from 'lodash';
import { Dictionary } from 'lodash';
import * as request from 'request-promise';
import {
	DiscourseConstructor,
	DiscourseEmitInstructions,
	DiscoursePostSearchResponse,
	DiscourseReceivedMessage,
	DiscourseResponse,
	DiscourseServiceEvent,
} from '../../discourse-types';
import {
	BasicEventInformation,
	CreateThreadResponse,
	MessengerAction,
	MessengerConstructor,
	MessengerEvent,
	ReceiptInformation,
	SourceDescription,
	TransmitInformation,
	UpdateThreadResponse,
} from '../../messenger-types';
import { ServiceType } from '../../service-types';
import { Translator, TranslatorError } from './translator';
import { MetadataEncoding, TranslatorScaffold } from './translator-scaffold';
import {
	EmitConverters,
	MetadataConfiguration,
	ResponseConverters,
	TranslatorErrorCode,
	TranslatorMetadata,
} from './translator-types';

/**
 * Class to enable the translating between messenger standard forms and service
 * specific forms.
 */
export class DiscourseTranslator extends TranslatorScaffold implements Translator {
	/**
	 * Converts a provided username, in Discourse format, into a generic username
	 * @param username  Username to convert
	 * @returns         Generic equivalent of that username
	 */
	public static convertUsernameToGeneric(username: string): string {
		// Generic has `-` at the end, Discourse has `_` at the beginning
		if (/^_/.test(username)) {
			return `${username.replace(/^_/, '')}-`;
		}
		return username;
	}

	/**
	 * Converts a provided username, in generic format, into a Discourse username
	 * @param username  Username to convert
	 * @returns         Discourse equivalent of that username
	 */
	public static convertUsernameToDiscourse(username: string): string {
		// Generic has `-` at the end, Discourse has `_` at the beginning
		if (/-$/.test(username)) {
			return `_${username.replace(/-$/, '')}`;
		}
		return username;
	}

	/**
	 * Undoes a few of the decisions that Discourse makes when receiving a POST
	 * @param comment  Comment to reverse engineer
	 * @returns        Best guess as to original comment
	 */
	public static reverseEngineerComment(comment: DiscourseReceivedMessage): string {
		// Because Discourse assumes that when my POST contains:
		// hyphen hyphen ("--") then what I mean is en dash ("–")
		// hyphen hyphen hyphen ("---") then what I mean in em dash ("—")
		return comment.cooked.replace(/–/g, '--').replace(/—/g, '---').replace(/&amp;/g, '&');
	}

	/**
	 * Converts provided message details into a Promise of emit instructions
	 * @param event           Message data to emit
	 * @param thread          Thread ID to emit to
	 * @param metadataConfig  Configuration of how to encode the metadata
	 * @returns               Promise that resolves to the instructions that will emit the message
	 */
	public static bundleMessage(
		event: BasicEventInformation, thread: string, metadataConfig: MetadataConfiguration
	): Promise<DiscourseEmitInstructions> {
		if (!event.details.message) {
			return Promise.reject(new TranslatorError(
				TranslatorErrorCode.IncompleteTransmitInformation, 'Cannot emit an absent message.'
			));
		}
		if (event.details.message.hidden) {
			event.details.message.text = `**${event.details.user.handle}** whispered:\n${event.details.message.text}`;
		}
		if (event.details.message.hidden) {
			event.details.message.text = `${event.details.user.handle} whispered:\n${event.details.message.text}`;
		}<commit_msg>SyncBot: Remove duplicated author citation from Discourse<commit_after>/*
 Copyright 2016-2017 Resin.io

 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.
 */

import * as Promise from 'bluebird';
import * as _ from 'lodash';
import { Dictionary } from 'lodash';
import * as request from 'request-promise';
import {
	DiscourseConstructor,
	DiscourseEmitInstructions,
	DiscoursePostSearchResponse,
	DiscourseReceivedMessage,
	DiscourseResponse,
	DiscourseServiceEvent,
} from '../../discourse-types';
import {
	BasicEventInformation,
	CreateThreadResponse,
	MessengerAction,
	MessengerConstructor,
	MessengerEvent,
	ReceiptInformation,
	SourceDescription,
	TransmitInformation,
	UpdateThreadResponse,
} from '../../messenger-types';
import { ServiceType } from '../../service-types';
import { Translator, TranslatorError } from './translator';
import { MetadataEncoding, TranslatorScaffold } from './translator-scaffold';
import {
	EmitConverters,
	MetadataConfiguration,
	ResponseConverters,
	TranslatorErrorCode,
	TranslatorMetadata,
} from './translator-types';

/**
 * Class to enable the translating between messenger standard forms and service
 * specific forms.
 */
export class DiscourseTranslator extends TranslatorScaffold implements Translator {
	/**
	 * Converts a provided username, in Discourse format, into a generic username
	 * @param username  Username to convert
	 * @returns         Generic equivalent of that username
	 */
	public static convertUsernameToGeneric(username: string): string {
		// Generic has `-` at the end, Discourse has `_` at the beginning
		if (/^_/.test(username)) {
			return `${username.replace(/^_/, '')}-`;
		}
		return username;
	}

	/**
	 * Converts a provided username, in generic format, into a Discourse username
	 * @param username  Username to convert
	 * @returns         Discourse equivalent of that username
	 */
	public static convertUsernameToDiscourse(username: string): string {
		// Generic has `-` at the end, Discourse has `_` at the beginning
		if (/-$/.test(username)) {
			return `_${username.replace(/-$/, '')}`;
		}
		return username;
	}

	/**
	 * Undoes a few of the decisions that Discourse makes when receiving a POST
	 * @param comment  Comment to reverse engineer
	 * @returns        Best guess as to original comment
	 */
	public static reverseEngineerComment(comment: DiscourseReceivedMessage): string {
		// Because Discourse assumes that when my POST contains:
		// hyphen hyphen ("--") then what I mean is en dash ("–")
		// hyphen hyphen hyphen ("---") then what I mean in em dash ("—")
		return comment.cooked.replace(/–/g, '--').replace(/—/g, '---').replace(/&amp;/g, '&');
	}

	/**
	 * Converts provided message details into a Promise of emit instructions
	 * @param event           Message data to emit
	 * @param thread          Thread ID to emit to
	 * @param metadataConfig  Configuration of how to encode the metadata
	 * @returns               Promise that resolves to the instructions that will emit the message
	 */
	public static bundleMessage(
		event: BasicEventInformation, thread: string, metadataConfig: MetadataConfiguration
	): Promise<DiscourseEmitInstructions> {
		if (!event.details.message) {
			return Promise.reject(new TranslatorError(
				TranslatorErrorCode.IncompleteTransmitInformation, 'Cannot emit an absent message.'
			));
		}
		if (event.details.message.hidden) {
			event.details.message.text = `**${event.details.user.handle}** whispered:\n${event.details.message.text}`;
		}
{'fraction_non_alphanumeric': 0.1043272817707038, 'fraction_numerical': 0.0029843322556577967, 'mean_word_length': 4.011214953271028, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '4732115', 'n_tokens_mistral': 2280, 'n_tokens_neox': 2136, 'n_words': 779}