Skip to content

Commit

Permalink
added --dinner-chat startup param, code quality optimization.
Browse files Browse the repository at this point in the history
- added `--dinner-chat`/`-chd` startup param for setup dinner chat id
- fixed some unclosed resource
  - TrackerDataManager changed method to get FileChannel (I don't know if it works)
  - MornyCLI scanner uses try-with-resources
- declared directly used dependencies
  - okhttp: 4.11.0
  - gson: 2.10.1
- some minor refactors
  - MornyCoeur.main rename to .init
  - added package `internal` and move @BuildConfigField to it
  - added new util class OkHttpPublic with MediaTypes in use
  - added javadoc for TrackerDataManager
  - and so on...
  • Loading branch information
Eyre-S committed Aug 20, 2023
1 parent 69a3393 commit 7589e86
Show file tree
Hide file tree
Showing 14 changed files with 133 additions and 69 deletions.
18 changes: 11 additions & 7 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
id 'application'
id 'maven-publish'
id 'com.github.johnrengelman.shadow' version '8.1.1'
id 'com.github.gmazzo.buildconfig' version '3.1.0'
id 'com.github.gmazzo.buildconfig' version '4.1.2'
id 'org.ajoberstar.grgit' version '5.2.0'
}

Expand Down Expand Up @@ -73,15 +73,16 @@ repositories {

dependencies {

compileOnlyApi "com.github.spotbugs:spotbugs-annotations:${libSpotbugsVersion}"
compileOnlyApi "com.github.spotbugs:spotbugs-annotations:${lib_spotbugs_v}"

api "cc.sukazyo:messiva:${libMessivaVersion}"
api "cc.sukazyo:messiva:${lib_messiva_v}"

implementation "com.github.pengrad:java-telegram-bot-api:${libJavaTelegramBotApiVersion}"
implementation "com.github.pengrad:java-telegram-bot-api:${lib_javatelegramapi_v}"
implementation "com.squareup.okhttp3:okhttp:${lib_okhttp_v}"
implementation "com.google.code.gson:gson:${lib_gson_v}"

testImplementation "org.junit.jupiter:junit-jupiter-api:${libJunitVersion}"
testImplementation "org.junit.jupiter:junit-jupiter-params:${libJunitVersion}"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${libJunitVersion}"
testImplementation platform("org.junit:junit-bom:${lib_junit_v}")
testImplementation "org.junit.jupiter:junit-jupiter"

}

Expand All @@ -91,6 +92,9 @@ application {

test {
useJUnitPlatform()
testLogging {
events "passed", "skipped", "failed"
}
}

java {
Expand Down
13 changes: 8 additions & 5 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ MORNY_ARCHIVE_NAME = morny-coeur
MORNY_CODE_STORE = https://github.com/Eyre-S/Coeur-Morny-Cono
MORNY_COMMIT_PATH = https://github.com/Eyre-S/Coeur-Morny-Cono/commit/%s

VERSION = 1.0.0-RC3.8
VERSION = 1.0.0-RC3.9

USE_DELTA = false
VERSION_DELTA =
Expand All @@ -14,10 +14,13 @@ CODENAME = beiping

# dependencies

libSpotbugsVersion = 4.7.3
lib_spotbugs_v = 4.7.3

libMessivaVersion = 0.1.1
lib_messiva_v = 0.1.1

libJavaTelegramBotApiVersion = 6.2.0
lib_javatelegramapi_v = 6.2.0

libJunitVersion = 5.9.0
lib_okhttp_v = 4.11.0
lib_gson_v = 2.10.1

lib_junit_v = 5.10.0
2 changes: 1 addition & 1 deletion src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private MornyCoeur (MornyConfig config) {
* @see #MornyCoeur 程序初始化方法
* @param config morny 实例的配置选项数据
*/
public static void main (MornyConfig config) {
public static void init (MornyConfig config) {
if (INSTANCE == null) {

logger.info("Coeur Starting");
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/cc/sukazyo/cono/morny/MornyConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,12 @@ public static class Prototype {
public long eventOutdatedTimestamp = -1;
public boolean commandLoginRefresh = false;
public boolean commandLogoutClear = false;
@Nonnull public Set<Long> dinnerTrustedReaders = new HashSet<>();
@Nonnull public final Set<Long> dinnerTrustedReaders = new HashSet<>();
public long dinnerChatId = -1001707106392L;
public long reportToChat = -1001650050443L;
public long medicationNotifyToChat = -1001729016815L;
@Nonnull public ZoneOffset medicationTimerUseTimezone = ZoneOffset.UTC;
@Nonnull public Set<Integer> medicationNotifyAt = new HashSet<>();
@Nonnull public final Set<Integer> medicationNotifyAt = new HashSet<>();

}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cc/sukazyo/cono/morny/MornySystem.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cc.sukazyo.cono.morny;

import cc.sukazyo.cono.morny.daemon.MornyReport;
import cc.sukazyo.cono.morny.util.BuildConfigField;
import cc.sukazyo.cono.morny.internal.BuildConfigField;
import cc.sukazyo.cono.morny.util.FileUtils;

import javax.annotation.Nonnull;
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/cc/sukazyo/cono/morny/ServerMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public class ServerMain {
* <b>自 {@code 0.5.0.4},旧的直接通过参数为 bot token & username 赋值的方式已被删除</b>
* 使用参数所进行取值的 token 和 username 已被转移至 {@code --token} 和 {@code --username} 参数<br>
*
* @see MornyCoeur#main
* @see MornyCoeur#init
* @since 0.4.0.0
* @param args 参数组
*/
Expand Down Expand Up @@ -143,12 +143,17 @@ public static void main (@Nonnull String[] args) {
config.trustedChat = Long.parseLong(args[i]);
continue;
}
//noinspection SpellCheckingInspection
// noinspection SpellCheckingInspection
case "--trusted-reader-dinner", "-trsd" -> {
i++;
config.dinnerTrustedReaders.add(Long.parseLong(args[i]));
continue;
}
case "--dinner-chat", "-chd" -> {
i++;
config.dinnerChatId = Long.parseLong(args[i]);
continue;
}
case "--auto-cmd", "-cmd", "-c" -> {
config.commandLoginRefresh = true;
config.commandLogoutClear = true;
Expand Down Expand Up @@ -278,7 +283,7 @@ public static void main (@Nonnull String[] args) {

Thread.currentThread().setName(THREAD_MORNY_INIT);
try {
MornyCoeur.main(new MornyConfig(config));
MornyCoeur.init(new MornyConfig(config));
} catch (MornyConfig.CheckFailure.NullTelegramBotKey ignore) {
logger.info("Parameter required has no value:\n --token.");
} catch (MornyConfig.CheckFailure e) {
Expand Down
37 changes: 22 additions & 15 deletions src/main/java/cc/sukazyo/cono/morny/bot/command/EventHack.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,50 +37,57 @@ public class EventHack implements ITelegramCommand {
@Override
public void execute (@Nonnull InputCommand command, @Nonnull Update event) {

boolean isOk = false;
enum Status {
OK,
FORBIDDEN_FOR_ANY
}
Status status;

String x_mode = "";
if (command.hasArgs()) {
x_mode = command.getArgs()[0];
}

switch (x_mode) {
case "any":
case "any" -> {
if (MornyCoeur.trustedInstance().isTrusted(event.message().from().id())) {
OnEventHackHandle.registerHack(
event.message().messageId(),
event.message().from().id(),
event.message().chat().id(),
OnEventHackHandle.HackType.ANY
);isOk = true;
);
status = Status.OK;
} else {
status = Status.FORBIDDEN_FOR_ANY;
}
break;
case "group":
}
case "group" -> {
OnEventHackHandle.registerHack(
event.message().messageId(),
event.message().from().id(),
event.message().chat().id(),
OnEventHackHandle.HackType.GROUP
);isOk = true;
break;
default:
);
status = Status.OK;
}
default -> {
OnEventHackHandle.registerHack(
event.message().messageId(),
event.message().from().id(),
event.message().chat().id(),
OnEventHackHandle.HackType.USER
);isOk = true;
break;
);
status = Status.OK;
}
}

if (isOk) {
MornyCoeur.extra().exec(new SendSticker(
switch (status) {
case OK -> MornyCoeur.extra().exec(new SendSticker(
event.message().chat().id(),
TelegramStickers.ID_WAITING
).replyToMessageId(event.message().messageId())
);
} else {
MornyCoeur.extra().exec(new SendSticker(
case FORBIDDEN_FOR_ANY -> MornyCoeur.extra().exec(new SendSticker(
event.message().chat().id(),
TelegramStickers.ID_403
).replyToMessageId(event.message().messageId())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

public class OnRandomlyTriggered extends EventListener {

/**
* function CODE_IK0XA1
*/
// @Override
// /**
// * function CODE_IK0XA1
// */
// // @Override
// public boolean onMessage (@Nonnull Update update) {
//
// if (update.message().text() == null) return false;
Expand Down
46 changes: 42 additions & 4 deletions src/main/java/cc/sukazyo/cono/morny/daemon/TrackerDataManager.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package cc.sukazyo.cono.morny.daemon;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.StandardOpenOption;
import java.util.HashMap;
import java.util.TreeSet;
import java.util.concurrent.locks.ReentrantLock;
Expand All @@ -15,9 +15,11 @@

public class TrackerDataManager {

/** {@link TrackerDaemon} 的锁。保证在程序中只有一个 TrackerDaemon 会运行。 */
public static final ReentrantLock trackingLock = new ReentrantLock();

/** {@link #record Tracker 缓存}的锁 <p> 为保证对 Tracker 缓存的操作不会造成线程冲突,在操作缓存数据前应先取得此锁。 */
private static final ReentrantLock recordLock = new ReentrantLock();
/** Tracker 数据的内存缓存 <p> 进行数据操作前请先取得对应的{@link #recordLock 锁} */
private static HashMap<Long, HashMap<Long, TreeSet<Long>>> record = new HashMap<>();

public static final TrackerDaemon DAEMON = new TrackerDaemon();
Expand Down Expand Up @@ -55,6 +57,15 @@ public void run () {

}

/**
* 向 Tracker 缓存写入一条 tracker 数据.
* <p>
* <font color=green>这个方法对于 Tracker 缓存是原子化的。</font>
*
* @param chat tracker 所属的 Telegram Chat ID
* @param user tracker 所记录的 Telegram User ID
* @param timestamp tracker 被生成时的 UTC 时间戳
*/
public static void record (long chat, long user, long timestamp) {
recordLock.lock();
if (!record.containsKey(chat)) record.put(chat, new HashMap<>());
Expand All @@ -65,16 +76,36 @@ public static void record (long chat, long user, long timestamp) {
recordLock.unlock();
}

/**
* 开启 {@link TrackerDaemon}.
* <p>
* <font color=orange>由于 Tracker 已废弃,这个方法已无作用。</font>
*/
@SuppressWarnings("unused")
public static void init () {
DAEMON.start();
}

/**
* 执行 Tracker 的保存逻辑.
* @see #reset() 弹出 Tracker 缓存
* @see #save(HashMap) 执行硬盘写入操作
*/
public static void save () {
logger.info("start writing tracker data.");
save(reset());
logger.info("done writing tracker data.");
}

/**
* 将 Tracker 的缓存数据弹出.
* <p>
* 这个方法将返回现在 Tracker 的所有缓存数据,然后清除缓存。
* <p>
* <font color=green>这个方法对于 Tracker 缓存是原子化的。</font>
*
* @return 当前 Tracker 所包含的内容
*/
private static HashMap<Long, HashMap<Long, TreeSet<Long>>> reset () {
recordLock.lock();
HashMap<Long, HashMap<Long, TreeSet<Long>>> recordOld = record;
Expand All @@ -83,6 +114,11 @@ record = new HashMap<>();
return recordOld;
}

/**
* 将 Tracker 数据写入到硬盘.
*
* @param record 需要保存的 Tracker 数据集
*/
private static void save (HashMap<Long, HashMap<Long, TreeSet<Long>>> record) {

{
Expand All @@ -109,10 +145,12 @@ private static void save (HashMap<Long, HashMap<Long, TreeSet<Long>>> record) {
}

assert channelCurrent != null;
channelCurrent.write(ByteBuffer.wrap(
final int result = channelCurrent.write(ByteBuffer.wrap(
String.format("%d\n", timestamp).getBytes(StandardCharsets.UTF_8)
));

if (result == 0) logger.warn("writing tracker data %d/%d/%d: write only 0 bytes! is anything wrong?");

} catch (Exception e) {
final String message = String.format("exception in write tracker data: %d/%d/%d", chat, user, timestamp);
logger.error(message);
Expand All @@ -130,7 +168,7 @@ private static FileChannel openFile (long chat, long user, long day) throws IOEx
if (!data.isDirectory()) if (!data.mkdirs()) throw new IOException("Cannot create file directory " + data.getPath());
File file = new File(data, String.valueOf(day));
if (!file.isFile()) if (!file.createNewFile()) throw new IOException("Cannot create file " + file.getPath());
return new FileOutputStream(file, true).getChannel();
return FileChannel.open(file.toPath(), StandardOpenOption.APPEND);
}

}
9 changes: 3 additions & 6 deletions src/main/java/cc/sukazyo/cono/morny/data/NbnhhshQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import com.google.gson.Gson;

import okhttp3.MediaType;
import cc.sukazyo.cono.morny.util.OkHttpPublic.MediaTypes;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
Expand All @@ -23,21 +23,18 @@ public static class GuessResult {
public Word[] words;
}

public record GuessReq (String text) {
}
public record GuessReq (String text) {}

public static final String API_URL = "https://lab.magiconch.com/api/nbnhhsh/";
public static final String API_GUESS_METHOD = "guess/";
public static final String API_GUESS_DATA_TEMPLATE = "{ \"text\": \"%s\" }";

private static final OkHttpClient httpClient = new OkHttpClient();
public static final MediaType JSON = MediaType.get("application/json; charset=utf-8");

public static GuessResult sendGuess (String text) throws IOException {
final String reqJsonText = new Gson().toJson(new GuessReq(text));
Request request = new Request.Builder()
.url(API_URL + API_GUESS_METHOD)
.post(RequestBody.create(JSON, reqJsonText))
.post(RequestBody.create(reqJsonText, MediaTypes.JSON))
.build();
try (Response response = httpClient.newCall(request).execute()) {
final ResponseBody body = response.body();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cc.sukazyo.cono.morny.util;
package cc.sukazyo.cono.morny.internal;


import java.lang.annotation.Documented;
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/cc/sukazyo/cono/morny/util/OkHttpPublic.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package cc.sukazyo.cono.morny.util;

import okhttp3.MediaType;

public class OkHttpPublic {

public static class MediaTypes {

public static final MediaType JSON = MediaType.get("application/json; charset=utf-8");

}

}
Loading

0 comments on commit 7589e86

Please sign in to comment.