Ejemplo n.º 1
0
 def render(self):
     prompt = self.get_prompt()
     answers = self.ask(prompt)
     message = Message(
         sender=self.state['user'].login,
         text=answers['text'],
         receiver=answers['receiver'],
         redis=self.redis
     )
     message.publish()
Ejemplo n.º 2
0
 def test_should_create_message(self, author, room, content):
     # given
     self.setup()
     # when
     self.message = Message(author, content, room)
     # then
     assert self.message.author == author
     assert self.message.content == content
     assert self.message.room == room
     assert self.message.date.day == datetime.now().day
     assert self.message.date.month == datetime.now().month
     assert self.message.date.year == datetime.now().year
     # cleanup
     self.cleanup()
Ejemplo n.º 3
0
class TestMessage:
    def setup(self, author=None, room=None, content=None):
        self.message = Message(author, content, room)

    def cleanup(self):
        self.message = None

    @given(author=text(), room=text(), content=text())
    def test_should_create_message(self, author, room, content):
        # given
        self.setup()
        # when
        self.message = Message(author, content, room)
        # then
        assert self.message.author == author
        assert self.message.content == content
        assert self.message.room == room
        assert self.message.date.day == datetime.now().day
        assert self.message.date.month == datetime.now().month
        assert self.message.date.year == datetime.now().year
        # cleanup
        self.cleanup()

    @given(author=text(), room=text(), content=text())
    def test_should_create_from_json(self, author, room, content):
        # given
        self.setup()
        json_message = MessageFixture.json_message(author, room, content)
        # when
        self.message = Message.from_json(json_message)
        # then
        assert self.message.author == author
        assert self.message.content == content
        assert self.message.room == room
        assert self.message.date.day == datetime.now().day
        assert self.message.date.month == datetime.now().month
        assert self.message.date.year == datetime.now().year
        # cleanup
        self.cleanup()

    def test_should_convert_to_json(self):
        # TODO: find out why this test fails randomly
        # given
        self.setup('author', 'room', 'content')

        # when
        json_message = self.message.to_json()
        # then
        message = Message.from_json(json_message)
        assert message.author == self.message.author
        assert message.content == self.message.content
        assert message.room == self.message.room
        assert message.date == self.message.date
        # cleanup
        self.cleanup()
Ejemplo n.º 4
0
 def work(self, user, delay):
     while True:
         time.sleep(random() * delay)
         time.sleep(random() * delay)
         if (random() < 0.2):
             print('👁 %s wants to read a message' % user.login)
             mid = self.redis.zpopmin(
                 '%s:%s' % (constants.INCOMING_MESSAGES_Z, user.login))
             if not mid or not mid[0]:
                 print('❗️But %s has not a new message' % user.login)
                 continue
             mid = mid[0][0]
             msg = Message.load(mid, self.redis)
             msg.read(user.login)
             print('👀 %s read a message %s' % (user.login, mid))
         else:
             receiver = self.logins[math.floor(random() * len(self.logins))]
             print('✏️ %s writes a message to %s' % (user.login, receiver))
             msg = Message(sender=user.login,
                           receiver=receiver,
                           text=lorem.sentence(),
                           redis=self.redis)
             msg.publish()
Ejemplo n.º 5
0
    def test_should_convert_to_json(self):
        # TODO: find out why this test fails randomly
        # given
        self.setup('author', 'room', 'content')

        # when
        json_message = self.message.to_json()
        # then
        message = Message.from_json(json_message)
        assert message.author == self.message.author
        assert message.content == self.message.content
        assert message.room == self.message.room
        assert message.date == self.message.date
        # cleanup
        self.cleanup()
Ejemplo n.º 6
0
 def render(self):
     actions = {
         APPROVE: self.approve,
         BLOCK: self.block
     }
     while True:
         self.clear()
         message = Message.get_next_unprocessed(self.redis)
         if message:
             print(message.pstr())
             answers = self.ask(PROMPT)
             if answers['action'] in actions:
                 actions[answers['action']](message)
             self.clear()
         else:
             print('No new messages :(')
         answers = self.ask(CONTINUE_PROMPT)
         if not answers['action']:
             return
Ejemplo n.º 7
0
 def render(self):
     while True:
         self.clear()
         if self.selected_message:
             print(self.selected_message.pstr())
         choices = [PREV, NEXT, BACK]
         choices.extend(self.load())
         actions = {NEXT: self.next_page, PREV: self.prev_page, BACK: None}
         answers = self.ask([{
             'type': 'list',
             'name': 'item',
             'message': 'Navigation',
             'choices': choices
         }])
         if answers['item'] in actions:
             if answers['item'] == BACK:
                 return
             actions[answers['item']]()
             self.clear()
         else:
             # it contains id of selected message
             self.selected_message = Message.load(answers['item'],
                                                  self.redis)
             self.selected_message.read(self.state['user'].login)
Ejemplo n.º 8
0
    def on_message(self, raw_message):
        msg = Message.from_json(raw_message)
        self.data_store.store_message(msg)

        self.send_to_room(msg.room, msg)
Ejemplo n.º 9
0
 def setup(self, author=None, room=None, content=None):
     self.message = Message(author, content, room)
Ejemplo n.º 10
0
 def on_message(self, raw_message):
     message = Message.from_json(raw_message)
     self.send_message(message)
Ejemplo n.º 11
0
 def process_old(self):
     while True:
         message = Message.get_next_unprocessed(self.redis)
         if not message:
             break
         self.moderate_message(message)
Ejemplo n.º 12
0
 def handle_message_created_event(self, message):
     self.moderate_message(Message.load(message['data'], self.redis))