def process_message(db: Client, message: Message): if not message.text: return chat_doc = db.collection("chats").document(str(message.chat_id)) chat_doc.set(message.chat.to_dict()) chat_doc.collection("messages").document(str(message.message_id)).set( message.to_dict() ) user = message.from_user try: db.collection("users").document(str(user.id)).create(user.to_dict()) except AlreadyExists: logging.info("%s already existed", user.first_name)
def auto_update_dispatcher_from_firebase_repliers(db: Client, dp: Dispatcher) -> Watch: # Create a callback on_snapshot function to capture changes def on_snapshot(col_snapshot, changes, read_time): existing_handlers: list = dp.handlers[0] existing_handlers.remove(UNKNOWN_COMMAND_ERROR_HANDLER) for change in changes: doc: dict = change.document.to_dict() id: str = change.document.id logging.info("%s replier %s - %r", change.type.name, id, doc) for h in existing_handlers: # delete the old handler regardless, because firebase sends "ADDED" events more than once if getattr(h, "name", "") == id: existing_handlers.remove(h) if change.type.name in ("ADDED", "MODIFIED"): # then just add it back again dp.add_handler( create_replier(name=id, **doc).to_message_handler()) elif change.type.name == "REMOVED": pass # make sure the "unknown command" handler is the last in the list existing_handlers.append(UNKNOWN_COMMAND_ERROR_HANDLER) query_watch = db.collection(u"repliers").on_snapshot(on_snapshot) return query_watch
def get_firebase_repliers(db: Client) -> list[BaseReplier]: for ref in db.collection("repliers").get(): ref_dict = ref.to_dict() try: yield create_replier(name=ref.id, **ref_dict) except Exception as exc: logging.exception("Could not load replier %s - %r", ref.id, ref_dict, exc_info=exc)
def parse_query(testcase): # 'query' testcase contains: # - 'coll_path': collection ref path. # - 'clauses': array of one or more 'Clause' elements # - 'query': the actual google.firestore_v1.StructuredQuery message # to be constructed. # - 'is_error' (as other testcases). # # 'Clause' elements are unions of: # - 'select': [field paths] # - 'where': (field_path, op, json_value) # - 'order_by': (field_path, direction) # - 'offset': int # - 'limit': int # - 'start_at': 'Cursor' # - 'start_after': 'Cursor' # - 'end_at': 'Cursor' # - 'end_before': 'Cursor' # # 'Cursor' contains either: # - 'doc_snapshot': 'DocSnapshot' # - 'json_values': [string] # # 'DocSnapshot' contains: # 'path': str # 'json_data': str from google.auth.credentials import Credentials from google.cloud.firestore_v1 import Client from google.cloud.firestore_v1 import Query _directions = {"asc": Query.ASCENDING, "desc": Query.DESCENDING} credentials = mock.create_autospec(Credentials) client = Client("projectID", credentials) path = parse_path(testcase.coll_path) collection = client.collection(*path) query = collection for clause in testcase.clauses: if "select" in clause: field_paths = [ ".".join(field_path.field) for field_path in clause.select.fields ] query = query.select(field_paths) elif "where" in clause: path = ".".join(clause.where.path.field) value = convert_data(json.loads(clause.where.json_value)) query = query.where(path, clause.where.op, value) elif "order_by" in clause: path = ".".join(clause.order_by.path.field) direction = clause.order_by.direction direction = _directions.get(direction, direction) query = query.order_by(path, direction=direction) elif "offset" in clause: query = query.offset(clause.offset) elif "limit" in clause: query = query.limit(clause.limit) elif "start_at" in clause: cursor = parse_cursor(clause.start_at, client) query = query.start_at(cursor) elif "start_after" in clause: cursor = parse_cursor(clause.start_after, client) query = query.start_after(cursor) elif "end_at" in clause: cursor = parse_cursor(clause.end_at, client) query = query.end_at(cursor) elif "end_before" in clause: cursor = parse_cursor(clause.end_before, client) query = query.end_before(cursor) else: # pragma: NO COVER raise ValueError("Unknown query clause: {}".format(clause)) return query
def process_replier(db: Client, name: str, type: ReplierType, pattern: str, replies: list[str]): db.collection("repliers").document(name).create( dict(type=type.value, pattern=pattern, data=replies))
def parse_query(testcase): # 'query' testcase contains: # - 'coll_path': collection ref path. # - 'clauses': array of one or more 'Clause' elements # - 'query': the actual google.firestore_v1.StructuredQuery message # to be constructed. # - 'is_error' (as other testcases). # # 'Clause' elements are unions of: # - 'select': [field paths] # - 'where': (field_path, op, json_value) # - 'order_by': (field_path, direction) # - 'offset': int # - 'limit': int # - 'start_at': 'Cursor' # - 'start_after': 'Cursor' # - 'end_at': 'Cursor' # - 'end_before': 'Cursor' # # 'Cursor' contains either: # - 'doc_snapshot': 'DocSnapshot' # - 'json_values': [string] # # 'DocSnapshot' contains: # 'path': str # 'json_data': str from google.auth.credentials import Credentials from google.cloud.firestore_v1 import Client from google.cloud.firestore_v1 import Query _directions = {"asc": Query.ASCENDING, "desc": Query.DESCENDING} credentials = mock.create_autospec(Credentials) client = Client("projectID", credentials) path = parse_path(testcase.coll_path) collection = client.collection(*path) query = collection for clause in testcase.clauses: kind = clause.WhichOneof("clause") if kind == "select": field_paths = [ ".".join(field_path.field) for field_path in clause.select.fields ] query = query.select(field_paths) elif kind == "where": path = ".".join(clause.where.path.field) value = convert_data(json.loads(clause.where.json_value)) query = query.where(path, clause.where.op, value) elif kind == "order_by": path = ".".join(clause.order_by.path.field) direction = clause.order_by.direction direction = _directions.get(direction, direction) query = query.order_by(path, direction=direction) elif kind == "offset": query = query.offset(clause.offset) elif kind == "limit": query = query.limit(clause.limit) elif kind == "start_at": cursor = parse_cursor(clause.start_at, client) query = query.start_at(cursor) elif kind == "start_after": cursor = parse_cursor(clause.start_after, client) query = query.start_after(cursor) elif kind == "end_at": cursor = parse_cursor(clause.end_at, client) query = query.end_at(cursor) elif kind == "end_before": cursor = parse_cursor(clause.end_before, client) query = query.end_before(cursor) else: # pragma: NO COVER raise ValueError("Unknown query clause: {}".format(kind)) return query