Skip to content

Commit

Permalink
更新
Browse files Browse the repository at this point in the history
  • Loading branch information
mcchampions committed Jan 31, 2024
1 parent 1f1cebc commit f7bb65e
Show file tree
Hide file tree
Showing 44 changed files with 264 additions and 214 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
import lombok.RequiredArgsConstructor;

import java.util.Date;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.ConcurrentSkipListSet;

/**
* 记录API调用的日志
Expand All @@ -20,7 +20,7 @@ public class ApiResultsLogger {
@NonNull
private final String botAuthorization;

private final ConcurrentSkipListMap<Long, LinkedHashSet<Result>> resultsLogMap = new ConcurrentSkipListMap<>();
private final ConcurrentSkipListMap<Long, Set<Result>> resultsLogMap = new ConcurrentSkipListMap<>();

/**
* 添加API执行结果记录
Expand All @@ -30,14 +30,8 @@ public class ApiResultsLogger {
public void addResult(@NonNull Result result) {
CompletableFuture.runAsync(() -> {
long timestamp = result.getTimestamp();
Set<Result> results = resultsLogMap.get(timestamp);
if (results == null) {
resultsLogMap.put(timestamp, new LinkedHashSet<>(Set.of(result)));
return;
}
synchronized (results) {
results.add(result);
}
Set<Result> results = resultsLogMap.computeIfAbsent(timestamp, k -> new ConcurrentSkipListSet<>());
results.add(result);
});
}

Expand All @@ -46,7 +40,7 @@ public void addResult(@NonNull Result result) {
*
* @return 日志
*/
public CompletableFuture<Map<Long, LinkedHashSet<Result>>> getAllLogs() {
public CompletableFuture<Map<Long, Set<Result>>> getAllLogs() {
return getLogs((Date) null, null);
}

Expand All @@ -57,19 +51,9 @@ public CompletableFuture<Map<Long, LinkedHashSet<Result>>> getAllLogs() {
* @param endDate 结束
* @return 日志
*/
public CompletableFuture<Map<Long, LinkedHashSet<Result>>> getLogs(Date startDate, Date endDate) {
long startTimestamp;
long endTimestamp;
if (startDate == null) {
startTimestamp = 0L;
} else {
startTimestamp = startDate.getTime();
}
if (endDate == null) {
endTimestamp = Long.MAX_VALUE;
} else {
endTimestamp = endDate.getTime();
}
public CompletableFuture<Map<Long, Set<Result>>> getLogs(Date startDate, Date endDate) {
long startTimestamp = startDate == null ? 0L : startDate.getTime();
long endTimestamp = endDate == null ? Long.MAX_VALUE : endDate.getTime();
return getLogs(startTimestamp, endTimestamp);
}

Expand All @@ -80,7 +64,7 @@ public CompletableFuture<Map<Long, LinkedHashSet<Result>>> getLogs(Date startDat
* @param end 结束(样式:yyyy-MM-dd HH:mm:ss)
* @return 日志
*/
public CompletableFuture<Map<Long, LinkedHashSet<Result>>> getLogs(@NonNull String start, @NonNull String end) {
public CompletableFuture<Map<Long, Set<Result>>> getLogs(@NonNull String start, @NonNull String end) {
Date startDate = DateUtil.parse(start, DateUtil.Format_Three);
Date endDate = DateUtil.parse(end, DateUtil.Format_Three);
return getLogs(startDate, endDate);
Expand All @@ -93,12 +77,12 @@ public CompletableFuture<Map<Long, LinkedHashSet<Result>>> getLogs(@NonNull Stri
* @param endTimestamp 结束
* @return 日志
*/
public CompletableFuture<Map<Long, LinkedHashSet<Result>>> getLogs(long startTimestamp, long endTimestamp) {
public CompletableFuture<Map<Long, Set<Result>>> getLogs(long startTimestamp, long endTimestamp) {
return CompletableFuture.supplyAsync(() -> {
Map<Long, LinkedHashSet<Result>> subMap = resultsLogMap.subMap(startTimestamp, true, endTimestamp, true);
ConcurrentSkipListMap<Long, LinkedHashSet<Result>> deepCopiedMap = new ConcurrentSkipListMap<>();
for (Map.Entry<Long, LinkedHashSet<Result>> entry : subMap.entrySet()) {
deepCopiedMap.put(entry.getKey(), (LinkedHashSet<Result>) entry.getValue().clone());
Map<Long, Set<Result>> subMap = resultsLogMap.subMap(startTimestamp, true, endTimestamp, true);
Map<Long, Set<Result>> deepCopiedMap = new ConcurrentSkipListMap<>();
for (Map.Entry<Long, Set<Result>> entry : subMap.entrySet()) {
deepCopiedMap.put(entry.getKey(), new ConcurrentSkipListSet<>(entry.getValue()));
}
return deepCopiedMap;
});
Expand All @@ -113,4 +97,4 @@ public String toString() {
});
return sb.toString().trim();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;

import java.io.File;
Expand All @@ -26,6 +27,7 @@
*/
@Getter
@RequiredArgsConstructor
@Slf4j
public class Bot {
/**
* 机器人唯一标识
Expand All @@ -52,7 +54,7 @@ public class Bot {
* @return authorization
*/
public String getAuthorization() {
return BaseUtil.Authorization(clientId, token);
return BaseUtil.generateAuthorization(clientId, token);
}

/**
Expand Down Expand Up @@ -124,7 +126,7 @@ public ApiResultsLogger getApiResultsLogger() {
if (apiResultsLogger != null) {
return apiResultsLogger;
} else {
DodoOpenJava.LOGGER.error("没有启用日志记录系统就调用Bot#getApiResultsLogger方法,bot:" + getAuthorization());
log.error("没有启用日志记录系统就调用Bot#getApiResultsLogger方法,bot:" + getAuthorization());
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,24 @@

import lombok.Getter;
import lombok.NonNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import lombok.extern.slf4j.Slf4j;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
* API
*/
@Slf4j
public class DodoOpenJava {
public static final String BASEURL = "https://botopen.imdodo.com/api/v2/";

private static final HashSet<Bot> bots = new HashSet<>();

@Getter
private static final ConcurrentHashMap<String, ApiResultsLogger> logMap = new ConcurrentHashMap<>();

public static final Logger LOGGER = LoggerFactory.getLogger(DodoOpenJava.class);
private static final Map<String, ApiResultsLogger> logMap = new ConcurrentHashMap<>();

/**
* 新建 Bot
Expand Down Expand Up @@ -69,7 +68,7 @@ public static void enableApiResultsLogger(Bot bot) {
public static void enableApiResultsLogger(String authorization) {
synchronized (logMap) {
if (logMap.containsKey(authorization)) {
LOGGER.warn("已经调用过DodoOpenJava#enableApiResultsLogger");
log.warn("已经调用过DodoOpenJava#enableApiResultsLogger");
return;
}
logMap.put(authorization, new ApiResultsLogger(authorization));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* api结果
*/
@Getter
public class Result {
public class Result implements Comparable {
public static final JSONObject STATUS_CODE = new JSONObject("""
{
"0": "成功",
Expand Down Expand Up @@ -169,4 +169,9 @@ public <T> T ifFailure(@NonNull Function<Result, T> function) {
public String toString() {
return getJSONObjectData().toString();
}

@Override
public int compareTo(Object o) {
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class BotApi {
* @throws IOException 发送请求失败后抛出
*/
public static Result getBotInfo(String clientId, String token) throws IOException {
return getBotInfo(BaseUtil.Authorization(clientId, token));
return getBotInfo(BaseUtil.generateAuthorization(clientId, token));
}

/**
Expand All @@ -49,7 +49,7 @@ public static Result getBotInfo(String authorization) throws IOException {
* @throws IOException 发送请求失败后抛出
*/
public static Result setBotIslandLeave(String clientId, String token, String islandSourceId) throws IOException {
return setBotIslandLeave(BaseUtil.Authorization(clientId, token), islandSourceId);
return setBotIslandLeave(BaseUtil.generateAuthorization(clientId, token), islandSourceId);
}

/**
Expand Down Expand Up @@ -80,7 +80,7 @@ public static Result setBotIslandLeave(String authorization, String islandSource
*/

public static Result getBotInviteList(String clientId, String token, int pageSize, long maxId) throws IOException {
return getBotInviteList(BaseUtil.Authorization(clientId, token), pageSize, maxId);
return getBotInviteList(BaseUtil.generateAuthorization(clientId, token), pageSize, maxId);
}

/**
Expand Down Expand Up @@ -112,7 +112,7 @@ public static Result getBotInviteList(String authorization, int pageSize, long m
*/

public static Result addBotInvite(String clientId, String token, String dodoSourceId) throws IOException {
return addBotInvite(BaseUtil.Authorization(clientId, token), dodoSourceId);
return addBotInvite(BaseUtil.generateAuthorization(clientId, token), dodoSourceId);
}

/**
Expand Down Expand Up @@ -142,7 +142,7 @@ public static Result addBotInvite(String authorization, String dodoSourceId) thr
*/

public static Result removeBotInvite(String clientId, String token, String dodoSourceId) throws IOException {
return removeBotInvite(BaseUtil.Authorization(clientId, token), dodoSourceId);
return removeBotInvite(BaseUtil.generateAuthorization(clientId, token), dodoSourceId);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class ChannelApi {
* @throws IOException 失败后抛出
*/
public static Result getChannelList(String clientId, String token, String islandSourceId) throws IOException {
return getChannelList(BaseUtil.Authorization(clientId, token), islandSourceId);
return getChannelList(BaseUtil.generateAuthorization(clientId, token), islandSourceId);
}

/**
Expand Down Expand Up @@ -52,7 +52,7 @@ public static Result getChannelList(String authorization, String islandSourceId)
* @throws IOException 失败后抛出
*/
public static Result getChannelInfo(String clientId, String token, String channelId) throws IOException {
return getChannelInfo(BaseUtil.Authorization(clientId, token), channelId);
return getChannelInfo(BaseUtil.generateAuthorization(clientId, token), channelId);
}

/**
Expand Down Expand Up @@ -83,7 +83,7 @@ public static Result getChannelInfo(String authorization, String channelId) thro
* @throws IOException 失败后抛出
*/
public static Result addChannel(String clientId, String token, String islandSourceId, String channelName, int channelType) throws IOException {
return addChannel(BaseUtil.Authorization(clientId, token), islandSourceId, channelName, channelType);
return addChannel(BaseUtil.generateAuthorization(clientId, token), islandSourceId, channelName, channelType);
}

/**
Expand Down Expand Up @@ -118,7 +118,7 @@ public static Result addChannel(String authorization, String islandSourceId, Str
* @throws IOException 失败后抛出
*/
public static Result editChannel(String clientId, String token, String islandSourceId, String channelName, String channelId) throws IOException {
return editChannel(BaseUtil.Authorization(clientId, token), islandSourceId, channelName, channelId);
return editChannel(BaseUtil.generateAuthorization(clientId, token), islandSourceId, channelName, channelId);
}

/**
Expand Down Expand Up @@ -152,7 +152,7 @@ public static Result editChannel(String authorization, String islandSourceId, St
* @throws IOException 失败后抛出
*/
public static Result deleteChannel(String clientId, String token, String islandSourceId, String channelId) throws IOException {
return deleteChannel(BaseUtil.Authorization(clientId, token), islandSourceId, channelId);
return deleteChannel(BaseUtil.generateAuthorization(clientId, token), islandSourceId, channelId);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class ChannelArticleApi {
* @throws IOException 失败后抛出
*/
public static Result addChannelArticle(String clientId, String token, String channelId, String title, String imageUrl, String content) throws IOException {
return addChannelArticle(BaseUtil.Authorization(clientId, token), channelId, title, content, imageUrl);
return addChannelArticle(BaseUtil.generateAuthorization(clientId, token), channelId, title, content, imageUrl);
}

/**
Expand Down Expand Up @@ -63,7 +63,7 @@ public static Result addChannelArticle(String authorization, String channelId, S
* @throws IOException 失败后抛出
*/
public static Result removeChannelArticle(String clientId, String token, int type, String id, String channelId) throws IOException {
return removeChannelArticle(BaseUtil.Authorization(clientId, token), type, id, channelId);
return removeChannelArticle(BaseUtil.generateAuthorization(clientId, token), type, id, channelId);
}

/**
Expand Down
Loading

0 comments on commit f7bb65e

Please sign in to comment.