def _create(self):
        _key = DeviceUser.__tablename__ + ".uuid." + self._user_uuid
        _user = self._redis.hmget(_key, ["user_icon", "user_fullname"])
        self._user_icon = _user[0]
        self._user_fullname = _user[1]
        
        _conv_icon = self._user_icon

        if self._conv_type != CONVERSATION_TYPE.P2S and len(self._member_list) > 1:
            _group_users = copy.deepcopy(self._member_list)
            _group_users.append(self._user_uuid)
            _conv_icon = create_group_icon(self._redis, _group_users)

        self._return_icon = _conv_icon
        self._return_name = self._conv_name

        _list = self._member_list
        if self._conv_type == CONVERSATION_TYPE.P2S:
            if _list == None or len(_list) == 0:
                _list = self._get_conversation_user_list()

        if _list == None or len(_list) == 0:
            self._handler.setErrorCode(API_ERR.NO_CONVERSATION_MEMBER)
            return None

        _assigned_uuid = None
        if len(_list) == 1:
            _assigned_uuid = _list[0]
            
        _conv_uuid = str(uuid.uuid1())
        _values = {
            "uuid": _conv_uuid,
            "user_uuid": self._user_uuid,
            "assigned_uuid": _assigned_uuid,
            "app_uuid": self._app_uuid,
            "group_uuid": self._group_uuid,
            "conversation_name": self._conv_name,
            "conversation_icon": _conv_icon,
            "conversation_type": self._conv_type,
            "status": CONVERSATION_STATUS.NEW,
        }
        # create it
        _row = ConversationInfo(**_values)
        _row.async_add(self._redis)
        _row.create_redis_keys(self._redis)

        self._userdata(_values, copy.deepcopy(_list))

        return self._result_data(_values, _list)
    def _update(self, _conv_uuid, _assigned_uuid):
        _redis = self.application.redis
        _key = ConversationInfo.__tablename__ + ".uuid." + _conv_uuid

        if not _redis.exists(_key):
            logging.error("no such conversation: %s" % _conv_uuid)
            self.setErrorCode(API_ERR.NO_CONVERSATION)
            return

        _row = ConversationInfo(uuid=_conv_uuid, assigned_uuid=_assigned_uuid)
        _row.async_update()
        _row.update_redis_keys(_redis)

        _rdata = self.getReturnData()
        return
예제 #3
0
    def _create(self):
        _key = DeviceUser.__tablename__ + ".uuid." + self._user_uuid
        _user = self._redis.hmget(_key, ["user_icon", "user_fullname"])
        self._user_icon = _user[0]
        self._user_name = _user[1]

        self._peer_uuid = self._member_list[0]
        _key = DeviceUser.__tablename__ + ".uuid." + self._peer_uuid

        self._peer_icon = self._redis.hget(_key, "user_icon")
        self._peer_name = self._redis.hget(_key, "user_fullname")

        _conv_uuid = str(uuid.uuid1())
        _values = {
            "uuid": _conv_uuid,
            "user_uuid": self._user_uuid,
        }
        # create it
        _row = ConversationInfo(**_values)
        _row.async_add(self._redis)
        _row.create_redis_keys(self._redis)

        self._userdata(_values)

        return self._result_data(_values)
예제 #4
0
    def _create(self):
        _key = DeviceUser.__tablename__ + ".uuid." + self._user_uuid
        _user = self._redis.hmget(_key, ["user_icon", "user_fullname"])
        self._user_icon = _user[0]
        self._user_fullname = _user[1]

        _conv_icon = self._user_icon

        if self._conv_type != CONVERSATION_TYPE.P2S and len(
                self._member_list) > 1:
            _group_users = copy.deepcopy(self._member_list)
            _group_users.append(self._user_uuid)
            _conv_icon = create_group_icon(self._redis, _group_users)

        self._return_icon = _conv_icon
        self._return_name = self._conv_name

        _list = self._member_list
        if self._conv_type == CONVERSATION_TYPE.P2S:
            if _list == None or len(_list) == 0:
                _list = self._get_conversation_user_list()

        if _list == None or len(_list) == 0:
            self._handler.setErrorCode(API_ERR.NO_CONVERSATION_MEMBER)
            return None

        _assigned_uuid = None
        if len(_list) == 1:
            _assigned_uuid = _list[0]

        _conv_uuid = str(uuid.uuid1())
        _values = {
            "uuid": _conv_uuid,
            "user_uuid": self._user_uuid,
            "assigned_uuid": _assigned_uuid,
            "app_uuid": self._app_uuid,
            "group_uuid": self._group_uuid,
            "conversation_name": self._conv_name,
            "conversation_icon": _conv_icon,
            "conversation_type": self._conv_type,
            "status": CONVERSATION_STATUS.NEW,
        }
        # create it
        _row = ConversationInfo(**_values)
        _row.async_add()
        _row.create_redis_keys(self._redis)

        self._userdata(_values, copy.deepcopy(_list))

        return self._result_data(_values, _list)
    def _update(self, _conv_uuid, _assigned_uuid):
        _redis = self.application.redis
        _key = ConversationInfo.__tablename__ + ".uuid." + _conv_uuid

        if not _redis.exists(_key):
            logging.error("no such conversation: %s" % _conv_uuid)
            self.setErrorCode(API_ERR.NO_CONVERSATION)
            return

        _row = ConversationInfo(uuid=_conv_uuid, assigned_uuid=_assigned_uuid)
        _row.async_update(_redis)
        _row.update_redis_keys(_redis)

        _rdata = self.getReturnData()
        return
