<commit_before>import { Injectable } from '@angular/core';
import { Storage, HostType } from '@microsoft/office-js-helpers';
import { Observable } from 'rxjs/Observable';
import * as jsyaml from 'js-yaml';
import { PlaygroundError, AI, post, Strings, environment } from '../helpers';
import { Request, ResponseTypes, GitHubService } from '../services';
import { Action } from '@ngrx/store';
import { GitHub, Snippet, UI } from '../actions';
import { Effect, Actions } from '@ngrx/effects';
import * as cuid from 'cuid';
import { Store } from '@ngrx/store';
import * as fromRoot from '../reducers';
import { isEmpty, find, assign, reduce, forIn } from 'lodash';

@Injectable()
export class SnippetEffects {
    private _store = new Storage<ISnippet>(`playground_${environment.current.host}_snippets`);

    private _defaults = <ISnippet>{
        id: '',
        gist: '',
        host: environment.current.host,
        api_set: {},
        platform: environment.current.platform,
        created_at: Date.now(),
        modified_at: Date.now(),
        origin: environment.current.config.editorUrl,
        author: '',
        name: Strings.defaultSnippetTitle, // UI unknown
        description: '',
        script: { content: '', language: 'typescript' },
        style: { content: '', language: 'css' },
        template: { content: '', language: 'html' },
        libraries: ''
    };

    constructor(
        private actions$: Actions,
        private _request: Request,
        private _github: GitHubService,
        reduxStore: Store<fromRoot.State>,
    ) {
        this._defaults.author = this._github.profile ? this._github.profile.login : '';
    }

    @Effect({ dispatch: false })
    loggedIn$: Observable<Action> = this.actions$
        .ofType(GitHub.GitHubActionTypes.LOGGED_IN)
        .do(() => this._defaults.author = this._github.profile ? this._github.profile.login : '');

