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

feat: 新增一个扩展工具面板,支持批量艾特、私聊、屏蔽消息、在线统计功能 #112

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cn.xeblog.commons.entity;

import cn.xeblog.commons.enums.CountEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
* @author 鼓励师
* @date 2023/11/8 17:24
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class StatisticsMsgDTO implements Serializable {

private CountEnum countEnum;

private boolean toAll;
private boolean showDetail;
private int showSize;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package cn.xeblog.commons.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
* 系统状态
*
* @author 鼓励师
* @date 2023/11/1 10:56
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class SystemStateDTO implements Serializable {

private String message;
/**
* 下面两个参数可以做成配置 如果有必要
*/
private Boolean supportStatistics = true;
private Boolean privateChat = true;

public SystemStateDTO(String message) {
this.message = message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public UserMsgDTO(Object content, MsgType msgType, String[] toUsers) {

public enum MsgType {
TEXT,
PRIVATE,
IMAGE
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,9 @@ public enum Action {
/**
* react
*/
REACT;
REACT,
/**
* 统计在线用户
*/
STATISTICS;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package cn.xeblog.commons.enums;

import lombok.Getter;

/**
* @author 鼓励师
* @date 2023/8/30 11:01
*/
@Getter
public enum CountEnum {

COUNTRY("country", "国家"),
PROVINCE("province", "省份"),
CITY("city", "城市"),
ISP("isp", "运营商"),
USER_STATUS("status", "用户状态"),
;

CountEnum(String countType, String desc) {
this.countType = countType;
this.desc = desc;
}

private final String countType;
private final String desc;

public static CountEnum getByIndex(int index) {
for (int i = 0; i < values().length; i++) {
if (index == i) {
return values()[i];
}
}
return null;
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.awt.datatransfer.StringSelection;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -219,6 +221,11 @@ private static void bindPopupMenu() {
}));
}

public static void showSystemMsg(String msg) {
String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("MM/dd HH:mm"));
showSystemMsg(time, msg);
}

public static void showSystemMsg(String time, String msg) {
ConsoleAction.renderText(String.format("[%s] 系统消息:%s\n", time, msg), Style.SYSTEM_MSG);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import cn.xeblog.plugin.cache.DataCache;
import cn.xeblog.plugin.enums.Command;
import cn.xeblog.plugin.listener.MainWindowInitializedEventListener;
import cn.xeblog.plugin.tools.encourage.cache.EncourageCache;
import cn.xeblog.plugin.ui.MainWindow;
import cn.xeblog.plugin.util.CommandHistoryUtils;
import cn.xeblog.plugin.util.UploadUtils;
Expand All @@ -28,8 +29,9 @@
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.*;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;

/**
* @author anlingyi
Expand Down Expand Up @@ -300,46 +302,25 @@ private static void sendMsg() {
Command.handle(content);
} else {
if (DataCache.isOnline) {
if (sendCounter == 0 && System.currentTimeMillis() - sendCounterStartTime < INTERVAL_TIME) {
sendCounterStartTime = 0;
freezeEndTime = System.currentTimeMillis() + FREEZE_TIME;
}

long endTime = freezeEndTime - System.currentTimeMillis();
if (endTime > 0) {
ConsoleAction.showSimpleMsg("消息发送过于频繁,请于" + endTime / 1000 + "s后再发...");
if (checkFreeze()) {
return;
}

String[] toUsers = null;
List<String> toUserList = ReUtil.findAll("(@)([^\\s]+)([\\s]*)", content, 2);
if (CollectionUtil.isNotEmpty(toUserList)) {
List<String> removeList = new ArrayList<>();
for (String toUser : toUserList) {
if (DataCache.getUser(toUser) == null) {
removeList.add(toUser);
}
}
if (!removeList.isEmpty()) {
toUserList.removeAll(removeList);
}
if (!toUserList.isEmpty()) {
toUserList.add(DataCache.username);
toUsers = ArrayUtil.toArray(new HashSet<>(toUserList), String.class);
}
}

if (sendCounter == -1) {
sendCounter = 0;
}
if (++sendCounter >= 6) {
sendCounter = 0;
}
if (sendCounter == 1) {
sendCounterStartTime = System.currentTimeMillis();
UserMsgDTO.MsgType msgType = UserMsgDTO.MsgType.TEXT;
String[] toUsers;
if (EncourageCache.supportPrivateChat && EncourageCache.privateChatUser != null) {
toUsers = new String[]{EncourageCache.privateChatUser.getUsername()};
msgType = UserMsgDTO.MsgType.PRIVATE;
} else if (!EncourageCache.atUsers.isEmpty()) {
// 艾特勾选的用户
toUsers = getToUsers(content, EncourageCache.atUsers.stream().map(User::getUsername).collect(Collectors.toList()));
content += " @批量艾特[" + EncourageCache.atUsers.size() + "]人";
} else {
toUsers = getToUsers(content, Collections.emptyList());
}

MessageAction.send(new UserMsgDTO(content, toUsers), Action.CHAT);
MessageAction.send(new UserMsgDTO(content, msgType, toUsers), Action.CHAT);
} else {
ConsoleAction.showLoginMsg();
}
Expand All @@ -350,6 +331,52 @@ private static void sendMsg() {
ConsoleAction.gotoConsoleLow();
}

private static boolean checkFreeze() {
if (sendCounter == 0 && System.currentTimeMillis() - sendCounterStartTime < INTERVAL_TIME) {
sendCounterStartTime = 0;
freezeEndTime = System.currentTimeMillis() + FREEZE_TIME;
}

long endTime = freezeEndTime - System.currentTimeMillis();
if (endTime > 0) {
ConsoleAction.showSimpleMsg("消息发送过于频繁,请于" + endTime / 1000 + "s后再发...");
return true;
}

if (sendCounter == -1) {
sendCounter = 0;
}
if (++sendCounter >= 6) {
sendCounter = 0;
}
if (sendCounter == 1) {
sendCounterStartTime = System.currentTimeMillis();
}
return false;
}

public static String[] getToUsers(String content, List<String> extraList) {
String[] toUsers = null;
List<String> toUserList = new ArrayList<>(ReUtil.findAll("(@)([^\\s]+)([\\s]*)", content, 2));
toUserList.addAll(extraList);
if (CollectionUtil.isNotEmpty(toUserList)) {
List<String> removeList = new ArrayList<>();
for (String toUser : toUserList) {
if (DataCache.getUser(toUser) == null) {
removeList.add(toUser);
}
}
if (!removeList.isEmpty()) {
toUserList.removeAll(removeList);
}
if (!toUserList.isEmpty()) {
toUserList.add(DataCache.username);
toUsers = ArrayUtil.toArray(new HashSet<>(toUserList), String.class);
}
}
return toUsers;
}

public static void clean() {
contentArea.setText("");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
package cn.xeblog.plugin.action.handler.message;

import cn.hutool.core.util.StrUtil;
import cn.xeblog.commons.entity.Response;
import cn.xeblog.commons.entity.SystemStateDTO;
import cn.xeblog.commons.enums.MessageType;
import cn.xeblog.plugin.action.ConsoleAction;
import cn.xeblog.commons.entity.Response;
import cn.xeblog.plugin.annotation.DoMessage;
import cn.xeblog.plugin.tools.encourage.cache.EncourageCache;

/**
* @author anlingyi
* @date 2020/8/19
*/
@DoMessage(MessageType.SYSTEM)
public class SystemMessageHandler extends AbstractMessageHandler<String> {
public class SystemMessageHandler extends AbstractMessageHandler<Object> {

@Override
protected void process(Response<String> response) {
ConsoleAction.showSystemMsg(response.getTime(), response.getBody());
protected void process(Response<Object> response) {
Object responseBody = response.getBody();
if (responseBody instanceof String) {
ConsoleAction.showSystemMsg(response.getTime(), String.valueOf(responseBody));
} else if (responseBody instanceof SystemStateDTO) {
SystemStateDTO systemStateDTO = (SystemStateDTO) responseBody;
String message = systemStateDTO.getMessage();
if (StrUtil.isNotEmpty(message)) {
ConsoleAction.showSystemMsg(response.getTime(), message);
}
EncourageCache.supportStatistics = systemStateDTO.getSupportStatistics();
EncourageCache.supportPrivateChat = systemStateDTO.getPrivateChat();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import cn.xeblog.plugin.annotation.DoMessage;
import cn.xeblog.plugin.cache.DataCache;
import cn.xeblog.plugin.enums.Style;
import cn.xeblog.plugin.tools.encourage.cache.EncourageCache;
import cn.xeblog.plugin.util.NotifyUtils;
import com.intellij.ide.actions.OpenFileAction;
import com.intellij.openapi.application.ApplicationManager;
Expand Down Expand Up @@ -43,6 +44,14 @@ protected void process(Response<UserMsgDTO> response) {
User user = response.getUser();
UserMsgDTO body = response.getBody();
boolean isImage = body.getMsgType() == UserMsgDTO.MsgType.IMAGE;

if (EncourageCache.checkBlock(user)) {
if (EncourageCache.showTips) {
ConsoleAction.showSystemMsg(response.getTime(), String.format("用户[%s]发了[%s]", user.getUsername(), isImage ? "一张图片" : "一条消息"));
}
return;
}

if (isImage) {
renderImage(response);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import cn.xeblog.commons.enums.UserStatus;
import cn.xeblog.plugin.action.ConnectionAction;
import cn.xeblog.plugin.tools.browser.config.BrowserConfig;
import cn.xeblog.plugin.tools.encourage.cache.EncourageCache;
import cn.xeblog.plugin.tools.read.ReadConfig;
import com.intellij.openapi.project.Project;
import io.netty.channel.Channel;
Expand Down Expand Up @@ -76,7 +77,6 @@ public class DataCache {
public static List<OnlineServer> serverList;

/**

* 阅读配置
*/
public static ReadConfig readConfig = new ReadConfig();
Expand All @@ -94,7 +94,7 @@ public class DataCache {
*/
public static User getUser(String username) {
if (userMap == null) {
return null;
return null;
}

return userMap.get(username);
Expand Down Expand Up @@ -138,6 +138,10 @@ public static void removeUser(User user) {
if (StrUtil.equals(origin.getId(), user.getId())) {
userMap.remove(user.getUsername());
}

if (EncourageCache.privateChatUser != null && StrUtil.equals(origin.getId(), user.getId())) {
EncourageCache.privateChatUser = null;
}
}

public static int getOnlineUserTotal() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
@AllArgsConstructor
public enum Tools {
READ("阅读", false),
BROWSER("浏览器", false);
BROWSER("浏览器", false),
ENCOURAGE("附加工具", true);

/**
* 工具名称
Expand Down
Loading