예제 #6
0
    def save(self):
        _task = {
            "uuid": self._uuid,
            "app_uuid": self._app_uuid,
            "conversation_uuid": self._conversation_uuid,
            "conversation_type": self._conversation_type,
            "message_type": self._message_type,
            "message_subtype": self._message_subtype,
            "from_uuid": self._from_uuid,
            "from_type": self._from_type,
            "from_device_uuid": self._from_device_uuid,
            "to_uuid": self._to_uuid,
            "to_type": self._to_type,
            "body": self._message_body,
            "task_status": TASK_STATUS.PENDING,
        }
        _row = MessagePushTask(**_task)
        _row.async_add()
        _row.create_redis_keys(self._redis)

        _row = ConversationInfo(uuid=self._conversation_uuid,
                                status=CONVERSATION_STATUS.OPEN,
                                latest_task=self._uuid)
        _row.async_update()
        _row.update_redis_keys(self._redis)

        _m = {"task_uuid": self._uuid}
        self._redis.rpush(REDIS_DISPATCHER_NOTIFICATION_KEY, json.dumps(_m))

        _key = ConversationUserData.__tablename__ + ".conversation_uuid." + self._conversation_uuid + ".datas"
        _datas = self._redis.smembers(_key)
        for _data_uuid in _datas:
            _row = ConversationUserData(
                uuid=_data_uuid, conversation_status=CONVERSATION_STATUS.OPEN)
            _row.async_update()
            _row.update_redis_keys(self._redis)

        # for message routing algorithm
        self._user_latest_send_message_time()
        return
    def _create(self, _member_uuid, _request):
        _app_uuid = _request.get("app_uuid")
        _user_uuid = _request.get("user_uuid")
        _redis = self.application.redis

        _key = DeviceUser.__tablename__ + ".uuid." + _user_uuid
        _portal_user_name = _redis.hget(_key, "user_fullname")
        _portal_user_icon = _redis.hget(_key, "user_icon")

        _key = DeviceUser.__tablename__ + ".uuid." + _member_uuid
        _member_user_name = _redis.hget(_key, "user_fullname")
        _member_user_icon = _redis.hget(_key, "user_icon")
        
        _conversation_uuid = str(uuid.uuid1())
        _values = {
            "uuid": _conversation_uuid,
            "app_uuid": _app_uuid,
            "user_uuid": _user_uuid,
            "assigned_uuid": _member_uuid,
            "conversation_type": CONVERSATION_TYPE.P2S,
            "status": CONVERSATION_STATUS.NEW,
        }
        # create it
        _row = ConversationInfo(**_values)
        _row.async_add(_redis)
        _row.create_redis_keys(_redis)

        _row = ConversationUserData(uuid=str(uuid.uuid1()),
                                    app_uuid=_app_uuid,
                                    user_uuid=_user_uuid,
                                    conversation_uuid=_conversation_uuid,
                                    conversation_type=CONVERSATION_TYPE.P2S,
                                    conversation_name=_member_user_name,
                                    conversation_icon=_member_user_icon,
                                    conversation_status=CONVERSATION_STATUS.NEW)
        _row.async_add(_redis)
        _row.create_redis_keys(_redis)

        _row = ConversationUserData(uuid=str(uuid.uuid1()),
                                    app_uuid=_app_uuid,
                                    user_uuid=_member_uuid,
                                    conversation_uuid=_conversation_uuid,
                                    conversation_type=CONVERSATION_TYPE.P2S,
                                    conversation_name=_portal_user_name,
                                    conversation_icon=_portal_user_icon,
                                    conversation_status=CONVERSATION_STATUS.NEW)
        _row.async_add(_redis)
        _row.create_redis_keys(_redis)

        self._return(_conversation_uuid, _request)
        return
예제 #8
0
    def _Task(self):
        super(ForwardMessageHandler, self)._Task()
        _request = json.loads(self.request.body)

        logging.info("ForwardMessage with %s." % (str(_request)))

        self.task_id = 0
        if "from_uuid" not in _request \
           or "to_type" not in _request \
           or "to_uuid" not in _request \
           or "task_uuid" not in _request:
            logging.error("send message failed for input.")
            self.setErrorCode(API_ERR.NO_PARA)
            return

        _old = redis_hash_to_dict(self.application.redis, MessagePushTask, _request["task_uuid"])
        if _old is None:
            logging.error("Error for no task uuid=%s." % (_request["task_uuid"]))
            self.setErrorCode(API_ERR.NO_TASK)
            return

        _from_uuid = _request.get("from_uuid")
        _conversation_uuid = _request.get("to_uuid")
        _uuid = str(uuid.uuid1())
        _now_datetime = datetime.datetime.now()
        _now = _now_datetime.strftime("%Y-%m-%d %H:%M:%S %f")

        _task = {
            "uuid": _uuid,
            "conversation_uuid": _conversation_uuid,
            "app_uuid": self.app_uuid,
            "message_type": _old.message_type,
            "message_subtype": _old.message_subtype,
            "from_uuid": _from_uuid,
            "from_type": YVOBJECT.DU,
            "from_device_uuid": _request["device_uuid"],
            "to_uuid": _conversation_uuid,
            "to_type": _request["to_type"],
            "body": _old.body,
            "task_status": TASK_STATUS.PENDING,
        }

        _row = MessagePushTask(**_task)
        _row.async_add(self.application.redis)
        _row.create_redis_keys(self.application.redis)

        _row = ConversationInfo(uuid=_conversation_uuid, lastest_task=_uuid)
        _row.async_update(self.application.redis)
        _row.update_redis_keys(self.application.redis)
        
        _m = {"task_uuid": _uuid}
        self.application.redis.rpush(REDIS_DISPATCHER_NOTIFICATION_KEY, json.dumps(_m))
        
        #link file
        if _old.message_subtype == MESSAGE_SUBTYPE.FILE:
            self._check_link_file(_old, _from_uuid)
        
        _rdata = self.getReturnData()
        _rdata["ts"] = int(time.mktime(_now_datetime.timetuple()))
        _rdata["ts"] = round(_rdata["ts"])
        _rdata["task"] = _uuid
        return
