添加内联查询结果的缓存时间和仅供个人缓存的适配逻辑,修复 user-information 缓存问题 #32

This commit is contained in:
A.C.Sukazyo Eyre 2022-05-04 11:41:18 +08:00
parent 043a67231e
commit 73a7b23bc2
Signed by: Eyre_S
GPG Key ID: EFB47D98FE082FAD
8 changed files with 71 additions and 17 deletions

View File

@ -1,6 +1,6 @@
## Core ## Core
VERSION = 0.6.2.0 VERSION = 0.6.3.0
# dependencies # dependencies

View File

@ -4,6 +4,6 @@ package cc.sukazyo.cono.morny;
* the final field that will be updated by gradle automatically. * the final field that will be updated by gradle automatically.
*/ */
public class GradleProjectConfigures { public class GradleProjectConfigures {
public static final String VERSION = "0.6.2.0"; public static final String VERSION = "0.6.3.0";
public static final long COMPILE_TIMESTAMP = 1647533461453L; public static final long COMPILE_TIMESTAMP = 1651635619528L;
} }

View File

@ -0,0 +1,36 @@
package cc.sukazyo.cono.morny.bot.api;
import com.pengrad.telegrambot.model.request.InlineQueryResult;
public class InlineQueryUnit<T extends InlineQueryResult<T>> {
public static final int DEFAULT_INLINE_CACHE_TIME = 300;
public static final boolean DEFAULT_INLINE_PERSONAL_RESP = false;
private int cacheTime = DEFAULT_INLINE_CACHE_TIME;
private boolean isPersonal = DEFAULT_INLINE_PERSONAL_RESP;
public final T result;
public InlineQueryUnit (T result) {
this.result = result;
}
public int cacheTime () {
return cacheTime;
}
public InlineQueryUnit<T> cacheTime (int cacheTime) {
this.cacheTime = cacheTime;
return this;
}
public boolean isPersonal () {
return isPersonal;
}
public InlineQueryUnit<T> isPersonal (boolean isPersonal) {
this.isPersonal = isPersonal;
return this;
}
}

View File

