Update
This commit is contained in:
parent
0542113d99
commit
eafcf2b389
|
@ -125,3 +125,13 @@ class Callbacks:
|
||||||
async def membership(self, room: MatrixRoom, event: RoomMemberEvent) -> None:
|
async def membership(self, room: MatrixRoom, event: RoomMemberEvent) -> None:
|
||||||
timestamp = make_datetime(event.server_timestamp)
|
timestamp = make_datetime(event.server_timestamp)
|
||||||
MembershipUpdates.update_membership(room, event, timestamp)
|
MembershipUpdates.update_membership(room, event, timestamp)
|
||||||
|
if event.membership == "join" and event.prev_membership in (
|
||||||
|
None,
|
||||||
|
"invite",
|
||||||
|
"leave",
|
||||||
|
):
|
||||||
|
content = event.content or {}
|
||||||
|
name = content.get("displayname")
|
||||||
|
logger.info(
|
||||||
|
f"New user joined in {room.display_name}: {name} ({event.state_key})"
|
||||||
|
)
|
||||||
|
|
|
@ -13,9 +13,9 @@ from nio import (
|
||||||
LoginError,
|
LoginError,
|
||||||
RoomMemberEvent,
|
RoomMemberEvent,
|
||||||
RoomMessageText,
|
RoomMessageText,
|
||||||
|
SyncError,
|
||||||
UnknownEvent,
|
UnknownEvent,
|
||||||
)
|
)
|
||||||
from nio.store.database import DefaultStore
|
|
||||||
from peewee import OperationalError
|
from peewee import OperationalError
|
||||||
from playhouse.db_url import connect
|
from playhouse.db_url import connect
|
||||||
|
|
||||||
|
@ -65,7 +65,6 @@ async def main():
|
||||||
client_config = AsyncClientConfig(
|
client_config = AsyncClientConfig(
|
||||||
max_limit_exceeded=0,
|
max_limit_exceeded=0,
|
||||||
max_timeouts=0,
|
max_timeouts=0,
|
||||||
store=DefaultStore,
|
|
||||||
store_sync_tokens=True,
|
store_sync_tokens=True,
|
||||||
encryption_enabled=config.encryption,
|
encryption_enabled=config.encryption,
|
||||||
)
|
)
|
||||||
|
@ -83,18 +82,13 @@ async def main():
|
||||||
client.access_token = config.user_token
|
client.access_token = config.user_token
|
||||||
client.user_id = config.user_id
|
client.user_id = config.user_id
|
||||||
|
|
||||||
# Set up event callbacks
|
|
||||||
callbacks = Callbacks(client, config)
|
|
||||||
client.add_event_callback(callbacks.message, (RoomMessageText,))
|
|
||||||
client.add_event_callback(callbacks.unknown, (UnknownEvent,))
|
|
||||||
client.add_event_callback(callbacks.membership, (RoomMemberEvent,))
|
|
||||||
|
|
||||||
# Keep trying to reconnect on failure (with some time in-between)
|
# Keep trying to reconnect on failure (with some time in-between)
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
if config.user_token:
|
if config.user_token:
|
||||||
# Use token to log in
|
# Use token to log in
|
||||||
client.load_store()
|
if config.encryption:
|
||||||
|
client.load_store()
|
||||||
|
|
||||||
# Sync encryption keys with the server
|
# Sync encryption keys with the server
|
||||||
if client.should_upload_keys:
|
if client.should_upload_keys:
|
||||||
|
@ -125,7 +119,22 @@ async def main():
|
||||||
# Login succeeded!
|
# Login succeeded!
|
||||||
|
|
||||||
logger.info(f"Logged in as {config.user_id}")
|
logger.info(f"Logged in as {config.user_id}")
|
||||||
await client.sync_forever(timeout=30000, full_state=True)
|
# Do a initial sync
|
||||||
|
resp = await client.sync(timeout=30000, full_state=True)
|
||||||
|
while isinstance(resp, SyncError):
|
||||||
|
logger.warning("Initial sync failed, retrying in 30s...")
|
||||||
|
sleep(30)
|
||||||
|
resp = await client.sync(timeout=30000, full_state=True)
|
||||||
|
logger.info("Initial sync completed.")
|
||||||
|
sync_token = resp.next_batch
|
||||||
|
|
||||||
|
# Set up event callbacks
|
||||||
|
callbacks = Callbacks(client, config)
|
||||||
|
client.add_event_callback(callbacks.message, (RoomMessageText,))
|
||||||
|
client.add_event_callback(callbacks.unknown, (UnknownEvent,))
|
||||||
|
client.add_event_callback(callbacks.membership, (RoomMemberEvent,))
|
||||||
|
|
||||||
|
await client.sync_forever(timeout=30000, full_state=True, since=sync_token)
|
||||||
|
|
||||||
except (ClientConnectionError, ServerDisconnectedError, TimeoutError):
|
except (ClientConnectionError, ServerDisconnectedError, TimeoutError):
|
||||||
logger.warning("Unable to connect to homeserver, retrying in 15s...")
|
logger.warning("Unable to connect to homeserver, retrying in 15s...")
|
||||||
|
|
Loading…
Reference in a new issue