예제 #9
0
    def _smart(self, _app_uuid, _user_uuid, _device_uuid):
        _key = AppUserData.__tablename__ + ".app_uuid." + _app_uuid + \
               ".is_service_user.True"
        _service_users = self.redis.smembers(_key)
        if _service_users == None or len(_service_users) == 0:
            self._ack_error(_device_uuid, DIS_ERR.CONVERSATION_NO_USER)
            return

        _user_hashs = {}
        for _user in _service_users:
            _user_hashs[_user] = redis_hash_to_dict(self.redis, DeviceUser, _user)
            
        _device_names = ["mobile_device_uuid", "browser_device_uuid"]

        _online_users = []
        for _user in _service_users:
            _user_hash = _user_hashs[_user]
            _online_devices = []
            for _device_name in _device_names:
                _user_device_uuid = _user_hash[_device_name]
                if _user_device_uuid == None or len(_user_device_uuid) == 0:
                    continue
                _key = DeviceInfo.__tablename__ + ".uuid." + _user_device_uuid
                if self.redis.hget(_key, "device_is_online") == "True":
                    _online_devices.append(_user_device_uuid)
            if len(_online_devices) > 0:
                _online_users.append(_user)
                
        if len(_online_users) == 0:
            return self._group(_app_uuid, _user_uuid, _device_uuid)

        _allocated_user = random.randint(0, len(_online_users)-1)
        _allocated_user = _online_users[_allocated_user]

        _key = DeviceUser.__tablename__ + ".uuid." + _allocated_user
        _allocated_user_name = _user_hashs[_allocated_user]["user_fullname"]
        _allocated_user_icon = _user_hashs[_allocated_user]["user_icon"]
        
        _key = DeviceUser.__tablename__ + ".uuid." + _user_uuid
        _portal_user_name = self.redis.hget(_key, "user_fullname")
        _portal_user_icon = self.redis.hget(_key, "user_icon")
        
        _conversation_uuid = str(uuid.uuid1())
        _row = ConversationInfo(uuid=_conversation_uuid,
                               app_uuid=_app_uuid,
                               user_uuid=_user_uuid,
                               status=CONVERSATION_STATUS.NEW,
                               conversation_type=CONVERSATION_TYPE.P2S)
        _row.async_add(self.redis)
        _row.create_redis_keys(self.redis)
        
        _row = ConversationUserData(uuid=str(uuid.uuid1()),
                                    app_uuid=_app_uuid,
                                    user_uuid=_user_uuid,
                                    conversation_uuid=_conversation_uuid,
                                    conversation_name=_allocated_user_name,
                                    conversation_icon=_allocated_user_icon,
                                    conversation_status=CONVERSATION_STATUS.NEW)
        _row.async_add(self.redis)
        _row.create_redis_keys(self.redis)

        _row = ConversationUserData(uuid=str(uuid.uuid1()),
                                    app_uuid=_app_uuid,
                                    user_uuid=_allocated_user,
                                    conversation_uuid=_conversation_uuid,
                                    conversation_name=_portal_user_name,
                                    conversation_icon=_portal_user_icon,
                                    conversation_status=CONVERSATION_STATUS.NEW)
        _row.async_add(self.redis)
        _row.create_redis_keys(self.redis)
        
        self._ack_success(_device_uuid, _conversation_uuid)
        return