@ -2,6 +2,7 @@ package cc.sukazyo.cono.morny.bot.event;
import cc.sukazyo.cono.morny.MornyCoeur; import cc.sukazyo.cono.morny.MornyCoeur;
import cc.sukazyo.cono.morny.bot.api.EventListener; import cc.sukazyo.cono.morny.bot.api.EventListener;
import cc.sukazyo.cono.morny.bot.api.InlineQueryUnit;
import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.model.Update;
import com.pengrad.telegrambot.model.request.InlineQueryResult; import com.pengrad.telegrambot.model.request.InlineQueryResult;
import com.pengrad.telegrambot.request.AnswerInlineQuery; import com.pengrad.telegrambot.request.AnswerInlineQuery;
@ -23,11 +24,22 @@ public class OnInlineQueries extends EventListener {
@Override @Override
public boolean onInlineQuery (@Nonnull Update update) { public boolean onInlineQuery (@Nonnull Update update) {
List<InlineQueryResult<?>> results = MornyCoeur.queryManager().query(update); List<InlineQueryUnit<?>> results = MornyCoeur.queryManager().query(update);
int cacheTime = Integer.MAX_VALUE;
boolean isPersonal = InlineQueryUnit.DEFAULT_INLINE_PERSONAL_RESP;
InlineQueryResult<?>[] inlineQueryResults = new InlineQueryResult<?>[results.size()];
for (int i = 0; i < results.size(); i++) {
inlineQueryResults[i] = results.get(i).result;
if (cacheTime > results.get(i).cacheTime()) cacheTime = results.get(i).cacheTime();
if (results.get(i).isPersonal()) isPersonal = true;
}
if (results.size() == 0) return false; if (results.size() == 0) return false;
MornyCoeur.extra().exec(new AnswerInlineQuery(update.inlineQuery().id(), results.toArray(InlineQueryResult[]::new))); MornyCoeur.extra().exec(new AnswerInlineQuery(
update.inlineQuery().id(), inlineQueryResults
).cacheTime(cacheTime).isPersonal(isPersonal));
return true; return true;
} }

View File

@ -2,12 +2,13 @@ package cc.sukazyo.cono.morny.bot.query;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import cc.sukazyo.cono.morny.bot.api.InlineQueryUnit;
import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.model.Update;
import com.pengrad.telegrambot.model.request.InlineQueryResult; import com.pengrad.telegrambot.model.request.InlineQueryResult;
public interface ITelegramQuery <T extends InlineQueryResult<T>> { public interface ITelegramQuery <T extends InlineQueryResult<T>> {
@Nullable @Nullable
T query (Update event); InlineQueryUnit<T> query (Update event);
} }

View File

@ -1,7 +1,7 @@
package cc.sukazyo.cono.morny.bot.query; package cc.sukazyo.cono.morny.bot.query;
import cc.sukazyo.cono.morny.bot.api.InlineQueryUnit;
import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.model.Update;
import com.pengrad.telegrambot.model.request.InlineQueryResult;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.util.ArrayList; import java.util.ArrayList;
@ -17,10 +17,10 @@ public class MornyQueries {
} }
@Nonnull @Nonnull
public List<InlineQueryResult<?>> query (@Nonnull Update event) { public List<InlineQueryUnit<?>> query (@Nonnull Update event) {
final List<InlineQueryResult<?>> results = new ArrayList<>(); final List<InlineQueryUnit<?>> results = new ArrayList<>();
for (ITelegramQuery<?> instance : queryInstances) { for (ITelegramQuery<?> instance : queryInstances) {
final InlineQueryResult<?> r = instance.query(event); final InlineQueryUnit<?> r = instance.query(event);
if (r!=null) results.add(r); if (r!=null) results.add(r);
} }
return results; return results;

View File

@ -2,6 +2,7 @@ package cc.sukazyo.cono.morny.bot.query;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import cc.sukazyo.cono.morny.bot.api.InlineQueryUnit;
import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.model.Update;
import com.pengrad.telegrambot.model.request.InlineQueryResultArticle; import com.pengrad.telegrambot.model.request.InlineQueryResultArticle;
import com.pengrad.telegrambot.model.request.InputTextMessageContent; import com.pengrad.telegrambot.model.request.InputTextMessageContent;
@ -16,11 +17,14 @@ public class MyInformation implements ITelegramQuery<InlineQueryResultArticle> {
@Override @Override
@Nullable @Nullable
public InlineQueryResultArticle query(Update event) { public InlineQueryUnit<InlineQueryResultArticle> query(Update event) {
if (!(event.inlineQuery().query() == null || "".equals(event.inlineQuery().query()))) return null; if (!(event.inlineQuery().query() == null || "".equals(event.inlineQuery().query()))) return null;
return new InlineQueryResultArticle(ID_PREFIX, TITLE, new InputTextMessageContent( return new InlineQueryUnit<>(new InlineQueryResultArticle(
TelegramUserInformation.informationOutputHTML(event.inlineQuery().from()) ID_PREFIX, TITLE,
).parseMode(ParseMode.HTML)); new InputTextMessageContent(
TelegramUserInformation.informationOutputHTML(event.inlineQuery().from())
).parseMode(ParseMode.HTML)
)).isPersonal(true).cacheTime(10);
} }
} }

View File

@ -1,5 +1,6 @@
package cc.sukazyo.cono.morny.bot.query; package cc.sukazyo.cono.morny.bot.query;
import cc.sukazyo.cono.morny.bot.api.InlineQueryUnit;
import cc.sukazyo.cono.morny.util.EncryptUtils; import cc.sukazyo.cono.morny.util.EncryptUtils;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -15,13 +16,13 @@ public class RawText implements ITelegramQuery<InlineQueryResultArticle> {
@Override @Override
@Nullable @Nullable
public InlineQueryResultArticle query (Update event) { public InlineQueryUnit<InlineQueryResultArticle> query (Update event) {
if (event.inlineQuery().query() == null || "".equals(event.inlineQuery().query())) return null; if (event.inlineQuery().query() == null || "".equals(event.inlineQuery().query())) return null;
return new InlineQueryResultArticle( return new InlineQueryUnit<>(new InlineQueryResultArticle(
ID_PREFIX + EncryptUtils.encryptByMD5(event.inlineQuery().query()), ID_PREFIX + EncryptUtils.encryptByMD5(event.inlineQuery().query()),
TITLE, TITLE,
new InputTextMessageContent(event.inlineQuery().query()) new InputTextMessageContent(event.inlineQuery().query())
); ));
} }
} }