From 9e3ec7656df639d0ab7f3334344870311c7acb93 Mon Sep 17 00:00:00 2001
From: Eduard Urbach <e.urbach@gmail.com>
Date: Mon, 12 Nov 2018 16:52:07 +0900
Subject: [PATCH] Added AMV search

---
 pages/index/searchroutes/searchroutes.go |  1 +
 pages/search/search.go                   | 18 +++++++++++++++---
 pages/search/search.pixy                 | 24 +++++++++++++++++++++++-
 pages/search/search.scarlet              |  3 ++-
 scripts/Actions/Search.ts                |  8 ++++++++
 5 files changed, 49 insertions(+), 5 deletions(-)

diff --git a/pages/index/searchroutes/searchroutes.go b/pages/index/searchroutes/searchroutes.go
index a76e1337..7895b9ad 100644
--- a/pages/index/searchroutes/searchroutes.go
+++ b/pages/index/searchroutes/searchroutes.go
@@ -16,6 +16,7 @@ func Register(l *layout.Layout) {
 	l.Page("/forum-search/*term", search.Forum)
 	l.Page("/soundtrack-search/*term", search.SoundTracks)
 	l.Page("/user-search/*term", search.Users)
+	l.Page("/amv-search/*term", search.AMVs)
 	l.Page("/company-search/*term", search.Companies)
 
 	// Multi-search
diff --git a/pages/search/search.go b/pages/search/search.go
index 6e2cd9bf..dcf0ea69 100644
--- a/pages/search/search.go
+++ b/pages/search/search.go
@@ -19,6 +19,7 @@ const (
 	maxPosts       = 2
 	maxThreads     = 2
 	maxSoundTracks = 3
+	maxAMVs        = 3
 	maxCharacters  = 22
 	maxCompanies   = 3
 )
@@ -29,7 +30,7 @@ func Get(ctx *aero.Context) string {
 	term = strings.TrimPrefix(term, "/")
 	user := utils.GetUser(ctx)
 
-	users, animes, posts, threads, tracks, characters, companies := search.All(
+	users, animes, posts, threads, tracks, characters, amvs, companies := search.All(
 		term,
 		maxUsers,
 		maxAnime,
@@ -37,15 +38,16 @@ func Get(ctx *aero.Context) string {
 		maxThreads,
 		maxSoundTracks,
 		maxCharacters,
+		maxAMVs,
 		maxCompanies,
 	)
 
-	return ctx.HTML(components.SearchResults(term, users, animes, posts, threads, tracks, characters, companies, nil, user))
+	return ctx.HTML(components.SearchResults(term, users, animes, posts, threads, tracks, characters, amvs, companies, nil, user))
 }
 
 // GetEmptySearch renders the search page with no contents.
 func GetEmptySearch(ctx *aero.Context) string {
-	return ctx.HTML(components.SearchResults("", nil, nil, nil, nil, nil, nil, nil, nil, utils.GetUser(ctx)))
+	return ctx.HTML(components.SearchResults("", nil, nil, nil, nil, nil, nil, nil, nil, nil, utils.GetUser(ctx)))
 }
 
 // Anime search.
@@ -96,6 +98,16 @@ func SoundTracks(ctx *aero.Context) string {
 	return ctx.HTML(components.SoundTrackSearchResults(tracks, user))
 }
 
+// AMVs search.
+func AMVs(ctx *aero.Context) string {
+	term := ctx.Get("term")
+	term = strings.TrimPrefix(term, "/")
+	user := utils.GetUser(ctx)
+
+	amvs := search.AMVs(term, maxAMVs)
+	return ctx.HTML(components.AMVSearchResults(amvs, user))
+}
+
 // Users search.
 func Users(ctx *aero.Context) string {
 	term := ctx.Get("term")
diff --git a/pages/search/search.pixy b/pages/search/search.pixy
index f08311f4..a44aa12f 100644
--- a/pages/search/search.pixy
+++ b/pages/search/search.pixy
@@ -1,4 +1,4 @@
-component SearchResults(term string, users []*arn.User, animes []*arn.Anime, posts []*arn.Post, threads []*arn.Thread, tracks []*arn.SoundTrack, characters []*arn.Character, companies []*arn.Company, quotes []*arn.Quote, user *arn.User)
+component SearchResults(term string, users []*arn.User, animes []*arn.Anime, posts []*arn.Post, threads []*arn.Thread, tracks []*arn.SoundTrack, characters []*arn.Character, amvs []*arn.AMV, companies []*arn.Company, quotes []*arn.Quote, user *arn.User)
 	h1.page-title= "Search: " + term
 
 	.search
@@ -34,6 +34,14 @@ component SearchResults(term string, users []*arn.User, animes []*arn.Anime, pos
 			#soundtrack-search-results
 				SoundTrackSearchResults(tracks, user)
 		
+		.widget
+			h3.widget-title
+				Icon("video-camera")
+				span AMVs
+
+			#amv-search-results
+				AMVSearchResults(amvs, user)
+
 		//- .widget
 		//- 	h3.widget-title
 		//- 		Icon("quote-left")
@@ -107,6 +115,20 @@ component SoundTrackSearchResults(tracks []*arn.SoundTrack, user *arn.User)
 					a(href=track.Link())= track.Title.ByUser(user)
 					span.soundtrack-search-anime= " - " + track.MainAnime().Title.Canonical
 
+component AMVSearchResults(amvs []*arn.AMV, user *arn.User)
+	if len(amvs) == 0
+		p.no-search-results.mountable No AMVs found.
+	else
+		ul.amv-search
+			each amv in amvs
+				li.mountable(data-mountable-type="amv")
+					a(href=amv.Link())= amv.Title.ByUser(user)
+
+					if amv.MainAnime() != nil
+						span.amv-search-anime= " - " + amv.MainAnime().Title.Canonical
+					else
+						span.amv-search-anime= fmt.Sprintf(" - %d anime", len(amv.ExtraAnimeIDs))
+
 component CompanySearchResults(companies []*arn.Company)
 	if len(companies) == 0
 		p.no-search-results.mountable No companies found.
diff --git a/pages/search/search.scarlet b/pages/search/search.scarlet
index 275ea5cd..a7f1504e 100644
--- a/pages/search/search.scarlet
+++ b/pages/search/search.scarlet
@@ -37,7 +37,8 @@
 	margin-bottom 1rem
 	opacity 0.8
 
-.soundtrack-search-anime
+.soundtrack-search-anime,
+.amv-search-anime
 	opacity 0.35
 
 .no-search-results
diff --git a/scripts/Actions/Search.ts b/scripts/Actions/Search.ts
index 6571d024..b6ff8680 100644
--- a/scripts/Actions/Search.ts
+++ b/scripts/Actions/Search.ts
@@ -11,6 +11,7 @@ var correctResponseRendered = {
 	"forum": false,
 	"soundtrack": false,
 	"user": false,
+	"amv": false,
 	"company": false
 }
 
@@ -23,6 +24,7 @@ var characterSearchResults: HTMLElement
 var forumSearchResults: HTMLElement
 var soundtrackSearchResults: HTMLElement
 var userSearchResults: HTMLElement
+var amvSearchResults: HTMLElement
 var companySearchResults: HTMLElement
 
 // Delay before a request is sent
@@ -60,6 +62,7 @@ export async function search(arn: AnimeNotifier, search: HTMLInputElement, evt?:
 	correctResponseRendered.forum = false
 	correctResponseRendered.soundtrack = false
 	correctResponseRendered.user = false
+	correctResponseRendered.amv = false
 	correctResponseRendered.company = false
 
 	// Set browser URL
@@ -108,6 +111,7 @@ export async function search(arn: AnimeNotifier, search: HTMLInputElement, evt?:
 			forumSearchResults = document.getElementById("forum-search-results")
 			soundtrackSearchResults = document.getElementById("soundtrack-search-results")
 			userSearchResults = document.getElementById("user-search-results")
+			amvSearchResults = document.getElementById("amv-search-results")
 			companySearchResults = document.getElementById("company-search-results")
 			searchPageTitle = document.getElementsByTagName("h1")[0]
 		}
@@ -147,6 +151,10 @@ export async function search(arn: AnimeNotifier, search: HTMLInputElement, evt?:
 			.then(showResponseInElement(arn, url, "user", userSearchResults))
 			.catch(console.error)
 
+			fetch("/_/amv-search/" + term, fetchOptions)
+			.then(showResponseInElement(arn, url, "amv", amvSearchResults))
+			.catch(console.error)
+
 			fetch("/_/company-search/" + term, fetchOptions)
 			.then(showResponseInElement(arn, url, "company", companySearchResults))
 			.catch(console.error)