예제 #10
0
    def _all(self, _app_uuid, _user_uuid, _device_uuid):
        _key = AppUserData.__tablename__ + ".app_uuid." + _app_uuid + \
               ".is_service_user.True"
        _allocated_users = self.redis.smembers(_key)
        if _allocated_users == None or len(_allocated_users) == 0:
            self._ack_error(_device_uuid, DIS_ERR.CONVERSATION_NO_USER)
            return

        _key = DeviceUser.__tablename__ + ".uuid." + _user_uuid
        _portal_user_name = self.redis.hget(_key, "user_fullname")
        _portal_user_icon = self.redis.hget(_key, "user_icon")
        
        _group_icon = create_group_icon(self.redis, _allocated_users)
        
        _conversation_uuid = str(uuid.uuid1())
        _row = ConversationInfo(uuid=_conversation_uuid,
                               app_uuid=_app_uuid,
                               user_uuid=_user_uuid,
                               status=CONVERSATION_STATUS.NEW,
                               conversation_type=CONVERSATION_TYPE.P2S)
        _row.async_add(self.redis)
        _row.create_redis_keys(self.redis)

        _conversation_name = []
        for _user in _allocated_users:
            _key = DeviceUser.__tablename__ + ".uuid." + _user
            _conversation_name.append(self.redis.hget(_key, "user_fullname"))
        _conversation_name = ",".join(_conversation_name)
        
        _row = ConversationUserData(uuid=str(uuid.uuid1()),
                                    app_uuid=_app_uuid,
                                    user_uuid=_user_uuid,
                                    conversation_uuid=_conversation_uuid,
                                    conversation_name=_conversation_name,
                                    conversation_icon=_group_icon,
                                    conversation_status=CONVERSATION_STATUS.NEW)
        _row.async_add(self.redis)
        _row.create_redis_keys(self.redis)

        for _user in _allocated_users:
            _row = ConversationUserData(uuid=str(uuid.uuid1()),
                                        app_uuid=_app_uuid,
                                        user_uuid=_user,
                                        conversation_uuid=_conversation_uuid,
                                        conversation_name=_portal_user_name,
                                        conversation_icon=_portal_user_icon,
                                        conversation_status=CONVERSATION_STATUS.NEW)
            _row.async_add(self.redis)
            _row.create_redis_keys(self.redis)
        
        self._ack_success(_device_uuid, _conversation_uuid)
        return
예제 #11
0
    def _group(self, _app_uuid, _user_uuid, _device_uuid, _group_uuid):
        if _group_uuid == None:
            # find the primary group
            _key = OrgGroup.__tablename__ + ".app_uuid." + _app_uuid + \
                   ".is_distributor.True"
            _group_uuid = self.redis.get(_key)

        if _group_uuid == None:
            logging.error("no distributor group in app: %s" % _app_uuid)
            self._ack_error(_device_uuid, DIS_ERR.CONVERSATION_NO_GROUP)
            return True

        _key = OrgGroupUserData.__tablename__ + ".group_uuid." + _group_uuid
        _users = self.redis.smembers(_key)
        if _users == None or len(_users) == 0:
            logging.error("no service user in group: %s" % _group_uuid)
            self._ack_error(_device_uuid, DIS_ERR.CONVERSATION_NO_USER)
            return True

        _ready_users = []
        for _user in _users:
            _key = DeviceUser.__tablename__ + ".uuid." + _user
            _service_user_status = self.redis.hget(_key, "service_user_status")
            if _service_user_status == SERVICE_USER_STATUS.READY:
                _ready_users.append(_user)

        if len(_ready_users) == 0:
            logging.info("waiting group user ready: %s" % _group_uuid)
            self._ack_waiting(_device_uuid)
            return False

        _allocated_user = random.randint(0, len(_ready_users)-1)
        _allocated_user = list(_users)[_allocated_user]

        _allocated_user_dict = redis_hash_to_dict(self.redis, DeviceUser, _allocated_user)
        _portal_user_dict = redis_hash_to_dict(self.redis, DeviceUser, _user_uuid)
        
        _conversation_uuid = str(uuid.uuid1())
        _row = ConversationInfo(uuid=_conversation_uuid,
                                app_uuid=_app_uuid,
                                group_uuid=_group_uuid,
                                user_uuid=_user_uuid,
                                assigned_uuid=_allocated_user,
                                status=CONVERSATION_STATUS.NEW,
                                conversation_type=CONVERSATION_TYPE.P2S)

        _row.async_add(self.redis)
        _row.create_redis_keys(self.redis)
        
        _row = ConversationUserData(uuid=str(uuid.uuid1()),
                                    app_uuid=_app_uuid,
                                    user_uuid=_user_uuid,
                                    conversation_uuid=_conversation_uuid,
                                    conversation_name=_allocated_user_dict["user_fullname"],
                                    conversation_icon=_allocated_user_dict["user_icon"],
                                    conversation_status=CONVERSATION_STATUS.NEW)
        _row.async_add(self.redis)
        _row.create_redis_keys(self.redis)

        _row = ConversationUserData(uuid=str(uuid.uuid1()),
                                    app_uuid=_app_uuid,
                                    user_uuid=_allocated_user,
                                    conversation_uuid=_conversation_uuid,
                                    conversation_name=_portal_user_dict["user_fullname"],
                                    conversation_icon=_portal_user_dict["user_icon"],
                                    conversation_status=CONVERSATION_STATUS.NEW)
        _row.async_add(self.redis)
        _row.create_redis_keys(self.redis)
        
        self._ack_success(_device_uuid, _conversation_uuid)
        return True
