diff --git a/commands/interaction/slash/search/google.js b/commands/interaction/slash/search/google.js index c04d097..c48f6df 100644 --- a/commands/interaction/slash/search/google.js +++ b/commands/interaction/slash/search/google.js @@ -21,11 +21,20 @@ function renderFooter(context, doodle){ } } +// These have to be synced with the backend (search_service/endpoints/google). +const SEARCH_CARD_TYPES = { + UNKNONW: 0, + SEARCH_RESULT: 1, + KNOWLEDGE_GRAPH: 2, + DOODLE: 3, + ENTITY: 4 +} + // TODO: create a favicon() util function createSearchResultPage(context, result, doodle){ let res; switch(result.type){ - case 1: // Search Result Entry + case SEARCH_CARD_TYPES.SEARCH_RESULT: res = page(createEmbed("default", context, { author: { iconUrl: `https://www.google.com/s2/favicons?domain=${encodeURIComponent(result.url)}&sz=256`, @@ -38,7 +47,7 @@ function createSearchResultPage(context, result, doodle){ if(result.thumbnail) res.embeds[0].thumbnail = { url: result.thumbnail }; break; - case 2: // Knowledge Graph Entry + case SEARCH_CARD_TYPES.KNOWLEDGE_GRAPH: let header = result.card.title; if(result.card.url) header = link(result.card.url, result.card.title) res = page(createEmbed("default", context, { @@ -54,7 +63,7 @@ function createSearchResultPage(context, result, doodle){ res.embeds[0].description += "\n" + cnt + citation(1, result.card.url, "Source") } break; - case 3: // Doodle Card + case SEARCH_CARD_TYPES.DOODLE: res = page(createEmbed("default", context, { description: `### ${result.card.title}\n${result.card.description}\n\n${link(result.card.learn_more, `Learn More ${icon("link_open_external")}`, "Learn more about this Doodle")}`, thumbnail: { @@ -66,6 +75,37 @@ function createSearchResultPage(context, result, doodle){ footer: renderFooter(context, doodle) })) break; + case SEARCH_CARD_TYPES.ENTITY: + res = createEmbed("default", context, { + author: { + name: result.card.title + }, + thumbnail: { + url: result.card.images.thumbnail + }, + description: `-# ${result.card.type}`, + footer: renderFooter(context, doodle) + }) + + if(!result.card.images.preview && result.card.link == ""){ + delete res.author + res.description = `### ${result.card.title}\n-# ${result.card.type}` + } + + if(result.card.color !== "") res.color = parseInt(result.card.color.substring(1,10), 16) + + if(result.card.description !== ""){ + res.description += "\n\n" + result.card.description + if(result.card.source) res.description += citation(1, result.card.source, "Source") + "\n\n" + link(result.card.source, `Learn More ${icon("link_open_external")}`, "Learn more about this topic"); + else if(result.card.link !== "") res.description += "\n\n" + link(result.card.link, `Learn More ${icon("link_open_external")}`, "Learn more about this topic"); + } + + if(result.card.images.preview) res.author.iconUrl = result.card.images.preview; + + if(result.card.link) res.author.url = result.card.link + + res = page(res); + break; default: res = page(createEmbed("error", context, "Unknown GoogleResult Type: " + result.type)) break; diff --git a/commands/message/search/google.js b/commands/message/search/google.js index 0d5a160..b798cb2 100644 --- a/commands/message/search/google.js +++ b/commands/message/search/google.js @@ -8,6 +8,7 @@ const { STATICS } = require('#utils/statics') // TODO: Turn this into a general purpose permissions constant const { Permissions } = require("detritus-client/lib/constants"); +const { description } = require('../../interaction/slash/search/google'); function renderFooter(context, doodle){ if(doodle.label) return { @@ -21,11 +22,20 @@ function renderFooter(context, doodle){ } } +// These have to be synced with the backend (search_service/endpoints/google). +const SEARCH_CARD_TYPES = { + UNKNONW: 0, + SEARCH_RESULT: 1, + KNOWLEDGE_GRAPH: 2, + DOODLE: 3, + ENTITY: 4 +} + // TODO: create a favicon() util function createSearchResultPage(context, result, doodle){ let res; switch(result.type){ - case 1: // Search Result Entry + case SEARCH_CARD_TYPES.SEARCH_RESULT: res = page(createEmbed("default", context, { author: { iconUrl: `https://www.google.com/s2/favicons?domain=${encodeURIComponent(result.url)}&sz=256`, @@ -38,7 +48,7 @@ function createSearchResultPage(context, result, doodle){ if(result.thumbnail) res.embeds[0].thumbnail = { url: result.thumbnail }; break; - case 2: // Knowledge Graph Entry + case SEARCH_CARD_TYPES.KNOWLEDGE_GRAPH: let header = result.card.title; if(result.card.url) header = link(result.card.url, result.card.title) res = page(createEmbed("default", context, { @@ -54,7 +64,7 @@ function createSearchResultPage(context, result, doodle){ res.embeds[0].description += "\n" + cnt + citation(1, result.card.url, "Source") } break; - case 3: // Doodle Card + case SEARCH_CARD_TYPES.DOODLE: res = page(createEmbed("default", context, { description: `### ${result.card.title}\n${result.card.description}\n\n${link(result.card.learn_more, `Learn More ${icon("link_open_external")}`, "Learn more about this Doodle")}`, thumbnail: { @@ -66,6 +76,37 @@ function createSearchResultPage(context, result, doodle){ footer: renderFooter(context, doodle) })) break; + case SEARCH_CARD_TYPES.ENTITY: + res = createEmbed("default", context, { + author: { + name: result.card.title + }, + thumbnail: { + url: result.card.images.thumbnail + }, + description: `-# ${result.card.type}`, + footer: renderFooter(context, doodle) + }) + + if(!result.card.images.preview && result.card.link == ""){ + delete res.author + res.description = `### ${result.card.title}\n-# ${result.card.type}` + } + + if(result.card.color !== "") res.color = parseInt(result.card.color.substring(1,10), 16) + + if(result.card.description !== ""){ + res.description += "\n\n" + result.card.description + if(result.card.source) res.description += citation(1, result.card.source, "Source") + "\n\n" + link(result.card.source, `Learn More ${icon("link_open_external")}`, "Learn more about this topic"); + else if(result.card.link !== "") res.description += "\n\n" + link(result.card.link, `Learn More ${icon("link_open_external")}`, "Learn more about this topic"); + } + + if(result.card.images.preview) res.author.iconUrl = result.card.images.preview; + + if(result.card.link) res.author.url = result.card.link + + res = page(res); + break; default: res = page(createEmbed("error", context, "Unknown GoogleResult Type: " + result.type)) break;