Skip to content

Commit 9b4f815

Browse files
committed
1. 添加FriendShipmanger
2. Contact添加获取信息的方法 3. Room添加member方法 4. Wechaty添加say和onReady事件 5. WechatyListener修改FriendShiplistener参数 6.
1 parent c5b240c commit 9b4f815

File tree

10 files changed

+150
-53
lines changed

10 files changed

+150
-53
lines changed

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

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class Wechaty private constructor(private var wechatyOptions: WechatyOptions) :
4343
val roomManager = RoomManager(this)
4444
val roomInvitationManager = RoomInvitationManager(this)
4545
val imageManager = ImageManager(this)
46-
46+
val friendShipManager = FriendShipManager(this)
4747
init {
4848
if (wechatyOptions.memory == null) {
4949
this.memory = MemoryCard(wechatyOptions.name)
@@ -88,29 +88,30 @@ class Wechaty private constructor(private var wechatyOptions: WechatyOptions) :
8888
fun say(something: Any): Future<Void> {
8989

9090
val msgId: String?
91-
this.puppet.selfId()?.let {
92-
when (something) {
93-
is String -> {
94-
msgId = puppet.messageSendText(it, something).get()
95-
}
96-
is Contact -> {
97-
msgId = puppet.messageSendContact(it, something.id).get()
98-
}
99-
is FileBox -> {
100-
msgId = puppet.messageSendFile(it, something).get()
101-
}
102-
is UrlLink -> {
103-
msgId = puppet.messageSendUrl(it, something.payload).get()
104-
}
105-
is MiniProgram -> {
106-
msgId = puppet.messageSendMiniProgram(it, something.payload).get()
107-
}
108-
else -> {
109-
throw Exception("unsupported arg:$something")
110-
}
111-
}
112-
return@let
113-
}
91+
this.userSelf().say(something)
92+
// this.puppet.selfId()?.let {
93+
// when (something) {
94+
// is String -> {
95+
// msgId = puppet.messageSendText(it, something).get()
96+
// }
97+
// is Contact -> {
98+
// msgId = puppet.messageSendContact(it, something.id).get()
99+
// }
100+
// is FileBox -> {
101+
// msgId = puppet.messageSendFile(it, something).get()
102+
// }
103+
// is UrlLink -> {
104+
// msgId = puppet.messageSendUrl(it, something.payload).get()
105+
// }
106+
// is MiniProgram -> {
107+
// msgId = puppet.messageSendMiniProgram(it, something.payload).get()
108+
// }
109+
// else -> {
110+
// throw Exception("unsupported arg:$something")
111+
// }
112+
// }
113+
// return@let
114+
// }
114115
return CompletableFuture.completedFuture(null)
115116
}
116117
fun onLogin(listener: LoginListener):Wechaty{
@@ -123,12 +124,16 @@ class Wechaty private constructor(private var wechatyOptions: WechatyOptions) :
123124
fun onScan(listener: ScanListener):Wechaty{
124125
return on(EventEnum.SCAN,listener);
125126
}
127+
128+
fun onReady(listener: ReadyListener): Wechaty {
129+
return on(EventEnum.READY, listener)
130+
}
126131
fun onFriendship(listener: FriendshipListener): Wechaty {
127132
return on(EventEnum.FRIENDSHIP, listener)
128133
}
129134

130135
fun onRoomJoin(listener: RoomJoinListener):Wechaty {
131-
return on(EventEnum.ROOM_JOIN,listener)
136+
return on(EventEnum.ROOM_JOIN, listener)
132137
}
133138

134139
fun onRoomLeave(listener: RoomLeaveListener):Wechaty {
@@ -170,6 +175,14 @@ class Wechaty private constructor(private var wechatyOptions: WechatyOptions) :
170175
})
171176
return this
172177
}
178+
private fun on(event: Event,listener: ReadyListener):Wechaty{
179+
super.on(event, object : Listener {
180+
override fun handler(vararg any: Any) {
181+
listener.handler()
182+
}
183+
})
184+
return this
185+
}
173186

174187
private fun on(event: Event, listener: LogoutListener): Wechaty {
175188
super.on(event, object : Listener {
@@ -212,7 +225,7 @@ class Wechaty private constructor(private var wechatyOptions: WechatyOptions) :
212225
private fun on(event: Event, listener: FriendshipListener): Wechaty {
213226
super.on(event, object : Listener {
214227
override fun handler(vararg any: Any) {
215-
listener.handler(any[0] as String)
228+
listener.handler(any[0] as Friendship)
216229
}
217230
})
218231
return this
@@ -222,7 +235,7 @@ class Wechaty private constructor(private var wechatyOptions: WechatyOptions) :
222235
super.on(eventName, object : Listener {
223236
override fun handler(vararg any: Any) {
224237
// roomInvitationId
225-
listener.handler(any[0] as String)
238+
listener.handler(any[0] as RoomInvitation)
226239
}
227240
})
228241
return this
@@ -315,10 +328,10 @@ class Wechaty private constructor(private var wechatyOptions: WechatyOptions) :
315328
EventEnum.FRIENDSHIP -> {
316329
puppet.on(it, object : PuppetFriendshipListener {
317330
override fun handler(payload: EventFriendshipPayload) {
318-
val friendship = friendship().load(payload.friendshipId)
331+
// val friendship = friendship().load(payload.friendshipId)
332+
val friendship = friendShipManager.load(payload.friendshipId)
319333
friendship.ready()
320-
// emit(EventEnum.FRIENDSHIP, friendship)
321-
emit(EventEnum.FRIENDSHIP, payload.friendshipId)
334+
emit(EventEnum.FRIENDSHIP, friendship)
322335
}
323336
})
324337
}
@@ -401,7 +414,7 @@ class Wechaty private constructor(private var wechatyOptions: WechatyOptions) :
401414
puppet.on(it, object : PuppetRoomLeaveListener {
402415
override fun handler(payload: EventRoomLeavePayload) {
403416
val room = roomManager.load(payload.roomId)
404-
room.sync()
417+
room.sync().get()
405418

406419
val leaverList = payload.removeeIdList.map { id ->
407420
val contact = contactManager.loadSelf(id)
@@ -423,7 +436,7 @@ class Wechaty private constructor(private var wechatyOptions: WechatyOptions) :
423436
puppet.on(it, object : PuppetRoomTopicListener {
424437
override fun handler(payload: EventRoomTopicPayload) {
425438
val room = roomManager.load(payload.roomId)
426-
room.sync()
439+
room.sync().get()
427440

428441
val changer = contactManager.loadSelf(payload.changerId)
429442
changer.ready()

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package io.github.wechaty
22

33
import io.github.wechaty.schemas.ScanStatus
4-
import io.github.wechaty.user.Contact
5-
import io.github.wechaty.user.ContactSelf
6-
import io.github.wechaty.user.Message
7-
import io.github.wechaty.user.Room
4+
import io.github.wechaty.user.*
85
import java.util.*
96

107
@FunctionalInterface
@@ -19,7 +16,7 @@ interface ErrorListener {
1916

2017
@FunctionalInterface
2118
interface FriendshipListener {
22-
fun handler(friendshipId: String)
19+
fun handler(friendship: Friendship)
2320
}
2421

2522
@FunctionalInterface
@@ -66,7 +63,7 @@ interface RoomTopicListener {
6663

6764
@FunctionalInterface
6865
interface RoomInviteListener {
69-
fun handler(roomInvitationId: String)
66+
fun handler(roomInvitation : RoomInvitation)
7067
}
7168

7269
@FunctionalInterface

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

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,26 +93,43 @@ open class Contact(wechaty: Wechaty,val id:String) : Sayable, Accessory(wechaty)
9393
return (payload != null && StringUtils.isNotEmpty(payload!!.name))
9494
}
9595

96-
fun friend(): Boolean? {
97-
return payload?.friend
96+
fun friend(): Boolean {
97+
return payload?.friend ?: false
9898
}
9999

100-
fun name():String{
100+
fun name():String {
101101
return payload?.name ?: ""
102102
}
103+
103104
fun type(): ContactType {
104105
return payload?.type ?: ContactType.Unknown
105106
}
107+
106108
fun gender(): ContactGender {
107109
return payload?.gender ?: ContactGender.Unknown
108110
}
109111

110-
fun province(): String? {
111-
return payload?.province
112+
fun address(): String {
113+
return this.payload?.address ?: ""
114+
}
115+
fun province(): String {
116+
return this.payload?.province ?: ""
117+
}
118+
119+
fun signature(): String {
120+
return this.payload?.signature ?: ""
121+
}
122+
123+
fun city(): String {
124+
return this.payload?.city ?: ""
125+
}
126+
127+
fun star(): Boolean {
128+
return this.payload?.star ?: false
112129
}
113130

114-
fun city(): String? {
115-
return payload?.city
131+
fun weixin(): String {
132+
return this.payload?.weixin ?: ""
116133
}
117134

118135
fun self(): Boolean {
@@ -139,8 +156,8 @@ open class Contact(wechaty: Wechaty,val id:String) : Sayable, Accessory(wechaty)
139156

140157
}
141158

142-
fun getAlias():String?{
143-
return payload?.alias ?:null
159+
fun getAlias():String? {
160+
return payload?.alias
144161
}
145162

146163
open fun avatar(): Future<FileBox> {
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package io.github.wechaty.user
22

33
import io.github.wechaty.Accessory
4+
import io.github.wechaty.Puppet
45
import io.github.wechaty.Wechaty
6+
import io.github.wechaty.schemas.ContactPayload
57

68
class Favorite(wechaty: Wechaty):Accessory(wechaty){
79

8-
9-
10-
}
10+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Friendship (wechaty: Wechaty):Accessory(wechaty){
1818

1919
private var payload:FriendshipPayload? = null
2020

21-
fun load(id:String):Friendship{
21+
fun load(id:String):Friendship {
2222
this.id = id
2323
return this
2424
}
@@ -41,6 +41,7 @@ class Friendship (wechaty: Wechaty):Accessory(wechaty){
4141
return this.payload!!.hello ?: ""
4242
}
4343

44+
// 这个应该是静态方法吧
4445
fun add(contact: Contact, hello:String){
4546
log.debug("add contact: {} hello: {}",contact,hello)
4647
wechaty.getPuppet().friendshipAdd(contact.id!!,hello).get()
@@ -56,7 +57,6 @@ class Friendship (wechaty: Wechaty):Accessory(wechaty){
5657
}
5758
this.payload = wechaty.getPuppet().friendshipPayload(id!!).get()
5859
contact().ready()
59-
6060
}
6161

6262
fun contact():Contact{

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,12 @@ open class Message(wechaty: Wechaty,val id: String) : Sayable, Accessory(wechaty
132132
}
133133

134134
fun age(): Long {
135+
// 这里会是负数,payload里面的时间提前了
135136
val ageMilliseconds = Date().time - this.date().time
136137
val ageSeconds = Math.floor(ageMilliseconds / 1000.0).toLong()
137138
return ageSeconds
138139
}
140+
139141
fun forward(to: Any): Future<Void> {
140142
log.debug("Message, forward({})", to)
141143
when(to) {

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import io.github.wechaty.io.github.wechaty.schemas.EventEnum
1515
import io.github.wechaty.schemas.RoomMemberQueryFilter
1616
import io.github.wechaty.schemas.RoomPayload
1717
import io.github.wechaty.type.Sayable
18+
import io.github.wechaty.user.manager.RoomManager
19+
import io.github.wechaty.utils.JsonUtils
1820
import io.github.wechaty.utils.QrcodeUtils
1921
import io.grpc.netty.shaded.io.netty.util.concurrent.CompleteFuture
2022
import org.apache.commons.collections4.CollectionUtils
@@ -303,7 +305,18 @@ class Room(wechaty: Wechaty, val id: String) : Accessory(wechaty), Sayable {
303305
return@supplyAsync QrcodeUtils.guardQrCodeValue(qrCodeValue)
304306
}
305307
}
308+
fun member(query: RoomMemberQueryFilter?): Contact? {
309+
val memberList = memberAll(query)
306310

311+
if (memberList == null || memberList.size == 0) {
312+
return null
313+
}
314+
if (memberList.size > 1) {
315+
log.warn("Room, member({}) get {} contacts, use the first one by default", query?.let { JsonUtils.write(it) }, memberList.size)
316+
}
317+
318+
return memberList[0];
319+
}
307320
fun memberAll(query: RoomMemberQueryFilter?): List<Contact> {
308321

309322
if (query == null) {

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ class Tag(wechaty:Wechaty,val id:String):Accessory(wechaty){
1414
wechaty.getPuppet().tagContactRemove(this.id!!,from.id!!).get()
1515
}
1616

17-
1817
companion object{
1918
private val log = LoggerFactory.getLogger(Tag::class.java)
2019
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package io.github.wechaty.user.manager
2+
3+
import com.github.benmanes.caffeine.cache.Cache
4+
import com.github.benmanes.caffeine.cache.Caffeine
5+
import io.github.wechaty.Accessory
6+
import io.github.wechaty.Wechaty
7+
import io.github.wechaty.schemas.*
8+
import io.github.wechaty.user.Contact
9+
import io.github.wechaty.user.ContactSelf
10+
import io.github.wechaty.user.Friendship
11+
import io.github.wechaty.user.Tag
12+
import org.apache.commons.collections4.CollectionUtils
13+
import org.apache.commons.lang3.StringUtils
14+
import org.slf4j.LoggerFactory
15+
import java.util.*
16+
17+
class FriendShipManager(wechaty: Wechaty):Accessory(wechaty) {
18+
19+
private val friendshipCache: Cache<String, Friendship> = Caffeine.newBuilder().build()
20+
21+
fun load(id:String): Friendship {
22+
return friendshipCache.get(id) {
23+
Friendship(wechaty, id)
24+
}!!
25+
}
26+
27+
// 查找发送请求的好友
28+
fun search(queryFilter: FriendshipSearchCondition): Contact? {
29+
val friendshipId = wechaty.getPuppet().friendshipSearch(queryFilter).get();
30+
if(StringUtils.isEmpty(friendshipId)){
31+
return null
32+
}
33+
val contact = wechaty.contactManager.load(friendshipId!!)
34+
contact.ready()
35+
return contact
36+
}
37+
38+
// 添加好友
39+
fun add(contact: Contact, hello:String) {
40+
log.debug("add contact: {} hello: {}", contact, hello)
41+
wechaty.getPuppet().friendshipAdd(contact.id, hello).get()
42+
}
43+
44+
companion object {
45+
private val log = LoggerFactory.getLogger(FriendShipManager::class.java)
46+
}
47+
48+
49+
}

wechaty/src/main/kotlin/io/github/wechaty/user/manager/TagManager.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ class TagManager(wechaty: Wechaty):Accessory(wechaty){
2626
wechaty.getPuppet().tagContactDelete(tag.id)
2727
}
2828

29+
fun tags():List<Tag>{
30+
val tagIdList = wechaty.getPuppet().tagContactList().get()
31+
return tagIdList.map {
32+
wechaty.tagManager.load(it)
33+
}
34+
}
35+
2936
companion object{
3037
private val log = LoggerFactory.getLogger(TagManager::class.java)
3138
}

0 commit comments

Comments
 (0)