예제 #12
0
    def _new_conversation(self):
        _service_users = DeviceUser.__tablename__ + ".is_service_user.True"
        _service_users = self.application.redis.smembers(_service_users)

        if not _service_users:
            logging.error("no service user ???????")
            return

        _service_users = list(_service_users)
        _peer_uuid = _service_users[0]
        if len(_service_users) != 1:
            _peer_uuid = None

        _key = DeviceUser.__tablename__ + ".uuid." + self._user_uuid
        _portal_user_name = self.application.redis.hget(_key, "user_fullname")
        _portal_user_icon = self.application.redis.hget(_key, "user_icon")

        _conversation_uuid = str(uuid.uuid1())
        _row = ConversationInfo(uuid=_conversation_uuid,
                                user_uuid=self._user_uuid)
        _row.async_add(self.application.redis)
        _row.create_redis_keys(self.application.redis)

        _conversation_name = []
        for _user in _service_users:
            _key = DeviceUser.__tablename__ + ".uuid." + _user
            _name = self.application.redis.hget(_key, "user_fullname")
            if not _name:
                continue
            _conversation_name.append(_name)
        _conversation_name = ",".join(_conversation_name)

        _row = ConversationUserData(
            uuid=str(uuid.uuid1()),
            user_uuid=self._user_uuid,
            peer_uuid=_peer_uuid,
            conversation_uuid=_conversation_uuid,
            conversation_name=_conversation_name,
            conversation_type=CONVERSATION_TYPE.P2S,
            conversation_status=CONVERSATION_STATUS.NEW)
        _row.async_add(self.application.redis)
        _row.create_redis_keys(self.application.redis)

        for _user in _service_users:
            _row = ConversationUserData(
                uuid=str(uuid.uuid1()),
                user_uuid=_user,
                peer_uuid=self._user_uuid,
                conversation_uuid=_conversation_uuid,
                conversation_name=_portal_user_name,
                conversation_icon=_portal_user_icon,
                conversation_type=CONVERSATION_TYPE.S2P,
                conversation_status=CONVERSATION_STATUS.NEW)
            _row.async_add(self.application.redis)
            _row.create_redis_keys(self.application.redis)
        return
예제 #13
0
    def _group(self, _app_uuid, _user_uuid, _device_uuid, _group_uuid):
        if _group_uuid == None:
            # find the primary group
            _key = OrgGroup.__tablename__ + ".app_uuid." + _app_uuid + \
                   ".is_distributor.True"
            _group_uuid = self.redis.get(_key)

        if _group_uuid == None:
            logging.error("no distributor group in app: %s" % _app_uuid)
            self._ack_error(_device_uuid, DIS_ERR.CONVERSATION_NO_GROUP)
            return True

        _key = OrgUserGroupData.__tablename__ + ".group_uuid." + _group_uuid
        _users = self.redis.smembers(_key)
        if _users == None or len(_users) == 0:
            logging.error("no service user in group: %s" % _group_uuid)
            self._ack_error(_device_uuid, DIS_ERR.CONVERSATION_NO_USER)
            return True

        _ready_users = []
        for _user in _users:
            _key = DeviceUser.__tablename__ + ".uuid." + _user
            _service_user_status = self.redis.hget(_key, "service_user_status")
            if _service_user_status == SERVICE_USER_STATUS.READY:
                _ready_users.append(_user)

        if len(_ready_users) == 0:
            logging.info("waiting group user ready: %s" % _group_uuid)
            self._ack_waiting(_device_uuid)
            self._back_to_queue(_app_uuid, _user_uuid, _device_uuid,
                                _group_uuid)
            return False

        _allocated_user = random.randint(0, len(_ready_users) - 1)
        _allocated_user = list(_users)[_allocated_user]

        _allocated_user_dict = redis_hash_to_dict(self.redis, DeviceUser,
                                                  _allocated_user)
        _portal_user_dict = redis_hash_to_dict(self.redis, DeviceUser,
                                               _user_uuid)

        _conversation_uuid = str(uuid.uuid1())
        _row = ConversationInfo(uuid=_conversation_uuid,
                                app_uuid=_app_uuid,
                                group_uuid=_group_uuid,
                                user_uuid=_user_uuid,
                                assigned_uuid=_allocated_user,
                                status=CONVERSATION_STATUS.NEW,
                                conversation_type=CONVERSATION_TYPE.P2S)

        _row.async_add()
        _row.create_redis_keys(self.redis)

        _row = ConversationUserData(
            uuid=str(uuid.uuid1()),
            app_uuid=_app_uuid,
            user_uuid=_user_uuid,
            conversation_uuid=_conversation_uuid,
            conversation_name=_allocated_user_dict["user_fullname"],
            conversation_icon=_allocated_user_dict["user_icon"],
            conversation_status=CONVERSATION_STATUS.NEW)
        _row.async_add()
        _row.create_redis_keys(self.redis)

        _row = ConversationUserData(
            uuid=str(uuid.uuid1()),
            app_uuid=_app_uuid,
            user_uuid=_allocated_user,
            conversation_uuid=_conversation_uuid,
            conversation_name=_portal_user_dict["user_fullname"],
            conversation_icon=_portal_user_dict["user_icon"],
            conversation_status=CONVERSATION_STATUS.NEW)
        _row.async_add()
        _row.create_redis_keys(self.redis)

        self._ack_success(_device_uuid, _conversation_uuid)
        return True
 def _update_group(self):
     _row = ConversationInfo(uuid=self._conv.get("uuid"), group_uuid=self._group_uuid)
     _row.async_update()
     _row.update_redis_keys(self.application.redis)
     return
    def _new_conversation(self):
        _service_users = DeviceUser.__tablename__ + ".is_service_user.True"
        _service_users = self.application.redis.smembers(_service_users)

        if not _service_users:
            logging.error("no service user ???????")
            return

        _service_users = list(_service_users)
        _assigned_uuid = _service_users[0]

        _key = DeviceUser.__tablename__ + ".uuid." + self._user_uuid
        _portal_user_name = self.application.redis.hget(_key, "user_fullname")
        _portal_user_icon = self.application.redis.hget(_key, "user_icon")

        _conversation_uuid = str(uuid.uuid1())
        _row = ConversationInfo(
            uuid=_conversation_uuid,
            user_uuid=self._user_uuid
        )
        _row.async_add(self.application.redis)
        _row.create_redis_keys(self.application.redis)
        
        _conversation_name = []
        for _user in _service_users:
            _key = DeviceUser.__tablename__ + ".uuid." + _user
            _name = self.application.redis.hget(_key, "user_fullname")
            if not _name:
                continue
            _conversation_name.append(_name)
        _conversation_name = ",".join(_conversation_name)

        _row = ConversationUserData(uuid=str(uuid.uuid1()),
                                    user_uuid=self._user_uuid,
                                    peer_uuid=_assigned_uuid,
                                    conversation_uuid=_conversation_uuid,
                                    conversation_name=_conversation_name,
                                    conversation_type=CONVERSATION_TYPE.P2S,
                                    conversation_status=CONVERSATION_STATUS.NEW)
        _row.async_add(self.application.redis)
        _row.create_redis_keys(self.application.redis)

        
        for _user in _service_users:
            _row = ConversationUserData(uuid=str(uuid.uuid1()),
                                        user_uuid=_user,
                                        peer_uuid=self._user_uuid,
                                        conversation_uuid=_conversation_uuid,
                                        conversation_name=_portal_user_name,
                                        conversation_icon=_portal_user_icon,
                                        conversation_type=CONVERSATION_TYPE.S2P,
                                        conversation_status=CONVERSATION_STATUS.NEW)
            _row.async_add(self.application.redis)
            _row.create_redis_keys(self.application.redis)
        return
