Пример #1
0
 def test_annoyiances(self):
     dest = User('0', 'LtWorf', None)
     asyncio.run(self.client._addmagic('ciao @here',
                                       dest)) == 'ciao <!here>'
     asyncio.run(self.client._addmagic('ciao @channel',
                                       dest)) == 'ciao <!channel>'
     asyncio.run(self.client._addmagic('ciao @everyone </',
                                       dest)) == 'ciao <!everyone> &lt;/'
Пример #2
0
    def events_iter(self) -> Iterator[Optional[SlackEvent]]:
        while True:
            if self._internalevents:
                data: Optional[Dict[str, Any]] = self._internalevents.pop()
            else:
                data = self._read()

            if not data:
                yield None
                continue

            r: Optional[SlackEvent] = None
            log('Scanning ', data)
            if not isinstance(data, dict):
                continue

            # Skip stuff sent by this client
            try:
                if data['fields']['args'][0]['_id'].startswith(
                        self._id_prefix):
                    continue
            except Exception:
                pass

            if data.get('msg') == 'changed' and data.get(
                    'collection') == 'stream-room-messages':  # New message
                try:
                    # If the sender is unknown, add it
                    if data['fields']['args'][0]['u'][
                            '_id'] not in self._users:
                        self._users[
                            data['fields']['args'][0]['u']['_id']] = User(
                                id=data['fields']['args'][0]['u']['_id'],
                                name=data['fields']['args'][0]['u']
                                ['username'],
                                profile=Profile(real_name='noname'),
                            )
                    r = Message(
                        channel=data['fields']['args'][0]['rid'],
                        user=data['fields']['args'][0]['u']['_id'],
                        text=data['fields']['args'][0]['msg'],
                    )
                    if 'editedBy' in data['fields']['args'][0]:
                        r = MessageEdit(type='message',
                                        subtype='message_changed',
                                        channel=r.channel,
                                        previous=NoChanMessage(user=r.user,
                                                               text=''),
                                        current=NoChanMessage(user=r.user,
                                                              text=r.text))
                except Exception:
                    pass

            if r is None:
                log('Not handled: ', data)
            else:
                yield r
Пример #3
0
 def get_members(self, id_: str) -> Set[str]:
     data = self.loader.load(self._call('getUsersOfRoom', [id_, True], True), ChannelUsers)
     for i in data.records:
         if i.id_ not in self._users:
             self._users[i.id_] = User(
                 id=i.id_,
                 name=i.username,
                 profile=Profile(real_name=i.name),
             )
     return {i.id_ for i in data.records}
Пример #4
0
    def events_iter(self):  # -> Iterator[Optional[SlackEvent]]:
        while True:
            if self._internalevents:
                data = self._internalevents.pop()
            else:
                data = self._read()

            if not data:
                yield None
                continue

            r = None  # type: Optional[SlackEvent]
            print('Scanning ', data)
            if not isinstance(data, dict):
                continue

            if data.get('msg') == 'changed' and data.get(
                    'collection') == 'stream-room-messages':  # New message
                try:
                    # If the sender is unknown, add it
                    if data['fields']['args'][0]['u'][
                            '_id'] not in self._users:
                        self._users[
                            data['fields']['args'][0]['u']['_id']] = User(
                                id=data['fields']['args'][0]['u']['_id'],
                                name=data['fields']['args'][0]['u']
                                ['username'],
                                profile=Profile(real_name='noname'),
                            )
                    r = Message(
                        channel=data['fields']['args'][0]['rid'],
                        user=data['fields']['args'][0]['u']['_id'],
                        text=data['fields']['args'][0]['msg'],
                    )
                    if 'editedBy' in data['fields']['args'][0]:
                        r = MessageEdit(previous=Message(channel=r.channel,
                                                         user=r.user,
                                                         text=''),
                                        current=r)
                except:
                    pass

            if r is None:
                print('Not handled: ', data)
            else:
                yield r
Пример #5
0
 async def get_user(self, id_):
     return User('0', 'LtWorf', None)
Пример #6
0
 async def get_user_by_name(self, username):
     return User(username, username, None)
Пример #7
0
 def test_escapes(self):
     dest = User('0', 'LtWorf', None)
     assert asyncio.run(self.client._addmagic('<', dest)) == '&lt;'
     assert asyncio.run(self.client._addmagic('>ciao', dest)) == '&gt;ciao'