Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

memory-card and stateswitch #64

Open
wants to merge 49 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
620aeb9
extends Event
LuSingTea Jul 7, 2020
db80819
add some demo
LuSingTea Jul 7, 2020
83cb4b5
add StorageBackend implements
LuSingTea Jul 7, 2020
f9543fa
add MemoryCard.kt
LuSingTea Jul 7, 2020
8312cb2
add MemoryCard.kt method like load save, delete
LuSingTea Jul 7, 2020
7854172
add StateSwitch.kt method like on,off
LuSingTea Jul 7, 2020
3f09b5e
update StorageNop.kt StorageObs.kt StorageS3.kt
LuSingTea Jul 8, 2020
db363ce
简化json存储形式
LuSingTea Jul 8, 2020
50d9131
update StorageObs.kt and MemoryCard.kt
LuSingTea Jul 9, 2020
96bb05f
添加华为云
LuSingTea Jul 9, 2020
62cfbd5
add some onXXX Event
LuSingTea Jul 9, 2020
8fd067c
update contact
LuSingTea Jul 11, 2020
203fced
update format
LuSingTea Jul 11, 2020
10c8c7d
完善obs后端和memory card接口
LuSingTea Jul 11, 2020
4d202d9
add conurrent dependency
LuSingTea Jul 11, 2020
bb44289
update some format
LuSingTea Jul 14, 2020
0236b41
update Storage and StateSwitch
LuSingTea Jul 16, 2020
ab8ee96
修改包名命名错误,添加阿里云oss依赖
LuSingTea Jul 16, 2020
1dc8f51
1. 添加阿里云oss后端
LuSingTea Jul 17, 2020
e47298c
1. 完善阿里云oss后端
LuSingTea Jul 18, 2020
6c5fce5
1. 讲match修改为containsMatchIn
LuSingTea Jul 18, 2020
faa5b6f
1. 修改格式
LuSingTea Jul 18, 2020
62342f3
1. 将方法改为同步
LuSingTea Jul 19, 2020
709fb78
1. 删除多余的close语句
LuSingTea Jul 19, 2020
6cc7707
1. 删除不应该显示的信息
LuSingTea Jul 19, 2020
2925fd7
1. 添加StateSwitchListener
LuSingTea Jul 19, 2020
95add40
1. ready变为runblocking
LuSingTea Jul 19, 2020
37def84
1. 添加StateSwitchListener
LuSingTea Jul 19, 2020
d3e2065
Delete MemoryCardTest.kt
LuSingTea Jul 19, 2020
d029137
1. 添加memorycard和stateswitch测试文件
LuSingTea Jul 21, 2020
d278f8b
Merge remote-tracking branch 'origin/master'
LuSingTea Jul 21, 2020
59a9c33
1. 修改stateSwitch on和off的返回值
LuSingTea Jul 22, 2020
6ab498f
1. 暂时添加了memory和stateswitch
LuSingTea Jul 24, 2020
35efab6
1. 添加获取qrcode,name,signture方法
LuSingTea Jul 24, 2020
2eea542
1. 添加获取owner和avatar方法
LuSingTea Jul 24, 2020
9fe61a1
1. 添加hello, type方法
LuSingTea Jul 24, 2020
a9d0ad2
1. Contact.kt添加type,province等一系列方法
LuSingTea Jul 25, 2020
1170012
1. Contact.kt添加friend方法
LuSingTea Jul 26, 2020
225d494
1. 添加plugin-contrib依赖(忘了为什么添加了)
LuSingTea Aug 3, 2020
c5b240c
1. 改正日志实例的类
LuSingTea Aug 3, 2020
9b4f815
1. 添加FriendShipmanger
LuSingTea Aug 3, 2020
67b62c2
1. 修复查找房间联系人的时候联系人没有payload的bug
LuSingTea Aug 3, 2020
4629b61
1. 简化代码
LuSingTea Aug 3, 2020
66b7270
Merge branch 'master' into master
LuSingTea Aug 3, 2020
1bf44c7
Merge remote-tracking branch 'origin/master'
LuSingTea Aug 3, 2020
6c129a6
1 解决冲突
LuSingTea Aug 3, 2020
b8bf47a
1 删除暂时没用的测试文件
LuSingTea Aug 6, 2020
c594ad8
delete demo file
LuSingTea Aug 6, 2020
fac8b23
1. 获取属性的方法变为getXXX
LuSingTea Aug 9, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@
<artifactId>wechaty</artifactId>
<version>${wechaty.version}</version>
</dependency>
<dependency>
<groupId>io.github.wechaty</groupId>
<artifactId>wechaty-puppet-hostie</artifactId>
<version>${wechaty.version}</version>
</dependency>

