Skip to content

Commit

Permalink
feat: localize wikipedia_url in taxon responses
Browse files Browse the repository at this point in the history
  • Loading branch information
kueda committed Nov 6, 2024
1 parent 17ac7e9 commit 80b53a9
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions lib/models/taxon.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ const Taxon = class Taxon extends Model {
const ids = _.compact( _.map( taxa, "id" ) );
if ( _.isEmpty( ids ) ) { return; }
const query = squel.select( )
.field( "t.id, t.wikipedia_summary, td.locale, td.body" )
.field( "t.id, t.wikipedia_summary, t.wikipedia_title, t.name, td.locale, td.body, td.url" )
.from( "taxa t" )
.left_join( "taxon_descriptions td", null, "t.id = td.taxon_id" )
.where( "t.id IN ?", ids );
Expand All @@ -463,9 +463,17 @@ const Taxon = class Taxon extends Model {
_.each( rows, r => {
byTaxonID[r.id] = byTaxonID[r.id] || [];
if ( r.body ) {
byTaxonID[r.id].push( { locale: r.locale.toLowerCase( ), body: r.body } );
byTaxonID[r.id].push( {
locale: r.locale.toLowerCase( ),
body: r.body,
url: r.url
} );
}
byTaxonID[r.id].push( { locale: "en", body: r.wikipedia_summary } );
byTaxonID[r.id].push( {
locale: "en",
body: r.wikipedia_summary,
url: `https://en.wikipedia.org/wiki/${r.wikipedia_title || r.name}`
} );
} );
_.each( _.compact( taxa ), t => {
if ( byTaxonID[t.id] ) {
Expand All @@ -474,10 +482,14 @@ const Taxon = class Taxon extends Model {
const localePrefix = options.locale.split( "-" )[0];
summary = _.find( byTaxonID[t.id], d => _.startsWith( d.locale, `${localePrefix}` ) );
}
if ( summary ) { t.wikipedia_summary = summary.body; }
if ( summary ) {
t.wikipedia_summary = summary.body;
t.wikipedia_url = summary.url;
}
}
if ( !t.wikipedia_summary || t.wikipedia_summary.match( /^\d\d\d\d-\d\d-\d\d$/ ) ) {
t.wikipedia_summary = null;
t.wikipedia_url = null;
}
} );
}
Expand Down

0 comments on commit 80b53a9

Please sign in to comment.