cms/app-client/app/serializers/transcript.js
author ymh <ymh.work@gmail.com>
Sat, 10 Jun 2017 08:33:03 +0200
changeset 532 1190ea937f2d
parent 476 9cffc7f32f14
permissions -rw-r--r--
make things work after node 8, npm 5 migration. Migrate to lodash 4
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';
532
1190ea937f2d make things work after node 8, npm 5 migration. Migrate to lodash 4
ymh <ymh.work@gmail.com>
parents: 476
diff changeset
     2
import _ from 'lodash';
245
c9dd78a43b07 Transcript model and erializer
Chloe Laisne <chloe.laisne@gmail.com>
parents:
diff changeset
     3
c9dd78a43b07 Transcript model and erializer
Chloe Laisne <chloe.laisne@gmail.com>
parents:
diff changeset
     4
export default JSONAPISerializer.extend({
c9dd78a43b07 Transcript model and erializer
Chloe Laisne <chloe.laisne@gmail.com>
parents:
diff changeset
     5
257
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
     6
    normalizeResponse: function(store, primaryModelClass, payload, id) {
454
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
     7
        var speakerResources = payload['resources'].find(resource => resource['id'] === 'speakers') || {'content': { 'data': []}};
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
     8
        var speakers = _.reduce(
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
     9
          speakerResources['content']['data'],
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    10
          function(res, s) { res[s.id] = s; return res;},
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    11
          {});
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    12
        var topicsResources = payload['resources'].find(resource => resource['id'] === 'topics') || {'content': { 'data': []}};
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    13
        var topics = _.reduce(
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    14
          topicsResources['content']['data'],
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    15
          function(res, r) { res[r.id] = r; return res;},
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    16
          {});
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    17
257
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    18
        var translationISO = false;
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    19
454
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    20
        var turns = _.reduce(payload['annotation-types'] || [], function(res, t) {
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    21
            res[t['id']] = {
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    22
                title: t['dc:title'],
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    23
                begin: t['corpus:begin'],
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    24
                end: t['corpus:end'],
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    25
                annotations: []
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    26
            };
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    27
            return res;
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    28
        },{});
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    29
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    30
        var sections = _.map(payload['lists'] || [], function(list){
476
9cffc7f32f14 correct transcripts without topics
ymh <ymh.work@gmail.com>
parents: 461
diff changeset
    31
          var topicRef = list['meta']['corpus:topic']?list['meta']['corpus:topic']['id-ref']:null;
9cffc7f32f14 correct transcripts without topics
ymh <ymh.work@gmail.com>
parents: 461
diff changeset
    32
          var topic = topicRef?topics[topicRef]:null;
454
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    33
          return {
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    34
            title: (topic && topic['desc'])?topic['desc'] : null,
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    35
            begin: list['meta']['corpus:begin'],
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    36
            end: list['meta']['corpus:end'],
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    37
            turns: _.reduce((list['items'] || []), function(res,item) {
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    38
              if(item['id-ref'] && turns[item['id-ref']]) {
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    39
                res.push(turns[item['id-ref']]);
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    40
              }
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    41
              return res;
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    42
            }, [])
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    43
          };
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    44
        });
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    45
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    46
        var currentSpeaker = null;
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    47
        var annotations = _.map(payload['annotations'] || [], function(annotation) {
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    48
            var annot = {
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    49
              original: annotation['content']['data']['content'],
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    50
              begin: annotation['begin'],
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    51
              end: annotation['end']
257
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    52
            };
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    53
            if(annotation['content']['data']['transl']) {
454
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    54
                annot['translation'] = annotation['content']['data']['transl']['@value'];
257
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    55
            }
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    56
            if(annotation['content']['data']['words']) {
454
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    57
                var words = _.map(annotation['content']['data']['words'], function(word) {
461
9b7a6c099870 add morphenes to transcripts
ymh <ymh.work@gmail.com>
parents: 454
diff changeset
    58
9b7a6c099870 add morphenes to transcripts
ymh <ymh.work@gmail.com>
parents: 454
diff changeset
    59
                    var morphenes = _.map(word['morphenes'] || [], function(morph) {
9b7a6c099870 add morphenes to transcripts
ymh <ymh.work@gmail.com>
parents: 454
diff changeset
    60
                      return {
9b7a6c099870 add morphenes to transcripts
ymh <ymh.work@gmail.com>
parents: 454
diff changeset
    61
                        original: morph['content']['@value'] || morph['content'],
9b7a6c099870 add morphenes to transcripts
ymh <ymh.work@gmail.com>
parents: 454
diff changeset
    62
                        translation: morph['transl']['@value'] || morph['transl'],
9b7a6c099870 add morphenes to transcripts
ymh <ymh.work@gmail.com>
parents: 454
diff changeset
    63
                        begin: morph['begin'],
9b7a6c099870 add morphenes to transcripts
ymh <ymh.work@gmail.com>
parents: 454
diff changeset
    64
                        end: morph['end']
9b7a6c099870 add morphenes to transcripts
ymh <ymh.work@gmail.com>
parents: 454
diff changeset
    65
                      };
9b7a6c099870 add morphenes to transcripts
ymh <ymh.work@gmail.com>
parents: 454
diff changeset
    66
                    });
454
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    67
                    return {
461
9b7a6c099870 add morphenes to transcripts
ymh <ymh.work@gmail.com>
parents: 454
diff changeset
    68
                        original: word['content']['@value'] || word['content'],
9b7a6c099870 add morphenes to transcripts
ymh <ymh.work@gmail.com>
parents: 454
diff changeset
    69
                        translation: word['transl']['@value'] || word['transl'],
9b7a6c099870 add morphenes to transcripts
ymh <ymh.work@gmail.com>
parents: 454
diff changeset
    70
                        begin: word['begin'],
9b7a6c099870 add morphenes to transcripts
ymh <ymh.work@gmail.com>
parents: 454
diff changeset
    71
                        end: word['end'],
9b7a6c099870 add morphenes to transcripts
ymh <ymh.work@gmail.com>
parents: 454
diff changeset
    72
                        morphenes: morphenes
454
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    73
                    };
257
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    74
                });
454
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    75
                annot['literal'] = words;
257
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    76
            }
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    77
            if(annotation['content']['data']['speaker']) {
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    78
                if(typeof annotation['content']['data']['speaker'] === 'object') {
454
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    79
                    var speaker = speakers[annotation['content']['data']['speaker']['id-ref']];
257
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    80
                    if(speaker) {
454
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    81
                        annot['speaker'] = speaker['name'];
257
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    82
                    }
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    83
                } else {
454
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    84
                    annot['speaker'] = annotation['content']['data']['speaker'];
257
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    85
                }
454
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    86
                annot['showSpeaker'] = (annot['speaker'] === currentSpeaker);
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    87
                currentSpeaker = annot['speaker'];
257
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    88
            }
454
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    89
            if(annotation['type'] && turns[annotation['type']]) {
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    90
              var type = turns[annotation['type']];
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    91
              annot['type'] = type;
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    92
              type.annotations.push(annot);
256
18f0c3ee9aa5 Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 255
diff changeset
    93
            }
257
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
    94
            if(!translationISO && annotation['content']['data']['transl']) {
264
e0fc9f698b9b undo bad merge on player component + correct jshint
ymh <ymh.work@gmail.com>
parents: 258
diff changeset
    95
                translationISO = annotation['content']['data']['transl']['@language'];
256
18f0c3ee9aa5 Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 255
diff changeset
    96
            }
454
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    97
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
    98
            return annot;
256
18f0c3ee9aa5 Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 255
diff changeset
    99
        });
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
        var response = {
245
c9dd78a43b07 Transcript model and erializer
Chloe Laisne <chloe.laisne@gmail.com>
parents:
diff changeset
   102
            'data': {
454
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
   103
                id: id,
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
   104
                type: 'transcript',
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
   105
                attributes: {
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
   106
                    title: {},
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
   107
                    annotations: annotations,
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
   108
                    sections: sections,
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 264
diff changeset
   109
                    turns: turns
256
18f0c3ee9aa5 Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 255
diff changeset
   110
                }
18f0c3ee9aa5 Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 255
diff changeset
   111
            }
245
c9dd78a43b07 Transcript model and erializer
Chloe Laisne <chloe.laisne@gmail.com>
parents:
diff changeset
   112
        };
256
18f0c3ee9aa5 Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 255
diff changeset
   113
257
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
   114
        if(Array.isArray(payload['meta']['dc:title'])) {
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
   115
            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
   116
            if(original) {
18f0c3ee9aa5 Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 255
diff changeset
   117
                response.data.attributes.title.original = original['@value'];
18f0c3ee9aa5 Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 255
diff changeset
   118
            }
257
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
   119
            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
   120
            if(translation) {
18f0c3ee9aa5 Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 255
diff changeset
   121
                response.data.attributes.title.translation = translation['@value'];
264
e0fc9f698b9b undo bad merge on player component + correct jshint
ymh <ymh.work@gmail.com>
parents: 258
diff changeset
   122
            }
256
18f0c3ee9aa5 Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 255
diff changeset
   123
        } else {
257
eba9edbd8f46 Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents: 256
diff changeset
   124
            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
   125
        }
18f0c3ee9aa5 Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 255
diff changeset
   126
18f0c3ee9aa5 Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 255
diff changeset
   127
18f0c3ee9aa5 Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 255
diff changeset
   128
        return response;
18f0c3ee9aa5 Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 255
diff changeset
   129
    }
245
c9dd78a43b07 Transcript model and erializer
Chloe Laisne <chloe.laisne@gmail.com>
parents:
diff changeset
   130
c9dd78a43b07 Transcript model and erializer
Chloe Laisne <chloe.laisne@gmail.com>
parents:
diff changeset
   131
});