<dependency>
<groupId>io.github.wechaty</groupId>
<artifactId>java-wechaty-plugin-contrib</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
Expand Down Expand Up @@ -62,11 +74,7 @@
<version>1.7.30</version>
</dependency>

<dependency>
<groupId>io.github.wechaty</groupId>
<artifactId>java-wechaty-plugin-contrib</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
Expand Down
36 changes: 12 additions & 24 deletions examples/src/main/java/io/github/wechaty/example/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import io.github.wechaty.Wechaty;
import io.github.wechaty.user.Contact;
import io.github.wechaty.user.Room;
import io.github.wechaty.utils.QrcodeUtils;
import okhttp3.OkHttpClient;
Expand All @@ -16,37 +17,24 @@ public class Main {
public static void main(String[] args){

Wechaty bot = Wechaty.instance("your_token")
.onScan((qrcode, statusScanStatus, data) -> System.out.println(QrcodeUtils.getQr(qrcode)))
.onLogin(user -> System.out.println(user))
.onScan((qrcode, statusScanStatus, data) -> {
System.out.println(QrcodeUtils.getQr(qrcode));
System.out.println("Online Image: https://wechaty.github.io/qrcode/" + qrcode);
})
.onLogin(user -> System.out.println(user.name() + "login"))
.onMessage(message -> {
Room room = message.room();
String text = message.text();
if (StringUtils.equals(text, "#ding")) {
Contact from = message.from();
if (StringUtils.equals(text, "ding")) {
if (room != null) {
room.say("dong");
}
else {
// say something to from contact
from.say("hello:" + from.name());
}
}
}).start(true);

// }

// Room room = bot.room();
//
// RoomQueryFilter roomQueryFilter = new RoomQueryFilter();
//
// roomQueryFilter.setTopic("ChatOps - Donut");
//
// Future<List<Room>> all = room.findAll(roomQueryFilter);
//
// List<Room> rooms = all.get();
//
// Room room1 = rooms.get(0);
//
// FileBox fileBox = FileBox.fromFile("dong.jpg", "dong.jpg");
//
// room1.say(fileBox).get();

}


}
1 change: 0 additions & 1 deletion wechaty-puppet-hostie/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,6 @@ class GrpcPuppet(puppetOptions: PuppetOptions) : Puppet(puppetOptions) {
}

override fun contactRawPayload(contractId: String): Future<ContactPayload> {

val request = Contact.ContactPayloadRequest.newBuilder()
.setId(contractId)
.build()
Expand Down
1 change: 1 addition & 0 deletions wechaty-puppet/aaaa.memory-card.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"a":"b","\rparent\nmap":{"a":1,"b":2},"\rparent\nlist":["1","2","3"]}
1 change: 1 addition & 0 deletions wechaty-puppet/default.memory-card.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"a":"b"}
1 change: 1 addition & 0 deletions wechaty-puppet/parent.memory-card.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"a":"b","\rparent\nmap":{"a":1,"b":2},"\rparent\nlist":["1","2","3"]}
37 changes: 26 additions & 11 deletions wechaty-puppet/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
</dependency>

<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-coroutines-core</artifactId>
<version>1.3.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.ben-manes.caffeine/caffeine -->
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
Expand All @@ -40,15 +44,11 @@
<artifactId>commons-lang3</artifactId>
</dependency>

