From 1c449cee529953934253d444a11365cdae2cddaf Mon Sep 17 00:00:00 2001 From: Jun Tsai Date: Mon, 22 Jun 2020 21:17:18 +0800 Subject: [PATCH] #21 move wechaty-hostie to wechaty-puppet-hostie --- pom.xml | 4 +- report/pom.xml | 2 +- .../pom.xml | 2 +- .../scala/wechaty/hostie/Configuration.scala | 0 .../scala/wechaty/hostie/PuppetHostie.scala | 0 .../hostie/support/ContactRawSupport.scala | 0 .../support/ContactSelfRawSupport.scala | 0 .../hostie/support/FriendshipRawSupport.scala | 0 .../hostie/support/GrpcEventSupport.scala | 0 .../wechaty/hostie/support/GrpcSupport.scala | 0 .../hostie/support/MessageRawSupport.scala | 0 .../support/RoomInvitationRawSupport.scala | 0 .../hostie/support/RoomMemberRawSupport.scala | 0 .../hostie/support/RoomRawSupport.scala | 0 .../hostie/support/TagRawSupport.scala | 0 .../padplus/support/ContactRawSupport.scala | 9 ++- .../support/ContactSelfRawSupport.scala | 45 +++++------ .../padplus/support/GrpcEventSupport.scala | 77 +++++++++++++------ .../wechaty/padplus/support/GrpcSupport.scala | 31 ++++++++ .../padplus/support/LocalStoreSupport.scala | 3 + wechaty/pom.xml | 2 +- 21 files changed, 123 insertions(+), 52 deletions(-) rename {wechaty-hostie => wechaty-puppet-hostie}/pom.xml (94%) rename {wechaty-hostie => wechaty-puppet-hostie}/src/main/scala/wechaty/hostie/Configuration.scala (100%) rename {wechaty-hostie => wechaty-puppet-hostie}/src/main/scala/wechaty/hostie/PuppetHostie.scala (100%) rename {wechaty-hostie => wechaty-puppet-hostie}/src/main/scala/wechaty/hostie/support/ContactRawSupport.scala (100%) rename {wechaty-hostie => wechaty-puppet-hostie}/src/main/scala/wechaty/hostie/support/ContactSelfRawSupport.scala (100%) rename {wechaty-hostie => wechaty-puppet-hostie}/src/main/scala/wechaty/hostie/support/FriendshipRawSupport.scala (100%) rename {wechaty-hostie => wechaty-puppet-hostie}/src/main/scala/wechaty/hostie/support/GrpcEventSupport.scala (100%) rename {wechaty-hostie => wechaty-puppet-hostie}/src/main/scala/wechaty/hostie/support/GrpcSupport.scala (100%) rename {wechaty-hostie => wechaty-puppet-hostie}/src/main/scala/wechaty/hostie/support/MessageRawSupport.scala (100%) rename {wechaty-hostie => wechaty-puppet-hostie}/src/main/scala/wechaty/hostie/support/RoomInvitationRawSupport.scala (100%) rename {wechaty-hostie => wechaty-puppet-hostie}/src/main/scala/wechaty/hostie/support/RoomMemberRawSupport.scala (100%) rename {wechaty-hostie => wechaty-puppet-hostie}/src/main/scala/wechaty/hostie/support/RoomRawSupport.scala (100%) rename {wechaty-hostie => wechaty-puppet-hostie}/src/main/scala/wechaty/hostie/support/TagRawSupport.scala (100%) diff --git a/pom.xml b/pom.xml index 4bd0bbf3..fa0997df 100644 --- a/pom.xml +++ b/pom.xml @@ -17,7 +17,7 @@ wechaty wechaty-puppet - wechaty-hostie + wechaty-puppet-hostie wechaty-puppet-padplus report @@ -36,7 +36,7 @@ io.github.wechaty - wechaty-hostie_${scala-binary-version} + wechaty-puppet-hostie_${scala-binary-version} ${project.version} diff --git a/report/pom.xml b/report/pom.xml index 365b2565..177d9d1c 100644 --- a/report/pom.xml +++ b/report/pom.xml @@ -18,7 +18,7 @@ io.github.wechaty - wechaty-hostie_${scala-binary-version} + wechaty-puppet-hostie_${scala-binary-version} io.github.wechaty diff --git a/wechaty-hostie/pom.xml b/wechaty-puppet-hostie/pom.xml similarity index 94% rename from wechaty-hostie/pom.xml rename to wechaty-puppet-hostie/pom.xml index 4c37e79a..bbf420b9 100644 --- a/wechaty-hostie/pom.xml +++ b/wechaty-puppet-hostie/pom.xml @@ -7,7 +7,7 @@ 0.0.12-SNAPSHOT 4.0.0 - wechaty-hostie_2.12 + wechaty-puppet-hostie_2.12 jar diff --git a/wechaty-hostie/src/main/scala/wechaty/hostie/Configuration.scala b/wechaty-puppet-hostie/src/main/scala/wechaty/hostie/Configuration.scala similarity index 100% rename from wechaty-hostie/src/main/scala/wechaty/hostie/Configuration.scala rename to wechaty-puppet-hostie/src/main/scala/wechaty/hostie/Configuration.scala diff --git a/wechaty-hostie/src/main/scala/wechaty/hostie/PuppetHostie.scala b/wechaty-puppet-hostie/src/main/scala/wechaty/hostie/PuppetHostie.scala similarity index 100% rename from wechaty-hostie/src/main/scala/wechaty/hostie/PuppetHostie.scala rename to wechaty-puppet-hostie/src/main/scala/wechaty/hostie/PuppetHostie.scala diff --git a/wechaty-hostie/src/main/scala/wechaty/hostie/support/ContactRawSupport.scala b/wechaty-puppet-hostie/src/main/scala/wechaty/hostie/support/ContactRawSupport.scala similarity index 100% rename from wechaty-hostie/src/main/scala/wechaty/hostie/support/ContactRawSupport.scala rename to wechaty-puppet-hostie/src/main/scala/wechaty/hostie/support/ContactRawSupport.scala diff --git a/wechaty-hostie/src/main/scala/wechaty/hostie/support/ContactSelfRawSupport.scala b/wechaty-puppet-hostie/src/main/scala/wechaty/hostie/support/ContactSelfRawSupport.scala similarity index 100% rename from wechaty-hostie/src/main/scala/wechaty/hostie/support/ContactSelfRawSupport.scala rename to wechaty-puppet-hostie/src/main/scala/wechaty/hostie/support/ContactSelfRawSupport.scala diff --git a/wechaty-hostie/src/main/scala/wechaty/hostie/support/FriendshipRawSupport.scala b/wechaty-puppet-hostie/src/main/scala/wechaty/hostie/support/FriendshipRawSupport.scala similarity index 100% rename from wechaty-hostie/src/main/scala/wechaty/hostie/support/FriendshipRawSupport.scala rename to wechaty-puppet-hostie/src/main/scala/wechaty/hostie/support/FriendshipRawSupport.scala diff --git a/wechaty-hostie/src/main/scala/wechaty/hostie/support/GrpcEventSupport.scala b/wechaty-puppet-hostie/src/main/scala/wechaty/hostie/support/GrpcEventSupport.scala similarity index 100% rename from wechaty-hostie/src/main/scala/wechaty/hostie/support/GrpcEventSupport.scala rename to wechaty-puppet-hostie/src/main/scala/wechaty/hostie/support/GrpcEventSupport.scala diff --git a/wechaty-hostie/src/main/scala/wechaty/hostie/support/GrpcSupport.scala b/wechaty-puppet-hostie/src/main/scala/wechaty/hostie/support/GrpcSupport.scala similarity index 100% rename from wechaty-hostie/src/main/scala/wechaty/hostie/support/GrpcSupport.scala rename to wechaty-puppet-hostie/src/main/scala/wechaty/hostie/support/GrpcSupport.scala diff --git a/wechaty-hostie/src/main/scala/wechaty/hostie/support/MessageRawSupport.scala b/wechaty-puppet-hostie/src/main/scala/wechaty/hostie/support/MessageRawSupport.scala similarity index 100% rename from wechaty-hostie/src/main/scala/wechaty/hostie/support/MessageRawSupport.scala rename to wechaty-puppet-hostie/src/main/scala/wechaty/hostie/support/MessageRawSupport.scala diff --git a/wechaty-hostie/src/main/scala/wechaty/hostie/support/RoomInvitationRawSupport.scala b/wechaty-puppet-hostie/src/main/scala/wechaty/hostie/support/RoomInvitationRawSupport.scala similarity index 100% rename from wechaty-hostie/src/main/scala/wechaty/hostie/support/RoomInvitationRawSupport.scala rename to wechaty-puppet-hostie/src/main/scala/wechaty/hostie/support/RoomInvitationRawSupport.scala diff --git a/wechaty-hostie/src/main/scala/wechaty/hostie/support/RoomMemberRawSupport.scala b/wechaty-puppet-hostie/src/main/scala/wechaty/hostie/support/RoomMemberRawSupport.scala similarity index 100% rename from wechaty-hostie/src/main/scala/wechaty/hostie/support/RoomMemberRawSupport.scala rename to wechaty-puppet-hostie/src/main/scala/wechaty/hostie/support/RoomMemberRawSupport.scala diff --git a/wechaty-hostie/src/main/scala/wechaty/hostie/support/RoomRawSupport.scala b/wechaty-puppet-hostie/src/main/scala/wechaty/hostie/support/RoomRawSupport.scala similarity index 100% rename from wechaty-hostie/src/main/scala/wechaty/hostie/support/RoomRawSupport.scala rename to wechaty-puppet-hostie/src/main/scala/wechaty/hostie/support/RoomRawSupport.scala diff --git a/wechaty-hostie/src/main/scala/wechaty/hostie/support/TagRawSupport.scala b/wechaty-puppet-hostie/src/main/scala/wechaty/hostie/support/TagRawSupport.scala similarity index 100% rename from wechaty-hostie/src/main/scala/wechaty/hostie/support/TagRawSupport.scala rename to wechaty-puppet-hostie/src/main/scala/wechaty/hostie/support/TagRawSupport.scala diff --git a/wechaty-puppet-padplus/src/main/scala/wechaty/padplus/support/ContactRawSupport.scala b/wechaty-puppet-padplus/src/main/scala/wechaty/padplus/support/ContactRawSupport.scala index fbf3dec0..ee74bbf5 100644 --- a/wechaty-puppet-padplus/src/main/scala/wechaty/padplus/support/ContactRawSupport.scala +++ b/wechaty-puppet-padplus/src/main/scala/wechaty/padplus/support/ContactRawSupport.scala @@ -1,7 +1,8 @@ package wechaty.padplus.support +import wechaty.padplus.grpc.PadPlusServerOuterClass.ApiType import wechaty.puppet.ResourceBox -import wechaty.puppet.schemas.Contact +import wechaty.puppet.schemas.{Contact, Puppet} import wechaty.puppet.support.ContactSupport /** @@ -29,5 +30,9 @@ trait ContactRawSupport { /** * contact */ - override protected def contactRawPayload(contactId: String): Contact.ContactPayload = ??? + override protected def contactRawPayload(contactId: String): Contact.ContactPayload = { + val json = Puppet.objectMapper.createObjectNode() + json.put("userName",contactId) + val response = request[](ApiType.GET_CONTACT,Some(json.toString)) + } } diff --git a/wechaty-puppet-padplus/src/main/scala/wechaty/padplus/support/ContactSelfRawSupport.scala b/wechaty-puppet-padplus/src/main/scala/wechaty/padplus/support/ContactSelfRawSupport.scala index fce2ffc3..d8e6d9b9 100644 --- a/wechaty-puppet-padplus/src/main/scala/wechaty/padplus/support/ContactSelfRawSupport.scala +++ b/wechaty-puppet-padplus/src/main/scala/wechaty/padplus/support/ContactSelfRawSupport.scala @@ -26,27 +26,28 @@ trait ContactSelfRawSupport { override def logout(): Unit = ??? - def contactSelfInfo(): PadplusContactPayload ={ - val contactPayload:GetContactSelfInfoGrpcResponse=requestForObject(ApiType.GET_CONTACT_SELF_INFO) - val payload = new PadplusContactPayload - payload.alias = contactPayload.alias; - payload.bigHeadUrl = contactPayload.bigHeadImg; - payload.city = contactPayload.city - payload.contactFlag = 3 - payload.contactType = 0 - payload.country = contactPayload.country - payload.nickName = contactPayload.nickName - payload.province = contactPayload.province - payload.remark = "" - payload.sex = ContactGender(contactPayload.sex) - payload.signature = contactPayload.signature - payload.smallHeadUrl = contactPayload.smallHeadImg - payload.stranger = "" - payload.tagList = "" - payload.ticket = "" - payload.userName = contactPayload.userName - payload.verifyFlag = 0 - - payload + def contactSelfInfo(callback:PadplusContactPayload=>Unit):Unit={ + requestForCallback(ApiType.GET_CONTACT_SELF_INFO) { + contactPayload: GetContactSelfInfoGrpcResponse => + val payload = new PadplusContactPayload + payload.alias = contactPayload.alias; + payload.bigHeadUrl = contactPayload.bigHeadImg; + payload.city = contactPayload.city + payload.contactFlag = 3 + payload.contactType = 0 + payload.country = contactPayload.country + payload.nickName = contactPayload.nickName + payload.province = contactPayload.province + payload.remark = "" + payload.sex = ContactGender(contactPayload.sex) + payload.signature = contactPayload.signature + payload.smallHeadUrl = contactPayload.smallHeadImg + payload.stranger = "" + payload.tagList = "" + payload.ticket = "" + payload.userName = contactPayload.userName + payload.verifyFlag = 0 + callback(payload) + } } } diff --git a/wechaty-puppet-padplus/src/main/scala/wechaty/padplus/support/GrpcEventSupport.scala b/wechaty-puppet-padplus/src/main/scala/wechaty/padplus/support/GrpcEventSupport.scala index 4f0d6333..7567ea80 100644 --- a/wechaty-puppet-padplus/src/main/scala/wechaty/padplus/support/GrpcEventSupport.scala +++ b/wechaty-puppet-padplus/src/main/scala/wechaty/padplus/support/GrpcEventSupport.scala @@ -11,8 +11,8 @@ import io.grpc.stub.StreamObserver import javax.imageio.ImageIO import org.apache.commons.io.IOUtils import wechaty.padplus.PuppetPadplus -import wechaty.padplus.grpc.PadPlusServerOuterClass.{ResponseType, StreamResponse} -import wechaty.padplus.schemas.GrpcSchemas.GrpcMessagePayload +import wechaty.padplus.grpc.PadPlusServerOuterClass.{ApiType, ResponseType, StreamResponse} +import wechaty.padplus.schemas.GrpcSchemas.{GrpcMessagePayload, GrpcQrCodeLogin} import wechaty.padplus.schemas.ModelContact.PadplusContactPayload import wechaty.padplus.schemas.ModelUser.ScanData import wechaty.padplus.schemas.PadplusEnums.QrcodeStatus @@ -78,29 +78,53 @@ trait GrpcEventSupport extends StreamObserver[StreamResponse]{ eventMessagePayload.messageId = payload.MsgId saveRawMessagePayload(payload.MsgId,rawMessageStr) this.emit(PuppetEventName.MESSAGE, eventMessagePayload) + case ResponseType.QRCODE_LOGIN => + val loginData = objectMapper.readValue(response.getData,classOf[GrpcQrCodeLogin]) + val padplusContactPayload=new PadplusContactPayload + padplusContactPayload.alias=loginData.alias + padplusContactPayload.bigHeadUrl=loginData.headImgUrl + padplusContactPayload.nickName=loginData.nickName + padplusContactPayload.sex=ContactGender.Unknown + padplusContactPayload.userName=loginData.userName + padplusContactPayload.verifyFlag=0 + saveRawContactPayload(padplusContactPayload.userName,padplusContactPayload) + val eventLoginPayload = new EventLoginPayload + eventLoginPayload.contactId = padplusContactPayload.userName + emit(PuppetEventName.LOGIN, eventLoginPayload) + request(ApiType.GET_CONTACT, ) + + case ResponseType.AUTO_LOGIN => logger.debug("response data:{}",response.getData) val autoLoginData = objectMapper.readTree(response.getData) - val wechatUser = autoLoginData.get("wechatUser") - - val rawContactPayload = new PadplusContactPayload - if(wechatUser.has("alias")) - rawContactPayload.alias=wechatUser.get("alias").asText("") - rawContactPayload.bigHeadUrl = wechatUser.get("headImgUrl").asText() - rawContactPayload.nickName = wechatUser.get("nickName").asText() - rawContactPayload.sex = ContactGender.Unknown - rawContactPayload.userName = wechatUser.get("userName").asText() - saveRawContactPayload(rawContactPayload.userName,rawContactPayload) - // "{\"uin\":1213374243,\"online\":true,\"wechatUser\":{\"headImgUrl\":\"http://wx.qlogo.cn/mmhead/ver_1/iag5D2R2U9ibgTW2eh7XUbPTHqpEMP2DhSpXSBeQYzEPWgEmLIx5IDibwicGh4fTh4IibkL4hNianoiaTzXmVORnm1O4ZjhxfPosKzkMPSwic8Iicylk/0\",\"nickName\":\"\351\230\277\350\224\241\",\"uin\":1213374243,\"userName\":\"wxid_gbk03zsepqny22\",\"alias\":\"\",\"verifyFlag\":0}}" - selfId = Some(rawContactPayload.userName) - -// val padplusContact = contactSelfInfo() -// selfId = Some(padplusContact.userName) -// logger.debug("contactSelf:{}",padplusContact) -// saveRawContactPayload(padplusContact.userName,padplusContact) - val eventLoginPayload = new EventLoginPayload - eventLoginPayload.contactId = rawContactPayload.userName - emit(PuppetEventName.LOGIN,eventLoginPayload) + if(autoLoginData.get("online").asBoolean()) { + val wechatUser = autoLoginData.get("wechatUser") + + if (wechatUser != null) { + val rawContactPayload = new PadplusContactPayload + if (wechatUser.has("alias")) + rawContactPayload.alias = wechatUser.get("alias").asText("") + rawContactPayload.bigHeadUrl = wechatUser.get("headImgUrl").asText() + rawContactPayload.nickName = wechatUser.get("nickName").asText() + rawContactPayload.sex = ContactGender.Unknown + rawContactPayload.userName = wechatUser.get("userName").asText() + saveRawContactPayload(rawContactPayload.userName, rawContactPayload) + // "{\"uin\":1213374243,\"online\":true,\"wechatUser\":{\"headImgUrl\":\"http://wx.qlogo.cn/mmhead/ver_1/iag5D2R2U9ibgTW2eh7XUbPTHqpEMP2DhSpXSBeQYzEPWgEmLIx5IDibwicGh4fTh4IibkL4hNianoiaTzXmVORnm1O4ZjhxfPosKzkMPSwic8Iicylk/0\",\"nickName\":\"\351\230\277\350\224\241\",\"uin\":1213374243,\"userName\":\"wxid_gbk03zsepqny22\",\"alias\":\"\",\"verifyFlag\":0}}" + selfId = Some(rawContactPayload.userName) + } + + contactSelfInfo { padplusContact => + selfId = Some(padplusContact.userName) + logger.debug("contactSelf:{}", padplusContact) + saveRawContactPayload(padplusContact.userName, padplusContact) + val eventLoginPayload = new EventLoginPayload + eventLoginPayload.contactId = padplusContact.userName + emit(PuppetEventName.LOGIN, eventLoginPayload) + } + }else{ + deleteUin() + request(ApiType.GET_QRCODE) + } case _ => // val user = objectMapper.readTree(response.getData()) // val userName = user.get("userName").asText() @@ -116,7 +140,14 @@ trait GrpcEventSupport extends StreamObserver[StreamResponse]{ case QrcodeStatus.Scanned => case QrcodeStatus.Confirmed => - + contactSelfInfo { padplusContact => + selfId = Some(padplusContact.userName) + logger.debug("contactSelf:{}", padplusContact) + saveRawContactPayload(padplusContact.userName, padplusContact) + val eventLoginPayload = new EventLoginPayload + eventLoginPayload.contactId = padplusContact.userName + emit(PuppetEventName.LOGIN, eventLoginPayload) + } case QrcodeStatus.Canceled | QrcodeStatus.Expired=> } diff --git a/wechaty-puppet-padplus/src/main/scala/wechaty/padplus/support/GrpcSupport.scala b/wechaty-puppet-padplus/src/main/scala/wechaty/padplus/support/GrpcSupport.scala index cfedcda2..7ea9c63b 100644 --- a/wechaty-puppet-padplus/src/main/scala/wechaty/padplus/support/GrpcSupport.scala +++ b/wechaty-puppet-padplus/src/main/scala/wechaty/padplus/support/GrpcSupport.scala @@ -120,6 +120,37 @@ trait GrpcSupport { val response = request(apiType,data) Puppet.objectMapper.readValue(response.getResult,classTag.runtimeClass).asInstanceOf[T] } + protected def requestForCallback[T](apiType: ApiType,data:Option[Any]=None)(callback:T=>Unit)(implicit classTag: ClassTag[T]): ResponseObject ={ + val request = RequestObject.newBuilder() + request.setToken(option.token.get) + uinOpt match{ + case Some(id) => + request.setUin(id) + case _ => + } + request.setApiType(apiType) + data match{ + case Some(str:String) => + request.setParams(str) + case Some(d) => + request.setParams(Puppet.objectMapper.writeValueAsString(d)) + case _ => + } + val requestId = UUID.randomUUID().toString + request.setRequestId(requestId) + val traceId= UUID.randomUUID().toString + request.setTraceId(traceId) + logger.debug("request:{}",request.build()) + val callbackDelegate=(streamResponse:StreamResponse)=>{ + val obj=Puppet.objectMapper.readValue(streamResponse.getData,classTag.runtimeClass).asInstanceOf[T] + callback(obj) + } + //FIXME response timeout or not successful??? + callbackPool.put(traceId,callbackDelegate) + val response = grpcClient.request(request.build()) + logger.debug("request->response:{}",response) + response + } protected def request(apiType: ApiType,data:Option[Any]=None): ResponseObject ={ val request = RequestObject.newBuilder() request.setToken(option.token.get) diff --git a/wechaty-puppet-padplus/src/main/scala/wechaty/padplus/support/LocalStoreSupport.scala b/wechaty-puppet-padplus/src/main/scala/wechaty/padplus/support/LocalStoreSupport.scala index cf951c68..3c28a83d 100644 --- a/wechaty-puppet-padplus/src/main/scala/wechaty/padplus/support/LocalStoreSupport.scala +++ b/wechaty-puppet-padplus/src/main/scala/wechaty/padplus/support/LocalStoreSupport.scala @@ -26,6 +26,9 @@ trait LocalStoreSupport { protected def getUin: Option[String]={ store.get(uinKey).map(_.toStringUtf8) } + protected def deleteUin():Unit ={ + store.delete(uinKey) + } protected def saveRawContactPayload(contactId:String,payload:PadplusContactPayload): Unit ={ store.put(contactKeyFormat.format(contactId),objectMapper.writeValueAsString(payload)) } diff --git a/wechaty/pom.xml b/wechaty/pom.xml index 958d7a2a..13ae1f5d 100644 --- a/wechaty/pom.xml +++ b/wechaty/pom.xml @@ -14,7 +14,7 @@ io.github.wechaty - wechaty-hostie_${scala-binary-version} + wechaty-puppet-hostie_${scala-binary-version} io.github.wechaty