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.

66 lines
1.6 KiB

  1. // Copyright (c) 2020 Tulir Asokan
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. let widgetId = null
  7. window.onmessage = event => {
  8. if (!window.parent || !event.data) {
  9. return
  10. }
  11. const request = event.data
  12. if (!request.requestId || !request.widgetId || !request.action || request.api !== "toWidget") {
  13. return
  14. }
  15. if (widgetId) {
  16. if (widgetId !== request.widgetId) {
  17. return
  18. }
  19. } else {
  20. widgetId = request.widgetId
  21. }
  22. let response
  23. if (request.action === "visibility") {
  24. response = {}
  25. } else if (request.action === "capabilities") {
  26. response = { capabilities: ["m.sticker"] }
  27. } else {
  28. response = { error: { message: "Action not supported" } }
  29. }
  30. window.parent.postMessage({ ...request, response }, event.origin)
  31. }
  32. export function sendSticker(content) {
  33. const data = {
  34. content: { ...content },
  35. // `name` is for Element Web (and also the spec)
  36. // Element Android uses content -> body as the name
  37. name: content.body,
  38. }
  39. // Custom field that stores the ID even for non-telegram stickers
  40. delete data.content.id
  41. // This is for Element iOS
  42. const widgetData = {
  43. ...data,
  44. description: content.body,
  45. file: `${content.id}.png`,
  46. }
  47. // Element iOS explodes if there are extra fields present
  48. delete widgetData.content["net.maunium.telegram.sticker"]
  49. window.parent.postMessage({
  50. api: "fromWidget",
  51. action: "m.sticker",
  52. requestId: `sticker-${Date.now()}`,
  53. widgetId,
  54. data,
  55. widgetData,
  56. }, "*")
  57. }