<!-- &lt;!&ndash;log4j2核心包&ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>org.apache.logging.log4j</groupId>-->
<!-- <artifactId>log4j-api</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.apache.logging.log4j</groupId>-->
<!-- <artifactId>log4j-core</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
<version>1.11.636</version>
</dependency>

<!-- &lt;!&ndash;用于与slf4j保持桥接&ndash;&gt;-->
<!-- <dependency>-->
Expand Down Expand Up @@ -105,7 +105,22 @@
<artifactId>okhttp</artifactId>
</dependency>


<!-- https://mvnrepository.com/artifact/com.huaweicloud/esdk-obs-java -->
<dependency>
<groupId>com.huaweicloud</groupId>
<artifactId>esdk-obs-java</artifactId>
<version>3.19.7</version>
</dependency>
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.10.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<scm>
Expand Down
146 changes: 108 additions & 38 deletions wechaty-puppet/src/main/kotlin/Puppet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import io.github.wechaty.io.github.wechaty.watchdag.WatchDog
import io.github.wechaty.io.github.wechaty.watchdag.WatchdogFood
import io.github.wechaty.io.github.wechaty.watchdag.WatchdogListener
import io.github.wechaty.listener.*
import io.github.wechaty.memorycard.MemoryCard
import io.github.wechaty.schemas.*
import io.github.wechaty.utils.FutureUtils
import io.github.wechaty.utils.JsonUtils
import okhttp3.internal.toImmutableList
import org.apache.commons.collections4.CollectionUtils
import org.apache.commons.lang3.StringUtils
import org.slf4j.LoggerFactory
Expand All @@ -42,7 +44,7 @@ abstract class Puppet : EventEmitter {
private val HEARTBEAT_COUNTER = AtomicLong()
private val HOSTIE_KEEPALIVE_TIMEOUT = 15 * 1000L
private val DEFAULT_WATCHDOG_TIMEOUT = 60L
// private var memory: MemoryCard
private var memory: MemoryCard

private val executorService = Executors.newSingleThreadScheduledExecutor()

Expand All @@ -61,15 +63,20 @@ abstract class Puppet : EventEmitter {
/**
*
*/

constructor(puppetOptions: PuppetOptions) {

count.addAndGet(1)
this.puppetOptions = puppetOptions

// this.memory = MemoryCard()
// this.memory.load()

// for test
this.memory = MemoryCard()
try {
this.memory.load()
log.debug("Puppet, constructor() memory.load() done")
}
catch (e: Exception) {
log.warn("Puppet, constructor() memory.load() rejection: {}", e)
}

val timeOut = puppetOptions.timeout ?: DEFAULT_WATCHDOG_TIMEOUT
watchDog = WatchDog(1000 * timeOut, "puppet")
Expand Down Expand Up @@ -441,13 +448,13 @@ abstract class Puppet : EventEmitter {

if (StringUtils.isNotBlank(query.id)) {
stream = stream?.filter {
StringUtils.equals(query.alias, it.alias)
StringUtils.equals(query.id, it.id)
}
}

if (StringUtils.isNotBlank(query.weixin)) {
stream = stream?.filter {
StringUtils.equals(query.alias, it.alias)
StringUtils.equals(query.weixin, it.weixin)
}
}

Expand Down Expand Up @@ -700,8 +707,46 @@ abstract class Puppet : EventEmitter {

}

protected fun messageQueryFilterFactory(query: MessageQueryFilter) {
TODO()
protected fun messageQueryFilterFactory(query: MessageQueryFilter): MessagePayloadFilterFunction {
val clz = query::class.java
val fields = clz.fields
val list = fields.map {
it.name to it.get(query)
}

var filterFunctionList = ArrayList<MessagePayloadFilterFunction>()

list.forEach { pair ->
if (StringUtils.isNotEmpty(pair.second.toString())) {
val filterFunction = if (StringUtils.equals(pair.first.toString(), "textReg")) {
{
payload: MessagePayload -> Boolean
val clazz = payload::class.java
val field = clazz.getField(pair.first)
val realValue = field.get(payload).toString()
Regex(pair.second.toString()).matches(realValue)
}
}
else {
{
payload: MessagePayload -> Boolean
val clazz = payload::class.java
val field = clazz.getField(pair.first)
val realValue = field.get(payload).toString()
StringUtils.equals(realValue, pair.second.toString())
}
}
filterFunctionList.add(filterFunction)
}
}
val allFilterFunction: MessagePayloadFilterFunction = {
payload: MessagePayload ->
filterFunctionList.all {
func -> func(payload)
}
}
return allFilterFunction

}

fun messageForward(conversationId: String, messageId: String): Future<String?> {
Expand Down Expand Up @@ -819,37 +864,36 @@ abstract class Puppet : EventEmitter {
abstract fun roomMemberList(roomId: String): Future<List<String>>

fun roomMemberSearch(roomId: String, query: RoomMemberQueryFilter): Future<List<String>> {
TODO()
}

fun roomReach(query: RoomQueryFilter?): Future<List<String>> {
TODO()
}
return CompletableFuture.supplyAsync {
val roomMemberIdList = roomMemberList(roomId).get()

fun roomValidate(roomId: String): Future<Boolean> {
return CompletableFuture.completedFuture(true)
}
val contactQuery = if (query.name != null || query.contactAlias != null) {
val contactQueryFilter = ContactQueryFilter()
contactQueryFilter.name = query.name
contactQueryFilter
}
else {
val contactQueryFilter = ContactQueryFilter()
contactQueryFilter.alias = query.contactAlias
contactQueryFilter
}

fun roomPayloadCache(roomId: String): RoomPayload? {
return cacheRoomPayload.getIfPresent(roomId)
}
var idList = ArrayList(contactSearch(contactQuery, roomMemberIdList).get())

val memberPayloadList = roomMemberIdList.mapNotNull { x -> roomMemberPayload(roomId, x).get() }

fun roomPayload(roomId: String): Future<RoomPayload> {
return CompletableFuture.supplyAsync {
return@supplyAsync cacheRoomPayload.get(roomId) { t: String ->
val get = roomRawPayload(t).get()
return@get roomRawPayloadParser(get).get()
if (query.roomAlias != null) {
val result = memberPayloadList.filter { payload ->
payload.roomAlias === query.roomAlias
}.map { payload ->
payload.id
}
idList.addAll(result)
}
return@supplyAsync idList;
}
}

fun roomPayloadDirty(roomId: String): Future<Void> {
cacheRoomPayload.invalidate(roomId)
return CompletableFuture.completedFuture(null)
}


fun roomSearch(query: RoomQueryFilter): Future<List<String>> {
return CompletableFuture.supplyAsync {
val allRoomList = roomList().get()
Expand Down Expand Up @@ -879,6 +923,30 @@ abstract class Puppet : EventEmitter {
}
}

fun roomValidate(roomId: String): Future<Boolean> {
return CompletableFuture.completedFuture(true)
}

fun roomPayloadCache(roomId: String): RoomPayload? {
return cacheRoomPayload.getIfPresent(roomId)
}


fun roomPayload(roomId: String): Future<RoomPayload> {
return CompletableFuture.supplyAsync {
return@supplyAsync cacheRoomPayload.get(roomId) { t: String ->
val get = roomRawPayload(t).get()
return@get roomRawPayloadParser(get).get()
}
}
}

fun roomPayloadDirty(roomId: String): Future<Void> {
cacheRoomPayload.invalidate(roomId)
return CompletableFuture.completedFuture(null)
}


/**
* Concat roomId & contactId to one string
*/
Expand Down Expand Up @@ -912,13 +980,15 @@ abstract class Puppet : EventEmitter {
}
}

// fun setMemory(memoryCard: MemoryCard){
// this.memory = memoryCard
// }
fun setMemory(memoryCard: MemoryCard) {
log.debug("Puppet, setMemory()")

// fun getEventBus(): EventBus {
// return eb
// }
this.memory = memoryCard
}

override fun toString(): String {
return "Puppet#${count}<${this.puppetOptions?.name}>(${this.memory.getName()})"
}

companion object {
private val log = LoggerFactory.getLogger(Puppet::class.java)
Expand Down
Loading