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.

61 lines
1.4 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. window.parent.postMessage({
  23. ...request,
  24. response: request.action === "capabilities" ? {
  25. capabilities: ["m.sticker"],
  26. } : {
  27. error: { message: "Action not supported" },
  28. },
  29. }, event.origin)
  30. }
  31. export function sendSticker(content) {
  32. const data = {
  33. content,
  34. // `name` is for Element Web (and also the spec)
  35. // Element Android uses content -> body as the name
  36. name: content.body,
  37. }
  38. // This is for Element iOS
  39. const widgetData = {
  40. ...data,
  41. description: content.body,
  42. file: `${content["net.maunium.telegram.sticker"].id}.png`,
  43. }
  44. // Element iOS explodes if there are extra fields present
  45. delete widgetData.content["net.maunium.telegram.sticker"]
  46. window.parent.postMessage({
  47. api: "fromWidget",
  48. action: "m.sticker",
  49. requestId: `sticker-${Date.now()}`,
  50. widgetId,
  51. data,
  52. widgetData,
  53. }, "*")
  54. }