<commit_before>﻿import { Injectable } from '@angular/core';
import { Subject, Observable } from 'rxjs/Rx';

import { WebSocketService } from './websocket.service';
import { ParserService } from './parser.service';

// Models
import { User } from '../models/user/user.model';
import { Collaborator } from '../models/user/collaborator.model';

import { LinkTable } from '../models/link/link-table.model';
import { Link } from '../models/link/link.model';
import { ID } from '../models/id.model';

import { StorySummary } from '../models/story/story-summary.model';
import { Story } from '../models/story/story.model';
import { Section } from '../models/story/section.model';
import { Paragraph } from '../models/story/paragraph.model';
import { Tooltip } from '../models/story/tooltip.model';
import { ContentObject } from '../models/story/content-object.model';

import { WikiSummary } from '../models/wiki/wiki-summary.model';
import { Wiki } from '../models/wiki/wiki.model';
import { Segment } from '../models/wiki/segment.model';
import { PageSummary } from '../models/wiki/page-summary.model';
import { Page } from '../models/wiki/page.model';
import { Heading } from '../models/wiki/heading.model';
import { Reference } from '../models/wiki/reference.model';

const url: string = 'ws://localhost:8080/ws/demo';

@Injectable()
export class ApiService {
    public data = {
        inflight: false,
        user: new User(),
        stories: new Array<StorySummary>(),
        wikis: new Array<WikiSummary>(),
        linkTable: new LinkTable(),

        storyNode: [],
        storyDisplay: '',
        story: new Story(),
        section: new Section(),
        content: new Array<Paragraph>(),

        wikiNav: [],
        wikiNode: [],
        wikiDisplay: '',
        wiki: new Wiki(),
        segment: new Segment(),
        page: new Page(),
        pageid: [],

        tooltip: new Tooltip(),
        contentObject: new ContentObject()
    }

    public outgoing = {};
    public message_id: number = 0;
    public messages: Subject<string>;

    constructor(
        private socket: WebSocketService,
        private parser: ParserService) { }

    public connect() {
        this.messages = <Subject<string>>this.socket.connect(url)
            .map((res: MessageEvent) => {
                this.data.inflight = false;

                let response: string = res.data;
                let reply = JSON.parse(response);
                if (reply.event) {
                    return reply.event;
                } else {
                    if (!reply.reply_to_id) {
                        reply = JSON.parse(reply);
                    }

                    let message_id: number = reply.reply_to_id;
                    let out: any = this.outgoing[message_id];
                    let action: string = out.action;
                    let callback: Function = out.callback;

                    switch (action) {
                        case 'get_user_preferences':
                            this.data.user = reply;
                            break;
                        case 'get_user_stories':
                            this.data.stories = reply.stories;
                            this.data.stories.push({
                                story_id: null,
                                title: null,
                                access_level: null
                            });
                            break;
                        case 'get_user_wikis':
                            this.data.wikis = reply.wikis;
                            break;

                        case 'create_story':
                            this.data.stories.unshift({
                                story_id: reply.story_id,
                                title: reply.story_title,
                                access_level: reply.access_level
                            });
                            this.data.story = reply;
                            this.refreshStory(reply.story_id);
                        case 'get_story_information':
                            reply.story_id = this.data.story.story_id;
                            this.data.story = reply;
                            this.data.wiki.wiki_id = reply.wiki_id;
                            this.data.storyDisplay = '<h1>Summary</h1>' + reply.summary;
                            this.refreshWiki(reply.wiki_id, true);
                            break;
                        case 'get_story_hierarchy':
                        case 'get_section_hierarchy':
                            this.data.section = reply.hierarchy;
                            this.data.storyNode = [
                                this.parser.sectionToTree(this.parser, reply.hierarchy)
                            ];
                            break;
                        case 'get_section_content':
                            this.data.content = reply.content;
                            this.data.contentObject = this.parser.parseContent(reply.content, this.data.linkTable);
                            this.data.storyDisplay = this.parser.setContentDisplay(reply.content);
                            break;

                        case 'add_inner_subsection':
                            this.refreshStory();
                            break

                        case 'add_paragraph':
                            callback(reply);
                        case 'edit_paragraph':
                        case 'delete_paragraph':
                            this.refreshContent();
                            break;

                        case 'create_link':
                            callback(reply);
                        case 'delete_link':
                            break;

                        case 'create_wiki':
                            this.data.wiki = reply;
                            this.refreshWiki(reply.wiki_id);
                        case 'get_wiki_information':
                            reply.wiki_id = this.data.wiki.wiki_id;
                            this.data.wiki = reply;
                            this.data.wikiDisplay = this.parser.setWikiDisplay(reply);
                            break;
                        case 'get_wiki_hierarchy':
                        case 'get_wiki_segment_hierarchy':
                            this.data.segment = reply.hierarchy;
                            this.data.wikiNav = this.parser.parseWiki(reply.hierarchy);
                            this.data.linkTable = this.parser.parseLinkTable(reply.link_table);
                            break;
                        case 'get_wiki_segment':
                            reply = JSON.parse(JSON.stringify(reply).replace("template_headings", "headings"));
                            this.data.page = reply;
                            break;
                        case 'get_wiki_page':
                            this.data.page = reply;
                            this.data.tooltip.text = '<b>' + reply.title + '</b>';
                            if (reply.headings && reply.headings[0]) {
                                this.data.tooltip.text += '<br/><u>' + reply.headings[0].title + '</u><br/>' + reply.headings[0].text;
                            }
                            break;
                        case 'add_page':
                            this.data.pageid.push(reply.page_id);
                            this.refreshWiki();
                            break;
                        case 'add_segment':
                            this.data.pageid.push(reply.segment_id);
                            this.refreshWiki();
                            break;
                        case 'add_template_heading':
                            break;
                        case 'add_heading':
                            break;
                        case 'create_wiki':
                            break;
                        case 'edit_segment':
                            break;
                        case 'edit_page':
                            break;
                        case 'edit_heading':
                            break;
                        case 'change_alias_name':
                            this.refreshWiki();
                            this.refreshContent();
                            break;

                        default:
                            console.log('Unknown action: ' + action)
                            break;
                    }
                    delete this.outgoing[message_id];
                    return action;
                }
            });
        this.messages.subscribe((action: string) => {
            console.log(action);
        });
    }

