mirror of
https://github.com/Eyre-S/Coeur-Morny-Cono.git
synced 2024-11-22 19:24:53 +08:00
添加内联查询结果的缓存时间和仅供个人缓存的适配逻辑,修复 user-information 缓存问题 #32
This commit is contained in:
parent
043a67231e
commit
73a7b23bc2
@ -1,6 +1,6 @@
|
||||
## Core
|
||||
|
||||
VERSION = 0.6.2.0
|
||||
VERSION = 0.6.3.0
|
||||
|
||||
# dependencies
|
||||
|
||||
|
@ -4,6 +4,6 @@ package cc.sukazyo.cono.morny;
|
||||
* the final field that will be updated by gradle automatically.
|
||||
*/
|
||||
public class GradleProjectConfigures {
|
||||
public static final String VERSION = "0.6.2.0";
|
||||
public static final long COMPILE_TIMESTAMP = 1647533461453L;
|
||||
public static final String VERSION = "0.6.3.0";
|
||||
public static final long COMPILE_TIMESTAMP = 1651635619528L;
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -2,6 +2,7 @@ package cc.sukazyo.cono.morny.bot.event;
|
||||
|
||||
import cc.sukazyo.cono.morny.MornyCoeur;
|
||||
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.request.InlineQueryResult;
|
||||
import com.pengrad.telegrambot.request.AnswerInlineQuery;
|
||||
@ -23,11 +24,22 @@ public class OnInlineQueries extends EventListener {
|
||||
@Override
|
||||
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;
|
||||
|
||||
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;
|
||||
|
||||
}
|
||||
|
@ -2,12 +2,13 @@ package cc.sukazyo.cono.morny.bot.query;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import cc.sukazyo.cono.morny.bot.api.InlineQueryUnit;
|
||||
import com.pengrad.telegrambot.model.Update;
|
||||
import com.pengrad.telegrambot.model.request.InlineQueryResult;
|
||||
|
||||
public interface ITelegramQuery <T extends InlineQueryResult<T>> {
|
||||
|
||||
@Nullable
|
||||
T query (Update event);
|
||||
InlineQueryUnit<T> query (Update event);
|
||||
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
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.request.InlineQueryResult;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.ArrayList;
|
||||
@ -17,10 +17,10 @@ public class MornyQueries {
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public List<InlineQueryResult<?>> query (@Nonnull Update event) {
|
||||
final List<InlineQueryResult<?>> results = new ArrayList<>();
|
||||
public List<InlineQueryUnit<?>> query (@Nonnull Update event) {
|
||||
final List<InlineQueryUnit<?>> results = new ArrayList<>();
|
||||
for (ITelegramQuery<?> instance : queryInstances) {
|
||||
final InlineQueryResult<?> r = instance.query(event);
|
||||
final InlineQueryUnit<?> r = instance.query(event);
|
||||
if (r!=null) results.add(r);
|
||||
}
|
||||
return results;
|
||||
|
@ -2,6 +2,7 @@ package cc.sukazyo.cono.morny.bot.query;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import cc.sukazyo.cono.morny.bot.api.InlineQueryUnit;
|
||||
import com.pengrad.telegrambot.model.Update;
|
||||
import com.pengrad.telegrambot.model.request.InlineQueryResultArticle;
|
||||
import com.pengrad.telegrambot.model.request.InputTextMessageContent;
|
||||
@ -16,11 +17,14 @@ public class MyInformation implements ITelegramQuery<InlineQueryResultArticle> {
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public InlineQueryResultArticle query(Update event) {
|
||||
public InlineQueryUnit<InlineQueryResultArticle> query(Update event) {
|
||||
if (!(event.inlineQuery().query() == null || "".equals(event.inlineQuery().query()))) return null;
|
||||
return new InlineQueryResultArticle(ID_PREFIX, TITLE, new InputTextMessageContent(
|
||||
TelegramUserInformation.informationOutputHTML(event.inlineQuery().from())
|
||||
).parseMode(ParseMode.HTML));
|
||||
return new InlineQueryUnit<>(new InlineQueryResultArticle(
|
||||
ID_PREFIX, TITLE,
|
||||
new InputTextMessageContent(
|
||||
TelegramUserInformation.informationOutputHTML(event.inlineQuery().from())
|
||||
).parseMode(ParseMode.HTML)
|
||||
)).isPersonal(true).cacheTime(10);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package cc.sukazyo.cono.morny.bot.query;
|
||||
|
||||
import cc.sukazyo.cono.morny.bot.api.InlineQueryUnit;
|
||||
import cc.sukazyo.cono.morny.util.EncryptUtils;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
@ -15,13 +16,13 @@ public class RawText implements ITelegramQuery<InlineQueryResultArticle> {
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public InlineQueryResultArticle query (Update event) {
|
||||
public InlineQueryUnit<InlineQueryResultArticle> query (Update event) {
|
||||
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()),
|
||||
TITLE,
|
||||
new InputTextMessageContent(event.inlineQuery().query())
|
||||
);
|
||||
));
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user