예제 #16
0
    def _create_conversation(self, _request, _rule, _target_services):
        _rule_uuid = None
        if _rule != None:
            _rule_uuid = _rule.get("uuid")

        _app_uuid = _request.get("app_uuid")
        _user_uuid = _request.get("user_uuid")

        _key = DeviceUser.__tablename__ + ".uuid." + _user_uuid
        _portal_user_name = self.redis.hget(_key, "user_fullname")
        _portal_user_icon = self.redis.hget(_key, "user_icon")

        _group_icon = create_group_icon(self.redis, _target_services)

        _assigned_uuid = None
        if len(_target_services) == 1:
            _assigned_uuid = _target_services[0]

        _conversation_uuid = str(uuid.uuid1())
        _row = ConversationInfo(uuid=_conversation_uuid,
                                app_uuid=_app_uuid,
                                user_uuid=_user_uuid,
                                assigned_uuid=_assigned_uuid,
                                rule_uuid=_rule_uuid,
                                status=CONVERSATION_STATUS.NEW,
                                conversation_type=CONVERSATION_TYPE.P2S)

        _row.async_add(self.redis)
        _row.create_redis_keys(self.redis)

        _conversation_name = []
        for _user in _target_services:
            _key = DeviceUser.__tablename__ + ".uuid." + _user
            _conversation_name.append(self.redis.hget(_key, "user_fullname"))
        _conversation_name = ",".join(_conversation_name)

        _row = ConversationUserData(
            uuid=str(uuid.uuid1()),
            app_uuid=_app_uuid,
            user_uuid=_user_uuid,
            conversation_uuid=_conversation_uuid,
            conversation_name=_conversation_name,
            conversation_icon=_group_icon,
            conversation_status=CONVERSATION_STATUS.NEW)
        _row.async_add(self.redis)
        _row.create_redis_keys(self.redis)

        for _user in _target_services:
            _row = ConversationUserData(
                uuid=str(uuid.uuid1()),
                app_uuid=_app_uuid,
                user_uuid=_user,
                conversation_uuid=_conversation_uuid,
                conversation_name=_portal_user_name,
                conversation_icon=_portal_user_icon,
                conversation_status=CONVERSATION_STATUS.NEW)
            _row.async_add(self.redis)
            _row.create_redis_keys(self.redis)

        return
예제 #17
0
    def _all(self, _app_uuid, _user_uuid, _device_uuid):
        _key = AppUserData.__tablename__ + ".app_uuid." + _app_uuid + \
               ".is_service_user.True"
        _allocated_users = self.redis.smembers(_key)
        if _allocated_users == None or len(_allocated_users) == 0:
            self._ack_error(_device_uuid, DIS_ERR.CONVERSATION_NO_USER)
            return

        _key = DeviceUser.__tablename__ + ".uuid." + _user_uuid
        _portal_user_name = self.redis.hget(_key, "user_fullname")
        _portal_user_icon = self.redis.hget(_key, "user_icon")

        _group_icon = create_group_icon(self.redis, _allocated_users)

        _conversation_uuid = str(uuid.uuid1())
        _row = ConversationInfo(uuid=_conversation_uuid,
                                app_uuid=_app_uuid,
                                user_uuid=_user_uuid,
                                status=CONVERSATION_STATUS.NEW,
                                conversation_type=CONVERSATION_TYPE.P2S)
        _row.async_add()
        _row.create_redis_keys(self.redis)

        _conversation_name = []
        for _user in _allocated_users:
            _key = DeviceUser.__tablename__ + ".uuid." + _user
            _conversation_name.append(self.redis.hget(_key, "user_fullname"))
        _conversation_name = ",".join(_conversation_name)

        _row = ConversationUserData(
            uuid=str(uuid.uuid1()),
            app_uuid=_app_uuid,
            user_uuid=_user_uuid,
            conversation_uuid=_conversation_uuid,
            conversation_name=_conversation_name,
            conversation_icon=_group_icon,
            conversation_status=CONVERSATION_STATUS.NEW)
        _row.async_add()
        _row.create_redis_keys(self.redis)

        for _user in _allocated_users:
            _row = ConversationUserData(
                uuid=str(uuid.uuid1()),
                app_uuid=_app_uuid,
                user_uuid=_user,
                conversation_uuid=_conversation_uuid,
                conversation_name=_portal_user_name,
                conversation_icon=_portal_user_icon,
                conversation_status=CONVERSATION_STATUS.NEW)
            _row.async_add()
            _row.create_redis_keys(self.redis)

        self._ack_success(_device_uuid, _conversation_uuid)
        return