    public refreshStory(storyId: ID = this.data.story.story_id, info: boolean = false) {
        if (info) {
            this.send({ action: 'get_story_information', story_id: storyId });
        }
        this.send({ action: 'get_story_hierarchy', story_id: storyId });
    }

    public refreshContent(sectionId: ID = this.data.section.section_id) {<commit_msg>Delete alias<commit_after>﻿import { Injectable } from '@angular/core';
import { Subject, Observable } from 'rxjs/Rx';

import { WebSocketService } from './websocket.service';
import { ParserService } from './parser.service';

// Models
import { User } from '../models/user/user.model';
import { Collaborator } from '../models/user/collaborator.model';

import { LinkTable } from '../models/link/link-table.model';
import { Link } from '../models/link/link.model';
import { ID } from '../models/id.model';

import { StorySummary } from '../models/story/story-summary.model';
import { Story } from '../models/story/story.model';
import { Section } from '../models/story/section.model';
import { Paragraph } from '../models/story/paragraph.model';
import { Tooltip } from '../models/story/tooltip.model';
import { ContentObject } from '../models/story/content-object.model';

import { WikiSummary } from '../models/wiki/wiki-summary.model';
import { Wiki } from '../models/wiki/wiki.model';
import { Segment } from '../models/wiki/segment.model';
import { PageSummary } from '../models/wiki/page-summary.model';
import { Page } from '../models/wiki/page.model';
import { Heading } from '../models/wiki/heading.model';
import { Reference } from '../models/wiki/reference.model';

const url: string = 'ws://localhost:8080/ws/demo';

@Injectable()
export class ApiService {
    public data = {
        inflight: false,
        user: new User(),
        stories: new Array<StorySummary>(),
        wikis: new Array<WikiSummary>(),
        linkTable: new LinkTable(),

        storyNode: [],
        storyDisplay: '',
        story: new Story(),
        section: new Section(),
        content: new Array<Paragraph>(),

        wikiNav: [],
        wikiNode: [],
        wikiDisplay: '',
        wiki: new Wiki(),
        segment: new Segment(),
        page: new Page(),
        pageid: [],

        tooltip: new Tooltip(),
        contentObject: new ContentObject()
    }

    public outgoing = {};
    public message_id: number = 0;
    public messages: Subject<string>;

    constructor(
        private socket: WebSocketService,
        private parser: ParserService) { }

    public connect() {
        this.messages = <Subject<string>>this.socket.connect(url)
            .map((res: MessageEvent) => {
                this.data.inflight = false;

                let response: string = res.data;
                let reply = JSON.parse(response);
                if (reply.event) {
                    return reply.event;
                } else {
                    if (!reply.reply_to_id) {
                        reply = JSON.parse(reply);
                    }

                    let message_id: number = reply.reply_to_id;
                    let out: any = this.outgoing[message_id];
                    let action: string = out.action;
                    let callback: Function = out.callback;

                    switch (action) {
                        case 'get_user_preferences':
                            this.data.user = reply;
                            break;
                        case 'get_user_stories':
                            this.data.stories = reply.stories;
                            this.data.stories.push({
                                story_id: null,
                                title: null,
                                access_level: null
                            });
                            break;
                        case 'get_user_wikis':
                            this.data.wikis = reply.wikis;
                            break;

                        case 'create_story':
                            this.data.stories.unshift({
                                story_id: reply.story_id,
                                title: reply.story_title,
                                access_level: reply.access_level
                            });
                            this.data.story = reply;
                            this.refreshStory(reply.story_id);
                        case 'get_story_information':
                            reply.story_id = this.data.story.story_id;
                            this.data.story = reply;
                            this.data.wiki.wiki_id = reply.wiki_id;
                            this.data.storyDisplay = '<h1>Summary</h1>' + reply.summary;
                            this.refreshWiki(reply.wiki_id, true);
                            break;
                        case 'get_story_hierarchy':
                        case 'get_section_hierarchy':
                            this.data.section = reply.hierarchy;
                            this.data.storyNode = [
                                this.parser.sectionToTree(this.parser, reply.hierarchy)
                            ];
                            break;
                        case 'get_section_content':
                            this.data.content = reply.content;
                            this.data.contentObject = this.parser.parseContent(reply.content, this.data.linkTable);
                            this.data.storyDisplay = this.parser.setContentDisplay(reply.content);
                            break;

                        case 'add_inner_subsection':
                            this.refreshStory();
                            break

                        case 'add_paragraph':
                            callback(reply);
                        case 'edit_paragraph':
                        case 'delete_paragraph':
                            this.refreshContent();
                            break;

                        case 'create_link':
                            callback(reply);
                        case 'delete_link':
                            break;

                        case 'create_wiki':
                            this.data.wiki = reply;
                            this.refreshWiki(reply.wiki_id);
                        case 'get_wiki_information':
                            reply.wiki_id = this.data.wiki.wiki_id;
                            this.data.wiki = reply;
                            this.data.wikiDisplay = this.parser.setWikiDisplay(reply);
                            break;
                        case 'get_wiki_hierarchy':
                        case 'get_wiki_segment_hierarchy':
                            this.data.segment = reply.hierarchy;
                            this.data.wikiNav = this.parser.parseWiki(reply.hierarchy);
                            this.data.linkTable = this.parser.parseLinkTable(reply.link_table);
                            break;
                        case 'get_wiki_segment':
                            reply = JSON.parse(JSON.stringify(reply).replace("template_headings", "headings"));
                            this.data.page = reply;
                            break;
                        case 'get_wiki_page':
                            this.data.page = reply;
                            this.data.tooltip.text = '<b>' + reply.title + '</b>';
                            if (reply.headings && reply.headings[0]) {
                                this.data.tooltip.text += '<br/><u>' + reply.headings[0].title + '</u><br/>' + reply.headings[0].text;
                            }
                            break;
                        case 'add_page':
                            this.data.pageid.push(reply.page_id);
                            this.refreshWiki();
                            break;
                        case 'add_segment':
                            this.data.pageid.push(reply.segment_id);
                            this.refreshWiki();
                            break;
                        case 'add_template_heading':
                            break;
                        case 'add_heading':
                            break;
                        case 'create_wiki':
                            break;
                        case 'edit_segment':
                            break;
                        case 'edit_page':
                            break;
                        case 'edit_heading':
                            break;
                        case 'change_alias_name':
                            this.refreshWiki();
                            this.refreshContent();
                            break;
                        case 'delete_alias':
                            this.refreshContent();
                            break

                        default:
                            console.log('Unknown action: ' + action)
                            break;
                    }
                    delete this.outgoing[message_id];
                    return action;
                }
            });
        this.messages.subscribe((action: string) => {
            console.log(action);
        });
    }

    public refreshStory(storyId: ID = this.data.story.story_id, info: boolean = false) {
        if (info) {
            this.send({ action: 'get_story_information', story_id: storyId });
        }
        this.send({ action: 'get_story_hierarchy', story_id: storyId });
    }

    public refreshContent(sectionId: ID = this.data.section.section_id) {
{'fraction_non_alphanumeric': 0.10098428408287563, 'fraction_numerical': 0.0010876067214095383, 'mean_word_length': 1.0810229715966957, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 35, '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': '2997129', 'n_tokens_mistral': 4379, 'n_tokens_neox': 4197, 'n_words': 819}