def sent_message(sender_id, receiver_id, message): try: ChatAppMessage.create(sender_id=sender_id, receiver_id=receiver_id, message=message) return True except Exception as Ex: pass return False
def delete_message(message_id): try: query = ChatAppMessage.delete().where(ChatAppMessage.id == message_id) if query.execute() >= 1: return True except Exception as Ex: pass return False
def get_messages(sender_id, receiver_id): try: _ret_data = (ChatAppMessage.select().where( (ChatAppMessage.sender == sender_id) & (ChatAppMessage.receiver == receiver_id) | (ChatAppMessage.sender == receiver_id) & (ChatAppMessage.receiver == sender_id)).order_by( ChatAppMessage.sent_date.asc())) return _ret_data except Exception as Ex: pass
def get_receiver_message(sender_id): try: _ret_dat = (ChatAppUser.select( ChatAppUser.id, ChatAppUser.username).where( ChatAppUser.id.in_( ChatAppMessage.select( ChatAppMessage.receiver.distinct()).where( ChatAppMessage.sender == sender_id)))) return _ret_dat except Exception as Ex: print(Ex)
def list_friend_by_sent_date(user_id): try: query = (ChatAppMessage.select( ChatAppMessage.sender, ChatAppMessage.receiver, ChatAppMessage.is_read, ChatAppMessage.message, ChatAppMessage.sent_date).where( (ChatAppMessage.sender == user_id) | (ChatAppMessage.receiver == user_id)).order_by( ChatAppMessage.sent_date.desc())) return list(query.dicts()) except Exception as Ex: pass
def delete_all_message(user_id, friend_id): try: query = ChatAppMessage.delete().where( (ChatAppMessage.sender == user_id) & (ChatAppMessage.receiver == friend_id) | (ChatAppMessage.sender == friend_id) & (ChatAppMessage.receiver == user_id)) if query.execute() >= 1: return True except Exception as Ex: pass return False
def read_message(receiver_id, sender, date): try: query = (ChatAppMessage.update({ ChatAppMessage.is_read: 1, ChatAppMessage.read_date: date }).where(ChatAppMessage.receiver == receiver_id, ChatAppMessage.sender == sender)) if query.execute() >= 1: return True except ChatAppMessage.DoesNotExist: return False return False
def get_sender_message(receiver_id): try: _ret_data = (ChatAppUser.select( ChatAppUser.id, ChatAppUser.username).where( ChatAppUser.id.in_( ChatAppMessage.select( ChatAppMessage.sender.distinct()).where( (ChatAppMessage.receiver == receiver_id) & (ChatAppMessage.is_read == 0))))) return _ret_data except Exception as Ex: print(Ex)