account.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. package libamuse
  2. import (
  3. "notabug.org/apiote/amuse/accounts"
  4. "notabug.org/apiote/amuse/datastructure"
  5. "notabug.org/apiote/amuse/db"
  6. "notabug.org/apiote/amuse/tmdb"
  7. "errors"
  8. "fmt"
  9. "os"
  10. "strings"
  11. "time"
  12. "notabug.org/apiote/gott"
  13. )
  14. func VerifyAuthToken(token accounts.Authentication) (accounts.User, error) {
  15. if token.Token == "" {
  16. return accounts.User{}, accounts.AuthError{Err: errors.New("401")}
  17. }
  18. session, err := db.GetSession(token.Token)
  19. if err != nil {
  20. if _, ok := err.(db.EmptyError); ok {
  21. err = accounts.AuthError{Err: err}
  22. }
  23. fmt.Fprintf(os.Stderr, "Get session err: %v", err)
  24. return accounts.User{}, err
  25. }
  26. now := time.Now()
  27. if session.Expiry.Before(now) {
  28. return accounts.User{}, accounts.AuthError{Err: errors.New("Session expired")}
  29. }
  30. dbUser, err := db.GetUser(session.Username)
  31. if err != nil {
  32. if _, ok := err.(db.EmptyError); ok {
  33. err = accounts.AuthError{Err: err}
  34. }
  35. fmt.Fprintf(os.Stderr, "Get user err: %v", err)
  36. return accounts.User{}, err
  37. }
  38. user := accounts.User{
  39. Username: dbUser.Username,
  40. IsAdmin: dbUser.IsAdmin,
  41. Session: token.Token,
  42. Timezone: dbUser.Timezone,
  43. }
  44. return user, nil
  45. }
  46. func addToWantlist(args ...interface{}) (interface{}, error) {
  47. data := args[0].(*RequestData)
  48. result := args[1].(*Result)
  49. itemType := args[2].(string)
  50. err := db.AddToWantList(result.user.Username, data.id, datastructure.ItemType(itemType))
  51. result.result2 = 1
  52. return gott.Tuple(args), err
  53. }
  54. func getItem(args ...interface{}) (interface{}, error) {
  55. itemTypeName := args[2].(string)
  56. itemType := datastructure.ItemType(itemTypeName)
  57. var arg interface{}
  58. var err error
  59. switch itemType {
  60. case datastructure.ItemTypeFilm:
  61. arg, err = gott.
  62. NewResult(gott.Tuple(args)).
  63. Bind(getFilm).
  64. Bind(getCollection).
  65. Finish()
  66. if err == nil {
  67. args = arg.(gott.Tuple)
  68. }
  69. case datastructure.ItemTypeTvserie:
  70. arg, err = gott.
  71. NewResult(gott.Tuple(args)).
  72. Bind(getTvSerie).
  73. Finish()
  74. if err == nil {
  75. args = arg.(gott.Tuple)
  76. }
  77. case datastructure.ItemTypeBook:
  78. arg, err = gott.
  79. NewResult(gott.Tuple(args)).
  80. Bind(getBook).
  81. Bind(getDescription).
  82. Bind(getCover).
  83. Finish()
  84. if err == nil {
  85. args = arg.(gott.Tuple)
  86. }
  87. default:
  88. err = errors.New("Wrong ItemType: " + itemTypeName)
  89. }
  90. return gott.Tuple(args), err
  91. }
  92. func cacheItem(args ...interface{}) (interface{}, error) {
  93. data := args[0].(*RequestData)
  94. result := args[1].(*Result)
  95. refs := result.result2.(int)
  96. item := result.result.(datastructure.Item)
  97. itemInfo := item.GetItemInfo()
  98. err := db.SaveCacheItem(item.GetItemType(), data.id, itemInfo, refs)
  99. return gott.Tuple(args), err
  100. }
  101. func AddToWantlist(username string, auth accounts.Authentication, itemId, itemType, language, mimetype string) error {
  102. auth.Necessary = true
  103. _, err := gott.
  104. NewResult(gott.Tuple{&RequestData{id: itemId, language: language, mimetype: mimetype, auth: auth, username: username}, &Result{}, itemType}).
  105. Bind(parseLanguage).
  106. Bind(verifyToken).
  107. Bind(verifyUser).
  108. Bind(getItem).
  109. Bind(addToWantlist).
  110. Bind(cacheItem).
  111. Finish()
  112. return err
  113. }
  114. func splitItemId(args ...interface{}) interface{} {
  115. data := args[0].(*RequestData)
  116. itemId := args[3].(string)
  117. id := strings.Split(itemId, "/")
  118. data.id = id[0]
  119. return gott.Tuple(args)
  120. }
  121. func parseExperienceDate(args ...interface{}) (interface{}, error) {
  122. result := args[1].(*Result)
  123. datetime := args[4].(string)
  124. var t time.Time
  125. var err error = nil
  126. if datetime == "" {
  127. t = time.Now()
  128. } else {
  129. var location *time.Location
  130. if datetime != "0001-01-01T00:00:00" {
  131. location, _ = time.LoadLocation(result.user.Timezone)
  132. } else {
  133. location = time.UTC
  134. }
  135. t, err = time.ParseInLocation("2006-01-02T15:04:05", datetime, location)
  136. }
  137. t = t.In(time.UTC)
  138. result.result2 = t
  139. return gott.Tuple(args), err
  140. }
  141. func getSpecials(args ...interface{}) (interface{}, error) {
  142. itemId := args[3].(string)
  143. var err error
  144. id := strings.Split(itemId, "/")
  145. if len(id) > 1 && id[1][3] == 'A' {
  146. arg, err := gott.
  147. NewResult(gott.Tuple(args)).
  148. Bind(getSeason).
  149. Finish()
  150. if err == nil {
  151. args = arg.(gott.Tuple)
  152. }
  153. }
  154. return gott.Tuple(args), err
  155. }
  156. func addToExperiences(args ...interface{}) (interface{}, error) {
  157. data := args[0].(*RequestData)
  158. result := args[1].(*Result)
  159. t := result.result2.(time.Time)
  160. itemType := args[2].(string)
  161. itemId := args[3].(string)
  162. var (
  163. err error = nil
  164. refs int
  165. )
  166. id := strings.Split(itemId, "/")
  167. if len(id) > 1 && id[1][3] == 'A' {
  168. serie := result.result.(*tmdb.TvSerie)
  169. var season int
  170. fmt.Sscanf(id[1][1:3], "%d", &season)
  171. episodes := []string{}
  172. for _, episode := range serie.Seasons[season].Episodes {
  173. episodes = append(episodes, data.id+"/"+episode.Episode_code)
  174. }
  175. refs, err = db.WatchWholeSerie(result.user.Username, id[0], episodes, datastructure.ItemType(itemType), t)
  176. } else {
  177. refs, err = db.AddToExperiences(result.user.Username, itemId, datastructure.ItemType(itemType), t)
  178. }
  179. result.result2 = refs
  180. if len(id) > 1 {
  181. return gott.Tuple(args), errors.New("Skip")
  182. }
  183. return gott.Tuple(args), err
  184. }
  185. func clearSpecials(args ...interface{}) (interface{}, error) {
  186. data := args[0].(*RequestData)
  187. result := args[1].(*Result)
  188. itemType := args[2].(string)
  189. itemId := args[3].(string)
  190. id := strings.Split(itemId, "/")
  191. var err error
  192. if len(id) > 1 && id[1][3] == 'A' {
  193. serie := result.result.(*tmdb.TvSerie)
  194. var season int
  195. fmt.Sscanf(id[1][1:3], "%d", &season)
  196. if season == 0 {
  197. episodes := []string{}
  198. for _, episode := range serie.Seasons[0].Episodes {
  199. episodes = append(episodes, data.id+"/"+episode.Episode_code)
  200. }
  201. err = db.ClearSpecials(result.user.Username, id[0], episodes, datastructure.ItemType(itemType))
  202. }
  203. }
  204. return gott.Tuple(args), err
  205. }
  206. func removeFromWantList(args ...interface{}) (interface{}, error) {
  207. data := args[0].(*RequestData)
  208. result := args[1].(*Result)
  209. itemType := args[2].(string)
  210. err := db.RemoveFromWantList(result.user.Username, data.id, datastructure.ItemType(itemType))
  211. return gott.Tuple(args), err
  212. }
  213. func removeCacheItem(args ...interface{}) (interface{}, error) {
  214. data := args[0].(*RequestData)
  215. itemType := args[2].(string)
  216. err := db.RemoveCacheItem(datastructure.ItemType(itemType), data.id)
  217. return gott.Tuple(args), err
  218. }
  219. func AddToExperiences(username string, auth accounts.Authentication, itemId, itemType, datetime, language, mimetype string) error {
  220. auth.Necessary = true
  221. _, err := gott.
  222. NewResult(gott.Tuple{&RequestData{language: language, mimetype: mimetype, auth: auth, username: username}, &Result{}, itemType, itemId, datetime}).
  223. Map(splitItemId).
  224. Bind(parseLanguage).
  225. Bind(verifyToken).
  226. Bind(verifyUser).
  227. Bind(parseExperienceDate).
  228. Bind(getItem).
  229. Bind(getSpecials).
  230. Bind(clearSpecials).
  231. Bind(addToExperiences).
  232. Bind(cacheItem).
  233. Bind(removeFromWantList).
  234. Bind(removeCacheItem).
  235. Finish()
  236. if err != nil {
  237. if err.Error() == "Skip" {
  238. err = nil
  239. }
  240. if emptyErr, ok := err.(db.EmptyError); ok && emptyErr.Error() == "Empty delete" {
  241. err = nil
  242. }
  243. }
  244. return err
  245. }
  246. func removeSession(args ...interface{}) (interface{}, error) {
  247. data := args[0].(*RequestData)
  248. var token string
  249. if data.id == "0" {
  250. token = data.auth.Token
  251. } else {
  252. token = data.id
  253. }
  254. username := data.username
  255. err := db.RemoveSession(username, token)
  256. return gott.Tuple(args), err
  257. }
  258. func SessionDelete(username string, auth accounts.Authentication, session, languages, mimetype string) error {
  259. auth.Necessary = true
  260. _, err := gott.
  261. NewResult(gott.Tuple{&RequestData{id: session, language: languages, mimetype: mimetype, auth: auth, username: username}, &Result{}}).
  262. Bind(parseLanguage).
  263. Bind(verifyToken).
  264. Bind(verifyUser).
  265. Bind(removeSession).
  266. Finish()
  267. return err
  268. }