MatrixClient: RoomMessageMedia support

This commit is contained in:
miruka 2019-09-13 22:34:20 -04:00
parent a2ab7b3090
commit a5095274ba
5 changed files with 45 additions and 26 deletions

View File

@ -170,6 +170,7 @@
- Live-reloading accounts.json
- nio
- RoomMessageMedia info attribute
- `AsyncClient.share_group_session`: send device batches concurrently
- Running blocking DB function calls in executor
- Guard against asyncio OSError Network unreachable

View File

@ -502,6 +502,7 @@ class MatrixClient(nio.AsyncClient):
ev: nio.Event,
content: str,
type_specifier: TypeSpecifier = TypeSpecifier.none,
**fields,
) -> None:
await self.register_nio_room(room)
@ -533,6 +534,8 @@ class MatrixClient(nio.AsyncClient):
target_id = target_id,
target_name = target_name,
target_avatar = target_avatar,
**fields,
)
# Add the Event to model
@ -629,6 +632,26 @@ class MatrixClient(nio.AsyncClient):
await self.register_nio_event(room, ev, content=co)
async def onRoomMessageMedia(self, room, ev) -> None:
info = ev.source["content"].get("info", {})
thumb_info = info.get("thumbnail_info", {})
await self.register_nio_event(
room,
ev,
content = ev.url,
media_title = ev.body,
media_width = info.get("w") or 0,
media_height = info.get("h") or 0,
media_duration = info.get("duration") or 0,
media_size = info.get("size") or 0,
media_mime = info.get("mimetype") or 0,
thumbnail_url = info.get("thumbnail_url") or "",
thumbnail_width = thumb_info.get("w") or 0,
thumbnail_height = thumb_info.get("h") or 0,
)
async def onRoomCreateEvent(self, room, ev) -> None:
co = "%1 allowed users on other matrix servers to join this room." \
if ev.federate else \

View File

@ -2,7 +2,6 @@ import re
from dataclasses import dataclass, field
from datetime import datetime
from typing import Any, Dict, List, Optional, Tuple
from urllib.parse import urlparse
import lxml # nosec
@ -130,6 +129,17 @@ class Event(ModelItem):
is_local_echo: bool = False
local_event_type: str = ""
media_title: str = ""
media_width: int = 0
media_height: int = 0
media_duration: int = 0
media_size: int = 0
media_mime: str = ""
thumbnail_url: str = ""
thumbnail_width: int = 0
thumbnail_height: int = 0
def __post_init__(self) -> None:
self.inline_content = HTML_FILTER.filter_inline(self.content)
@ -149,27 +159,14 @@ class Event(ModelItem):
return self.local_event_type or type(self.source).__name__
@property
def preview_links(self) -> List[Tuple[str, str]]:
def links(self) -> List[str]:
if not self.content.strip():
return []
links = [
(self._get_preview_type(link[0], link[2]), link[2])
for link in lxml.html.iterlinks(self.content)
]
return [l for l in links if l[0] != "page"] # TODO
@staticmethod
def _get_preview_type(el: lxml.html.HtmlElement, link: str) -> str:
path = urlparse(link).path.lower()
for ext in ("jpg", "jpeg", "png", "gif", "bmp", "webp", "tiff", "svg"):
if el.tag == "img" or path.endswith(ext):
return "image"
return "page"
if isinstance(self.source, nio.RoomMessageMedia):
return [self.thumbnail_url or self.content]
return [link[2] for link in lxml.html.iterlinks(self.content)]
@dataclass

View File

@ -148,16 +148,14 @@ Row {
Repeater {
id: previewLinksRepeater
model: previewLinks
model: eventDelegate.links
HLoader {
Component.onCompleted: {
if (modelData[0] == "image") {
setSource(
"EventImage.qml",
{ source: modelData[1] },
)
}
setSource(
"EventImage.qml",
{ source: modelData },
)
}
}
}

View File

@ -47,7 +47,7 @@ Column {
readonly property bool unselectableNameLine:
hideNameLine && ! (onRight && ! combine)
readonly property var previewLinks: model.preview_links
readonly property var links: model.links
function json() {