Skip to content

Commit

Permalink
#21 move wechaty-hostie to wechaty-puppet-hostie
Browse files Browse the repository at this point in the history
  • Loading branch information
jcai committed Jun 22, 2020
1 parent 86db3f4 commit 1c449ce
Show file tree
Hide file tree
Showing 21 changed files with 123 additions and 52 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<modules>
<module>wechaty</module>
<module>wechaty-puppet</module>
<module>wechaty-hostie</module>
<module>wechaty-puppet-hostie</module>
<module>wechaty-puppet-padplus</module>
<module>report</module>
</modules>
Expand All @@ -36,7 +36,7 @@
</dependency>
<dependency>
<groupId>io.github.wechaty</groupId>
<artifactId>wechaty-hostie_${scala-binary-version}</artifactId>
<artifactId>wechaty-puppet-hostie_${scala-binary-version}</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
Expand Down
2 changes: 1 addition & 1 deletion report/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</dependency>
<dependency>
<groupId>io.github.wechaty</groupId>
<artifactId>wechaty-hostie_${scala-binary-version}</artifactId>
<artifactId>wechaty-puppet-hostie_${scala-binary-version}</artifactId>
</dependency>
<dependency>
<groupId>io.github.wechaty</groupId>
Expand Down
2 changes: 1 addition & 1 deletion wechaty-hostie/pom.xml → wechaty-puppet-hostie/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<version>0.0.12-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>wechaty-hostie_2.12</artifactId>
<artifactId>wechaty-puppet-hostie_2.12</artifactId>
<packaging>jar</packaging>


Expand Down
Original file line number Diff line number Diff line change
@@ -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

/**
Expand Down Expand Up @@ -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))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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=>

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
2 changes: 1 addition & 1 deletion wechaty/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<dependencies>
<dependency>
<groupId>io.github.wechaty</groupId>
<artifactId>wechaty-hostie_${scala-binary-version}</artifactId>
<artifactId>wechaty-puppet-hostie_${scala-binary-version}</artifactId>
</dependency>
<dependency>
<groupId>io.github.wechaty</groupId>
Expand Down

0 comments on commit 1c449ce

Please sign in to comment.