html.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. package front
  2. import (
  3. "notabug.org/apiote/amuse/accounts"
  4. "notabug.org/apiote/amuse/config"
  5. "notabug.org/apiote/amuse/datastructure"
  6. "notabug.org/apiote/amuse/i18n"
  7. "notabug.org/apiote/amuse/tmdb"
  8. "notabug.org/apiote/amuse/wikidata"
  9. "bytes"
  10. "golang.org/x/text/language"
  11. "html/template"
  12. "log"
  13. "strings"
  14. "time"
  15. "github.com/pquerna/otp"
  16. )
  17. type RenderData struct {
  18. Data interface{}
  19. Strings i18n.Translation
  20. State struct {
  21. Error error
  22. User accounts.User
  23. }
  24. }
  25. func (d RenderData) LetAmuse0() string {
  26. let_amuse := strings.Split(d.Strings.Index["let_amuse_you"], "{}")
  27. let_amuse[0], let_amuse[1] = strings.Trim(let_amuse[0], " "), strings.Trim(let_amuse[1], " ")
  28. return let_amuse[0]
  29. }
  30. func (d RenderData) LetAmuse1() string {
  31. let_amuse := strings.Split(d.Strings.Index["let_amuse_you"], "{}")
  32. let_amuse[0], let_amuse[1] = strings.Trim(let_amuse[0], " "), strings.Trim(let_amuse[1], " ")
  33. return let_amuse[1]
  34. }
  35. func (d RenderData) GetPremiereType(index int) string {
  36. return i18n.GetPremiereType(index, d.Strings)
  37. }
  38. func (d RenderData) FormatDate(date time.Time) string {
  39. return i18n.FormatDate(date, d.Strings.Global["date_format"], d.Strings.Global)
  40. }
  41. func (d RenderData) FormatDateNice(date time.Time, timezone string) string {
  42. return i18n.FormatDateNice(date, d.Strings, timezone)
  43. }
  44. func (d RenderData) RenderAsciiDoc(s string) template.HTML {
  45. return i18n.RenderAsciiDoc(s)
  46. }
  47. func (d RenderData) GetErrorData(code int, kind string) string {
  48. return i18n.GetErrorData(code, d.Strings, kind)
  49. }
  50. type HtmlRenderer struct {
  51. user accounts.User
  52. }
  53. func render(languages []language.Tag, data RenderData, file string) string {
  54. err := i18n.LoadServerLangs()
  55. if err != nil {
  56. // todo return http:500
  57. }
  58. language, err := i18n.Match(languages)
  59. if err != nil {
  60. // todo return http:500
  61. }
  62. strings, err := i18n.LoadStrings(language)
  63. if err != nil {
  64. // todo return http:500
  65. }
  66. data.Strings = strings
  67. t, err := template.ParseFiles(config.DataHome + "/templates/" + file + ".html")
  68. if err != nil {
  69. log.Println(err)
  70. // todo return http:500
  71. }
  72. b := bytes.NewBuffer([]byte{})
  73. err = t.Execute(b, data)
  74. if err != nil {
  75. log.Println(err)
  76. // todo return http:500
  77. }
  78. return b.String()
  79. }
  80. func (r HtmlRenderer) RenderFilm(film *tmdb.Film, languages []language.Tag) string {
  81. data := RenderData{Data: film}
  82. data.State.User = r.user
  83. return render(languages, data, "film")
  84. }
  85. func (r HtmlRenderer) RenderSearch(tmdbResults *tmdb.SearchResults, inventaireResults *wikidata.SearchResults, languages []language.Tag) string {
  86. results := struct {
  87. T *tmdb.SearchResults
  88. I *wikidata.SearchResults
  89. }{tmdbResults, inventaireResults}
  90. data := RenderData{Data: results}
  91. data.State.User = r.user
  92. return render(languages, data, "search")
  93. }
  94. func (r HtmlRenderer) RenderIndex(randomComedy string, languages []language.Tag) string {
  95. data := RenderData{Data: randomComedy}
  96. data.State.User = r.user
  97. return render(languages, data, "index")
  98. }
  99. func (r HtmlRenderer) RenderTvSerie(tvSerie *tmdb.TvSerie, languages []language.Tag) string {
  100. data := RenderData{Data: tvSerie}
  101. data.State.User = r.user
  102. return render(languages, data, "tvserie")
  103. }
  104. func (r HtmlRenderer) RenderPerson(person *tmdb.Person, languages []language.Tag) string {
  105. data := RenderData{Data: person}
  106. data.State.User = r.user
  107. return render(languages, data, "person")
  108. }
  109. func (r HtmlRenderer) RenderBook(book datastructure.Book, languages []language.Tag) string {
  110. data := RenderData{Data: book}
  111. data.State.User = r.user
  112. return render(languages, data, "book")
  113. }
  114. func (r HtmlRenderer) RenderBookSerie(bookSerie wikidata.BookSerie, languages []language.Tag) string {
  115. data := RenderData{Data: bookSerie}
  116. data.State.User = r.user
  117. return render(languages, data, "bookserie")
  118. }
  119. func (r HtmlRenderer) RenderAbout(languages []language.Tag) string {
  120. data := RenderData{}
  121. data.State.User = r.user
  122. return render(languages, data, "about")
  123. }
  124. func (HtmlRenderer) RenderErrorPage(code int, err error, languages []language.Tag) string {
  125. e := struct {
  126. Code int
  127. User string
  128. ItemType string
  129. ItemID string
  130. IsOnWantList bool
  131. }{
  132. Code: code,
  133. }
  134. if tmdbNotFountError, ok := err.(tmdb.NotFountError); ok {
  135. e.User = tmdbNotFountError.User
  136. e.ItemType = tmdbNotFountError.ItemType
  137. e.ItemID = tmdbNotFountError.ItemID
  138. e.IsOnWantList = tmdbNotFountError.IsOnWantList
  139. }
  140. data := RenderData{Data: e}
  141. return render(languages, data, "error")
  142. }
  143. func (HtmlRenderer) RenderLogin(languages []language.Tag, authError error, target string) string {
  144. data := RenderData{Data: target}
  145. data.State.Error = authError
  146. return render(languages, data, "login")
  147. }
  148. func (HtmlRenderer) RenderLoggedOut(languages []language.Tag) string {
  149. data := RenderData{}
  150. return render(languages, data, "loggedout")
  151. }
  152. func (HtmlRenderer) RenderSignup(languages []language.Tag, authError error, key *otp.Key, sfaEnabled bool, username, qr string) string {
  153. secret := struct {
  154. Secret string
  155. SfaEnabled bool
  156. Username string
  157. Qr template.URL
  158. }{key.Secret(), sfaEnabled, username, template.URL(qr)}
  159. data := RenderData{Data: secret}
  160. data.State.Error = authError
  161. var template string
  162. if config.OpenRegistration {
  163. template = "signup"
  164. } else {
  165. template = "signup_locked"
  166. }
  167. return render(languages, data, template)
  168. }
  169. func (r HtmlRenderer) RenderSignedup(languages []language.Tag, recoveryCodes []string) string {
  170. data := RenderData{Data: recoveryCodes}
  171. data.State.User = r.user
  172. return render(languages, data, "signedup")
  173. }
  174. func (r HtmlRenderer) RenderWatchlist(watchlist datastructure.Watchlist, languages []language.Tag) string {
  175. data := RenderData{Data: watchlist}
  176. data.State.User = r.user
  177. return render(languages, data, "watchlist")
  178. }
  179. func (r HtmlRenderer) RenderTvQueue(tvqueue datastructure.TvQueue, languages []language.Tag) string {
  180. data := RenderData{Data: tvqueue}
  181. data.State.User = r.user
  182. return render(languages, data, "tvqueue")
  183. }
  184. func (r HtmlRenderer) RenderReadlist(readlist datastructure.Readlist, languages []language.Tag) string {
  185. data := RenderData{Data: readlist}
  186. data.State.User = r.user
  187. return render(languages, data, "readlist")
  188. }
  189. func (r HtmlRenderer) RenderExperiences(experiences datastructure.Experiences, languages []language.Tag) string {
  190. data := RenderData{Data: experiences}
  191. data.State.User = r.user
  192. return render(languages, data, "experiences")
  193. }
  194. func (r HtmlRenderer) RenderAccount(user accounts.User, languages []language.Tag) string {
  195. data := RenderData{Data: user}
  196. data.State.User = r.user
  197. return render(languages, data, "account")
  198. }