From 70415626c5ead32598128638f010b51a0542ddc9 Mon Sep 17 00:00:00 2001
From: marcdeb1 <marcdebrouchev@laposte.net>
Date: Mon, 22 Oct 2018 09:59:12 +0200
Subject: [PATCH] Added content type filter Added claim type filter Improved
 filtering and return no results when wrong input List of types as input for
 mediaType and contentType

---
 server/controllers/lighthouse.js | 39 ++++++++++++++++++++++++++++++--
 server/routes/lighthouse.js      |  3 +++
 2 files changed, 40 insertions(+), 2 deletions(-)

diff --git a/server/controllers/lighthouse.js b/server/controllers/lighthouse.js
index 6eb8578..27466f1 100644
--- a/server/controllers/lighthouse.js
+++ b/server/controllers/lighthouse.js
@@ -255,14 +255,49 @@ function getAutoCompleteQuery (query) {
 };
 
 function getFilters (input) {
-  // this is the best place for putting things like filtering on the type of content
-  // Perhaps we can add search param that will filter on how people have categorized / tagged their content
   var filters = [];
   var bidStateFilter = {'bool': {'must_not': {'match': { 'bid_state': 'Accepted' }}}};
   if (input.nsfw === 'true' || input.nsfw === 'false') {
     const nsfwFilter = {'match': {'value.stream.metadata.nsfw': input.nsfw}};
     filters.push(nsfwFilter);
   }
+  if (input.contentType !== undefined) {
+    const contentTypes = input.contentType.split(',');
+    const contentFilter = {'terms': {'value.stream.source.contentType.keyword': contentTypes}};
+    filters.push(contentFilter);
+  }
+  if (input.mediaType !== undefined) {
+    const mediaTypes = input.mediaType.split(',');
+    const possibleTypes = ['audio', 'video', 'text', 'application', 'image'];
+    const shouldQueries = [];
+    for (var i = 0; i < mediaTypes.length; i++) {
+      if (possibleTypes.includes(mediaTypes[i])) {
+        const mediaFilter = {'prefix': {'value.stream.source.contentType.keyword': mediaTypes[i] + '/'}};
+        shouldQueries.push(mediaFilter);
+      } else if (mediaTypes[i] === 'cad') {
+        const cadTypes = ['SKP', 'simplify3d_stl'];
+        const cadFilter = {'terms': {'value.stream.source.contentType.keyword': cadTypes}};
+        shouldQueries.push(cadFilter);
+      }
+    }
+    if (shouldQueries.length === 0) {
+      const noneFilter = {'match_none': {}};
+      filters.push(noneFilter);
+    } else {
+      const mediaTypeFilter = {'bool': {'should': shouldQueries}};
+      filters.push(mediaTypeFilter);
+    }
+  }
+  if (input.claimType === 'channel' || input.claimType === 'file') {
+    var query = '';
+    if (input.claimType === 'channel') {
+      query = 'certificateType';
+    } else if (input.claimType === 'file') {
+      query = 'streamType';
+    }
+    const claimTypeFilter = {'match': {'value.claimType': query}};
+    filters.push(claimTypeFilter);
+  }
   if (filters.length > 0) {
     const filterQuery = [
       {
diff --git a/server/routes/lighthouse.js b/server/routes/lighthouse.js
index a48ce2b..0f05b09 100644
--- a/server/routes/lighthouse.js
+++ b/server/routes/lighthouse.js
@@ -20,6 +20,9 @@ router.get('/', LighthouseControllers.info);
  *   - (query) size {Integer} The amount of results to return at max
  *   - (query) from {Integer} The number to start from, good for pagination
  *   - (query) nsfw {Boolean} If search should return nsfw content or not.
+ *   - (query) contentType {String} Filter by MIME type.
+ *   - (query) mediaType {String} Filter by media type, can be audio, video, image, application, text or cad.
+ *   - (query) claimType {String} Return channels or files only, value can be 'channel' or 'file'.
  * responses:
  *   200:
  *     description: The search API returns an array of the found matching search items.