Skip to content

Commit ceec37a

Browse files
committed
full impl
1 parent e3cfe3d commit ceec37a

File tree

4 files changed

+166
-42
lines changed

4 files changed

+166
-42
lines changed

wechaty/src/main/kotlin/io/github/wechaty/Wechaty.kt

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ class Wechaty private constructor(private var wechatyOptions: WechatyOptions) :
3939
val roomManager = RoomManager(this)
4040
val roomInvitationManager = RoomInvitationManager(this)
4141
val imageManager = ImageManager(this)
42+
val friendshipManager = FriendshipManager(this)
4243

4344
init {
4445
// this.memory = wechatyOptions.memory
4546
installGlobalPlugin()
4647
}
4748

48-
4949
fun start(await: Boolean = false):Wechaty {
5050

5151
initPuppet()
@@ -72,6 +72,25 @@ class Wechaty private constructor(private var wechatyOptions: WechatyOptions) :
7272
puppet.stop()
7373
}
7474

75+
fun logout(){
76+
log.debug("Wechaty logout()")
77+
try {
78+
puppet.logout()
79+
} catch (e: Exception) {
80+
log.error("logout error",e)
81+
throw e
82+
}
83+
}
84+
85+
fun logonoff():Boolean{
86+
return try {
87+
puppet.logonoff()
88+
} catch (e: Exception) {
89+
false
90+
}
91+
}
92+
93+
7594
fun name(): String {
7695
return wechatyOptions.name
7796
}
@@ -172,16 +191,10 @@ class Wechaty private constructor(private var wechatyOptions: WechatyOptions) :
172191
}
173192

174193
private fun initPuppet() {
175-
// this.puppet = GrpcPuppet(puppetOptions)
176194
this.puppet = PuppetManager.resolveInstance(wechatyOptions).get()
177195
initPuppetEventBridge(puppet)
178196
}
179197

180-
fun friendship(): Friendship {
181-
return Friendship(this);
182-
}
183-
184-
185198
fun getPuppet(): Puppet {
186199
return puppet
187200
}
@@ -192,6 +205,13 @@ class Wechaty private constructor(private var wechatyOptions: WechatyOptions) :
192205
return user
193206
}
194207

208+
fun say(any: Any):Message?{
209+
return userSelf().say(any)
210+
}
211+
212+
fun ding(data:String?){
213+
this.puppet.ding(data)
214+
}
195215