예제 #18
0
파일: amd.py 프로젝트: GYGit/ppmessage
    def _create_conversation(self, _request, _rule, _target_services):
        _rule_uuid = None
        if _rule != None:
            _rule_uuid = _rule.get("uuid")
        
        _app_uuid = _request.get("app_uuid")
        _user_uuid = _request.get("user_uuid")
        
        _key = DeviceUser.__tablename__ + ".uuid." + _user_uuid
        _portal_user_name = self.redis.hget(_key, "user_fullname")
        _portal_user_icon = self.redis.hget(_key, "user_icon")
        
        _group_icon = create_group_icon(self.redis, _target_services)

        _assigned_uuid = None
        if len(_target_services) == 1:
            _assigned_uuid = _target_services[0]
            
        _conversation_uuid = str(uuid.uuid1())
        _row = ConversationInfo(uuid=_conversation_uuid,
                                app_uuid=_app_uuid,
                                user_uuid=_user_uuid,
                                assigned_uuid=_assigned_uuid,
                                rule_uuid=_rule_uuid,
                                status=CONVERSATION_STATUS.NEW,
                                conversation_type=CONVERSATION_TYPE.P2S)
        
        _row.async_add(self.redis)
        _row.create_redis_keys(self.redis)

        _conversation_name = []
        for _user in _target_services:
            _key = DeviceUser.__tablename__ + ".uuid." + _user
            _conversation_name.append(self.redis.hget(_key, "user_fullname"))
        _conversation_name = ",".join(_conversation_name)
        
        _row = ConversationUserData(uuid=str(uuid.uuid1()),
                                    app_uuid=_app_uuid,
                                    user_uuid=_user_uuid,
                                    conversation_uuid=_conversation_uuid,
                                    conversation_name=_conversation_name,
                                    conversation_icon=_group_icon,
                                    conversation_status=CONVERSATION_STATUS.NEW)
        _row.async_add(self.redis)
        _row.create_redis_keys(self.redis)
     
        for _user in _target_services:
            _row = ConversationUserData(uuid=str(uuid.uuid1()),
                                        app_uuid=_app_uuid,
                                        user_uuid=_user,
                                        conversation_uuid=_conversation_uuid,
                                        conversation_name=_portal_user_name,
                                        conversation_icon=_portal_user_icon,
                                        conversation_status=CONVERSATION_STATUS.NEW)
            _row.async_add(self.redis)
            _row.create_redis_keys(self.redis)

        return
예제 #19
0
    def _create(self, _member_uuid, _request):
        _user_uuid = _request.get("user_uuid")
        _redis = self.application.redis

        _key = DeviceUser.__tablename__ + ".uuid." + _user_uuid
        _portal_user_name = _redis.hget(_key, "user_fullname")
        _portal_user_icon = _redis.hget(_key, "user_icon")

        _key = DeviceUser.__tablename__ + ".uuid." + _member_uuid
        _member_user_name = _redis.hget(_key, "user_fullname")
        _member_user_icon = _redis.hget(_key, "user_icon")
        
        _conversation_uuid = str(uuid.uuid1())
        _values = {
            "uuid": _conversation_uuid,
            "user_uuid": _user_uuid
        }
        # create it
        _row = ConversationInfo(**_values)
        _row.async_add(_redis)
        _row.create_redis_keys(_redis)

        _row = ConversationUserData(uuid=str(uuid.uuid1()),
                                    user_uuid=_user_uuid,
                                    peer_uuid=_member_uuid,
                                    conversation_uuid=_conversation_uuid,
                                    conversation_type=CONVERSATION_TYPE.P2S,
                                    conversation_name=_member_user_name,
                                    conversation_icon=_member_user_icon,
                                    conversation_status=CONVERSATION_STATUS.NEW)
        _row.async_add(_redis)
        _row.create_redis_keys(_redis)

        _row = ConversationUserData(uuid=str(uuid.uuid1()),
                                    user_uuid=_member_uuid,
                                    peer_uuid=_user_uuid,
                                    conversation_uuid=_conversation_uuid,
                                    conversation_type=CONVERSATION_TYPE.S2P,
                                    conversation_name=_portal_user_name,
                                    conversation_icon=_portal_user_icon,
                                    conversation_status=CONVERSATION_STATUS.NEW)
        _row.async_add(_redis)
        _row.create_redis_keys(_redis)

        logging.info("return from new")
        return self._return(_conversation_uuid, _request)
