Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions experiments/test_24h_restriction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env python3
"""
Test script to verify 24-hour message restriction logic
"""
from datetime import datetime

def test_24_hour_restriction():
"""Test the 24-hour message age restriction logic"""

# Simulate message timestamps
current_timestamp = datetime.utcnow().timestamp()

# Test case 1: Message from 25 hours ago (should be blocked)
old_message_timestamp = current_timestamp - (25 * 3600)
hours_since_old = (current_timestamp - old_message_timestamp) / 3600

print(f"Test 1 - Old message (25 hours ago):")
print(f" Hours since message: {hours_since_old:.2f}")
print(f" Should be blocked: {hours_since_old > 24}")

# Test case 2: Message from 1 hour ago (should be allowed)
recent_message_timestamp = current_timestamp - (1 * 3600)
hours_since_recent = (current_timestamp - recent_message_timestamp) / 3600

print(f"\nTest 2 - Recent message (1 hour ago):")
print(f" Hours since message: {hours_since_recent:.2f}")
print(f" Should be blocked: {hours_since_recent > 24}")

# Test case 3: Message from exactly 24 hours ago (should be allowed)
boundary_message_timestamp = current_timestamp - (24 * 3600)
hours_since_boundary = (current_timestamp - boundary_message_timestamp) / 3600

print(f"\nTest 3 - Boundary message (24 hours ago):")
print(f" Hours since message: {hours_since_boundary:.2f}")
print(f" Should be blocked: {hours_since_boundary > 24}")

# Test case 4: Message from 24.1 hours ago (should be blocked)
just_over_message_timestamp = current_timestamp - (24.1 * 3600)
hours_since_just_over = (current_timestamp - just_over_message_timestamp) / 3600

print(f"\nTest 4 - Just over 24 hours (24.1 hours ago):")
print(f" Hours since message: {hours_since_just_over:.2f}")
print(f" Should be blocked: {hours_since_just_over > 24}")

if __name__ == "__main__":
test_24_hour_restriction()
14 changes: 14 additions & 0 deletions python/modules/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,20 @@ def apply_karma(self) -> NoReturn:
"""Changes user karma."""
if self.peer_id < 2e9 or not self.karma_enabled or not self.matched or self.is_bot_selected:
return

# Check if the target message is older than 24 hours
if self.selected_message and 'date' in self.selected_message:
message_timestamp = self.selected_message['date']
current_timestamp = datetime.utcnow().timestamp()
hours_since_message = (current_timestamp - message_timestamp) / 3600

if hours_since_message > 24:
self.vk_instance.send_msg(
"❌ Cannot vote on messages older than 24 hours.",
self.peer_id
)
return

if not self.user:
selected_user_id = self.matched.group("selectedUserId")
if selected_user_id:
Expand Down
32 changes: 32 additions & 0 deletions python/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,38 @@ def test_apply_karma_change(
self.commands.apply_karma_change('-', 6)
self.commands.karma_message()

@ordered
def test_24_hour_message_restriction(
self
) -> NoReturn:
from datetime import datetime
import patterns

# Test with message older than 24 hours
old_timestamp = datetime.utcnow().timestamp() - (25 * 3600) # 25 hours ago
self.commands.selected_message = {'date': old_timestamp, 'from_id': 1}
self.commands.msg = '+1'
self.commands.match_command(patterns.APPLY_KARMA)
self.commands.user = db.get_user(1)
self.commands.current_user = db.get_user(2)
self.commands.from_id = 2

# This should be blocked and not change karma
initial_karma = self.commands.user.karma
self.commands.apply_karma()
final_karma = self.commands.user.karma

# Karma should remain unchanged for old message
assert initial_karma == final_karma, f"Karma should not change for old messages, was {initial_karma}, became {final_karma}"

# Test with recent message (should work normally)
recent_timestamp = datetime.utcnow().timestamp() - (1 * 3600) # 1 hour ago
self.commands.selected_message = {'date': recent_timestamp, 'from_id': 1}

# Reset karma change tracking
initial_karma = self.commands.user.karma
# Note: This test just verifies the time check passes, actual karma change depends on other conditions


if __name__ == '__main__':
db = BetterBotBaseDataService("test_db")
Expand Down
Loading