1 import JSONAPISerializer from 'ember-data/serializers/json-api'; |
1 import JSONAPISerializer from 'ember-data/serializers/json-api'; |
|
2 import _ from 'lodash/lodash'; |
2 |
3 |
3 export default JSONAPISerializer.extend({ |
4 export default JSONAPISerializer.extend({ |
4 |
5 |
5 normalizeResponse: function(store, primaryModelClass, payload, id) { |
6 normalizeResponse: function(store, primaryModelClass, payload, id) { |
6 var speakers = payload['resources'].find(resource => resource['id'] === 'speakers'); |
7 var speakerResources = payload['resources'].find(resource => resource['id'] === 'speakers') || {'content': { 'data': []}}; |
7 var topics = payload['resources'].find(resource => resource['id'] === 'topics'); |
8 var speakers = _.reduce( |
|
9 speakerResources['content']['data'], |
|
10 function(res, s) { res[s.id] = s; return res;}, |
|
11 {}); |
|
12 var topicsResources = payload['resources'].find(resource => resource['id'] === 'topics') || {'content': { 'data': []}}; |
|
13 var topics = _.reduce( |
|
14 topicsResources['content']['data'], |
|
15 function(res, r) { res[r.id] = r; return res;}, |
|
16 {}); |
|
17 |
8 var translationISO = false; |
18 var translationISO = false; |
9 |
19 |
10 var buildFragment = function(annotation) { |
20 var turns = _.reduce(payload['annotation-types'] || [], function(res, t) { |
11 var fragment = { |
21 res[t['id']] = { |
12 'original': annotation['content']['data']['content'] |
22 title: t['dc:title'], |
|
23 begin: t['corpus:begin'], |
|
24 end: t['corpus:end'], |
|
25 annotations: [] |
|
26 }; |
|
27 return res; |
|
28 },{}); |
|
29 |
|
30 var sections = _.map(payload['lists'] || [], function(list){ |
|
31 var topic = topics[list['meta']['corpus:topic']['id-ref']]; |
|
32 return { |
|
33 title: (topic && topic['desc'])?topic['desc'] : null, |
|
34 begin: list['meta']['corpus:begin'], |
|
35 end: list['meta']['corpus:end'], |
|
36 turns: _.reduce((list['items'] || []), function(res,item) { |
|
37 if(item['id-ref'] && turns[item['id-ref']]) { |
|
38 res.push(turns[item['id-ref']]); |
|
39 } |
|
40 return res; |
|
41 }, []) |
|
42 }; |
|
43 }); |
|
44 |
|
45 var currentSpeaker = null; |
|
46 var annotations = _.map(payload['annotations'] || [], function(annotation) { |
|
47 var annot = { |
|
48 original: annotation['content']['data']['content'], |
|
49 begin: annotation['begin'], |
|
50 end: annotation['end'] |
13 }; |
51 }; |
14 if(annotation['content']['data']['transl']) { |
52 if(annotation['content']['data']['transl']) { |
15 fragment['translation'] = annotation['content']['data']['transl']['@value']; |
53 annot['translation'] = annotation['content']['data']['transl']['@value']; |
16 } |
54 } |
17 if(annotation['content']['data']['words']) { |
55 if(annotation['content']['data']['words']) { |
18 var words = []; |
56 var words = _.map(annotation['content']['data']['words'], function(word) { |
19 annotation['content']['data']['words'].forEach(function(word) { |
57 return { |
20 words.push({ |
|
21 'original': word['content'], |
58 'original': word['content'], |
22 'translation': word['transl']['@value'] |
59 'translation': word['transl']['@value'] |
23 }); |
60 }; |
24 }); |
61 }); |
25 fragment['literal'] = words; |
62 annot['literal'] = words; |
26 } |
63 } |
27 if(annotation['content']['data']['speaker']) { |
64 if(annotation['content']['data']['speaker']) { |
28 if(typeof annotation['content']['data']['speaker'] === 'object') { |
65 if(typeof annotation['content']['data']['speaker'] === 'object') { |
29 var speaker = speakers['content']['data'].find(speaker => speaker['id'] === annotation['content']['data']['speaker']['id-ref']); |
66 var speaker = speakers[annotation['content']['data']['speaker']['id-ref']]; |
30 if(speaker) { |
67 if(speaker) { |
31 fragment['speaker'] = speaker['name']; |
68 annot['speaker'] = speaker['name']; |
32 } |
69 } |
33 } else { |
70 } else { |
34 fragment['speaker'] = annotation['content']['data']['speaker']; |
71 annot['speaker'] = annotation['content']['data']['speaker']; |
35 } |
72 } |
|
73 annot['showSpeaker'] = (annot['speaker'] === currentSpeaker); |
|
74 currentSpeaker = annot['speaker']; |
36 } |
75 } |
37 return fragment; |
76 if(annotation['type'] && turns[annotation['type']]) { |
38 }; |
77 var type = turns[annotation['type']]; |
39 |
78 annot['type'] = type; |
40 var annotations = []; |
79 type.annotations.push(annot); |
41 payload['annotations'].forEach(function(annotation) { |
|
42 var previous = annotations[annotations.length - 1]; |
|
43 if(previous && annotation['begin'] === previous.begin && annotation['end'] === previous.end) { |
|
44 previous.fragments.push(buildFragment(annotation)); |
|
45 } else { |
|
46 var object = { |
|
47 'fragments': [buildFragment(annotation)], |
|
48 'begin': annotation['begin'], |
|
49 'end': annotation['end'], |
|
50 }; |
|
51 annotations.push(object); |
|
52 } |
80 } |
53 if(!translationISO && annotation['content']['data']['transl']) { |
81 if(!translationISO && annotation['content']['data']['transl']) { |
54 translationISO = annotation['content']['data']['transl']['@language']; |
82 translationISO = annotation['content']['data']['transl']['@language']; |
55 } |
83 } |
|
84 |
|
85 return annot; |
56 }); |
86 }); |
57 |
|
58 if(payload['annotation-types'].length) { |
|
59 var types = []; |
|
60 payload['annotation-types'].forEach(function(t) { |
|
61 var object = { |
|
62 'title': t['dc:title'], |
|
63 'begin': t['corpus:begin'], |
|
64 'end': t['corpus:end'] |
|
65 }; |
|
66 var fragments = annotations.find(function(annotation) { return annotation['begin'] === t['corpus:begin'] && annotation['end'] === t['corpus:end']; }); |
|
67 if(fragments) { |
|
68 object['fragments'] = fragments['fragments']; |
|
69 } |
|
70 types.push(object); |
|
71 }); |
|
72 annotations = types; |
|
73 } |
|
74 |
|
75 var sections = []; |
|
76 if(payload['lists'].length) { |
|
77 var lists = []; |
|
78 payload['lists'].forEach(function(list) { |
|
79 var topic = topics['content']['data'].find(topic => topic.id === list['meta']['corpus:topic']['id-ref']); |
|
80 sections.push({ |
|
81 'title': topic['desc'], |
|
82 'begin': list['meta']['corpus:begin'], |
|
83 'end': list['meta']['corpus:end'] |
|
84 }); |
|
85 lists.push(annotations.filter(annotation => annotation['begin'] >= list['meta']['corpus:begin'] && annotation['end'] <= list['meta']['corpus:end'])); |
|
86 }); |
|
87 annotations = lists; |
|
88 } else { |
|
89 annotations = [annotations]; |
|
90 } |
|
91 |
87 |
92 var response = { |
88 var response = { |
93 'data': { |
89 'data': { |
94 'id': id, |
90 id: id, |
95 'type': 'transcript', |
91 type: 'transcript', |
96 'attributes': { |
92 attributes: { |
97 'title': {}, |
93 title: {}, |
98 'annotations': annotations |
94 annotations: annotations, |
|
95 sections: sections, |
|
96 turns: turns |
99 } |
97 } |
100 } |
98 } |
101 }; |
99 }; |
102 |
|
103 if(sections.length) { |
|
104 response.data.attributes.sections = sections; |
|
105 } |
|
106 |
100 |
107 if(Array.isArray(payload['meta']['dc:title'])) { |
101 if(Array.isArray(payload['meta']['dc:title'])) { |
108 var original = payload['meta']['dc:title'].find(function(title) { return title['@language'] !== translationISO; }); |
102 var original = payload['meta']['dc:title'].find(function(title) { return title['@language'] !== translationISO; }); |
109 if(original) { |
103 if(original) { |
110 response.data.attributes.title.original = original['@value']; |
104 response.data.attributes.title.original = original['@value']; |