Ejemplo n.º 1
0
 async def get(self, uid: int, cached_msg: CachedMessage = None,
               requirement: FetchRequirement = FetchRequirement.METADATA) \
         -> Optional[Message]:
     redis = self._redis
     keys = self._keys
     ns_keys = self._ns_keys
     msg_keys = MessageKeys(keys, uid)
     await redis.unwatch()
     multi = redis.multi_exec()
     multi.sismember(keys.uids, uid)
     multi.smembers(msg_keys.flags)
     multi.hmget(msg_keys.immutable, b'time', b'emailid', b'threadid')
     multi.get(keys.abort)
     exists, flags, (time, email_id, thread_id), abort = \
         await multi.execute()
     MailboxAbort.assertFalse(abort)
     if not exists:
         if cached_msg is not None:
             if not isinstance(cached_msg, Message):
                 raise TypeError(cached_msg)
             return Message.copy_expunged(cached_msg)
         else:
             return None
     msg_flags = {Flag(flag) for flag in flags}
     msg_email_id = ObjectId.maybe(email_id)
     msg_thread_id = ObjectId.maybe(thread_id)
     msg_time = datetime.fromisoformat(time.decode('ascii'))
     return Message(uid,
                    msg_time,
                    msg_flags,
                    email_id=msg_email_id,
                    thread_id=msg_thread_id,
                    redis=redis,
                    ns_keys=ns_keys)
Ejemplo n.º 2
0
 def _get_object_id(cls, rec: Record, field: str) -> Optional[ObjectId]:
     return ObjectId.maybe(rec.fields.get(field))
Ejemplo n.º 3
0
 def test_maybe(self):
     self.assertEqual(ObjectId(None), ObjectId.maybe(None))
     self.assertEqual(ObjectId(None), ObjectId.maybe(b''))
     self.assertEqual(ObjectId(None), ObjectId.maybe(''))
     self.assertEqual(ObjectId(b'test'), ObjectId.maybe(b'test'))
     self.assertEqual(ObjectId(b'test'), ObjectId.maybe('te\u2026st'))