196216
private fun initPuppetEventBridge(puppet: Puppet) {
197217

@@ -227,7 +247,7 @@ class Wechaty private constructor(private var wechatyOptions: WechatyOptions) :
227247
EventEnum.FRIENDSHIP -> {
228248
puppet.on(it, object : PuppetFriendshipListener {
229249
override fun handler(payload: EventFriendshipPayload) {
230-
val friendship = friendship().load(payload.friendshipId)
250+
val friendship = friendshipManager.load(payload.friendshipId)
231251
friendship.ready()
232252
emit(EventEnum.FRIENDSHIP, friendship)
233253
}
@@ -356,6 +376,11 @@ class Wechaty private constructor(private var wechatyOptions: WechatyOptions) :
356376
}
357377
}
358378

379+
380+
override fun toString():String{
381+
return "wechaty"
382+
}
383+
359384
companion object Factory {
360385
@JvmStatic
361386
fun instance(token: String): Wechaty {

wechaty/src/main/kotlin/io/github/wechaty/user/Contact.kt

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@ import io.github.wechaty.Accessory
44
import io.github.wechaty.Puppet
55
import io.github.wechaty.Wechaty
66
import io.github.wechaty.filebox.FileBox
7+
import io.github.wechaty.schemas.ContactGender
78
import io.github.wechaty.schemas.ContactPayload
89
import io.github.wechaty.schemas.ContactQueryFilter
10+
import io.github.wechaty.schemas.ContactType
911
import io.github.wechaty.type.Sayable
1012
import io.github.wechaty.utils.FutureUtils
1113
import org.apache.commons.lang3.StringUtils
1214
import org.slf4j.LoggerFactory
1315
import java.util.concurrent.CompletableFuture
1416
import java.util.concurrent.Future
17+
import kotlin.math.E
1518

1619
open class Contact(wechaty: Wechaty,val id:String) : Sayable, Accessory(wechaty) {
1720

@@ -114,8 +117,61 @@ open class Contact(wechaty: Wechaty,val id:String) : Sayable, Accessory(wechaty)
114117
return payload?.alias ?:null
115118
}
116119

117-
open fun avatar(): Future<FileBox> {
118-
TODO()
120+
fun stranger():Boolean?{
121+
return if(friend() == null){
122+
null
123+
}else{
124+
!friend()!!
125+
}
126+
}
127+
128+
fun friend():Boolean?{
129+
return payload?.friend
130+
}
131+
132+
fun type():ContactType{
133+
return payload?.type ?: throw Exception("no payload")
134+
}
135+
136+
fun gender():ContactGender{
137+
return payload?.gender ?: ContactGender.Unknown
138+
}
139+
140+
fun province():String?{
141+
return payload?.province
142+
}
143+
144+
fun city():String?{
145+
return payload?.city
146+
}
147+
148+
open fun avatar(): FileBox {
149+
try {
150+
return wechaty.getPuppet().getContactAvatar(this.id).get()
151+
} catch (e: Exception) {
152+
log.error("error",e)
153+
TODO()
154+
}
155+
}
156+
157+
fun tags():List<Tag>{
158+
val tagIdList = wechaty.getPuppet().tagContactList(this.id).get()
159+
return try {
160+
tagIdList.map {
161+
wechaty.tagManager.load(it)
162+
}
163+
} catch (e: Exception) {
164+
log.error("error",e)
165+
listOf()
166+
}
167+
}
168+
169+
fun self():Boolean{
170+
val userId = puppet.selfId()
171+
if(StringUtils.isEmpty(userId)){
172+
return false
173+
}
174+
return StringUtils.equals(id,userId)
119175
}
120176

121177

wechaty/src/main/kotlin/io/github/wechaty/user/Friendship.kt

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,15 @@ import io.github.wechaty.Wechaty
55
import io.github.wechaty.schemas.FriendshipPayload
66
import io.github.wechaty.schemas.FriendshipSearchCondition
77
import io.github.wechaty.schemas.FriendshipType
8+
import io.github.wechaty.utils.JsonUtils
89
import org.apache.commons.lang3.StringUtils
910
import org.slf4j.LoggerFactory
1011

11-
class Friendship (wechaty: Wechaty):Accessory(wechaty){
12+
class Friendship (wechaty: Wechaty,val id:String):Accessory(wechaty){
1213

13-
constructor(wechaty: Wechaty,id: String):this(wechaty){
14-
this.id = id
15-
}
16-
17-
private var id:String? = null
1814

1915
private var payload:FriendshipPayload? = null
2016

21-
fun load(id:String):Friendship{
22-
this.id = id
23-
return this
24-
}
25-
26-
fun search(queryFilter: FriendshipSearchCondition):Contact?{
27-
val contactId = wechaty.getPuppet().friendshipSearch(queryFilter).get();
28-
if(StringUtils.isEmpty(contactId)){
29-
return null
30-
}
31-
val contact = wechaty.contactManager.load(contactId!!)
32-
contact.ready()
33-
return contact
34-
}
35-
36-
37-
fun add(contact: Contact, hello:String){
38-
log.debug("add contact: {} hello: {}",contact,hello)
39-
wechaty.getPuppet().friendshipAdd(contact.id!!,hello).get()
40-
}
41-
4217
fun isReady():Boolean{
4318
return payload != null
4419
}
@@ -67,17 +42,29 @@ class Friendship (wechaty: Wechaty):Accessory(wechaty){
6742
if(payload!!.type != FriendshipType.Receive){
6843
throw Exception("accept() need type to be FriendshipType.Receive, but it got a ${payload!!.type}")
6944
}
70-
71-
wechaty.getPuppet().friendshipAccept(this.id!!).get()
72-
45+
wechaty.getPuppet().friendshipAccept(this.id).get()
7346
val contact = contact()
74-
7547
contact.ready()
76-
7748
contact.sync()
49+
}
7850

51+
fun hello():String{
52+
if(payload==null){
53+
throw Exception("ne payload")
54+
}
55+
return this.payload?.hello ?: "";
56+
}
57+
58+
fun type():FriendshipType{
59+
return this.payload?.type ?:FriendshipType.Unknown
7960
}
8061

62+
fun toJson():String{
63+
if(payload==null){
64+
throw Exception("ne payload")
65+
}
66+
return JsonUtils.write(payload!!);
67+
}
8168

8269
companion object{
8370
private val log = LoggerFactory.getLogger(Friendship::class.java)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package io.github.wechaty.user.manager
2+
3+
import com.sun.xml.internal.ws.message.PayloadElementSniffer
4+
import io.github.wechaty.Accessory
5+
import io.github.wechaty.Wechaty
6+
import io.github.wechaty.schemas.FriendshipPayload
7+
import io.github.wechaty.schemas.FriendshipSearchCondition
8+
import io.github.wechaty.user.Contact
9+
import io.github.wechaty.user.Friendship
10+
import io.github.wechaty.utils.JsonUtils
11+
import org.apache.commons.lang3.StringUtils
12+
import org.slf4j.Logger
13+
import org.slf4j.LoggerFactory
14+
15+
class FriendshipManager (wechaty: Wechaty): Accessory(wechaty){
16+
17+
fun load(id:String): Friendship {
18+
return Friendship(wechaty,id)
19+
}
20+
21+
fun search(queryFilter: FriendshipSearchCondition): Contact?{
22+
log.debug("query filter {}",queryFilter)
23+
val contactId = wechaty.getPuppet().friendshipSearch(queryFilter).get();
24+
if(StringUtils.isEmpty(contactId)){
25+
return null
26+
}
27+
val contact = wechaty.contactManager.load(contactId!!)
28+
contact.ready()
29+
return contact
30+
}
31+
32+
fun add(contact: Contact,hello:String){
33+
log.debug("add {},{}",contact,hello)
34+
wechaty.getPuppet().friendshipAdd(contact.id,hello).get()
35+
}
36+
37+
fun del(contact: Contact){
38+
log.debug("del {}",contact)
39+
throw Exception("to be implemented")
40+
}
41+
42+
fun fromJSON(payload:String):Friendship{
43+
val readValue = JsonUtils.readValue<FriendshipPayload>(payload)
44+
return fromJSON(readValue)
45+
}
46+
47+
fun fromJSON(friendshipPayload: FriendshipPayload):Friendship{
48+
wechaty.getPuppet().friendshipPayload(friendshipPayload.id!!,friendshipPayload).get()
49+
return load(friendshipPayload.id!!)
50+
}
51+
52+
companion object{
53+
private val log: Logger = LoggerFactory.getLogger(FriendshipManager::class.java)
54+
}
55+
56+
}

0 commit comments

Comments
 (0)