Exemplo n.º 1
0
// Resolve redirects from (deduplicated) pages, replacing them in the results.
// XXX: We only replace the row.doc, and not row.id, row.key, nor row.value.
async function resolveRedirects(pagesResult) {
    // Get the targets of all docs' 'seeInstead' links.
    const targetPageIds = pagesResult.rows.map(get('doc.seeInstead._id'))
        .filter(x => x)

    // If these pages contain no redirects, easy job for us.
    if (targetPageIds.length === 0) return pagesResult

    // Fetch the targeted pages.
    // Note that multi-step redirections are resolved recursively here.
    // XXX: a cycle of redirections would kill us.
    const targetPagesResult = await getPages({
        pageIds: targetPageIds,
        followRedirects: true,
    })

    // Replace each page doc with its redirection target (if it had any).
    const targetRowsById = resultRowsById(targetPagesResult)
    const resolvedPagesResult = update('rows', rows => rows.map(
        update('doc', doc => (!doc.seeInstead)
            // Leave pages without a 'seeInstead' redirection link untouched...
            ? doc
            // ...and replace the contents of the others with the correct page.
            : {...targetRowsById[doc.seeInstead._id].doc}
        )
    ))(pagesResult)

    return resolvedPagesResult
}
Exemplo n.º 2
0
// Nest the page docs into the visit docs, and return the latter.
async function insertPagesIntoVisits({ visitsResult, pagesResult }) {
    // If pages are not already passed to us, get them.
    if (pagesResult === undefined) {
        // Get the page of each visit.
        const pageIds = visitsResult.rows.map(row => row.doc.page._id)
        pagesResult = await getPages({
            pageIds,
            // Assume that we always want to follow redirects.
            followRedirects: true,
        })
        // Because the results are lined up, we can use a small optimisation and
        // return directly.
        return update('rows', rows =>
            rows.map((row, i) =>
                update('doc.page', () => pagesResult.rows[i].doc)(row),
            ),
        )(visitsResult)
    }
    // Read each visit's doc.page._id and replace it with the specified page.
    const pagesById = resultRowsById(pagesResult)
    return update('rows', rows =>
        rows.map(update('doc.page', page => pagesById[page._id].doc)),
    )(visitsResult)
}