Skip to content

Commit 648ffc9

Browse files
authored
Merge pull request #27 from redmaple1/master
fix EventRoomTopicPayload timestamp typo
2 parents b759e1d + eaf72f7 commit 648ffc9

File tree

12 files changed

+166
-33
lines changed

12 files changed

+166
-33
lines changed

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,11 @@
191191
<artifactId>junit</artifactId>
192192
<version>4.12</version>
193193
</dependency>
194+
<dependency>
195+
<groupId>com.github.javafaker</groupId>
196+
<artifactId>javafaker</artifactId>
197+
<version>1.0.2</version>
198+
</dependency>
194199

195200
</dependencies>
196201
</dependencyManagement>

wechaty-puppet-hostie/src/main/kotlin/io/github/wechaty/grpc/GrpcPuppet.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import org.apache.commons.lang3.StringUtils
1616
import org.apache.commons.lang3.math.NumberUtils
1717
import org.slf4j.LoggerFactory
1818
import java.util.concurrent.CompletableFuture
19-
import java.util.concurrent.CountDownLatch
2019
import java.util.concurrent.Executors.newFixedThreadPool
2120
import java.util.concurrent.Future
2221
import java.util.concurrent.TimeUnit
@@ -404,7 +403,7 @@ class GrpcPuppet(puppetOptions: PuppetOptions) : Puppet(puppetOptions) {
404403
payload.province = response.province
405404
payload.signature = response.signature
406405
payload.star = response.star
407-
payload.type = ContractType.getByCode(response.type.number)
406+
payload.type = ContactType.getByCode(response.type.number)
408407
payload.weixin = response.weixin
409408
payload
410409
}

wechaty-puppet-mock/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@
4848
<groupId>junit</groupId>
4949
<artifactId>junit</artifactId>
5050
</dependency>
51+
<dependency>
52+
<groupId>com.github.javafaker</groupId>
53+
<artifactId>javafaker</artifactId>
54+
</dependency>
5155
</dependencies>
5256

5357
<build>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package io.github.wechaty
2+
3+
import com.github.javafaker.Faker
4+
import io.github.wechaty.filebox.FileBox
5+
import io.github.wechaty.schemas.*
6+
import java.util.*
7+
8+
/**
9+
* Data for mock
10+
* @author renxiaoya
11+
* @date 2020-06-01
12+
**/
13+
class MockData {
14+
15+
companion object {
16+
private val faker = Faker()
17+
18+
fun getFakeContactPayload(): ContactPayload {
19+
val contactPayload = ContactPayload(UUID.randomUUID().toString())
20+
contactPayload.address = faker.address().streetAddress()
21+
contactPayload.avatar = faker.avatar().toString()
22+
contactPayload.city = faker.address().city()
23+
contactPayload.friend = true
24+
contactPayload.gender = ContractGender.Male
25+
contactPayload.name = faker.name().firstName()
26+
contactPayload.province = faker.address().state()
27+
contactPayload.signature = faker.lorem().sentence()
28+
contactPayload.star = false
29+
contactPayload.type = ContactType.Personal
30+
return contactPayload
31+
}
32+
33+
fun getFakeImageFileBox(): FileBox {
34+
return FileBox.fromUrl(faker.avatar().image(), null)
35+
}
36+
37+
fun getMessagePayload(fromId: String, toId: String): MessagePayload {
38+
val messagePayload = getFakeMessagePayload()
39+
messagePayload.fromId = fromId
40+
messagePayload.toId = toId
41+
return messagePayload
42+
}
43+
44+
fun getFakeMessagePayload(): MessagePayload {
45+
val messagePayload = MessagePayload(UUID.randomUUID().toString())
46+
messagePayload.fromId = UUID.randomUUID().toString()
47+
messagePayload.mentionIdList = listOf()
48+
messagePayload.text = faker.lorem().sentence()
49+
messagePayload.timestamp = Date().time
50+
messagePayload.toId = UUID.randomUUID().toString()
51+
messagePayload.type = MessageType.Text
52+
messagePayload.roomId = "${UUID.randomUUID().toString()}@chatroom"
53+
messagePayload.filename = faker.file().fileName()
54+
return messagePayload
55+
}
56+
}
57+
58+
}

wechaty-puppet-mock/src/main/kotlin/wechaty/MockPuppet.kt renamed to wechaty-puppet-mock/src/main/kotlin/io/github/wechaty/MockPuppet.kt

Lines changed: 70 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1-
package wechaty
1+
package io.github.wechaty
22

3-
import io.github.wechaty.Puppet
43
import io.github.wechaty.filebox.FileBox
54
import io.github.wechaty.schemas.*
65
import org.slf4j.LoggerFactory
6+
import java.util.*
7+
import java.util.concurrent.CompletableFuture
78
import java.util.concurrent.Future
9+
import kotlin.concurrent.scheduleAtFixedRate
810

