Merge branch 'muc-presence' into 'master'

xep_0045: don't create empty item

See merge request poezio/slixmpp!51
This commit is contained in:
mathieui 2020-05-22 16:32:27 +02:00
commit bac1e9b44a

View file

@ -29,61 +29,61 @@ class MUCPresence(ElementBase):
affiliations = {'', } affiliations = {'', }
roles = {'', } roles = {'', }
def get_xml_item(self): def get_item_attr(self, attr, default):
item = self.xml.find('{http://jabber.org/protocol/muc#user}item')
if item is None:
return default
return item.get(attr)
def set_item_attr(self, attr, value):
item = self.xml.find('{http://jabber.org/protocol/muc#user}item') item = self.xml.find('{http://jabber.org/protocol/muc#user}item')
if item is None: if item is None:
item = ET.Element('{http://jabber.org/protocol/muc#user}item') item = ET.Element('{http://jabber.org/protocol/muc#user}item')
self.xml.append(item) self.xml.append(item)
item.attrib[attr] = value
return item return item
def del_item_attr(self, attr):
item = self.xml.find('{http://jabber.org/protocol/muc#user}item')
if item is not None and attr in item.attrib:
del item.attrib[attr]
def get_affiliation(self): def get_affiliation(self):
#TODO if no affilation, set it to the default and return default return self.get_item_attr('affiliation', '')
item = self.get_xml_item()
return item.get('affiliation', '')
def set_affiliation(self, value): def set_affiliation(self, value):
item = self.get_xml_item() self.set_item_attr('affiliation', value)
#TODO check for valid affiliation
item.attrib['affiliation'] = value
return self return self
def del_affiliation(self): def del_affiliation(self):
item = self.get_xml_item() # TODO: set default affiliation
#TODO set default affiliation self.del_item_attr('affiliation')
if 'affiliation' in item.attrib: del item.attrib['affiliation']
return self return self
def get_jid(self): def get_jid(self):
item = self.get_xml_item() return JID(self.get_item_attr('jid', ''))
return JID(item.get('jid', ''))
def set_jid(self, value): def set_jid(self, value):
item = self.get_xml_item()
if not isinstance(value, str): if not isinstance(value, str):
value = str(value) value = str(value)
item.attrib['jid'] = value self.set_item_attr('jid', value)
return self return self
def del_jid(self): def del_jid(self):
item = self.get_xml_item() self.del_item_attr('jid')
if 'jid' in item.attrib: del item.attrib['jid']
return self return self
def get_role(self): def get_role(self):
item = self.get_xml_item() return self.get_item_attr('role', '')
#TODO get default role, set default role if none
return item.get('role', '')
def set_role(self, value): def set_role(self, value):
item = self.get_xml_item() # TODO: check for valid role
#TODO check for valid role self.set_item_attr('role', value)
item.attrib['role'] = value
return self return self
def del_role(self): def del_role(self):
item = self.get_xml_item() # TODO: set default role
#TODO set default role self.del_item_attr('role')
if 'role' in item.attrib: del item.attrib['role']
return self return self
def get_nick(self): def get_nick(self):