Skip to content

Commit

Permalink
✨ feat: Add automatic download rule settings.
Browse files Browse the repository at this point in the history
  • Loading branch information
jarvis2f committed Jan 7, 2025
1 parent 88f65da commit 0c5016e
Show file tree
Hide file tree
Showing 11 changed files with 716 additions and 67 deletions.
35 changes: 28 additions & 7 deletions api/src/main/java/telegram/files/AutoDownloadVerticle.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import io.vertx.core.json.Json;
import io.vertx.core.json.JsonObject;
import org.drinkless.tdlib.TdApi;
import org.jooq.lambda.tuple.Tuple2;
import telegram.files.repository.FileRecord;
import telegram.files.repository.SettingAutoRecords;
import telegram.files.repository.SettingKey;
Expand All @@ -34,7 +35,7 @@ public class AutoDownloadVerticle extends AbstractVerticle {

private static final int DOWNLOAD_INTERVAL = 10 * 1000;

private static final List<String> FILE_TYPE_ORDER = List.of("photo", "video", "audio", "file");
private static final List<String> DEFAULT_FILE_TYPE_ORDER = List.of("photo", "video", "audio", "file");

// telegramId -> messages
private final Map<Long, LinkedList<TdApi.Message>> waitingDownloadMessages = new ConcurrentHashMap<>();
Expand Down Expand Up @@ -137,11 +138,11 @@ private void addHistoryMessage(SettingAutoRecords.Item auto, long currentTimeMil
log.debug("Scan history end! TelegramId: %d ChatId: %d".formatted(auto.telegramId, auto.chatId));
return;
}
if (StrUtil.isBlank(auto.nextFileType)) {
auto.nextFileType = FILE_TYPE_ORDER.getFirst();
}
Tuple2<String, List<String>> rule = handleRule(auto);

TelegramVerticle telegramVerticle = this.getTelegramVerticle(auto.telegramId);
TdApi.SearchChatMessages searchChatMessages = new TdApi.SearchChatMessages();
searchChatMessages.query = rule.v1;
searchChatMessages.chatId = auto.chatId;
searchChatMessages.fromMessageId = auto.nextFromMessageId;
searchChatMessages.limit = Math.min(MAX_WAITING_LENGTH, 100);
Expand All @@ -153,10 +154,11 @@ private void addHistoryMessage(SettingAutoRecords.Item auto, long currentTimeMil
return;
}
if (foundChatMessages.messages.length == 0) {
int nextTypeIndex = FILE_TYPE_ORDER.indexOf(auto.nextFileType) + 1;
if (nextTypeIndex < FILE_TYPE_ORDER.size()) {
List<String> fileTypes = rule.v2;
int nextTypeIndex = fileTypes.indexOf(auto.nextFileType) + 1;
if (nextTypeIndex < fileTypes.size()) {
String originalType = auto.nextFileType;
auto.nextFileType = FILE_TYPE_ORDER.get(nextTypeIndex);
auto.nextFileType = fileTypes.get(nextTypeIndex);
auto.nextFromMessageId = 0;
log.debug("%s No more %s files found! Switch to %s".formatted(auto.uniqueKey(), originalType, auto.nextFileType));
addHistoryMessage(auto, currentTimeMillis);
Expand All @@ -181,6 +183,25 @@ private void addHistoryMessage(SettingAutoRecords.Item auto, long currentTimeMil
}
}

private Tuple2<String, List<String>> handleRule(SettingAutoRecords.Item auto) {
SettingAutoRecords.Rule rule = auto.rule;
String query = null;
List<String> fileTypes = DEFAULT_FILE_TYPE_ORDER;
if (rule != null) {
if (StrUtil.isNotBlank(rule.query)) {
query = rule.query;
}
if (CollUtil.isNotEmpty(rule.fileTypes)) {
fileTypes = rule.fileTypes;
}
}
if (StrUtil.isBlank(auto.nextFileType)) {
auto.nextFileType = fileTypes.getFirst();
}

return new Tuple2<>(query, fileTypes);
}

private boolean isExceedLimit(long telegramId) {
List<TdApi.Message> waitingMessages = this.waitingDownloadMessages.get(telegramId);
return getSurplusSize(telegramId) <= 0 || (waitingMessages != null && waitingMessages.size() > limit);
Expand Down
4 changes: 2 additions & 2 deletions api/src/main/java/telegram/files/HttpVerticle.java
Original file line number Diff line number Diff line change
Expand Up @@ -605,8 +605,8 @@ private void handleAutoDownload(RoutingContext ctx) {
ctx.fail(400);
return;
}

telegramVerticle.toggleAutoDownload(Convert.toLong(chatId))
JsonObject params = ctx.body().asJsonObject();
telegramVerticle.toggleAutoDownload(Convert.toLong(chatId), params)
.onSuccess(r -> ctx.end())
.onFailure(ctx::fail);
}
Expand Down
41 changes: 26 additions & 15 deletions api/src/main/java/telegram/files/TelegramVerticle.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DatePattern;
Expand Down Expand Up @@ -439,7 +440,7 @@ public Future<Void> togglePauseDownload(Integer fileId, boolean isPaused) {
.mapEmpty();
}

public Future<Void> toggleAutoDownload(Long chatId) {
public Future<Void> toggleAutoDownload(Long chatId, JsonObject params) {
return DataVerticle.settingRepository.<SettingAutoRecords>getByKey(SettingKey.autoDownload)
.compose(settingAutoRecords -> {
if (settingAutoRecords == null) {
Expand All @@ -448,7 +449,14 @@ public Future<Void> toggleAutoDownload(Long chatId) {
if (settingAutoRecords.exists(this.telegramRecord.id(), chatId)) {
settingAutoRecords.remove(this.telegramRecord.id(), chatId);
} else {
settingAutoRecords.add(this.telegramRecord.id(), chatId);
SettingAutoRecords.Rule rule = null;
if (params != null && params.containsKey("rule")) {
rule = params.getJsonObject("rule").mapTo(SettingAutoRecords.Rule.class);
if (StrUtil.isBlank(rule.query) && CollUtil.isEmpty(rule.fileTypes)) {
rule = null;
}
}
settingAutoRecords.add(this.telegramRecord.id(), chatId, rule);
}
return DataVerticle.settingRepository.createOrUpdate(SettingKey.autoDownload.name(), Json.encode(settingAutoRecords));
})
Expand Down Expand Up @@ -824,19 +832,22 @@ public void onLogMessage(int verbosityLevel, String message) {

private Future<JsonArray> convertChat(List<TdApi.Chat> chats) {
return DataVerticle.settingRepository.<SettingAutoRecords>getByKey(SettingKey.autoDownload)
.map(settingAutoRecords -> settingAutoRecords == null ? Collections.emptySet()
: settingAutoRecords.getChatIds(this.telegramRecord.id()))
.map(enableAutoChatIds -> new JsonArray(chats.stream()
.map(chat -> new JsonObject()
.put("id", Convert.toStr(chat.id))
.put("name", chat.id == this.telegramRecord.id() ? "Saved Messages" : chat.title)
.put("type", TdApiHelp.getChatType(chat.type))
.put("avatar", Base64.encode((byte[]) BeanUtil.getProperty(chat, "photo.minithumbnail.data")))
.put("unreadCount", chat.unreadCount)
.put("lastMessage", "")
.put("lastMessageTime", "")
.put("autoEnabled", enableAutoChatIds.contains(chat.id))
)
.map(settingAutoRecords -> settingAutoRecords == null ? new HashMap<Long, SettingAutoRecords.Item>()
: settingAutoRecords.getItems(this.telegramRecord.id()))
.map(enableAutoChats -> new JsonArray(chats.stream()
.map(chat -> {
SettingAutoRecords.Item autoItem = enableAutoChats.get(chat.id);
return new JsonObject()
.put("id", Convert.toStr(chat.id))
.put("name", chat.id == this.telegramRecord.id() ? "Saved Messages" : chat.title)
.put("type", TdApiHelp.getChatType(chat.type))
.put("avatar", Base64.encode((byte[]) BeanUtil.getProperty(chat, "photo.minithumbnail.data")))
.put("unreadCount", chat.unreadCount)
.put("lastMessage", "")
.put("lastMessageTime", "")
.put("autoEnabled", autoItem != null)
.put("autoRule", autoItem == null ? null : autoItem.rule);
})
.toList()
));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class SettingAutoRecords {
Expand All @@ -18,19 +19,36 @@ public static class Item {

public long nextFromMessageId;

public Rule rule;

public Item() {
}

public Item(long telegramId, long chatId) {
public Item(long telegramId, long chatId, Rule rule) {
this.telegramId = telegramId;
this.chatId = chatId;
this.rule = rule;
}

public String uniqueKey() {
return telegramId + ":" + chatId;
}
}

public static class Rule {
public String query;

public List<String> fileTypes;

public Rule() {
}

public Rule(String query, List<String> fileTypes) {
this.query = query;
this.fileTypes = fileTypes;
}
}

public SettingAutoRecords() {
this.items = new ArrayList<>();
}
Expand All @@ -48,19 +66,18 @@ public void add(Item item) {
items.add(item);
}

public void add(long telegramId, long chatId) {
items.add(new Item(telegramId, chatId));
public void add(long telegramId, long chatId, Rule rule) {
items.add(new Item(telegramId, chatId, rule));
}

public void remove(long telegramId, long chatId) {
items.removeIf(item -> item.telegramId == telegramId && item.chatId == chatId);
}

public Set<Long> getChatIds(long telegramId) {
public Map<Long, Item> getItems(long telegramId) {
return items.stream()
.filter(item -> item.telegramId == telegramId)
.map(item -> item.chatId)
.collect(Collectors.toSet());
.collect(Collectors.toMap(i -> i.chatId, Function.identity()));
}

}
Loading

0 comments on commit 0c5016e

Please sign in to comment.