class GetMessagesController(Controller):
    def __init__(self):
            self.friendshipservice = KahlaFriendShipApiService()
            self.checkstatusservice = KahlaSignInStatusCheckService()
            self.conversionservice = KahlaConversationApiService()
            self.processmessage = ProcessMessage()

    # 定义参数
    def get_options(self):
	    return [
			Option('-u', '--username', dest='username'),
			Option('-t', '--take', dest='take')
	    ]

	# 处理输入参数, 检查合法性
    def run(self, username, take):
        # 这条必须编写, 并且带上传入的参数
        self.compute(username, take)

	# 处理业务逻辑
    def main(self, username, take):
        if self.checkstatusservice.check() == True:
            friends = self.friendshipservice.Friends()
            friendsdata = json.loads(friends.text)["items"]
            datas = []
            for x in friendsdata:
                if x['displayName'] == username:
                    me = json.loads(self.friendshipservice.Me().text)["value"]
                    r = self.conversionservice.GetMesssages(x['conversationId'], take)
                    resultdata = json.loads(r.text)
                    if resultdata["code"] == 0:
                        for xx in resultdata["items"]:
                            if x["discriminator"] == "PrivateConversation":
                                if xx["senderId"] != me["id"]:
                                    result = str(decrypt(bytes(xx["content"], "UTF-8"), bytes(x["aesKey"], "UTF-8")), "UTF-8")
                                    result = self.processmessage.processMessage(result)
                                    datas.append("{0} | {1}".format(x["displayName"], result))
                                else:
                                    result = str(decrypt(bytes(xx["content"], "UTF-8"), bytes(x["aesKey"], "UTF-8")), "UTF-8")
                                    result = self.processmessage.processMessage(result)
                                    datas.append("{0} | {1}".format(me["nickName"], result))                    
                            else:
                                data = json.loads(self.friendshipservice.UserDetail(xx['senderId']).text)["user"]
                                result = str(decrypt(bytes(xx["content"], "UTF-8"), bytes(x["aesKey"], "UTF-8")), "UTF-8")
                                result = self.processmessage.processMessage(result)
                                datas.append("{0} | {1} | {2}".format(x["displayName"], data["nickName"], result))
                        return datas
                    else:
                        return ["unknown error!"]
			
            return ["Your user name is incorrect!"]
        else:
            return ["You are not logged in!"]
    def received_message(self, message):
        self.processmessage = ProcessMessage()
        self.kfs = KahlaFriendShipApiService()
        me = self.kfs.Me()
        me = json.loads(me.text)["value"]
        messagedata = json.loads(str(message))
        if messagedata["typeDescription"] == "NewMessage":
            message = str(
                decrypt(bytes(messagedata["content"], "UTF-8"),
                        bytes(messagedata["aesKey"], "UTF-8")), "UTF-8")
            message = self.processmessage.processMessage(message)
            print("{0} | {1}".format(messagedata["sender"]["nickName"],
                                     message))
            return

        if messagedata["typeDescription"] == "WereDeletedEvent":
            print("The {0} deleted you from his friends.".format(
                messagedata["trigger"]["nickName"]))
            return

        if messagedata["typeDescription"] == "NewFriendRequestEvent":
            print("The {0} wants to be your friend.".format(
                messagedata["requester"]["nickName"]))
            return

        if messagedata["typeDescription"] == "FriendAcceptedEvent":
            print("The {0} agrees to your friend request.".format(
                messagedata["target"]["nickName"]))
            return

        if messagedata["typeDescription"] == "TimerUpdatedEvent":
            conversation = json.loads(
                self.conversionservice.ConversationDetail(
                    messagedata["conversationId"]).text)["value"]
            nikename = ""
            if conversation["requestUser"]["id"] != me["id"]:
                nikename = conversation["requestUser"]["nickName"]

            if conversation["targetUser"]["id"] != me["id"]:
                nikename = conversation["targetUser"]["nickName"]

            print(
                "The {0} modified the message self-destruct time of your session with him. The current self-destruct time is {1}"
                .format(nikename, second2time(messagedata["newTimer"])))
            return
 def __init__(self):
         self.friendshipservice = KahlaFriendShipApiService()
         self.checkstatusservice = KahlaSignInStatusCheckService()
         self.conversionservice = KahlaConversationApiService()
         self.processmessage = ProcessMessage()
示例#4
0
 def __init__(self):
     self.friendshipservice = FriendShipApiService()
     self.conversionservice = ConversationApiService()
     self.authservice = AuthApiService()
     self.processmessage = ProcessMessage()