911
class MockPuppet(puppetOptions: PuppetOptions) : Puppet(puppetOptions) {
12+
13+
private val timer: Timer = Timer()
14+
1015
override fun start(): Future<Void> {
1116

1217
log.info("MockPuppet start()")
@@ -24,66 +29,114 @@ class MockPuppet(puppetOptions: PuppetOptions) : Puppet(puppetOptions) {
2429
this.state.on(true)
2530
*/
2631
val eventScanPayload = EventScanPayload(ScanStatus.Cancel)
27-
eventScanPayload.qrcode = "https://github.com/wechaty/wechaty-puppet-mock"
32+
eventScanPayload.qrcode = "https://github.com/wechaty/java-wechaty/wechaty-puppet-mock"
2833
emit("scan", eventScanPayload)
2934

30-
TODO("Not yet implemented")
3135

36+
val userPayload = MockData.getFakeContactPayload()
37+
cacheContactPayload.put(userPayload.id, userPayload)
38+
39+
setId(userPayload.id)
40+
41+
emit("login", EventLoginPayload(userPayload.id))
42+
43+
timer.scheduleAtFixedRate(0, 5000) {
44+
val fromContactPayload = MockData.getFakeContactPayload()
45+
cacheContactPayload.put(fromContactPayload.id, fromContactPayload)
46+
val messagePayload = MockData.getMessagePayload(fromContactPayload.id, userPayload.id)
47+
48+
cacheMessagePayload.put(messagePayload.id, messagePayload)
49+
log.info("MockPuppet start() schedule pretending received a new message:${messagePayload.id}")
50+
emit("message", EventMessagePayload(messagePayload.id))
51+
}
52+
return CompletableFuture.completedFuture(null)
3253
}
3354

3455
override fun stop(): Future<Void> {
35-
TODO("Not yet implemented")
56+
log.info("MockPuppet stop()")
57+
//TODO StateSwitch
58+
/*
59+
if (this.state.off()) {
60+
log.warn('PuppetMock', 'stop() is called on a OFF puppet. await ready(off) and return.')
61+
await this.state.ready('off')
62+
return
63+
}
64+
65+
this.state.off('pending')
66+
*/
67+
timer.cancel()
68+
69+
//this.state.off(true)
70+
return CompletableFuture.completedFuture(null)
3671
}
3772

3873
override fun logout(): Future<Void> {
39-
TODO("Not yet implemented")
74+
log.info("MockPuppet logout()")
75+
val id = getId() ?: throw Exception("logout before login?")
76+
77+
emit("logout", EventLogoutPayload(id, "test"))
78+
setId(null)
79+
80+
return CompletableFuture.completedFuture(null)
4081
}
4182

4283
override fun ding(data: String?) {
43-
TODO("Not yet implemented")
84+
log.info("MockPuppet ding(${data ?: ""})")
85+
emit("dong", EventDongPayload(data ?: ""))
4486
}
4587

4688
override fun contactSelfName(name: String): Future<Void> {
47-
TODO("Not yet implemented")
89+
log.info("MockPuppet contactSelfName($name)")
90+
return CompletableFuture.completedFuture(null)
4891
}
4992

5093
override fun contactSelfQRCode(): Future<String> {
51-
TODO("Not yet implemented")
94+
log.info("MockPuppet contactSelfQRCode()")
95+
return CompletableFuture.completedFuture(CHATIE_OFFICIAL_ACCOUNT_QRCODE)
5296
}
5397

5498
override fun contactSelfSignature(signature: String): Future<Void> {
55-
TODO("Not yet implemented")
99+
log.info("MockPuppet contactSelfSignature($signature)")
100+
return CompletableFuture.completedFuture(null)
56101
}
57102

58103
override fun tagContactAdd(tagId: String, contactId: String): Future<Void> {
59-
TODO("Not yet implemented")
104+
log.info("MockPuppet tagContactAdd($tagId,$contactId)")
105+
return CompletableFuture.completedFuture(null)
60106
}
61107

62108
override fun tagContactDelete(tagId: String): Future<Void> {
63-
TODO("Not yet implemented")
109+
log.info("MockPuppet tagContactDelete($tagId)")
110+
return CompletableFuture.completedFuture(null)
64111
}
65112

66113
override fun tagContactList(contactId: String): Future<List<String>> {
67-
TODO("Not yet implemented")
114+
log.info("MockPuppet tagContactList($contactId)")
115+
return CompletableFuture.completedFuture(listOf())
68116
}
69117

70118
override fun tagContactList(): Future<List<String>> {
71-
TODO("Not yet implemented")
119+
log.info("MockPuppet tagContactList()")
120+
return CompletableFuture.completedFuture(listOf())
72121
}
73122

74123
override fun tagContactRemove(tagId: String, contactId: String): Future<Void> {
75-
TODO("Not yet implemented")
124+
log.info("MockPuppet tagContactRemove($tagId,$contactId)")
125+
return CompletableFuture.completedFuture(null)
76126
}
77127

78128
override fun contactAlias(contactId: String): Future<String> {
79-
TODO("Not yet implemented")
129+
log.info("MockPuppet contactAlias($contactId)")
130+
return CompletableFuture.completedFuture("mock alias")
80131
}
81132

82133
override fun contactAlias(contactId: String, alias: String?): Future<Void> {
83-
TODO("Not yet implemented")
134+
log.info("MockPuppet contactAlias($contactId,$alias)")
135+
return CompletableFuture.completedFuture(null)
84136
}
85137

86138
override fun getContactAvatar(contactId: String): Future<FileBox> {
139+
log.info("MockPuppet contactAvatar($contactId)")
87140
TODO("Not yet implemented")
88141
}
89142

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.github.wechaty
2+
3+
import io.github.wechaty.filebox.FileBox
4+
5+
/**
6+
* config for mock
7+
* @author renxiaoya
8+
* @date 2020-06-02
9+
**/
10+
const val CHATIE_OFFICIAL_ACCOUNT_QRCODE = "http://weixin.qq.com/r/qymXj7DEO_1ErfTs93y5"
11+
12+
fun qrCodeForChatie(): FileBox {
13+
return FileBox.fromQRCode(CHATIE_OFFICIAL_ACCOUNT_QRCODE)
14+
}

wechaty-puppet-mock/src/main/kotlin/wechaty/utils/MockitoHelper.kt renamed to wechaty-puppet-mock/src/main/kotlin/io/github/wechaty/utils/MockitoHelper.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package wechaty.utils
1+
package io.github.wechaty.utils
22

33
import org.mockito.Mockito
44

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ abstract class Puppet : EventEmitter {
4444

4545
private val executorService = Executors.newSingleThreadScheduledExecutor()
4646

47-
private lateinit var cacheContactPayload: Cache<String, ContactPayload>
48-
private lateinit var cacheFriendshipPayload: Cache<String, FriendshipPayload>
49-
private lateinit var cacheMessagePayload: Cache<String, MessagePayload>
50-
private lateinit var cacheRoomPayload: Cache<String, RoomPayload>
51-
private lateinit var cacheRoomMemberPayload: Cache<String, RoomMemberPayload>
52-
private lateinit var cacheRoomInvitationPayload: Cache<String, RoomInvitationPayload>
47+
protected lateinit var cacheContactPayload: Cache<String, ContactPayload>
48+
protected lateinit var cacheFriendshipPayload: Cache<String, FriendshipPayload>
49+
protected lateinit var cacheMessagePayload: Cache<String, MessagePayload>
50+
protected lateinit var cacheRoomPayload: Cache<String, RoomPayload>
51+
protected lateinit var cacheRoomMemberPayload: Cache<String, RoomMemberPayload>
52+
protected lateinit var cacheRoomInvitationPayload: Cache<String, RoomInvitationPayload>
5353
private val count = AtomicLong()
5454
private var id: String? = null
5555
protected var puppetOptions: PuppetOptions? = null

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ enum class ContractGender(var code: Int) {
1717
}
1818
}
1919

20-
enum class ContractType(var code: Int) {
20+
enum class ContactType(var code: Int) {
2121
Unknown(0), Personal(1), Official(2);
2222

2323
companion object {
24-
fun getByCode(code: Int): ContractType {
24+
fun getByCode(code: Int): ContactType {
2525
val values = values()
2626
for (value in values) {
2727
if (value.code == code) {
@@ -47,7 +47,7 @@ class ContactQueryFilter {
4747

4848
class ContactPayload(val id:String) {
4949
var gender: ContractGender? = null
50-
var type: ContractType? = null
50+
var type: ContactType? = null
5151
var name: String? = null
5252
var avatar: String? = null
5353
var address: String? = null

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ data class EventRoomTopicPayload(
7979
var newTopic:String,
8080
var oldTopic:String,
8181
var roomId:String,
82-
var timstamp:Long
82+
var timestamp:Long
8383
) {
8484
override fun toString(): String {
85-
return "EventRoomTopicePayload(changerId='$changerId', newTopic='$newTopic', oldTopic='$oldTopic', roomId='$roomId', timstamp=$timstamp)"
85+
return "EventRoomTopicePayload(changerId='$changerId', newTopic='$newTopic', oldTopic='$oldTopic', roomId='$roomId', timestamp=$timestamp)"
8686
}
8787
}
8888

0 commit comments

Comments
 (0)