Move common part of Contact and GC_Contact into a new CommonContact class.
The CommonClass helps to spot easily where GC_Contacts and Contacts differ. This is just the first step a potential refactoring in this area.
This commit is contained in:
parent
976175e04c
commit
bd714f66fc
3 changed files with 78 additions and 84 deletions
|
@ -33,26 +33,17 @@ import common.gajim
|
||||||
from common.caps import NullClientCaps, FEATURE_BLACKLIST
|
from common.caps import NullClientCaps, FEATURE_BLACKLIST
|
||||||
|
|
||||||
|
|
||||||
class Contact(object):
|
class CommonContact(object):
|
||||||
'''Information concerning each contact'''
|
|
||||||
def __init__(self, jid='', name='', groups=[], show='', status='', sub='',
|
def __init__(self, jid, resource, show, status, name, our_chatstate,
|
||||||
ask='', resource='', priority=0, keyID='', client_caps=None, caps_cache=None,
|
composing_xep, chatstate, client_caps=None, caps_cache=None):
|
||||||
our_chatstate=None, chatstate=None, last_status_time=None, msg_id = None,
|
|
||||||
composing_xep = None, mood={}, tune={},
|
|
||||||
activity={}):
|
|
||||||
self.jid = jid
|
self.jid = jid
|
||||||
self.name = name
|
self.resource = resource
|
||||||
self.contact_name = '' # nick choosen by contact
|
|
||||||
self.groups = groups
|
|
||||||
self.show = show
|
self.show = show
|
||||||
self.status = status
|
self.status = status
|
||||||
self.sub = sub
|
self.name = name
|
||||||
self.ask = ask
|
|
||||||
self.resource = resource
|
|
||||||
self.priority = priority
|
|
||||||
self.keyID = keyID
|
|
||||||
|
|
||||||
# Entity Capabilities
|
|
||||||
self.client_caps = client_caps or NullClientCaps()
|
self.client_caps = client_caps or NullClientCaps()
|
||||||
self._caps_cache = caps_cache or common.gajim.capscache
|
self._caps_cache = caps_cache or common.gajim.capscache
|
||||||
|
|
||||||
|
@ -64,14 +55,70 @@ class Contact(object):
|
||||||
# 'ask' if we sent the first 'active' chatstate and are waiting for reply
|
# 'ask' if we sent the first 'active' chatstate and are waiting for reply
|
||||||
# this holds what WE SEND to contact (our current chatstate)
|
# this holds what WE SEND to contact (our current chatstate)
|
||||||
self.our_chatstate = our_chatstate
|
self.our_chatstate = our_chatstate
|
||||||
self.msg_id = msg_id
|
|
||||||
# tell which XEP we're using for composing state
|
# tell which XEP we're using for composing state
|
||||||
# None = have to ask, XEP-0022 = use this xep,
|
# None = have to ask, XEP-0022 = use this xep,
|
||||||
# XEP-0085 = use this xep, False = no composing support
|
# XEP-0085 = use this xep, False = no composing support
|
||||||
self.composing_xep = composing_xep
|
self.composing_xep = composing_xep
|
||||||
# this is contact's chatstate
|
# this is contact's chatstate
|
||||||
self.chatstate = chatstate
|
self.chatstate = chatstate
|
||||||
|
|
||||||
|
def get_full_jid(self):
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def get_shown_name(self):
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def supports(self, requested_feature):
|
||||||
|
'''
|
||||||
|
Returns True if the contact has advertised to support the feature
|
||||||
|
identified by the given namespace. False otherwise.
|
||||||
|
'''
|
||||||
|
if self.show == 'offline':
|
||||||
|
# Unfortunately, if all resources are offline, the contact
|
||||||
|
# includes the last resource that was online. Check for its
|
||||||
|
# show, so we can be sure it's existant. Otherwise, we still
|
||||||
|
# return caps for a contact that has no resources left.
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
return self._client_supports(requested_feature)
|
||||||
|
|
||||||
|
def _client_supports(self, requested_feature):
|
||||||
|
lookup_item = self.client_caps.get_cache_lookup_strategy()
|
||||||
|
cache_item = lookup_item(self._caps_cache)
|
||||||
|
|
||||||
|
supported_features = cache_item.features
|
||||||
|
if requested_feature in supported_features:
|
||||||
|
return True
|
||||||
|
elif supported_features == [] and cache_item.queried in (0, 1):
|
||||||
|
# assume feature is supported, if we don't know yet, what the client
|
||||||
|
# is capable of
|
||||||
|
return requested_feature not in FEATURE_BLACKLIST
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
class Contact(CommonContact):
|
||||||
|
'''Information concerning each contact'''
|
||||||
|
def __init__(self, jid='', name='', groups=[], show='', status='', sub='',
|
||||||
|
ask='', resource='', priority=0, keyID='', client_caps=None, caps_cache=None,
|
||||||
|
our_chatstate=None, chatstate=None, last_status_time=None, msg_id = None,
|
||||||
|
composing_xep=None, mood={}, tune={}, activity={}):
|
||||||
|
|
||||||
|
CommonContact.__init__(self, jid, resource, show, status, name,
|
||||||
|
our_chatstate, composing_xep, chatstate, client_caps=client_caps,
|
||||||
|
caps_cache=caps_cache)
|
||||||
|
|
||||||
|
self.contact_name = '' # nick choosen by contact
|
||||||
|
self.groups = groups
|
||||||
|
|
||||||
|
self.sub = sub
|
||||||
|
self.ask = ask
|
||||||
|
|
||||||
|
self.priority = priority
|
||||||
|
self.keyID = keyID
|
||||||
|
self.msg_id = msg_id
|
||||||
self.last_status_time = last_status_time
|
self.last_status_time = last_status_time
|
||||||
|
|
||||||
self.mood = mood.copy()
|
self.mood = mood.copy()
|
||||||
self.tune = tune.copy()
|
self.tune = tune.copy()
|
||||||
self.activity = activity.copy()
|
self.activity = activity.copy()
|
||||||
|
@ -136,51 +183,18 @@ class Contact(object):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def supports(self, requested_feature):
|
class GC_Contact(CommonContact):
|
||||||
if self.show == 'offline':
|
|
||||||
# Unfortunately, if all resources are offline, the contact
|
|
||||||
# includes the last resource that was online. Check for its
|
|
||||||
# show, so we can be sure it's existant. Otherwise, we still
|
|
||||||
# return caps for a contact that has no resources left.
|
|
||||||
return False
|
|
||||||
else:
|
|
||||||
return self._client_supports(requested_feature)
|
|
||||||
|
|
||||||
def _client_supports(self, requested_feature):
|
|
||||||
lookup = self.client_caps.get_cache_lookup_strategy()
|
|
||||||
cache_item = lookup(self._caps_cache)
|
|
||||||
|
|
||||||
supported_features = cache_item.features
|
|
||||||
if requested_feature in supported_features:
|
|
||||||
return True
|
|
||||||
elif supported_features == [] and cache_item.queried in (0, 1):
|
|
||||||
# assume feature is supported, if we don't know yet, what the client
|
|
||||||
# is capable of
|
|
||||||
return requested_feature not in FEATURE_BLACKLIST
|
|
||||||
else:
|
|
||||||
return False
|
|
||||||
|
|
||||||
class GC_Contact:
|
|
||||||
'''Information concerning each groupchat contact'''
|
'''Information concerning each groupchat contact'''
|
||||||
def __init__(self, room_jid='', name='', show='', status='', role='',
|
def __init__(self, room_jid='', name='', show='', status='', role='',
|
||||||
affiliation='', jid = '', resource = '', our_chatstate = None,
|
affiliation='', jid='', resource='', our_chatstate=None,
|
||||||
composing_xep = None, chatstate = None):
|
composing_xep=None, chatstate=None):
|
||||||
|
|
||||||
|
CommonContact.__init__(self, jid, resource, show, status, name,
|
||||||
|
our_chatstate, composing_xep, chatstate)
|
||||||
|
|
||||||
self.room_jid = room_jid
|
self.room_jid = room_jid
|
||||||
self.name = name
|
|
||||||
self.show = show
|
|
||||||
self.status = status
|
|
||||||
self.role = role
|
self.role = role
|
||||||
self.affiliation = affiliation
|
self.affiliation = affiliation
|
||||||
self.jid = jid
|
|
||||||
self.resource = resource
|
|
||||||
|
|
||||||
# Entity Capabilities
|
|
||||||
self.client_caps = NullClientCaps()
|
|
||||||
self._caps_cache = common.gajim.capscache
|
|
||||||
|
|
||||||
self.our_chatstate = our_chatstate
|
|
||||||
self.composing_xep = composing_xep
|
|
||||||
self.chatstate = chatstate
|
|
||||||
|
|
||||||
def get_full_jid(self):
|
def get_full_jid(self):
|
||||||
return self.room_jid + '/' + self.name
|
return self.room_jid + '/' + self.name
|
||||||
|
@ -188,29 +202,6 @@ class GC_Contact:
|
||||||
def get_shown_name(self):
|
def get_shown_name(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
def supports(self, requested_feature):
|
|
||||||
if self.show == 'offline':
|
|
||||||
# Unfortunately, if all resources are offline, the contact
|
|
||||||
# includes the last resource that was online. Check for its
|
|
||||||
# show, so we can be sure it's existant. Otherwise, we still
|
|
||||||
# return caps for a contact that has no resources left.
|
|
||||||
return False
|
|
||||||
else:
|
|
||||||
return self._client_supports(requested_feature)
|
|
||||||
|
|
||||||
def _client_supports(self, requested_feature):
|
|
||||||
lookup_item = self.client_caps.get_cache_lookup_strategy()
|
|
||||||
cache_item = lookup_item(self._caps_cache)
|
|
||||||
|
|
||||||
supported_features = cache_item.features
|
|
||||||
if requested_feature in supported_features:
|
|
||||||
return True
|
|
||||||
elif supported_features == [] and cache_item.queried in (0, 1):
|
|
||||||
# assume feature is supported, if we don't know yet, what the client
|
|
||||||
# is capable of
|
|
||||||
return requested_feature not in FEATURE_BLACKLIST
|
|
||||||
else:
|
|
||||||
return False
|
|
||||||
|
|
||||||
class Contacts:
|
class Contacts:
|
||||||
'''Information concerning all contacts and groupchat contacts'''
|
'''Information concerning all contacts and groupchat contacts'''
|
||||||
|
|
|
@ -40,6 +40,7 @@ modules = ( 'test_xmpp_dispatcher_nb',
|
||||||
'test_xmpp_transports_nb',
|
'test_xmpp_transports_nb',
|
||||||
'test_resolver',
|
'test_resolver',
|
||||||
'test_caps',
|
'test_caps',
|
||||||
|
'test_contacts',
|
||||||
)
|
)
|
||||||
#modules = ()
|
#modules = ()
|
||||||
|
|
||||||
|
|
|
@ -6,14 +6,16 @@ import unittest
|
||||||
import lib
|
import lib
|
||||||
lib.setup_env()
|
lib.setup_env()
|
||||||
|
|
||||||
from common.contacts import Contact, GC_Contact
|
from common.contacts import CommonContact, Contact, GC_Contact
|
||||||
from common.caps import NullClientCaps
|
from common.caps import NullClientCaps
|
||||||
from common.xmpp import NS_MUC
|
from common.xmpp import NS_MUC
|
||||||
|
|
||||||
class TestCommonContact(unittest.TestCase):
|
class TestCommonContact(unittest.TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.contact = Contact()
|
self.contact = CommonContact(jid='', resource='', show='', status='',
|
||||||
|
name='', our_chatstate=None, composing_xep=None, chatstate=None,
|
||||||
|
client_caps=None, caps_cache=None)
|
||||||
|
|
||||||
def test_default_client_supports(self):
|
def test_default_client_supports(self):
|
||||||
'''
|
'''
|
||||||
|
|
Loading…
Add table
Reference in a new issue