def db_rm_blacklist(phone): """Removes a blacklisted phone from the database. Args: phone (str): Phone of the contact. """ DB.remove((CONTACT.phone == phone) & (CONTACT.blacklisted == True))
def db_rm_contact(name): """Remove a contact from the the database. Args: name (str): Name of the contact to remove. """ DB.remove(CONTACT.name == name.lower())
def db_toggle_bridge_by_wa(phone, toggle): result = DB.get((CONTACT.phone == phone)) if not result: return None DB.update({'enabled': toggle}, (CONTACT.phone == phone)) return toggle
def db_toggle_bridge_by_tg(group, toggle): result = DB.get((CONTACT.group == group)) if not result: return None DB.update({'enabled': toggle}, (CONTACT.group == group)) return toggle
def db_is_bridge_enabled_by_tg(group): result = DB.get((CONTACT.group == group)) if not result: return None return result.get('enabled')
def db_is_bridge_enabled_by_wa(phone): result = DB.get((CONTACT.phone == phone)) if not result: return None return result.get('enabled')
def db_get_group(contact): result = DB.get((CONTACT.name == contact.lower())) if not result: return None # return None for backward compatibility if there is no group column return result.get('group')
def db_add_blacklist(phone): """Add a new blacklisted phone to the database. Args: phone (str): Phone of the contact. Returns: ID of the inserted element. """ return DB.insert({'name': None, 'phone': phone, 'blacklisted': True})
def db_list_contacts(): """Obtain a list of contacts. The list contains tuples with (name, phone) Returns: List of tuples """ result = DB.search(CONTACT.blacklisted == False) return [(a['name'], a['phone'], a.get('group')) for a in result]
def get_blacklist(): """Obtain a list of blacklisted phones. Returns: List of strings. """ result = DB.search(CONTACT.blacklisted == True) if not result: return [] return [a['phone'] for a in result]
def get_contact(phone): """Get contact name from a phone number. Args: phone (str): Phone to search. Returns: String with the contact name or `None` if not found. """ result = DB.get((CONTACT.phone == phone) & (CONTACT.blacklisted == False)) if not result: return None return result['name']
def db_get_contact_by_group(group): """Get phone number from a group id. Args: group (int): Group id assigned to the contact. Returns: String with the phone number or `None` if not found. """ result = DB.get((CONTACT.group == group)) if not result: return None return result['name']
def is_blacklisted(phone): """Check if a phone number is blacklisted. Args: phone (str): Phone to check Returns: True or False """ result = DB.get((CONTACT.phone == phone) & (CONTACT.blacklisted == True)) if not result: return False return True
def db_add_contact(name, phone): """Add a new contact to the database. Args: name (str): Name to use for the contact. phone (str): Phone of the contact. Returns: ID of the inserted element. """ return DB.insert({ 'name': name.lower(), 'phone': phone, 'blacklisted': False })
def get_phone(contact): """Get phone number from a contact name. Args: contact (str): Name assigned to the contact Returns: String with the phone number or `None` if not found. """ result = DB.get((CONTACT.name == contact.lower()) & (CONTACT.blacklisted == False)) if not result: return None return result['phone']
def db_set_group(contact, group): DB.update({'group': group}, (CONTACT.name == contact.lower()))
def db_set_phone(contact, phone): DB.update({'phone': phone}, (CONTACT.name == contact.lower()))