forked from mindiell/hebdobot
65 lines
1.9 KiB
Python
65 lines
1.9 KiB
Python
from datetime import datetime
|
|
|
|
|
|
class Message:
|
|
def __init__(self, author, text):
|
|
self.author = author
|
|
self.text = text
|
|
self.timestamp = datetime.today()
|
|
|
|
|
|
class Topic:
|
|
def __init__(self, title, collective=True):
|
|
self.title = title
|
|
self.collective = collective
|
|
self.participants = []
|
|
self.messages = []
|
|
self.start_time = datetime.today()
|
|
self.end_time = None
|
|
|
|
@property
|
|
def duration(self):
|
|
if self.end_time:
|
|
minutes = (self.end_time - self.start_time).seconds // 60
|
|
seconds = (self.end_time - self.start_time).seconds % 60
|
|
else:
|
|
minutes = (datetime.today() - self.start_time).seconds // 60
|
|
seconds = (datetime.today() - self.start_time).seconds % 60
|
|
return f"{minutes}:{seconds:02d}"
|
|
|
|
@property
|
|
def individual(self):
|
|
return not self.collective
|
|
|
|
def add_message(self, sender, message):
|
|
self.messages.append(Message(sender, message))
|
|
if sender not in self.participants:
|
|
self.participants.append(sender)
|
|
|
|
def add_participant(self, sender):
|
|
if sender not in self.participants:
|
|
self.participants.append(sender)
|
|
|
|
def cancel_previous(self, author):
|
|
messages = [message for message in self.messages if message.author == author]
|
|
if len(messages) == 0:
|
|
return
|
|
message = messages[-1].text
|
|
self.messages.remove(messages[-1])
|
|
|
|
return message
|
|
|
|
def get_messages(self, author=None):
|
|
if self.collective:
|
|
return self.messages
|
|
return [message for message in self.messages if message.author == author]
|
|
|
|
def has_participant(self, author):
|
|
return author in self.get_participants()
|
|
|
|
def get_participants(self):
|
|
return set([message.author for message in self.messages])
|
|
|
|
def close(self):
|
|
self.end_time = datetime.today()
|