1
0
Fork 0

feat: set group avatar

This commit is contained in:
nicoco 2023-12-22 16:22:15 +01:00
parent 662d7c63fe
commit 7f12bb4179
3 changed files with 20 additions and 9 deletions

View File

@ -1,11 +1,12 @@
import re
from datetime import datetime, timezone
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Optional
from slidge.group import LegacyBookmarks, LegacyMUC, LegacyParticipant, MucType
from slixmpp.exceptions import XMPPError
from .generated import whatsapp
from .util import get_bytes_temp
if TYPE_CHECKING:
from .contact import Contact
@ -102,6 +103,11 @@ class MUC(LegacyMUC[str, str, Participant, str]):
else {}, # but better safe than sorry
)
async def on_avatar(self, data: Optional[bytes], mime: Optional[str]) -> None:
return self.session.whatsapp.SetAvatar(
self.legacy_id, await get_bytes_temp(data) if data else ""
)
class Bookmarks(LegacyBookmarks[str, MUC]):
session: "Session"

View File

@ -27,6 +27,7 @@ from .contact import Contact, Roster
from .gateway import Gateway
from .generated import go, whatsapp
from .group import MUC, Bookmarks
from .util import get_bytes_temp
MESSAGE_PAIR_SUCCESS = (
"Pairing successful! You might need to repeat this process in the future if the"
@ -588,11 +589,3 @@ async def get_url_temp(client: ClientSession, url: str) -> Optional[str]:
with fdopen(temp_file, "wb") as f:
f.write(await resp.read())
return temp_path
async def get_bytes_temp(buf: bytes) -> Optional[str]:
temp_file, temp_path = mkstemp(dir=global_config.HOME_DIR / "tmp")
with fdopen(temp_file, "wb") as f:
f.write(buf)
return temp_path
return None

12
slidge_whatsapp/util.py Normal file
View File

@ -0,0 +1,12 @@
from os import fdopen
from tempfile import mkstemp
from typing import Optional
from slidge.core import config as global_config
async def get_bytes_temp(buf: bytes) -> Optional[str]:
temp_file, temp_path = mkstemp(dir=global_config.HOME_DIR / "tmp")
with fdopen(temp_file, "wb") as f:
f.write(buf)
return temp_path