-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
2,208 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package telegram.files; | ||
|
||
import cn.hutool.core.collection.CollUtil; | ||
import cn.hutool.log.Log; | ||
import cn.hutool.log.LogFactory; | ||
import io.vertx.core.Future; | ||
import telegram.files.repository.SettingAutoRecords; | ||
import telegram.files.repository.SettingKey; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.function.Consumer; | ||
|
||
public class AutoRecordsHolder { | ||
private final Log log = LogFactory.get(); | ||
|
||
private final SettingAutoRecords autoRecords = new SettingAutoRecords(); | ||
|
||
private final List<Consumer<List<SettingAutoRecords.Item>>> onRemoveListeners = new ArrayList<>(); | ||
|
||
public AutoRecordsHolder() { | ||
} | ||
|
||
public SettingAutoRecords autoRecords() { | ||
return autoRecords; | ||
} | ||
|
||
public void registerOnRemoveListener(Consumer<List<SettingAutoRecords.Item>> onRemove) { | ||
onRemoveListeners.add(onRemove); | ||
} | ||
|
||
public Future<Void> init() { | ||
return DataVerticle.settingRepository.<SettingAutoRecords>getByKey(SettingKey.autoDownload) | ||
.onSuccess(settingAutoRecords -> { | ||
if (settingAutoRecords == null) { | ||
return; | ||
} | ||
settingAutoRecords.items.forEach(item -> HttpVerticle.getTelegramVerticle(item.telegramId) | ||
.ifPresentOrElse(telegramVerticle -> { | ||
if (telegramVerticle.authorized) { | ||
autoRecords.add(item); | ||
} else { | ||
log.warn("Init auto records fail. Telegram verticle not authorized: %s".formatted(item.telegramId)); | ||
} | ||
}, () -> log.warn("Init auto records fail. Telegram verticle not found: %s".formatted(item.telegramId)))); | ||
}) | ||
.onFailure(e -> log.error("Init auto records failed!", e)) | ||
.mapEmpty(); | ||
} | ||
|
||
public void onAutoRecordsUpdate(SettingAutoRecords records) { | ||
for (SettingAutoRecords.Item item : records.items) { | ||
if (!autoRecords.exists(item.telegramId, item.chatId)) { | ||
// new enabled | ||
HttpVerticle.getTelegramVerticle(item.telegramId) | ||
.ifPresentOrElse(telegramVerticle -> { | ||
if (telegramVerticle.authorized) { | ||
autoRecords.add(item); | ||
log.info("Add auto records success: %s".formatted(item.uniqueKey())); | ||
} else { | ||
log.warn("Add auto records fail. Telegram verticle not authorized: %s".formatted(item.telegramId)); | ||
} | ||
}, () -> log.warn("Add auto records fail. Telegram verticle not found: %s".formatted(item.telegramId))); | ||
} | ||
} | ||
// remove disabled | ||
List<SettingAutoRecords.Item> removedItems = new ArrayList<>(); | ||
autoRecords.items.removeIf(item -> { | ||
if (records.exists(item.telegramId, item.chatId)) { | ||
return false; | ||
} | ||
removedItems.add(item); | ||
log.info("Remove auto records success: %s".formatted(item.uniqueKey())); | ||
return true; | ||
}); | ||
if (CollUtil.isNotEmpty(removedItems)) { | ||
onRemoveListeners.forEach(listener -> listener.accept(removedItems)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.