    @Effect()
    import$: Observable<Action> = this.actions$
        .ofType(Snippet.SnippetActionTypes.IMPORT)
        .map((action: Snippet.ImportAction) => ({ data: action.payload, suffix: action.params }))
        .mergeMap(({ data, suffix }) => {
            let observable: Observable<ISnippet>;
            let importType = this._determineImportType(data);
            let info = '';

            switch (importType) {
                case 'DEFAULT':
                    info = environment.current.host;
                    if (environment.cache.contains('default_template')) {
                        observable = Observable.of(environment.cache.get('default_template'));
                    }
                    else {
                        observable = this._request
                            .get<string>(`${environment.current.config.samplesUrl}/samples/${environment.current.host.toLowerCase()}/default.yaml`, ResponseTypes.YAML)
                            .map(snippet => environment.cache.insert('default_template', snippet));
                    }
                    break;

                case 'CUID':
                    info = data;
                    observable = Observable.of(this._store.get(data));
                    break;

                case 'GIST':
                    data = data.replace(/https:\/\/gist.github.com\/.*?\//, '');
                    observable = this._github.gist(data)
                        .map(gist => {
                            let snippet = find(gist.files, (value, key) => value ? /\.ya?ml$/gi.test(key) : false);
                            if (gist.public) {
                                info = gist.id;
                            }
                            if (snippet == null) {
                                let output = this._upgrade(gist.files);
                                output.description = '';
                                output.author = '';
                                output.gist = data;
                                return output;
                            }
                            else {
                                return jsyaml.load(snippet.content);
                            }
                        });
                    break;

                case 'URL':
                    observable = this._request.get<ISnippet>(data, ResponseTypes.YAML);
                    break;

                case 'YAML':
                    let snippet = jsyaml.load(data);
                    info = snippet.id;
                    observable = Observable.of(snippet);
                    break;

                default: return null;
            }

            AI.trackEvent(Snippet.SnippetActionTypes.IMPORT, { type: importType, info: info });

            return observable
                .filter(snippet => !(snippet == null))
                .map(snippet => {
                    if (snippet.host && snippet.host !== environment.current.host) {
                        throw new PlaygroundError(`Cannot import a snippet created for ${snippet.host} in ${environment.current.host}.`);
                    }

                    if (snippet.api_set == null || environment.current.host === HostType.WEB) {
                        return snippet;
                    }

                    let unsupportedApiSet: { api: string, version: number } = null;
                    find(snippet.api_set, (version, api) => {
                        if (Office.context.requirements.isSetSupported(api, version)) {<commit_msg>temporarily disabling host check in snippet import<commit_after>import { Injectable } from '@angular/core';
import { Storage, HostType } from '@microsoft/office-js-helpers';
import { Observable } from 'rxjs/Observable';
import * as jsyaml from 'js-yaml';
import { PlaygroundError, AI, post, Strings, environment } from '../helpers';
import { Request, ResponseTypes, GitHubService } from '../services';
import { Action } from '@ngrx/store';
import { GitHub, Snippet, UI } from '../actions';
import { Effect, Actions } from '@ngrx/effects';
import * as cuid from 'cuid';
import { Store } from '@ngrx/store';
import * as fromRoot from '../reducers';
import { isEmpty, find, assign, reduce, forIn } from 'lodash';

@Injectable()
export class SnippetEffects {
    private _store = new Storage<ISnippet>(`playground_${environment.current.host}_snippets`);

    private _defaults = <ISnippet>{
        id: '',
        gist: '',
        host: environment.current.host,
        api_set: {},
        platform: environment.current.platform,
        created_at: Date.now(),
        modified_at: Date.now(),
        origin: environment.current.config.editorUrl,
        author: '',
        name: Strings.defaultSnippetTitle, // UI unknown
        description: '',
        script: { content: '', language: 'typescript' },
        style: { content: '', language: 'css' },
        template: { content: '', language: 'html' },
        libraries: ''
    };

    constructor(
        private actions$: Actions,
        private _request: Request,
        private _github: GitHubService,
        reduxStore: Store<fromRoot.State>,
    ) {
        this._defaults.author = this._github.profile ? this._github.profile.login : '';
    }

    @Effect({ dispatch: false })
    loggedIn$: Observable<Action> = this.actions$
        .ofType(GitHub.GitHubActionTypes.LOGGED_IN)
        .do(() => this._defaults.author = this._github.profile ? this._github.profile.login : '');

    @Effect()
    import$: Observable<Action> = this.actions$
        .ofType(Snippet.SnippetActionTypes.IMPORT)
        .map((action: Snippet.ImportAction) => ({ data: action.payload, suffix: action.params }))
        .mergeMap(({ data, suffix }) => {
            let observable: Observable<ISnippet>;
            let importType = this._determineImportType(data);
            let info = '';

            switch (importType) {
                case 'DEFAULT':
                    info = environment.current.host;
                    if (environment.cache.contains('default_template')) {
                        observable = Observable.of(environment.cache.get('default_template'));
                    }
                    else {
                        observable = this._request
                            .get<string>(`${environment.current.config.samplesUrl}/samples/${environment.current.host.toLowerCase()}/default.yaml`, ResponseTypes.YAML)
                            .map(snippet => environment.cache.insert('default_template', snippet));
                    }
                    break;

                case 'CUID':
                    info = data;
                    observable = Observable.of(this._store.get(data));
                    break;

                case 'GIST':
                    data = data.replace(/https:\/\/gist.github.com\/.*?\//, '');
                    observable = this._github.gist(data)
                        .map(gist => {
                            let snippet = find(gist.files, (value, key) => value ? /\.ya?ml$/gi.test(key) : false);
                            if (gist.public) {
                                info = gist.id;
                            }
                            if (snippet == null) {
                                let output = this._upgrade(gist.files);
                                output.description = '';
                                output.author = '';
                                output.gist = data;
                                return output;
                            }
                            else {
                                return jsyaml.load(snippet.content);
                            }
                        });
                    break;

                case 'URL':
                    observable = this._request.get<ISnippet>(data, ResponseTypes.YAML);
                    break;

                case 'YAML':
                    let snippet = jsyaml.load(data);
                    info = snippet.id;
                    observable = Observable.of(snippet);
                    break;

                default: return null;
            }

            AI.trackEvent(Snippet.SnippetActionTypes.IMPORT, { type: importType, info: info });

            return observable
                .filter(snippet => !(snippet == null))
                .map(snippet => {
                    // if (snippet.host && snippet.host !== environment.current.host) {
                    //     throw new PlaygroundError(`Cannot import a snippet created for ${snippet.host} in ${environment.current.host}.`);
                    // }

                    if (snippet.api_set == null || environment.current.host === HostType.WEB) {
                        return snippet;
                    }

                    let unsupportedApiSet: { api: string, version: number } = null;
                    find(snippet.api_set, (version, api) => {
                        if (Office.context.requirements.isSetSupported(api, version)) {
{'fraction_non_alphanumeric': 0.12185722150853368, 'fraction_numerical': 0.0, 'mean_word_length': 1.5205827937095282, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4095489', 'n_tokens_mistral': 2813, 'n_tokens_neox': 2662, 'n_words': 578}