cms/app-client/app/serializers/transcript.js
author Chloe Laisne <chloe.laisne@gmail.com>
Tue, 23 Aug 2016 00:32:11 +0200
changeset 258 12c694392e8e
parent 257 eba9edbd8f46
child 264 e0fc9f698b9b
permissions -rw-r--r--
Add topic to transcript
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
245
c9dd78a43b07 Transcript model and erializer
Chloe Laisne <chloe.laisne@gmail.com>
parents:
diff changeset
     1
import JSONAPISerializer from 'ember-data/serializers/json-api';
c9dd78a43b07 Transcript model and erializer
Chloe Laisne <chloe.laisne@gmail.com>
parents:
diff changeset
     2
c9dd78a43b07 Transcript model and erializer
Chloe Laisne <chloe.laisne@gmail.com>
parents:
diff changeset
     3
export default JSONAPISerializer.extend({
c9dd78a43b07 Transcript model and erializer
Chloe Laisne <chloe.laisne@gmail.com>
parents:
diff changeset
     4
257
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
     5
    normalizeResponse: function(store, primaryModelClass, payload, id) {
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
     6
        var speakers = payload['resources'].find(resource => resource['id'] === 'speakers');
258
12c694392e8e Add topic to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 257
diff changeset
     7
        var topics = payload['resources'].find(resource => resource['id'] === 'topics');
257
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
     8
        var translationISO = false;
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
     9
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    10
        var buildFragment = function(annotation) {
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    11
            var fragment = {
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    12
                'original': annotation['content']['data']['content']
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    13
            };
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    14
            if(annotation['content']['data']['transl']) {
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    15
                fragment['translation'] = annotation['content']['data']['transl']['@value'];
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    16
            }
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    17
            if(annotation['content']['data']['words']) {
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    18
                var words = [];
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    19
                annotation['content']['data']['words'].forEach(function(word) {
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    20
                    words.push({
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    21
                        'original': word['content'],
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    22
                        'translation': word['transl']['@value']
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    23
                    });
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    24
                });
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    25
                fragment['literal'] = words;
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    26
            }
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    27
            if(annotation['content']['data']['speaker']) {
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    28
                if(typeof annotation['content']['data']['speaker'] === 'object') {
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    29
                    var speaker = speakers['content']['data'].find(speaker => speaker['id'] === annotation['content']['data']['speaker']['id-ref']);
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    30
                    if(speaker) {
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    31
                        fragment['speaker'] = speaker['name'];
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    32
                    }
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    33
                } else {
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    34
                    fragment['speaker'] = annotation['content']['data']['speaker'];
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    35
                }
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    36
            }
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    37
            return fragment;
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    38
        };
256
18f0c3ee9aa5 Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 255
diff changeset
    39
18f0c3ee9aa5 Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 255
diff changeset
    40
        var annotations = [];
257
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    41
        payload['annotations'].forEach(function(annotation) {
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    42
            var previous = annotations[annotations.length - 1];
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    43
            if(previous && annotation['begin'] === previous.begin && annotation['end'] === previous.end) {
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    44
                previous.fragments.push(buildFragment(annotation));
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    45
            } else {
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    46
                var object = {
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    47
                    'fragments': [buildFragment(annotation)],
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    48
                    'begin': annotation['begin'],
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    49
                    'end': annotation['end'],
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    50
                };
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    51
                annotations.push(object);
256
18f0c3ee9aa5 Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 255
diff changeset
    52
            }
257
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    53
            if(!translationISO && annotation['content']['data']['transl']) {
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    54
                translationISO = annotation['content']['data']['transl']['@language'];    
256
18f0c3ee9aa5 Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 255
diff changeset
    55
            }
18f0c3ee9aa5 Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 255
diff changeset
    56
        });
18f0c3ee9aa5 Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 255
diff changeset
    57
258
12c694392e8e Add topic to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 257
diff changeset
    58
        if(payload['annotation-types'].length) {
12c694392e8e Add topic to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 257
diff changeset
    59
            var types = [];
12c694392e8e Add topic to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 257
diff changeset
    60
            payload['annotation-types'].forEach(function(t) {
12c694392e8e Add topic to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 257
diff changeset
    61
                var object = {
12c694392e8e Add topic to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 257
diff changeset
    62
                    'title': t['dc:title'],
12c694392e8e Add topic to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 257
diff changeset
    63
                    'begin': t['corpus:begin'],
12c694392e8e Add topic to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 257
diff changeset
    64
                    'end': t['corpus:end']
12c694392e8e Add topic to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 257
diff changeset
    65
                };
12c694392e8e Add topic to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 257
diff changeset
    66
                var fragments = annotations.find(function(annotation) { return annotation['begin'] === t['corpus:begin'] && annotation['end'] === t['corpus:end']; });
12c694392e8e Add topic to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 257
diff changeset
    67
                if(fragments) {
12c694392e8e Add topic to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 257
diff changeset
    68
                    object['fragments'] = fragments['fragments'];
12c694392e8e Add topic to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 257
diff changeset
    69
                }
12c694392e8e Add topic to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 257
diff changeset
    70
                types.push(object);
12c694392e8e Add topic to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 257
diff changeset
    71
            });
12c694392e8e Add topic to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 257
diff changeset
    72
            annotations = types;
12c694392e8e Add topic to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 257
diff changeset
    73
        }
12c694392e8e Add topic to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 257
diff changeset
    74
12c694392e8e Add topic to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 257
diff changeset
    75
        if(payload['lists'].length) {
12c694392e8e Add topic to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 257
diff changeset
    76
            var sections = [];
12c694392e8e Add topic to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 257
diff changeset
    77
            var lists = [];
12c694392e8e Add topic to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 257
diff changeset
    78
            payload['lists'].forEach(function(list) {
12c694392e8e Add topic to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 257
diff changeset
    79
                var topic = topics['content']['data'].find(topic => topic.id === list['meta']['corpus:topic']['id-ref']);
12c694392e8e Add topic to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 257
diff changeset
    80
                sections.push({
12c694392e8e Add topic to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 257
diff changeset
    81
                    'title': topic['desc'],
12c694392e8e Add topic to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 257
diff changeset
    82
                    'begin': list['meta']['corpus:begin'],
12c694392e8e Add topic to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 257
diff changeset
    83
                    'end': list['meta']['corpus:end']
12c694392e8e Add topic to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 257
diff changeset
    84
                });
12c694392e8e Add topic to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 257
diff changeset
    85
                lists.push(annotations.filter(annotation => annotation['begin'] >= list['meta']['corpus:begin'] && annotation['end'] <= list['meta']['corpus:end']));
12c694392e8e Add topic to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 257
diff changeset
    86
            });
12c694392e8e Add topic to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 257
diff changeset
    87
            annotations = lists;
12c694392e8e Add topic to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 257
diff changeset
    88
            console.log(sections);
12c694392e8e Add topic to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 257
diff changeset
    89
        } else {
12c694392e8e Add topic to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 257
diff changeset
    90
            annotations = [annotations];
12c694392e8e Add topic to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 257
diff changeset
    91
        }
12c694392e8e Add topic to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 257
diff changeset
    92
256
18f0c3ee9aa5 Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 255
diff changeset
    93
        var response = {
245
c9dd78a43b07 Transcript model and erializer
Chloe Laisne <chloe.laisne@gmail.com>
parents:
diff changeset
    94
            'data': {
256
18f0c3ee9aa5 Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 255
diff changeset
    95
                'id': id,
18f0c3ee9aa5 Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 255
diff changeset
    96
                'type': 'transcript',
18f0c3ee9aa5 Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 255
diff changeset
    97
                'attributes': {
18f0c3ee9aa5 Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 255
diff changeset
    98
                    'title': {},
18f0c3ee9aa5 Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 255
diff changeset
    99
                    'annotations': annotations
18f0c3ee9aa5 Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 255
diff changeset
   100
                }
18f0c3ee9aa5 Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 255
diff changeset
   101
            }
245
c9dd78a43b07 Transcript model and erializer
Chloe Laisne <chloe.laisne@gmail.com>
parents:
diff changeset
   102
        };
256
18f0c3ee9aa5 Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 255
diff changeset
   103
258
12c694392e8e Add topic to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 257
diff changeset
   104
        if(sections) {
12c694392e8e Add topic to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 257
diff changeset
   105
            response.data.attributes.sections = sections;
12c694392e8e Add topic to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 257
diff changeset
   106
        }
12c694392e8e Add topic to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 257
diff changeset
   107
257
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
   108
        if(Array.isArray(payload['meta']['dc:title'])) {
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
   109
            var original = payload['meta']['dc:title'].find(function(title) { return title['@language'] !== translationISO; });
256
18f0c3ee9aa5 Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 255
diff changeset
   110
            if(original) {
18f0c3ee9aa5 Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 255
diff changeset
   111
                response.data.attributes.title.original = original['@value'];
18f0c3ee9aa5 Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 255
diff changeset
   112
            }
257
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
   113
            var translation = payload['meta']['dc:title'].find(function(title) { return title['@language'] === translationISO; });
256
18f0c3ee9aa5 Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 255
diff changeset
   114
            if(translation) {
18f0c3ee9aa5 Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 255
diff changeset
   115
                response.data.attributes.title.translation = translation['@value'];
18f0c3ee9aa5 Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 255
diff changeset
   116
            }            
18f0c3ee9aa5 Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 255
diff changeset
   117
        } else {
257
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
   118
            response.data.attributes.title.original = payload['meta']['dc:title']['@value'];
256
18f0c3ee9aa5 Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 255
diff changeset
   119
        }
18f0c3ee9aa5 Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 255
diff changeset
   120
18f0c3ee9aa5 Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 255
diff changeset
   121
18f0c3ee9aa5 Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 255
diff changeset
   122
        return response;
18f0c3ee9aa5 Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 255
diff changeset
   123
    }
245
c9dd78a43b07 Transcript model and erializer
Chloe Laisne <chloe.laisne@gmail.com>
parents:
diff changeset
   124
c9dd78a43b07 Transcript model and erializer
Chloe Laisne <chloe.laisne@gmail.com>
parents:
diff changeset
   125
});