Skip to content

Commit fac8b23

Browse files
committed
1. 获取属性的方法变为getXXX
2. 添加messageQueryFilterFactory方法,新增相关的typealias
1 parent c594ad8 commit fac8b23

File tree

8 files changed

+205
-28
lines changed

8 files changed

+205
-28
lines changed

wechaty-puppet/src/main/kotlin/Puppet.kt

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -707,8 +707,46 @@ abstract class Puppet : EventEmitter {
707707

708708
}
709709

710-
protected fun messageQueryFilterFactory(query: MessageQueryFilter) {
711-
TODO()
710+
protected fun messageQueryFilterFactory(query: MessageQueryFilter): MessagePayloadFilterFunction {
711+
val clz = query::class.java
712+
val fields = clz.fields
713+
val list = fields.map {
714+
it.name to it.get(query)
715+
}
716+
717+
var filterFunctionList = ArrayList<MessagePayloadFilterFunction>()
718+
719+
list.forEach { pair ->
720+
if (StringUtils.isNotEmpty(pair.second.toString())) {
721+
val filterFunction = if (StringUtils.equals(pair.first.toString(), "textReg")) {
722+
{
723+
payload: MessagePayload -> Boolean
724+
val clazz = payload::class.java
725+
val field = clazz.getField(pair.first)
726+
val realValue = field.get(payload).toString()
727+
Regex(pair.second.toString()).matches(realValue)
728+
}
729+
}
730+
else {
731+
{
732+
payload: MessagePayload -> Boolean
733+
val clazz = payload::class.java
734+
val field = clazz.getField(pair.first)
735+
val realValue = field.get(payload).toString()
736+
StringUtils.equals(realValue, pair.second.toString())
737+
}
738+
}
739+
filterFunctionList.add(filterFunction)
740+
}
741+
}
742+
val allFilterFunction: MessagePayloadFilterFunction = {
743+
payload: MessagePayload ->
744+
filterFunctionList.all {
745+
func -> func(payload)
746+
}
747+
}
748+
return allFilterFunction
749+
712750
}
713751

714752
fun messageForward(conversationId: String, messageId: String): Future<String?> {

wechaty-puppet/src/main/kotlin/io/github/wechaty/schemas/Message.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class MessageQueryFilter {
6161
override fun toString(): String {
6262
return "MessageQueryFilter(fromId=$fromId, id=$id, roomId=$roomId, text=$text, toId=$toId, type=$type, textReg=$textReg)"
6363
}
64-
65-
6664
}
65+
66+
typealias MessagePayloadFilterFunction = (payload: MessagePayload) -> Boolean
67+
typealias MessagePayloadFilterFactory = (query: MessageQueryFilter) -> MessagePayloadFilterFunction

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

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,24 @@ open class Contact(wechaty: Wechaty,val id:String) : Sayable, Accessory(wechaty)
9898
return payload?.name ?: ""
9999
}
100100

101+
fun getName():String? {
102+
return payload?.name
103+
}
104+
101105
fun address(): String {
102106
return this.payload?.address ?: ""
103107
}
104108

109+
fun getAddress(): String? {
110+
return this.payload?.address
111+
}
112+
105113
fun signature(): String {
106114
return this.payload?.signature ?: ""
107115
}
116+
fun getSignature(): String? {
117+
return this.payload?.signature
118+
}
108119

109120
fun star(): Boolean {
110121
return this.payload?.star ?: false
@@ -114,6 +125,10 @@ open class Contact(wechaty: Wechaty,val id:String) : Sayable, Accessory(wechaty)
114125
return this.payload?.weixin ?: ""
115126
}
116127

128+
fun getWeixin(): String? {
129+
return this.payload?.weixin
130+
}
131+
117132
fun setAlias(newAlias:String){
118133
if(payload == null){
119134
throw Exception("no payload")
@@ -148,28 +163,54 @@ open class Contact(wechaty: Wechaty,val id:String) : Sayable, Accessory(wechaty)
148163
fun type():ContactType{
149164
return payload?.type ?: throw Exception("no payload")
150165
}
166+
fun getType(): ContactType {
167+
return payload?.type ?: throw Exception("no payload")
168+
}
151169

152170
fun gender():ContactGender{
153171
return payload?.gender ?: ContactGender.Unknown
154172
}
173+
fun getGender():ContactGender{
174+
return payload?.gender ?: throw Exception("no payload")
175+
}
155176

156177
fun province():String?{
157178
return payload?.province
158179
}
180+
fun getProvince():String?{
181+
return payload?.province
182+
}
159183

160184
fun city():String?{
161185
return payload?.city
162186
}
163187

188+
fun getCity():String?{
189+
return payload?.city
190+
}
191+
164192
open fun avatar(): FileBox {
165193
try {
166194
return wechaty.getPuppet().getContactAvatar(this.id).get()
167195
} catch (e: Exception) {
168196
log.error("error",e)
169-
TODO()
197+
return qrCodeForChatie()
198+
}
199+
}
200+
fun getAvatar(): FileBox {
201+
try {
202+
return wechaty.getPuppet().getContactAvatar(this.id).get()
203+
} catch (e: Exception) {
204+
log.error("error",e)
205+
return qrCodeForChatie()
170206
}
171207
}
172208

209+
fun qrCodeForChatie (): FileBox {
210+
val CHATIE_OFFICIAL_ACCOUNT_QRCODE = "http://weixin.qq.com/r/qymXj7DEO_1ErfTs93y5"
211+
return FileBox.fromQRCode(CHATIE_OFFICIAL_ACCOUNT_QRCODE)
212+
}
213+
173214
fun tags():List<Tag>{
174215
val tagIdList = wechaty.getPuppet().tagContactList(this.id).get()
175216
return try {
@@ -181,6 +222,17 @@ open class Contact(wechaty: Wechaty,val id:String) : Sayable, Accessory(wechaty)
181222
listOf()
182223
}
183224
}
225+
fun getTags():List<Tag> {
226+
val tagIdList = wechaty.getPuppet().tagContactList(this.id).get()
227+
return try {
228+
tagIdList.map {
229+
wechaty.tagManager.load(it)
230+
}
231+
} catch (e: Exception) {
232+
log.error("error",e)
233+
listOf()
234+
}
235+
}
184236

185237
fun self():Boolean {
186238
val userId = puppet.selfId()

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

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ class ContactSelf(wechaty: Wechaty, id: String) : Contact(wechaty, id) {
1313
puppet.setContactAvatar(super.id, fileBox)
1414
}
1515

16+
fun getAvatar(fileBox: FileBox) {
17+
puppet.setContactAvatar(super.id, fileBox)
18+
}
19+
1620
fun setName(name:String){
1721
puppet.contactSelfName(name).get()
1822
sync()
@@ -29,6 +33,17 @@ class ContactSelf(wechaty: Wechaty, id: String) : Contact(wechaty, id) {
2933
sync()
3034
}
3135
}
36+
fun setSignature(signature:String) {
37+
38+
var puppetId:String? = puppet.selfId()
39+
40+
let{
41+
puppetId != null
42+
}.run {
43+
puppet.contactSelfSignature(signature).get()
44+
sync()
45+
}
46+
}
3247

3348
fun qrcode(): Future<String> {
3449
log.info("Contact, qrcode()")
@@ -49,15 +64,33 @@ class ContactSelf(wechaty: Wechaty, id: String) : Contact(wechaty, id) {
4964
guardQrCodeValue(qrcodeValue)
5065
}
5166
}
67+
fun getQrcode(): Future<String> {
68+
log.info("Contact, qrcode()")
69+
70+
val puppetId: String = try {
71+
this.puppet.selfId().toString()
72+
}
73+
catch (e: Exception) {
74+
throw Exception("Can not get qrcode, user might be either not logged in or already logged out")
75+
}
76+
77+
if (this.id !== puppetId) {
78+
throw Exception("only can get qrcode for the login userself")
79+
}
80+
81+
return CompletableFuture.supplyAsync {
82+
val qrcodeValue = this.puppet.contactSelfQRCode().get()
83+
guardQrCodeValue(qrcodeValue)
84+
}
85+
}
5286

5387
fun name(name: String?): String? {
5488
if (name == null) {
5589
return super.name()
5690
}
5791
val puppetId = try {
5892
this.puppet.selfId()
59-
}
60-
catch (e: Exception) {
93+
} catch (e: Exception) {
6194
throw Exception("Can not get qrcode, user might be either not logged in or already logged out")
6295
}
6396
if (this.id !== puppetId) {
@@ -67,22 +100,21 @@ class ContactSelf(wechaty: Wechaty, id: String) : Contact(wechaty, id) {
67100
return null
68101
}
69102

70-
// fun signature (signature: String): Future<Void> {
71-
// log.debug("ContactSelf, signature()")
72-
//
73-
// val puppetId = try {
74-
// this.puppet.selfId()
75-
// }
76-
// catch (e: Exception) {
77-
// throw Exception("Can not get qrcode, user might be either not logged in or already logged out")
78-
// }
79-
//
80-
// if (this.id !== puppetId) {
81-
// throw Exception("only can get qrcode for the login userself")
82-
// }
83-
// // maybe
84-
// return this.puppet.contactSelfSignature(signature)
85-
// }
103+
fun getName(name: String?): String? {
104+
if (name == null) {
105+
return super.name()
106+
}
107+
val puppetId = try {
108+
this.puppet.selfId()
109+
} catch (e: Exception) {
110+
throw Exception("Can not get qrcode, user might be either not logged in or already logged out")
111+
}
112+
if (this.id !== puppetId) {
113+
throw Exception("only can get qrcode for the login userself")
114+
}
115+
this.puppet.contactSelfName(name)
116+
return null
117+
}
86118

87119
companion object {
88120
private val log = LoggerFactory.getLogger(ContactSelf::class.java)

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,14 @@ class Friendship (wechaty: Wechaty,val id:String):Accessory(wechaty){
7070
return this.payload?.hello ?: "";
7171
}
7272

73-
fun type():FriendshipType{
73+
fun type(): FriendshipType {
7474
return this.payload?.type ?:FriendshipType.Unknown
7575
}
7676

77+
fun getType(): FriendshipType {
78+
return this.payload?.type ?: throw Exception("ne payload")
79+
}
80+
7781
fun toJson():String{
7882
if(payload==null){
7983
throw Exception("ne payload")

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ open class Message(wechaty: Wechaty,val id: String) : Sayable, Accessory(wechaty
118118
}
119119
return this.payload?.type ?: MessageType.Unknown
120120
}
121+
fun getType(): MessageType{
122+
return this.payload?.type ?: throw Exception("no payload")
123+
}
121124

122125
fun self():Boolean{
123126
val selfId = puppet.selfId()
@@ -131,8 +134,18 @@ open class Message(wechaty: Wechaty,val id: String) : Sayable, Accessory(wechaty
131134
return Date(payload.timestamp!! * 1000)
132135
}
133136

137+
fun getDate(): Date {
138+
val payload = wechaty.getPuppet().messagePayload(this.id).get()
139+
return Date(payload.timestamp!! * 1000)
140+
}
141+
134142
fun age(): Long {
135-
// 这里会是负数,payload里面的时间提前了
143+
val ageMilliseconds = Date().time - this.date().time
144+
val ageSeconds = Math.floor(ageMilliseconds / 1000.0).toLong()
145+
return ageSeconds
146+
}
147+
148+
fun getAge(): Long {
136149
val ageMilliseconds = Date().time - this.date().time
137150
val ageSeconds = Math.floor(ageMilliseconds / 1000.0).toLong()
138151
return ageSeconds
@@ -202,7 +215,6 @@ open class Message(wechaty: Wechaty,val id: String) : Sayable, Accessory(wechaty
202215
return text()
203216
}
204217

205-
206218
fun ready():Future<Void>{
207219

208220
return CompletableFuture.supplyAsync {

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,12 +350,29 @@ class Room(wechaty: Wechaty, val id: String) : Accessory(wechaty), Sayable {
350350
}
351351
}
352352

353+
fun getAnnounce(): Future<String> {
354+
return CompletableFuture.supplyAsync {
355+
puppet.getRoomAnnounce(id).get()
356+
}
357+
}
358+
359+
fun setAnnounce(text: String): Future<Void> {
360+
return puppet.setRoomAnnounce(id, text)
361+
}
362+
353363
fun qrCode(): Future<String> {
354364
return CompletableFuture.supplyAsync {
355365
val qrCodeValue = puppet.roomQRCode(id).get()
356366
return@supplyAsync QrcodeUtils.guardQrCodeValue(qrCodeValue)
357367
}
358368
}
369+
fun getQrCode(): Future<String> {
370+
return CompletableFuture.supplyAsync {
371+
val qrCodeValue = puppet.roomQRCode(id).get()
372+
return@supplyAsync QrcodeUtils.guardQrCodeValue(qrCodeValue)
373+
}
374+
}
375+
359376
fun member(query: RoomMemberQueryFilter?): Contact? {
360377
val memberList = memberAll(query)
361378

@@ -398,9 +415,7 @@ class Room(wechaty: Wechaty, val id: String) : Accessory(wechaty), Sayable {
398415
}
399416

400417
fun alias(contact: Contact): String? {
401-
402418
val roomMemberPayload = wechaty.getPuppet().roomMemberPayload(this.id, contact.id).get()
403-
404419
return roomMemberPayload?.roomAlias
405420
}
406421

@@ -431,6 +446,11 @@ class Room(wechaty: Wechaty, val id: String) : Accessory(wechaty), Sayable {
431446
return puppet.roomAvatar(this.id).get()
432447
}
433448

449+
fun getAvatar(): FileBox {
450+
log.debug("getAvatar:{}", getAvatar())
451+
return puppet.roomAvatar(this.id).get()
452+
}
453+
434454
companion object {
435455
private val log = LoggerFactory.getLogger(Room::class.java)
436456
}

0 commit comments

Comments
 (0)