Add basic desktop notifications support

This commit is contained in:
miruka 2020-03-30 08:02:33 -04:00
parent 71785911a0
commit 69e414b31c
3 changed files with 34 additions and 1 deletions

View File

@ -9,6 +9,7 @@ lxml >= 4.5.1, < 5
matrix-nio[e2e] >= 0.15.0, < 0.16
mistune >= 0.8.4, < 0.9
pymediainfo >= 4.2.1, < 5
plyer >= 1.4.3, < 2
async_generator >= 1.10, < 2; python_version < "3.7"
dataclasses >= 0.6, < 0.7; python_version < "3.7"

View File

@ -9,10 +9,11 @@ import time
import traceback
from datetime import datetime
from pathlib import Path
from typing import Any, DefaultDict, Dict, List, Optional, Tuple
from typing import Any, DefaultDict, Dict, List, Optional, Tuple, Union
from urllib.parse import urlparse
import aiohttp
import plyer
from appdirs import AppDirs
import nio
@ -538,3 +539,17 @@ class Backend:
await asyncio.gather(*coros)
await session.close()
async def desktop_notify(
self, title: str, body: str = "", image: Union[Path, str] = "",
) -> None:
# XXX: images on windows must be .ICO
plyer.notification.notify(
title = title,
message = body,
app_name = __app_name__,
app_icon = str(image),
timeout = 10,
toast = False,
)

View File

@ -2101,6 +2101,23 @@ class MatrixClient(nio.AsyncClient):
item.id = f"echo-{tx_id}"
self.event_to_echo_ids[ev.event_id] = item.id
notif_room = room.display_name
notif_sender = item.sender_name or item.sender_id
body_start = f"{notif_sender}: "
if notif_room == notif_sender:
body_start = ""
elif isinstance(ev, nio.RoomMessageEmote):
body_start = f"<i>{notif_sender} </i>"
await self.backend.desktop_notify(
title = notif_room,
body = f"{body_start}{item.inline_content}",
# await self.backend.media_cache.get_thumbnail(
# item.sender_avatar, 32, 32,
# ),
)
model[item.id] = item
await self.set_room_last_event(room.room_id, item)