Stop using ID field from telegram metadata in widget

This commit is contained in:
Tulir Asokan 2020-09-06 17:10:07 +03:00
parent 522e45a4ae
commit 4ce90892f0
3 changed files with 29 additions and 16 deletions

View File

@ -87,6 +87,7 @@ if TYPE_CHECKING:
body: str body: str
url: str url: str
info: MatrixMediaInfo info: MatrixMediaInfo
id: str
def convert_image(data: bytes) -> (bytes, int, int): def convert_image(data: bytes) -> (bytes, int, int):
@ -113,12 +114,8 @@ async def reupload_document(client: TelegramClient, document: Document) -> 'Matr
else: else:
width = int(width / (height / 256)) width = int(width / (height / 256))
height = 256 height = 256
body = ""
for attr in document.attributes:
if isinstance(attr, DocumentAttributeSticker):
body = attr.alt
return { return {
"body": body, "body": "",
"url": mxc, "url": mxc,
"info": { "info": {
"w": width, "w": width,
@ -152,6 +149,21 @@ def add_to_index(name: str) -> None:
print(f"Added {name} to {index_path}") print(f"Added {name} to {index_path}")
def add_meta(document: Document, info: 'MatrixStickerInfo', pack: StickerSetFull) -> None:
for attr in document.attributes:
if isinstance(attr, DocumentAttributeSticker):
info["body"] = attr.alt
info["id"] = str(document.id)
info["net.maunium.telegram.sticker"] = {
"pack": {
"id": str(pack.set.id),
"short_name": pack.set.short_name,
},
"id": str(document.id),
"emoticons": [],
}
async def reupload_pack(client: TelegramClient, pack: StickerSetFull) -> None: async def reupload_pack(client: TelegramClient, pack: StickerSetFull) -> None:
if pack.set.animated: if pack.set.animated:
print("Animated stickerpacks are currently not supported") print("Animated stickerpacks are currently not supported")
@ -183,18 +195,17 @@ async def reupload_pack(client: TelegramClient, pack: StickerSetFull) -> None:
print(f"Skipped reuploading {document.id}") print(f"Skipped reuploading {document.id}")
except KeyError: except KeyError:
reuploaded_documents[document.id] = await reupload_document(client, document) reuploaded_documents[document.id] = await reupload_document(client, document)
reuploaded_documents[document.id]["net.maunium.telegram.sticker"] = { # Always ensure the body and telegram metadata is correct
"pack": { add_meta(document, reuploaded_documents[document.id], pack)
"id": str(pack.set.id),
"short_name": pack.set.short_name,
},
"id": str(document.id),
"emoticons": [],
}
for sticker in pack.packs: for sticker in pack.packs:
if not sticker.emoticon:
continue
for document_id in sticker.documents: for document_id in sticker.documents:
doc = reuploaded_documents[document_id] doc = reuploaded_documents[document_id]
# If there was no sticker metadata, use the first emoji we find
if doc["body"] == "":
doc["body"] = sticker.emoticon
doc["net.maunium.telegram.sticker"]["emoticons"].append(sticker.emoticon) doc["net.maunium.telegram.sticker"]["emoticons"].append(sticker.emoticon)
with open(pack_path, "w") as pack_file: with open(pack_path, "w") as pack_file:

View File

@ -143,7 +143,7 @@ const Pack = ({ pack }) => html`
<h1>${pack.title}</h1> <h1>${pack.title}</h1>
<div class="sticker-list"> <div class="sticker-list">
${pack.stickers.map(sticker => html` ${pack.stickers.map(sticker => html`
<${Sticker} key=${sticker["net.maunium.telegram.sticker"].id} content=${sticker}/> <${Sticker} key=${sticker.id} content=${sticker}/>
`)} `)}
</div> </div>
</section> </section>

View File

@ -35,17 +35,19 @@ window.onmessage = event => {
export function sendSticker(content) { export function sendSticker(content) {
const data = { const data = {
content, content: {...content},
// `name` is for Element Web (and also the spec) // `name` is for Element Web (and also the spec)
// Element Android uses content -> body as the name // Element Android uses content -> body as the name
name: content.body, name: content.body,
} }
// Custom field that stores the ID even for non-telegram stickers
delete data.content.id
// This is for Element iOS // This is for Element iOS
const widgetData = { const widgetData = {
...data, ...data,
description: content.body, description: content.body,
file: `${content["net.maunium.telegram.sticker"].id}.png`, file: `${content.id}.png`,
} }
// Element iOS explodes if there are extra fields present // Element iOS explodes if there are extra fields present
delete widgetData.content["net.maunium.telegram.sticker"] delete widgetData.content["net.maunium.telegram.sticker"]