def verify_code_lambda(code): """ This lambda checks the user submitted code. """ if not current_user_id(): raise SkygearException("You must log in to perform this action.", code=NotAuthenticated) thelambda = VerifyCodeLambda(settings) return thelambda(current_user_id(), code)
def verify_request_lambda(record_key): """ This lambda allows client to request verification (i.e. send email or send SMS). """ if not current_user_id(): raise SkygearException("You must log in to perform this action.", code=NotAuthenticated) thelambda = VerifyRequestLambda(settings, providers) return thelambda(current_user_id(), record_key)
def publish_typing(conversation, evt, at): """ { 'userid': { 'event': 'begin', 'at': '20161116T78:44:00Z' }, 'userid2': { 'event': 'begin', 'at': '20161116T78:44:00Z' } } """ user_id = current_user_id() channels = conversation['participant_ids'] data = {} data['user/' + user_id] = { 'event': evt, 'at': timestamp_to_rfc3339_utcoffset(at.timestamp()) } encoder = _RecordEncoder() for user_id in channels: _publish_event(user_id, 'typing', {encoder.encode_id(conversation.id): data}) return {'status': 'OK'}
def publish_typing(conversation, evt, at): """ { 'userid': { 'event': 'begin', 'at': '20161116T78:44:00Z' }, 'userid2': { 'event': 'begin', 'at': '20161116T78:44:00Z' } } """ user_id = current_user_id() channels = conversation['participant_ids'] data = {} data['user/' + user_id] = { 'event': evt, 'at': timestamp_to_rfc3339_utcoffset(at.timestamp()) } for user_id in channels: _publish_event(user_id, 'typing', { conversation['_id']: data }) return {'status': 'OK'}
def __init__(self, message): super().__init__(RecordID(self.record_type, str(uuid.uuid4())), current_user_id(), message.acl) for key in [ 'attachment', 'body', 'metadata', 'conversation', 'message_status', 'edited_by', 'edited_at' ]: if key in message: self[key] = message.get(key, None) self['parent'] = Reference(message.id)
def fetch_all_with_paging(cls, page, page_size): database = cls._get_database() offset = (page - 1) * page_size query_result = database.query( Query(cls.record_type, predicate=Predicate(user__eq=current_user_id()), offset=offset, limit=page_size, include=["conversation", "user"])) return [uc for uc in query_result]
def save(self) -> None: """ Save the Message record to the database. """ container = SkygearContainer(api_key=skyoptions.masterkey, user_id=current_user_id()) container.send_action('record:save', { 'database_id': '_public', 'records': [serialize_record(self.record)], 'atomic': True })
def fetch_one(cls, conversation_id, user_id=None): database = cls._get_database() if user_id is None: user_id = current_user_id() predicate = Predicate(user__eq=user_id, conversation__eq=conversation_id) query_result = database.query( Query(cls.record_type, predicate=predicate, limit=1, include=["conversation", "user"])) return UserConversation.from_record(query_result[0])\ if len(query_result) == 1 else None
def total_unread(user_id=None): if user_id is None: user_id = current_user_id() with db.conn() as conn: cur = conn.execute( ''' SELECT COUNT(*), SUM("unread_count") FROM %(schema_name)s.user_conversation WHERE "unread_count" > 0 AND "user" = %(user_id)s ''', { 'schema_name': AsIs(_get_schema_name()), 'user_id': user_id }) r = cur.first() conversation_count = r[0] message_count = r[1] return {'conversation': conversation_count, 'message': message_count}
def save(self) -> None: """ Save the collection of receipts to the database. This function does nothing if there is nothing in the collection. """ if not len(self): return records_to_save = [ serialize_record(receipt.record) for receipt in self ] container = SkygearContainer(api_key=skyoptions.masterkey, user_id=current_user_id()) container.send_action('record:save', { 'database_id': '_public', 'records': records_to_save, 'atomic': True })
def _get_container(): return SkygearContainer(api_key=skyoptions.masterkey, user_id=current_user_id())
def fetch(cls, message_id: str): """ Fetch the message from skygear. The conversation record is also fetched using eager load. """ # FIXME checking should not be necessary, passing correct type # is the responsibility of the caller. # message_id can be Reference, recordID or string if isinstance(message_id, Reference): message_id = message_id.recordID.key if isinstance(message_id, RecordID): message_id = message_id.key if not isinstance(message_id, str): raise ValueError() container = SkygearContainer(api_key=skyoptions.masterkey, user_id=current_user_id()) response = container.send_action( 'record:query', { 'database_id': '_union', 'record_type': 'message', 'limit': 1, 'sort': [], 'count': False, 'predicate': [ 'eq', { '$type': 'keypath', '$val': '_id' }, message_id ] } ) if 'error' in response: raise SkygearChatException(response['error']) if len(response['result']) == 0: raise SkygearChatException('no messages found', code=ResourceNotFound) obj = cls() messageDict = response['result'][0] obj.record = deserialize_record(messageDict) # Conversation is in publicDB, do cannot transient include print(obj.record['conversation_id'].recordID.key) response = container.send_action( 'record:query', { 'database_id': '_public', 'record_type': 'conversation', 'limit': 1, 'sort': [], 'count': False, 'predicate': [ 'eq', { '$type': 'keypath', '$val': '_id' }, obj.record['conversation_id'].recordID.key ] } ) if len(response['result']) == 0: raise SkygearChatException("no conversation found") conversationDict = response['result'][0] obj.conversationRecord = deserialize_record(conversationDict) return obj
def test_current_user_id(self): ctx.push_context({'user_id': '42'}) assert ctx.current_user_id()