作者 crossoverJie

:sparkles: Introducing new features.聊天记录查询

... ... @@ -20,4 +20,11 @@ public interface MsgLogger {
* 停止写入
*/
void stop() ;
/**
* 查询聊天记录
* @param key 关键字
* @return
*/
String query(String key) ;
}
... ...
... ... @@ -15,6 +15,8 @@ import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* Function:
... ... @@ -116,4 +118,29 @@ public class AsyncMsgLogger implements MsgLogger {
started = false;
worker.interrupt();
}
@Override
public String query(String key) {
StringBuilder sb = new StringBuilder();
Path path = Paths.get(appConfiguration.getMsgLoggerPath() + appConfiguration.getUserName() + "/");
try {
Stream<Path> list = Files.list(path);
List<Path> collect = list.collect(Collectors.toList());
for (Path file : collect) {
List<String> strings = Files.readAllLines(file);
for (String msg : strings) {
if (msg.trim().contains(key)) {
sb.append(msg).append("\n");
}
}
}
} catch (IOException e) {
LOGGER.info("IOException", e);
}
return sb.toString().replace(key,"\033[31;4m" + key+"\033[0m");
}
}
... ...
... ... @@ -108,6 +108,10 @@ public class MsgHandler implements MsgHandle {
//打印在线用户
printOnlineUsers();
} else if (msg.startsWith(SystemCommandEnumType.QUERY.getCommandType().trim() + " ")){
String[] split = msg.split(" ") ;
String res = msgLogger.query(split[1]);
System.out.println(res);
}else {
printAllCommand(allStatusCode);
}
... ...
... ... @@ -28,4 +28,12 @@ public class AsyncMsgLoggerTest {
TimeUnit.SECONDS.sleep(2);
}
@Test
public void query(){
String crossoverJie = msgLogger.query("crossoverJie");
System.out.println(crossoverJie);
}
}
\ No newline at end of file
... ...
... ... @@ -126,7 +126,7 @@ public class CommonTest {
"\n" +
"借助 CIM 你可以实现以下需求:" ;
String key = "IM" ;
String key = "CIM" ;
String[] split = allMsg.split("\n");
for (String msg : split) {
... ... @@ -150,8 +150,8 @@ public class CommonTest {
count ++ ;
}
System.out.println(sb.toString().replace(key,"**" + key+"**"));
System.out.println(sb.toString());
System.out.println(sb.toString().replace(key,"\033[31;4m" + key+"\033[0m"));
}
@Test
... ...
... ... @@ -16,7 +16,8 @@ public enum SystemCommandEnumType {
ALL(":all ","获取所有命令"),
ONLINE_USER(":olu ","获取所有在线用户"),
QUIT(":q ","退出程序")
QUIT(":q! ","退出程序"),
QUERY(":q ","查询聊天记录")
;
... ...