subscription_bookmarklet.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright 2018 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package ui // import "miniflux.app/ui"
  5. import (
  6. "net/http"
  7. "miniflux.app/config"
  8. "miniflux.app/http/request"
  9. "miniflux.app/http/response/html"
  10. "miniflux.app/ui/form"
  11. "miniflux.app/ui/session"
  12. "miniflux.app/ui/view"
  13. "mvdan.cc/xurls/v2"
  14. )
  15. func (h *handler) bookmarklet(w http.ResponseWriter, r *http.Request) {
  16. sess := session.New(h.store, request.SessionID(r))
  17. view := view.New(h.tpl, r, sess)
  18. user, err := h.store.UserByID(request.UserID(r))
  19. if err != nil {
  20. html.ServerError(w, r, err)
  21. return
  22. }
  23. categories, err := h.store.Categories(user.ID)
  24. if err != nil {
  25. html.ServerError(w, r, err)
  26. return
  27. }
  28. bookmarkletURL := request.QueryStringParam(r, "uri", "")
  29. // Extract URL from text supplied by Web Share Target API.
  30. //
  31. // This is because Android intents have no concept of URL, so apps
  32. // just shove a URL directly into the EXTRA_TEXT intent field.
  33. //
  34. // See https://bugs.chromium.org/p/chromium/issues/detail?id=789379.
  35. text := request.QueryStringParam(r, "text", "")
  36. if text != "" && bookmarkletURL == "" {
  37. bookmarkletURL = xurls.Relaxed().FindString(text)
  38. }
  39. view.Set("form", form.SubscriptionForm{URL: bookmarkletURL})
  40. view.Set("categories", categories)
  41. view.Set("menu", "feeds")
  42. view.Set("user", user)
  43. view.Set("countUnread", h.store.CountUnreadEntries(user.ID))
  44. view.Set("countErrorFeeds", h.store.CountUserFeedsWithErrors(user.ID))
  45. view.Set("defaultUserAgent", config.Opts.HTTPClientUserAgent())
  46. view.Set("hasProxyConfigured", config.Opts.HasHTTPClientProxyConfigured())
  47. html.OK(w, r, view.Render("add_subscription"))
  48. }