2020-10-30 23:18:55 +02:00
|
|
|
// maunium-stickerpicker - A fast and simple Matrix sticker picker widget.
|
|
|
|
// Copyright (C) 2020 Tulir Asokan
|
2020-09-05 12:16:27 +03:00
|
|
|
//
|
2020-10-30 23:18:55 +02:00
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2020-09-05 12:16:27 +03:00
|
|
|
let widgetId = null
|
|
|
|
|
|
|
|
window.onmessage = event => {
|
|
|
|
if (!window.parent || !event.data) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const request = event.data
|
|
|
|
if (!request.requestId || !request.widgetId || !request.action || request.api !== "toWidget") {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (widgetId) {
|
|
|
|
if (widgetId !== request.widgetId) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
widgetId = request.widgetId
|
|
|
|
}
|
|
|
|
|
2020-09-10 15:19:58 +03:00
|
|
|
let response
|
|
|
|
|
|
|
|
if (request.action === "visibility") {
|
|
|
|
response = {}
|
|
|
|
} else if (request.action === "capabilities") {
|
|
|
|
response = { capabilities: ["m.sticker"] }
|
|
|
|
} else {
|
|
|
|
response = { error: { message: "Action not supported" } }
|
|
|
|
}
|
|
|
|
|
|
|
|
window.parent.postMessage({ ...request, response }, event.origin)
|
2020-09-05 12:16:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export function sendSticker(content) {
|
2020-09-05 17:19:25 +03:00
|
|
|
const data = {
|
2020-09-10 15:19:58 +03:00
|
|
|
content: { ...content },
|
2020-09-05 17:19:25 +03:00
|
|
|
// `name` is for Element Web (and also the spec)
|
|
|
|
// Element Android uses content -> body as the name
|
|
|
|
name: content.body,
|
|
|
|
}
|
2020-09-06 17:10:07 +03:00
|
|
|
// Custom field that stores the ID even for non-telegram stickers
|
|
|
|
delete data.content.id
|
2020-09-05 17:19:25 +03:00
|
|
|
|
|
|
|
// This is for Element iOS
|
|
|
|
const widgetData = {
|
|
|
|
...data,
|
|
|
|
description: content.body,
|
2020-09-06 17:10:07 +03:00
|
|
|
file: `${content.id}.png`,
|
2020-09-05 17:19:25 +03:00
|
|
|
}
|
|
|
|
// Element iOS explodes if there are extra fields present
|
|
|
|
delete widgetData.content["net.maunium.telegram.sticker"]
|
|
|
|
|
2020-09-05 12:16:27 +03:00
|
|
|
window.parent.postMessage({
|
|
|
|
api: "fromWidget",
|
|
|
|
action: "m.sticker",
|
|
|
|
requestId: `sticker-${Date.now()}`,
|
|
|
|
widgetId,
|
2020-09-05 17:19:25 +03:00
|
|
|
data,
|
|
|
|
widgetData,
|
2020-09-05 12:16:27 +03:00
|
|
|
}, "*")
|
|
|
|
}
|