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.

52 lines
1.7 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 aiohttp import web
  17. from ..database import User
  18. from ..config import Config
  19. from .errors import Error
  20. routes = web.RouteTableDef()
  21. config: Config
  22. @routes.get("/index.json")
  23. async def get_packs(req: web.Request) -> web.Response:
  24. user: User = req["user"]
  25. packs = await user.get_packs()
  26. return web.json_response({
  27. "homeserver_url": user.homeserver_url,
  28. "is_sticker_server": True,
  29. "packs": [f"{pack.id}.json" for pack in packs],
  30. })
  31. @routes.get("/{pack_id}.json")
  32. async def get_pack(req: web.Request) -> web.Response:
  33. user: User = req["user"]
  34. pack = await user.get_pack(req.match_info["pack_id"])
  35. if pack is None:
  36. raise Error.pack_not_found
  37. stickers = await pack.get_stickers()
  38. return web.json_response({
  39. **pack.to_dict(),
  40. "stickers": [sticker.to_dict() for sticker in stickers],
  41. })
  42. def init(cfg: Config) -> None:
  43. global config
  44. config = cfg