function validateItem()

in public/src/js/modules/content-api.js [56:157]


function validateItem (item) {
    return new Promise(function (resolve, reject) {
        var snapId = snap.validateId(item.id()),
            capiId = articlePath(item.id()),
            data = cache.get('contentApi', capiId);

        if (snapId) {
            item.id(snapId);
            resolve(item);

        } else if (data) {
            item.id(capiId);
            populate(item, data);
            resolve(item);

        } else {
            // Tag combiners need conversion from tag1+tag2 to search?tag=tag1,tag2
            if (capiId.match(/\+/) && isGuardianUrl(item.id())) {
                capiId = 'search?tag=' + capiId.split(/\+/).join(',') + '&';
            } else {
                capiId += '?';
            }

            capiId += CONST.apiSearchParams;

            fetchContent(capiId)
            .then(function(res = {}) {
                var results = res.content,
                    resultsTitle = res.title || 'Unknown title',
                    capiItem,
                    pageCode,
                    err;

                // ContentApi item
                if (results && results.length === 1) {
                    capiItem = results[0];
                    pageCode = internalPageCode(capiItem);
                    if (pageCode) {
                        capiItem.capiId = capiItem.id;
                        populate(item, capiItem);
                        cache.put('contentApi', pageCode, capiItem);

                        const maybeUrlParams = decodeURIComponent(urlQuery(item.id()));
                        const maybeBlockId = maybeUrlParams.split('with:block-')[1];

                        if (maybeBlockId) {
                            item.meta.blockId(maybeBlockId);
                        }
                        item.id(pageCode);
                    } else {
                        err = 'Sorry, that article is malformed (has no internalPageCode)';
                    }

                // A snap, but not an absolute url
                } else if (!item.id().match(/^https?:\/\//)) {
                    err = 'Sorry, URLs must begin with http...';

                // A snap, but snaps can only be created to the Clipboard
                } else if (item.group.parentType !== 'Clipboard') {
                    err = 'Sorry, special links must be dragged to the Clipboard, initially';

                // A snap, but a link off of the tool itself
                } else if (item.id().indexOf(window.location.hostname) > -1) {
                    err = 'Sorry, that link cannot be added to a front';

                // A snap, that's setting it's own type, ie via dragged-in query params
                } else if (item.meta.snapType()) {
                    item.convertToSnap();

                // A snap, of type 'latest', ie.  where the target is a Guardian tag/section page.
                } else if (results && results.length > 1) {
                    modalDialog.confirm({
                        name: 'select_snap_type',
                        data: {
                            prefix: CONST.latestSnapPrefix,
                            resultsTitle: resultsTitle
                        }
                    }).then(function () {
                        item.convertToLatestSnap(resultsTitle);
                        resolve(item);
                    }, function () {
                        item.convertToLinkSnap();
                        resolve(item);
                    });

                    // Waiting for the modal to be closed
                    return;

                    // A snap, of default type 'link'.
                } else {
                    item.convertToLinkSnap();
                }

                if (err) {
                    reject(new Error(err));
                } else {
                    resolve(item);
                }
            });
        }
    });
}