You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
4.1 KiB

  1. # maunium-stickerpicker - A fast and simple Matrix sticker picker widget.
  2. # Copyright (C) 2020 Tulir Asokan
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Affero General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. from typing import Optional, TYPE_CHECKING
  17. import json
  18. from aiohttp import ClientSession
  19. from yarl import URL
  20. access_token: Optional[str] = None
  21. homeserver_url: Optional[str] = None
  22. upload_url: Optional[URL] = None
  23. if TYPE_CHECKING:
  24. from typing import TypedDict
  25. class MediaInfo(TypedDict):
  26. w: int
  27. h: int
  28. size: int
  29. mimetype: str
  30. thumbnail_url: Optional[str]
  31. thumbnail_info: Optional['MediaInfo']
  32. class StickerInfo(TypedDict, total=False):
  33. body: str
  34. url: str
  35. info: MediaInfo
  36. id: str
  37. msgtype: str
  38. else:
  39. MediaInfo = None
  40. StickerInfo = None
  41. async def load_config(path: str) -> None:
  42. global access_token, homeserver_url, upload_url
  43. try:
  44. with open(path) as config_file:
  45. config = json.load(config_file)
  46. homeserver_url = config["homeserver"]
  47. access_token = config["access_token"]
  48. try:
  49. giphy_api_key = config["giphy_api_key"]
  50. giphy_mxc_prefix = config["giphy_mxc_prefix"]
  51. except KeyError:
  52. # these two are not mandatory, assume GIF search is disabled
  53. print("Giphy related parameters not found in the config file.")
  54. except FileNotFoundError:
  55. print("Matrix config file not found. Please enter your homeserver and access token.")
  56. homeserver_url = input("Homeserver URL: ")
  57. access_token = input("Access token: ")
  58. print("If you want to enable GIF search, enter your giphy API key. Otherwise, leave it empty.")
  59. giphy_api_key = input("Giphy API key: ").strip()
  60. giphy_mxc_prefix = "mxc://giphy.mau.dev/"
  61. if giphy_api_key:
  62. print("If you want to self-host the matrix->giphy proxy, enter the mxc URI prefix here")
  63. print("Defaults to mxc://giphy.mau.dev/ if left empty.")
  64. giphy_mxc_prefix = input("Giphy MXC prefix: ").strip() or giphy_mxc_prefix
  65. whoami_url = URL(homeserver_url) / "_matrix" / "client" / "r0" / "account" / "whoami"
  66. if whoami_url.scheme not in ("https", "http"):
  67. whoami_url = whoami_url.with_scheme("https")
  68. user_id = await whoami(whoami_url, access_token)
  69. with open(path, "w") as config_file:
  70. json.dump({
  71. "homeserver": homeserver_url,
  72. "user_id": user_id,
  73. "access_token": access_token,
  74. "giphy_api_key": giphy_api_key,
  75. "giphy_mxc_prefix": giphy_mxc_prefix,
  76. }, config_file)
  77. print(f"Wrote config to {path}")
  78. upload_url = URL(homeserver_url) / "_matrix" / "media" / "r0" / "upload"
  79. async def whoami(url: URL, access_token: str) -> str:
  80. headers = {"Authorization": f"Bearer {access_token}"}
  81. async with ClientSession() as sess, sess.get(url, headers=headers) as resp:
  82. resp.raise_for_status()
  83. user_id = (await resp.json())["user_id"]
  84. print(f"Access token validated (user ID: {user_id})")
  85. return user_id
  86. async def upload(data: bytes, mimetype: str, filename: str) -> str:
  87. url = upload_url.with_query({"filename": filename})
  88. headers = {"Content-Type": mimetype, "Authorization": f"Bearer {access_token}"}
  89. async with ClientSession() as sess, sess.post(url, data=data, headers=headers) as resp:
  90. return (await resp.json())["content_uri"]