예제 #20
0
    def _smart(self, _app_uuid, _user_uuid, _device_uuid):
        _key = AppUserData.__tablename__ + ".app_uuid." + _app_uuid + \
               ".is_service_user.True"
        _service_users = self.redis.smembers(_key)
        if _service_users == None or len(_service_users) == 0:
            self._ack_error(_device_uuid, DIS_ERR.CONVERSATION_NO_USER)
            return

        _user_hashs = {}
        for _user in _service_users:
            _user_hashs[_user] = redis_hash_to_dict(self.redis, DeviceUser,
                                                    _user)

        _device_names = ["mobile_device_uuid", "browser_device_uuid"]

        _online_users = []
        for _user in _service_users:
            _user_hash = _user_hashs[_user]
            _online_devices = []
            for _device_name in _device_names:
                _user_device_uuid = _user_hash[_device_name]
                if _user_device_uuid == None or len(_user_device_uuid) == 0:
                    continue
                _key = DeviceInfo.__tablename__ + ".uuid." + _user_device_uuid
                if self.redis.hget(_key, "device_is_online") == "True":
                    _online_devices.append(_user_device_uuid)
            if len(_online_devices) > 0:
                _online_users.append(_user)

        if len(_online_users) == 0:
            return self._group(_app_uuid, _user_uuid, _device_uuid)

        _allocated_user = random.randint(0, len(_online_users) - 1)
        _allocated_user = _online_users[_allocated_user]

        _key = DeviceUser.__tablename__ + ".uuid." + _allocated_user
        _allocated_user_name = _user_hashs[_allocated_user]["user_fullname"]
        _allocated_user_icon = _user_hashs[_allocated_user]["user_icon"]

        _key = DeviceUser.__tablename__ + ".uuid." + _user_uuid
        _portal_user_name = self.redis.hget(_key, "user_fullname")
        _portal_user_icon = self.redis.hget(_key, "user_icon")

        _conversation_uuid = str(uuid.uuid1())
        _row = ConversationInfo(uuid=_conversation_uuid,
                                app_uuid=_app_uuid,
                                user_uuid=_user_uuid,
                                status=CONVERSATION_STATUS.NEW,
                                conversation_type=CONVERSATION_TYPE.P2S)
        _row.async_add()
        _row.create_redis_keys(self.redis)

        _row = ConversationUserData(
            uuid=str(uuid.uuid1()),
            app_uuid=_app_uuid,
            user_uuid=_user_uuid,
            conversation_uuid=_conversation_uuid,
            conversation_name=_allocated_user_name,
            conversation_icon=_allocated_user_icon,
            conversation_status=CONVERSATION_STATUS.NEW)
        _row.async_add()
        _row.create_redis_keys(self.redis)

        _row = ConversationUserData(
            uuid=str(uuid.uuid1()),
            app_uuid=_app_uuid,
            user_uuid=_allocated_user,
            conversation_uuid=_conversation_uuid,
            conversation_name=_portal_user_name,
            conversation_icon=_portal_user_icon,
            conversation_status=CONVERSATION_STATUS.NEW)
        _row.async_add()
        _row.create_redis_keys(self.redis)

        self._ack_success(_device_uuid, _conversation_uuid)
        return
예제 #21
0
    def _create(self, _member_uuid, _request):
        _app_uuid = _request.get("app_uuid")
        _user_uuid = _request.get("user_uuid")
        _redis = self.application.redis

        _key = DeviceUser.__tablename__ + ".uuid." + _user_uuid
        _portal_user_name = _redis.hget(_key, "user_name")
        _portal_user_icon = _redis.hget(_key, "user_icon")

        _key = DeviceUser.__tablename__ + ".uuid." + _member_uuid
        _member_user_name = _redis.hget(_key, "user_name")
        _member_user_icon = _redis.hget(_key, "user_icon")

        _conversation_uuid = str(uuid.uuid1())
        _values = {
            "uuid": _conversation_uuid,
            "app_uuid": _app_uuid,
            "user_uuid": _user_uuid,
            "assigned_uuid": _member_uuid,
            "conversation_type": CONVERSATION_TYPE.P2S,
            "status": CONVERSATION_STATUS.NEW,
        }
        # create it
        _row = ConversationInfo(**_values)
        _row.async_add()
        _row.create_redis_keys(_redis)

        _row = ConversationUserData(
            uuid=str(uuid.uuid1()),
            app_uuid=_app_uuid,
            user_uuid=_user_uuid,
            conversation_uuid=_conversation_uuid,
            conversation_type=CONVERSATION_TYPE.P2S,
            conversation_name=_member_user_name,
            conversation_icon=_member_user_icon,
            conversation_status=CONVERSATION_STATUS.NEW)
        _row.async_add()
        _row.create_redis_keys(_redis)

        _row = ConversationUserData(
            uuid=str(uuid.uuid1()),
            app_uuid=_app_uuid,
            user_uuid=_member_uuid,
            conversation_uuid=_conversation_uuid,
            conversation_type=CONVERSATION_TYPE.P2S,
            conversation_name=_portal_user_name,
            conversation_icon=_portal_user_icon,
            conversation_status=CONVERSATION_STATUS.NEW)
        _row.async_add()
        _row.create_redis_keys(_redis)

        self._return(_conversation_uuid, _request)
        return
 def _update_group(self):
     _row = ConversationInfo(uuid=self._conv.get("uuid"),
                             group_uuid=self._group_uuid)
     _row.async_update()
     _row.update_redis_keys(self.application.redis)
     return