Commit 08776cd9 authored by chdorner's avatar chdorner

Update client's document data extraction

The `annotation.uri` now always is a linkable URI if we got that data
when the annotation got created. There is no need anymore to try and
find another linkable URI from the document data.
This also removes the type check of `annotation.document.title` since
the presentation layer on the backend always returns an array of
strings (or the `title` property is missing).
parent 319e246c
......@@ -13,47 +13,23 @@
*
*/
function extractDocumentMetadata(annotation) {
var document_;
var uri = annotation.uri;
var domain = new URL(uri).hostname;
if (annotation.document) {
if (uri.indexOf('urn') === 0) {
var i;
for (i = 0; i < annotation.document.link.length; i++) {
var link = annotation.document.link[i];
if (link.href.indexOf('urn:') === 0) {
continue;
}
uri = link.href;
break;
}
}
var title = domain;
var documentTitle;
if (Array.isArray(annotation.document.title)) {
documentTitle = annotation.document.title[0];
} else {
documentTitle = annotation.document.title;
}
document_ = {
uri: uri,
domain: domain,
title: documentTitle || domain
};
} else {
document_ = {
uri: uri,
domain: domain,
title: domain
};
if (annotation.document && annotation.document.title) {
title = annotation.document.title[0];
}
if (document_.title.length > 30) {
document_.title = document_.title.slice(0, 30) + '…';
if (title.length > 30) {
title = title.slice(0, 30) + '…';
}
return document_;
return {
uri: uri,
domain: domain,
title: title,
};
}
/** Return `true` if the given annotation is a reply, `false` otherwise. */
......
......@@ -17,26 +17,6 @@ describe('annotation-metadata', function () {
assert.equal(extractDocumentMetadata(model).domain, 'example.com');
});
context('when model.uri starts with "urn"', function() {
it(
'uses the first document.link uri that doesn\'t start with "urn"',
function() {
var model = {
uri: 'urn:isbn:0451450523',
document: {
link: [
{href: 'urn:isan:0000-0000-9E59-0000-O-0000-0000-2'},
{href: 'http://example.com/'}
]
}
};
assert.equal(
extractDocumentMetadata(model).uri, 'http://example.com/');
}
);
});
context('when model.uri does not start with "urn"', function() {
it('uses model.uri as the uri', function() {
var model = {
......@@ -49,27 +29,13 @@ describe('annotation-metadata', function () {
});
});
context('when document.title is a string', function() {
it('returns document.title as title', function() {
var model = {
uri: 'http://example.com/',
document: {
title: 'My Document'
}
};
assert.equal(
extractDocumentMetadata(model).title, model.document.title);
});
});
context('when document.title is an array', function() {
it('returns document.title[0] as title', function() {
context('when document.title is an available', function() {
it('uses the first document title as the title', function() {
var model = {
uri: 'http://example.com/',
document: {
title: ['My Document', 'My Other Document']
}
},
};
assert.equal(
......@@ -114,8 +80,8 @@ describe('annotation-metadata', function () {
var model = {
uri: 'http://example.com/',
document: {
title: 'My Really Really Long Document Title'
}
title: ['My Really Really Long Document Title'],
},
};
assert.equal(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment