common.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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/front"
  7. "notabug.org/apiote/amuse/tmdb"
  8. "notabug.org/apiote/amuse/wikidata"
  9. "database/sql"
  10. "errors"
  11. "golang.org/x/text/language"
  12. "notabug.org/apiote/gott"
  13. )
  14. type Data interface {
  15. getLanguage() string
  16. getMimeType() string
  17. getAuth() accounts.Authentication
  18. getReqUsername() string
  19. }
  20. func verifyToken(args ...interface{}) (interface{}, error) {
  21. data := args[0].(Data)
  22. result := args[1].(*Result)
  23. user, err := VerifyAuthToken(data.getAuth())
  24. if _, ok := err.(accounts.AuthError); ok && !data.getAuth().Necessary {
  25. err = nil
  26. }
  27. result.user = user
  28. return gott.Tuple(args), err
  29. }
  30. func verifyUser(args ...interface{}) (interface{}, error) {
  31. data := args[0].(Data)
  32. result := args[1].(*Result)
  33. if result.user.Username != data.getReqUsername() {
  34. return gott.Tuple(args), accounts.AuthError{Err: errors.New("403")}
  35. }
  36. return gott.Tuple(args), nil
  37. }
  38. func createDbConnection(args ...interface{}) (interface{}, error) {
  39. requestData := args[0].(*RequestData)
  40. db, err := sql.Open("sqlite3", "./amuse.db")
  41. if err != nil {
  42. return gott.Tuple(args), err
  43. }
  44. requestData.connection = db
  45. return gott.Tuple(args), nil
  46. }
  47. func parseLanguage(args ...interface{}) (interface{}, error) {
  48. data := args[0].(Data)
  49. result := args[1].(*Result)
  50. tags, _, err := language.ParseAcceptLanguage(data.getLanguage())
  51. if tags == nil {
  52. tags = []language.Tag{}
  53. }
  54. if len(tags) == 0 {
  55. tags = append(tags, language.Make("en-GB"))
  56. }
  57. result.languages = tags
  58. return gott.Tuple(args), err
  59. }
  60. func createRenderer(args ...interface{}) (interface{}, error) {
  61. data := args[0].(Data)
  62. result := args[1].(*Result)
  63. renderer, err := front.NewRenderer(data.getMimeType(), result.user)
  64. result.renderer = renderer
  65. return gott.Tuple(args), err
  66. }
  67. func getDescription(args ...interface{}) (interface{}, error) {
  68. data := args[0].(*RequestData)
  69. result := args[1].(*Result)
  70. work := result.result.(datastructure.Work)
  71. var (
  72. description string
  73. err error
  74. )
  75. if work.GetArticle() != "" {
  76. languages := result.languages
  77. description, err = wikidata.GetWorkDescription(work.GetArticle(), languages[0].String(), data.connection)
  78. }
  79. work.SetDescription(description)
  80. return gott.Tuple(args), err
  81. }
  82. func getBasedOn(args ...interface{}) (interface{}, error) {
  83. data := args[0].(*RequestData)
  84. result := args[1].(*Result)
  85. show := result.result.(tmdb.Show)
  86. languages := result.languages
  87. book, err := wikidata.GetBookByTmdb(data.id, languages[0].String())
  88. show.AddBasedOn(book)
  89. return gott.Tuple(args), err
  90. }
  91. func getGenres(args ...interface{}) (interface{}, error) {
  92. result := args[1].(*Result)
  93. list := result.result.(datastructure.List)
  94. genres, err := tmdb.GetGenres(result.languages[0].String(), list.GetType())
  95. list.SetGenres(genres)
  96. result.result = list
  97. return gott.Tuple(args), err
  98. }
  99. func isOnWantList(args ...interface{}) (interface{}, error) {
  100. data := args[0].(*RequestData)
  101. result := args[1].(*Result)
  102. item := result.result.(datastructure.Item)
  103. if result.user.IsEmpty() {
  104. return gott.Tuple(args), nil
  105. }
  106. itemType := item.GetItemType()
  107. isOnList, err := db.IsOnWantList(result.user.Username, data.id, itemType)
  108. item.SetOnWantList(isOnList)
  109. return gott.Tuple(args), err
  110. }
  111. func updateCache(args ...interface{}) (interface{}, error) {
  112. data := args[0].(*RequestData)
  113. result := args[1].(*Result)
  114. item := result.result.(datastructure.Item)
  115. itemInfo := item.GetItemInfo()
  116. err := db.UpdateCacheItem(item.GetItemType(), data.id, itemInfo)
  117. return gott.Tuple(args), err
  118. }
  119. type RequestData struct {
  120. id string
  121. etag string
  122. connection *sql.DB
  123. language string
  124. mimetype string
  125. code int
  126. auth accounts.Authentication
  127. username string
  128. }
  129. type Result struct {
  130. languages []language.Tag
  131. renderer front.Renderer
  132. user accounts.User
  133. result interface{}
  134. result2 interface{} // todo this is ugly -> to []interface{} with .result
  135. page string
  136. }
  137. func (d RequestData) getLanguage() string {
  138. return d.language
  139. }
  140. func (d RequestData) getMimeType() string {
  141. return d.mimetype
  142. }
  143. func (d RequestData) getAuth() accounts.Authentication {
  144. return d.auth
  145. }
  146. func (d RequestData) getReqUsername() string {
  147. return d.username
  148. }