123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- package libamuse
- import (
- "notabug.org/apiote/amuse/accounts"
- "notabug.org/apiote/amuse/datastructure"
- "notabug.org/apiote/amuse/db"
- "notabug.org/apiote/amuse/tmdb"
- "errors"
- "fmt"
- "os"
- "strings"
- "time"
- "notabug.org/apiote/gott"
- )
- func VerifyAuthToken(token accounts.Authentication) (accounts.User, error) {
- if token.Token == "" {
- return accounts.User{}, accounts.AuthError{Err: errors.New("401")}
- }
- session, err := db.GetSession(token.Token)
- if err != nil {
- if _, ok := err.(db.EmptyError); ok {
- err = accounts.AuthError{Err: err}
- }
- fmt.Fprintf(os.Stderr, "Get session err: %v", err)
- return accounts.User{}, err
- }
- now := time.Now()
- if session.Expiry.Before(now) {
- return accounts.User{}, accounts.AuthError{Err: errors.New("Session expired")}
- }
- dbUser, err := db.GetUser(session.Username)
- if err != nil {
- if _, ok := err.(db.EmptyError); ok {
- err = accounts.AuthError{Err: err}
- }
- fmt.Fprintf(os.Stderr, "Get user err: %v", err)
- return accounts.User{}, err
- }
- user := accounts.User{
- Username: dbUser.Username,
- IsAdmin: dbUser.IsAdmin,
- Session: token.Token,
- Timezone: dbUser.Timezone,
- }
- return user, nil
- }
- func addToWantlist(args ...interface{}) (interface{}, error) {
- data := args[0].(*RequestData)
- result := args[1].(*Result)
- itemType := args[2].(string)
- err := db.AddToWantList(result.user.Username, data.id, datastructure.ItemType(itemType))
- result.result2 = 1
- return gott.Tuple(args), err
- }
- func getItem(args ...interface{}) (interface{}, error) {
- itemTypeName := args[2].(string)
- itemType := datastructure.ItemType(itemTypeName)
- var arg interface{}
- var err error
- switch itemType {
- case datastructure.ItemTypeFilm:
- arg, err = gott.
- NewResult(gott.Tuple(args)).
- Bind(getFilm).
- Bind(getCollection).
- Finish()
- if err == nil {
- args = arg.(gott.Tuple)
- }
- case datastructure.ItemTypeTvserie:
- arg, err = gott.
- NewResult(gott.Tuple(args)).
- Bind(getTvSerie).
- Finish()
- if err == nil {
- args = arg.(gott.Tuple)
- }
- case datastructure.ItemTypeBook:
- arg, err = gott.
- NewResult(gott.Tuple(args)).
- Bind(getBook).
- Bind(getDescription).
- Bind(getCover).
- Finish()
- if err == nil {
- args = arg.(gott.Tuple)
- }
- default:
- err = errors.New("Wrong ItemType: " + itemTypeName)
- }
- return gott.Tuple(args), err
- }
- func cacheItem(args ...interface{}) (interface{}, error) {
- data := args[0].(*RequestData)
- result := args[1].(*Result)
- refs := result.result2.(int)
- item := result.result.(datastructure.Item)
- itemInfo := item.GetItemInfo()
- err := db.SaveCacheItem(item.GetItemType(), data.id, itemInfo, refs)
- return gott.Tuple(args), err
- }
- func AddToWantlist(username string, auth accounts.Authentication, itemId, itemType, language, mimetype string) error {
- auth.Necessary = true
- _, err := gott.
- NewResult(gott.Tuple{&RequestData{id: itemId, language: language, mimetype: mimetype, auth: auth, username: username}, &Result{}, itemType}).
- Bind(parseLanguage).
- Bind(verifyToken).
- Bind(verifyUser).
- Bind(getItem).
- Bind(addToWantlist).
- Bind(cacheItem).
- Finish()
- return err
- }
- func splitItemId(args ...interface{}) interface{} {
- data := args[0].(*RequestData)
- itemId := args[3].(string)
- id := strings.Split(itemId, "/")
- data.id = id[0]
- return gott.Tuple(args)
- }
- func parseExperienceDate(args ...interface{}) (interface{}, error) {
- result := args[1].(*Result)
- datetime := args[4].(string)
- var t time.Time
- var err error = nil
- if datetime == "" {
- t = time.Now()
- } else {
- var location *time.Location
- if datetime != "0001-01-01T00:00:00" {
- location, _ = time.LoadLocation(result.user.Timezone)
- } else {
- location = time.UTC
- }
- t, err = time.ParseInLocation("2006-01-02T15:04:05", datetime, location)
- }
- t = t.In(time.UTC)
- result.result2 = t
- return gott.Tuple(args), err
- }
- func getSpecials(args ...interface{}) (interface{}, error) {
- itemId := args[3].(string)
- var err error
- id := strings.Split(itemId, "/")
- if len(id) > 1 && id[1][3] == 'A' {
- arg, err := gott.
- NewResult(gott.Tuple(args)).
- Bind(getSeason).
- Finish()
- if err == nil {
- args = arg.(gott.Tuple)
- }
- }
- return gott.Tuple(args), err
- }
- func addToExperiences(args ...interface{}) (interface{}, error) {
- data := args[0].(*RequestData)
- result := args[1].(*Result)
- t := result.result2.(time.Time)
- itemType := args[2].(string)
- itemId := args[3].(string)
- var (
- err error = nil
- refs int
- )
- id := strings.Split(itemId, "/")
- if len(id) > 1 && id[1][3] == 'A' {
- serie := result.result.(*tmdb.TvSerie)
- var season int
- fmt.Sscanf(id[1][1:3], "%d", &season)
- episodes := []string{}
- for _, episode := range serie.Seasons[season].Episodes {
- episodes = append(episodes, data.id+"/"+episode.Episode_code)
- }
- refs, err = db.WatchWholeSerie(result.user.Username, id[0], episodes, datastructure.ItemType(itemType), t)
- } else {
- refs, err = db.AddToExperiences(result.user.Username, itemId, datastructure.ItemType(itemType), t)
- }
- result.result2 = refs
- if len(id) > 1 {
- return gott.Tuple(args), errors.New("Skip")
- }
- return gott.Tuple(args), err
- }
- func clearSpecials(args ...interface{}) (interface{}, error) {
- data := args[0].(*RequestData)
- result := args[1].(*Result)
- itemType := args[2].(string)
- itemId := args[3].(string)
- id := strings.Split(itemId, "/")
- var err error
- if len(id) > 1 && id[1][3] == 'A' {
- serie := result.result.(*tmdb.TvSerie)
- var season int
- fmt.Sscanf(id[1][1:3], "%d", &season)
- if season == 0 {
- episodes := []string{}
- for _, episode := range serie.Seasons[0].Episodes {
- episodes = append(episodes, data.id+"/"+episode.Episode_code)
- }
- err = db.ClearSpecials(result.user.Username, id[0], episodes, datastructure.ItemType(itemType))
- }
- }
- return gott.Tuple(args), err
- }
- func removeFromWantList(args ...interface{}) (interface{}, error) {
- data := args[0].(*RequestData)
- result := args[1].(*Result)
- itemType := args[2].(string)
- err := db.RemoveFromWantList(result.user.Username, data.id, datastructure.ItemType(itemType))
- return gott.Tuple(args), err
- }
- func removeCacheItem(args ...interface{}) (interface{}, error) {
- data := args[0].(*RequestData)
- itemType := args[2].(string)
- err := db.RemoveCacheItem(datastructure.ItemType(itemType), data.id)
- return gott.Tuple(args), err
- }
- func AddToExperiences(username string, auth accounts.Authentication, itemId, itemType, datetime, language, mimetype string) error {
- auth.Necessary = true
- _, err := gott.
- NewResult(gott.Tuple{&RequestData{language: language, mimetype: mimetype, auth: auth, username: username}, &Result{}, itemType, itemId, datetime}).
- Map(splitItemId).
- Bind(parseLanguage).
- Bind(verifyToken).
- Bind(verifyUser).
- Bind(parseExperienceDate).
- Bind(getItem).
- Bind(getSpecials).
- Bind(clearSpecials).
- Bind(addToExperiences).
- Bind(cacheItem).
- Bind(removeFromWantList).
- Bind(removeCacheItem).
- Finish()
- if err != nil {
- if err.Error() == "Skip" {
- err = nil
- }
- if emptyErr, ok := err.(db.EmptyError); ok && emptyErr.Error() == "Empty delete" {
- err = nil
- }
- }
- return err
- }
- func removeSession(args ...interface{}) (interface{}, error) {
- data := args[0].(*RequestData)
- var token string
- if data.id == "0" {
- token = data.auth.Token
- } else {
- token = data.id
- }
- username := data.username
- err := db.RemoveSession(username, token)
- return gott.Tuple(args), err
- }
- func SessionDelete(username string, auth accounts.Authentication, session, languages, mimetype string) error {
- auth.Necessary = true
- _, err := gott.
- NewResult(gott.Tuple{&RequestData{id: session, language: languages, mimetype: mimetype, auth: auth, username: username}, &Result{}}).
- Bind(parseLanguage).
- Bind(verifyToken).
- Bind(verifyUser).
- Bind(removeSession).
- Finish()
- return err
- }
|