Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0
Ce dépôt a été archivé le 2022-05-07. Vous pouvez voir ses fichiers ou le cloner, mais pas ouvrir de ticket ou de demandes d'ajout, ni soumettre de changements.
nyaa-pantsu/common/search.go
Ramon Dantas d8e17478f8 Country flags (language) for torrents. (#970)
* Add flags for torrents

Add a new field, .Language, to the Torrent model, and a new package,
torrentLanguages, which maps languages to flags. Added also a flag icon pack
from googlei18n/region-flags, with (mostly) public domain flags from Wikipedia.

* Optimize flags

* Use FlagSprites CSS instead of .png files

* Only use flags for languages we support

* Add test for CSS flags

Ensure that we have all the flags for the languages we support.

* Add AdditionalLanguages field to config

This allows us to support additional languages for new uploaded torrents,
even if we have no translation for it.

* Minor CSS fix

* Add "other" and "multiple" torrent languages

Also removed the TorrentLanguage struct, as it wasn't much useful.

* Fix test

* Add colspan=2 to category when language is empty

Also hide the language column if empty.

* Add lang field to search.

Hopefully it works with Elasticsearch as well, but I haven't tested
(lol Java)

* Add language field to ES index and settings

* Add language column to JS template

* Add keyword type to language ES field

* Remove 'raw' from keyword

* Set "simple" analyzer on language

* Document .Language field on Torrent model
2017-06-12 09:14:26 +10:00

173 lignes
2,5 Kio
Go

package common
import (
"strconv"
"strings"
)
type Status uint8
const (
ShowAll Status = iota
FilterRemakes
Trusted
APlus
)
func (st *Status) ToString() string {
switch *st {
case FilterRemakes:
return "1"
case Trusted:
return "2"
case APlus:
return "3"
}
return ""
}
func (st *Status) Parse(s string) {
switch s {
case "1":
*st = FilterRemakes
break
case "2":
*st = Trusted
break
case "3":
*st = APlus
break
default:
*st = ShowAll
}
}
type SortMode uint8
const (
ID SortMode = iota
Name
Date
Downloads
Size
Seeders
Leechers
Completed
)
func (s *SortMode) Parse(str string) {
switch str {
case "1":
*s = Name
break
case "2":
*s = Date
break
case "3":
*s = Downloads
break
case "4":
*s = Size
break
case "5":
*s = Seeders
break
case "6":
*s = Leechers
break
case "7":
*s = Completed
break
default:
*s = ID
}
}
/* INFO Always need to keep in sync with the field that are used in the
* elasticsearch index.
* TODO Verify the field in postgres database
*/
func (s *SortMode) ToESField() string {
switch *s {
case ID:
return "id"
case Name:
return "name.raw"
case Date:
return "date"
case Downloads:
return "downloads"
case Size:
return "filesize"
case Seeders:
return "seeders"
case Leechers:
return "leechers"
case Completed:
return "completed"
}
return "id"
}
type Category struct {
Main, Sub uint8
}
func (c Category) String() (s string) {
if c.Main != 0 {
s += strconv.Itoa(int(c.Main))
}
s += "_"
if c.Sub != 0 {
s += strconv.Itoa(int(c.Sub))
}
return
}
func (c Category) IsSet() bool {
return c.IsMainSet() && c.IsSubSet()
}
func (c Category) IsMainSet() bool {
return c.Main != 0
}
func (c Category) IsSubSet() bool {
return c.Sub != 0
}
// Parse sets category by string
// returns true if string is valid otherwise returns false
func (c *Category) Parse(s string) (ok bool) {
parts := strings.Split(s, "_")
if len(parts) == 2 {
tmp, err := strconv.ParseUint(parts[0], 10, 8)
if err == nil {
c.Main = uint8(tmp)
tmp, err = strconv.ParseUint(parts[1], 10, 8)
if err == nil {
c.Sub = uint8(tmp)
ok = true
}
}
}
return
}
// deprecated for TorrentParam
type SearchParam struct {
TorrentID uint
FromID uint // Search for torrentID > FromID
Order bool // True means acsending
Status Status
Sort SortMode
Category Category
FromDate string
ToDate string
Page int
UserID uint
Max uint
NotNull string
Language string
Query string
}