def get_client_device_ids(self, client_id): client_tokens = Database.get_session().query(NotifyToken) \ .filter(NotifyToken.client_id == client_id) \ .order_by(desc(NotifyToken.created_at)) \ .all() Database.get().session.remove() return client_tokens
def get_by_message_id(self, message_id): message_user_read = Database.get_session().query(MessageUserRead) \ .filter(MessageUserRead.message_id == message_id) \ .one_or_none() if message_user_read: message_user_read.get().session.remove() return message_user_read
def get_client_key_by_owner_group(self, group_id, client_id): client = Database.get_session().query(GroupClientKey) \ .join(GroupChat, GroupClientKey.group_id == GroupChat.id, isouter=True) \ .filter(GroupChat.owner_group_id == group_id, GroupClientKey.client_id == client_id) \ .one_or_none() Database.get().session.remove() return client
def get_all_in_group(self, group_id): client = Database.get_session().query(GroupClientKey) \ .filter(GroupClientKey.group_id == group_id) \ .order_by(GroupClientKey.client_id.asc()) \ .all() Database.get().session.remove() return client
def get_users(self, client_id): user = Database.get_session().query(User) \ .filter(User.id != client_id) \ .filter(User.last_login_at != None) \ .all() Database.get().session.remove() return user
def get_google_user(self, email, auth_source): user = Database.get_session().query(User) \ .filter(User.email == email) \ .filter(User.auth_source == auth_source) \ .one_or_none() Database.get().session.remove() return user
def get_joined_group_type(self, client_id, group_type): result = Database.get_session().query(GroupChat, GroupClientKey.id, GroupChat.group_clients) \ .join(GroupClientKey, GroupChat.id == GroupClientKey.group_id) \ .filter(GroupClientKey.client_id == client_id) \ .filter(GroupChat.group_type == group_type) \ .all() Database.get().session.remove() return result
def get(self, group_id): group = Database.get_session().query(GroupChat, Message) \ .join(Message, GroupChat.last_message_id == Message.id, isouter=True) \ .filter(GroupChat.id == group_id) \ .one_or_none() Database.get().session.remove() return group
def search(self, keyword): search = "%{}%".format(keyword) group = Database.get_session().query(GroupChat, Message) \ .join(Message, GroupChat.last_message_id == Message.id, isouter=True) \ .filter(GroupChat.group_name.like(search)) \ .all() Database.get().session.remove() return group
def search(self, keyword, client_id): search = "%{}%".format(keyword) user = Database.get_session().query(User) \ .filter(User.id != client_id) \ .filter(User.display_name.ilike(search)) \ .filter(User.last_login_at != None) \ .all() Database.get().session.remove() return user
def get_clients_in_group(self, group_id): result = Database.get_session().query(GroupClientKey, User) \ .options(joinedload(User.tokens)) \ .join(User, GroupClientKey.client_id == User.id, isouter=True) \ .filter(GroupClientKey.group_id == group_id) \ .order_by(GroupClientKey.client_id.asc()) \ .all() Database.get().session.remove() return result
def get_joined(self, client_id): result = Database.get_session().query(GroupChat, Message, GroupClientKey) \ .join(GroupClientKey, GroupChat.id == GroupClientKey.group_id) \ .join(Message, GroupChat.last_message_id == Message.id, isouter=True) \ .options(joinedload(Message.users_read).joinedload(MessageUserRead.user)) \ .filter(GroupClientKey.client_id == client_id) \ .all() Database.get().session.remove() return result
def delete(self): try: Database.get_session().delete(self) Database.get_session().commit() except Exception as e: Database.get_session().rollback() logger.error(e)
def add(self): try: Database.get_session().add(self) Database.get_session().commit() except: Database.get_session().rollback() raise
def update_bulk_client_key(self, client_id, list_group_client_key): try: for group_client_key in list_group_client_key: sql_update = 'UPDATE group_client_key SET ' \ 'device_id=:device_id, ' \ 'client_key=:client_key, ' \ 'client_sender_key_id=:client_sender_key_id, ' \ 'client_sender_key=:client_sender_key, ' \ 'client_public_key=:client_public_key, ' \ 'client_private_key=:client_private_key, ' \ 'updated_at=NOW() ' \ 'WHERE group_id=:group_id ' \ 'AND client_id=:client_id' Database.get_session().execute( sql_update, { 'device_id': group_client_key.deviceId, 'client_key': group_client_key.clientKeyDistribution, 'client_sender_key_id': group_client_key.senderKeyId, 'client_sender_key': group_client_key.senderKey, 'client_public_key': group_client_key.publicKey, 'client_private_key': group_client_key.privateKey, 'group_id': group_client_key.groupId, 'client_id': client_id }) Database.get_session().commit() return True except Exception as e: logger.error(e) Database.get_session().rollback() return False
def update(self): try: Database.get_session().merge(self) Database.get_session().commit() # Database.get().session.remove() except Exception as e: Database.get_session().rollback() logger.error(e)
def add(self): try: Database.get_session().add(self) Database.get_session().commit() return self except Exception as e: Database.get_session().rollback() logger.error(e)
def get_message_in_group(self, group_id, offset, from_time): message = Database.get_session().query(Message) \ .options(joinedload(Message.users_read).joinedload(MessageUserRead.user)) \ .filter(Message.group_id == group_id) message = message.order_by(Message.created_at.desc()) if from_time != 0: dt = datetime.fromtimestamp(from_time / 1000) # from time in milisecond => second message = message.filter(Message.created_at < dt) if offset != 0: message = message.limit(offset) result = message.all() Database.get().session.remove() return result
def add(self): client = self.get_by_client_id(client_id=self.client_id) if client is not None: return False else: try: Database.get_session().add(self) Database.get_session().commit() return True except Exception as e: Database.get_session().rollback() logger.error(e)
def update(self): server_info = self.get() if server_info is not None: self.id = server_info.id try: Database.get_session().merge(self) Database.get_session().commit() except Exception as e: Database.get_session().rollback() logger.error(e) else: self.add() return True
def add(self): client = self.get(self.group_id, self.client_id) if client is not None: self.id = client.id self.update() return self else: try: Database.get_session().add(self) Database.get_session().commit() return self except Exception as e: Database.get_session().rollback() logger.error(e)
def add(self): client_device = self.get(self.client_id, self.device_id) if client_device is not None: self.id = client_device.id self.update() else: self.id = str(uuid.uuid4()) try: Database.get_session().add(self) Database.get_session().commit() except Exception as e: Database.get_session().rollback() logger.error(e) return self
def add(self): Database.get_session().add(self) Database.get_session().commit() return self
def get_group_type(self, group_id): group = Database.get_session().query(GroupChat.group_type) \ .filter(GroupChat.id == group_id) \ .one_or_none() Database.get().session.remove() return group
def get_group_rtc_token(self, group_id): result = Database.get_session().query(GroupChat.group_rtc_token) \ .filter(GroupChat.id == group_id) \ .first() Database.get().session.remove() return result
def get_clients(self, client_ids): client_tokens = Database.get_session().query(NotifyToken) \ .filter(NotifyToken.client_id.in_(client_ids)) \ .all() Database.get().session.remove() return client_tokens
def get_by_group_owner(self, owner_group_id): result = Database.get_session().query(GroupChat) \ .filter(GroupChat.owner_group_id == owner_group_id) \ .all() Database.get().session.remove() return result
def get(self, message_id): message = Database.get_session().query(Message) \ .filter(Message.id == message_id) \ .one_or_none() Database.get().session.remove() return message
def get(self, notify_id): notify = Database.get_session().query(Notify) \ .filter(Notify.id == notify_id) \ .one_or_none() Database.get().session.remove() return notify
def get(self): server_info = Database.get_session().query(ServerInfo) \ .filter(ServerInfo.id == 1) \ .one_or_none() Database.get().session.remove() return server_info