Skip to content

Commit

Permalink
Merge pull request #4 from Bytespeicher/fix_age
Browse files Browse the repository at this point in the history
Fix detection of old messages for federated messages
  • Loading branch information
mape2k authored Jan 14, 2025
2 parents 1a2d7cc + fdae66d commit 98546d1
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions app/bot.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import datetime
import json
import nio
import os
Expand Down Expand Up @@ -267,13 +268,30 @@ async def _receiveMessage(
self, room: nio.MatrixRoom, event: nio.RoomMessageText
):

# Get timestamp from message
try:
messageTimestamp = \
datetime.datetime.fromtimestamp(
event.server_timestamp/1000.0
)
except AttributeError:
print(
"Message without timestamp received in room %s from %s: %s"
% (room.display_name, room.user_name(event.sender), event.body)
)
return

# Calculate message timestamp limit (not older then 5 seconds)
messageTimestampLimit = \
datetime.datetime.now() - datetime.timedelta(seconds=5)

# only accept messages
# - from other users
# - starting with control sign
# - not older as 5000ms
# - not older then message timestamp limit
if (event.sender != room.own_user_id
and event.body.startswith(self._config['controlsign'])
and event.source['unsigned']['age'] <= 5000):
and messageTimestamp >= messageTimestampLimit):

# Log message
print(
Expand Down

0 comments on commit 98546d1

Please sign in to comment.