修复可能的崩溃

修复ApiKey配置消失
This commit is contained in:
Nanako 2024-12-10 13:02:15 +08:00
parent 44bbf7582e
commit 15968a8ec6
4 changed files with 78 additions and 21 deletions

View File

@ -1,5 +1,5 @@
{ {
"url": "http://192.168.1.104:3010", "url": "https://nanako.site:51304",
"username": "Nanako", "username": "Nanako",
"password": "Nasiko@90", "password": "Nasiko@90",
"ignoredFiles": [ "ignoredFiles": [

View File

@ -87,16 +87,16 @@ const defaultSauceNAOResponse = {
}; };
let myLogger; let myLogger;
let SauceNAOErrStr; let SauceNAOErrStr;
const sauceNAOApiKeyID = 'sauceNAO_api_key';
class PicSearcherApp extends App_1.App { class PicSearcherApp extends App_1.App {
constructor(info, logger, accessors) { constructor(info, logger, accessors) {
super(info, logger, accessors); super(info, logger, accessors);
this.rootUrl = ''; this.rootUrl = '';
this.sauceNAOApiKey = ''; this.sauceNAOApiKey = 'ac635e5f5011234871260aac1d37ac8360ed979c';
myLogger = logger; myLogger = logger;
} }
async initialize(configurationExtend, environmentRead) { async initialize(configurationExtend, environmentRead) {
this.rootUrl = await environmentRead.getEnvironmentVariables().getValueByName('ROOT_URL'); this.rootUrl = await environmentRead.getEnvironmentVariables().getValueByName('ROOT_URL');
this.sauceNAOApiKey = await environmentRead.getSettings().getValueById('SauceNAOApiKey');
myLogger.log('rootUrl:', this.rootUrl); myLogger.log('rootUrl:', this.rootUrl);
return super.initialize(configurationExtend, environmentRead); return super.initialize(configurationExtend, environmentRead);
} }
@ -114,20 +114,20 @@ class PicSearcherApp extends App_1.App {
await sendSearchResults(modify, message.room, result, author); await sendSearchResults(modify, message.room, result, author);
} }
async onSettingUpdated(setting, configurationModify, read, http) { async onSettingUpdated(setting, configurationModify, read, http) {
if (setting.id === 'SauceNAOApiKey') { if (setting.id === sauceNAOApiKeyID) {
this.sauceNAOApiKey = setting.value; this.sauceNAOApiKey = setting.value;
} }
return super.onSettingUpdated(setting, configurationModify, read, http); return super.onSettingUpdated(setting, configurationModify, read, http);
} }
async extendConfiguration(configuration, environmentRead) { async extendConfiguration(configuration, environmentRead) {
await configuration.settings.provideSetting({ await configuration.settings.provideSetting({
id: 'SauceNAOApiKey', id: sauceNAOApiKeyID,
type: settings_1.SettingType.STRING, type: settings_1.SettingType.STRING,
packageValue: '', packageValue: '',
required: true, required: true,
public: false, public: false,
i18nLabel: 'searcher_api_key_label', i18nLabel: 'sauceNAO_api_key',
i18nDescription: 'searcher_api_key_description', i18nDescription: 'sauceNAO_api_key_desc',
}); });
} }
async sendMessage(textMessage, room, author, modify) { async sendMessage(textMessage, room, author, modify) {
@ -227,10 +227,12 @@ async function sendSearchResults(modify, room, results, appUser) {
continue; continue;
} }
const color = getInterpolatedColor(similarityValue); const color = getInterpolatedColor(similarityValue);
const titleText = data.title || '未知标题';
const attachmentObject = { const attachmentObject = {
title: { title: {
value: `${data.title} (${header.similarity}%)`, value: `${titleText} (${header.similarity}%)`,
}, },
text: header.index_name,
thumbnailUrl: header.thumbnail, thumbnailUrl: header.thumbnail,
collapsed: attachments.length !== 0, collapsed: attachments.length !== 0,
color, color,
@ -249,7 +251,7 @@ async function sendSearchResults(modify, room, results, appUser) {
}); });
} }
} }
if (data.pixiv_id) { if (data.member_id) {
attachmentObject.actions.push({ attachmentObject.actions.push({
type: messages_1.MessageActionType.BUTTON, type: messages_1.MessageActionType.BUTTON,
text: '作者Pixiv', text: '作者Pixiv',
@ -298,9 +300,34 @@ async function sendSearchResults(modify, room, results, appUser) {
url: `https://www.imdb.com/title/${data.imdb_id}`, url: `https://www.imdb.com/title/${data.imdb_id}`,
}); });
} }
if (data.member_name !== undefined) {
attachmentObject.author = {
name: data.member_name,
};
}
if (data.author_name !== undefined) {
attachmentObject.author = {
name: data.author_name,
};
}
if (Array.isArray(data.creator)) {
attachmentObject.author = {
name: data.creator.join(', '),
};
}
if (data.eng_name !== undefined) {
attachmentObject.author = {
name: data.eng_name,
};
}
if (data.jp_name !== undefined) {
attachmentObject.author = {
name: data.jp_name,
};
}
attachments.push(attachmentObject); attachments.push(attachmentObject);
} }
const successStr = `搜索成功,短时间内剩余查询次数:${results.header.short_remaining}/${results.header.short_limit},长时间内剩余查询次数:${results.header.long_remaining}/${results.header.long_limit}`; const successStr = `搜索成功,30秒内剩余查询次数:${results.header.short_remaining}/${results.header.short_limit}24小时内剩余查询次数:${results.header.long_remaining}/${results.header.long_limit}`;
const text = attachments.length > 0 ? successStr : SauceNAOErrStr.length > 0 ? SauceNAOErrStr : '没有找到相关结果'; const text = attachments.length > 0 ? successStr : SauceNAOErrStr.length > 0 ? SauceNAOErrStr : '没有找到相关结果';
const messageBuilder = modify.getCreator().startMessage() const messageBuilder = modify.getCreator().startMessage()
.setRoom(room) .setRoom(room)

View File

@ -222,10 +222,11 @@ const defaultSauceNAOResponse: ISauceNAOResponse = {
let myLogger: ILogger; let myLogger: ILogger;
let SauceNAOErrStr: string; let SauceNAOErrStr: string;
const sauceNAOApiKeyID = 'sauceNAO_api_key';
export class PicSearcherApp extends App implements IPostMessageSent { export class PicSearcherApp extends App implements IPostMessageSent {
private rootUrl: string = ''; private rootUrl: string = '';
private sauceNAOApiKey: string = ''; private sauceNAOApiKey: string = 'ac635e5f5011234871260aac1d37ac8360ed979c';
constructor(info: IAppInfo, logger: ILogger, accessors: IAppAccessors) { constructor(info: IAppInfo, logger: ILogger, accessors: IAppAccessors) {
super(info, logger, accessors); super(info, logger, accessors);
@ -234,7 +235,6 @@ export class PicSearcherApp extends App implements IPostMessageSent {
public async initialize(configurationExtend: IConfigurationExtend, environmentRead: IEnvironmentRead): Promise<void> { public async initialize(configurationExtend: IConfigurationExtend, environmentRead: IEnvironmentRead): Promise<void> {
this.rootUrl = await environmentRead.getEnvironmentVariables().getValueByName('ROOT_URL'); this.rootUrl = await environmentRead.getEnvironmentVariables().getValueByName('ROOT_URL');
this.sauceNAOApiKey = await environmentRead.getSettings().getValueById('SauceNAOApiKey');
myLogger.log('rootUrl:', this.rootUrl); myLogger.log('rootUrl:', this.rootUrl);
return super.initialize(configurationExtend, environmentRead); return super.initialize(configurationExtend, environmentRead);
} }
@ -256,7 +256,7 @@ export class PicSearcherApp extends App implements IPostMessageSent {
} }
public async onSettingUpdated(setting: ISetting, configurationModify: IConfigurationModify, read: IRead, http: IHttp): Promise<void> { public async onSettingUpdated(setting: ISetting, configurationModify: IConfigurationModify, read: IRead, http: IHttp): Promise<void> {
if (setting.id === 'SauceNAOApiKey') { if (setting.id === sauceNAOApiKeyID) {
this.sauceNAOApiKey = setting.value; this.sauceNAOApiKey = setting.value;
} }
return super.onSettingUpdated(setting, configurationModify, read, http); return super.onSettingUpdated(setting, configurationModify, read, http);
@ -264,13 +264,13 @@ export class PicSearcherApp extends App implements IPostMessageSent {
protected async extendConfiguration(configuration: IConfigurationExtend, environmentRead: IEnvironmentRead): Promise<void> { protected async extendConfiguration(configuration: IConfigurationExtend, environmentRead: IEnvironmentRead): Promise<void> {
await configuration.settings.provideSetting({ await configuration.settings.provideSetting({
id: 'SauceNAOApiKey', id: sauceNAOApiKeyID,
type: SettingType.STRING, type: SettingType.STRING,
packageValue: '', packageValue: '',
required: true, required: true,
public: false, public: false,
i18nLabel: 'searcher_api_key_label', i18nLabel: 'sauceNAO_api_key',
i18nDescription: 'searcher_api_key_description', i18nDescription: 'sauceNAO_api_key_desc',
}); });
} }
@ -419,11 +419,13 @@ async function sendSearchResults(
// Dynamically generate color // Dynamically generate color
const color = getInterpolatedColor(similarityValue); const color = getInterpolatedColor(similarityValue);
const titleText = data.title || '未知标题';
const attachmentObject: IMessageAttachment = { const attachmentObject: IMessageAttachment = {
title: { title: {
value: `${data.title} (${header.similarity}%)`, // Title content value: `${titleText} (${header.similarity}%)`, // Title content
} as IMessageAttachmentTitle, } as IMessageAttachmentTitle,
text: header.index_name,
thumbnailUrl: header.thumbnail, // Optional: Image URL thumbnailUrl: header.thumbnail, // Optional: Image URL
collapsed: attachments.length !== 0, // First one expanded, others collapsed collapsed: attachments.length !== 0, // First one expanded, others collapsed
color, // Dynamically set color color, // Dynamically set color
@ -443,7 +445,9 @@ async function sendSearchResults(
} as IMessageAction); } as IMessageAction);
} }
} }
if (data.pixiv_id) {
// Add more buttons for different sources
if (data.member_id) {
attachmentObject.actions.push({ attachmentObject.actions.push({
type: MessageActionType.BUTTON, type: MessageActionType.BUTTON,
text: '作者Pixiv', text: '作者Pixiv',
@ -493,10 +497,36 @@ async function sendSearchResults(
} as IMessageAction); } as IMessageAction);
} }
if (data.member_name !== undefined) {
attachmentObject.author = {
name: data.member_name,
} as IMessageAttachmentAuthor;
}
if (data.author_name !== undefined) {
attachmentObject.author = {
name: data.author_name,
} as IMessageAttachmentAuthor;
}
if (Array.isArray(data.creator)) {
attachmentObject.author = {
name: data.creator.join(', '),
} as IMessageAttachmentAuthor;
}
if (data.eng_name !== undefined) {
attachmentObject.author = {
name: data.eng_name,
} as IMessageAttachmentAuthor;
}
if (data.jp_name !== undefined) {
attachmentObject.author = {
name: data.jp_name,
} as IMessageAttachmentAuthor;
}
attachments.push(attachmentObject); attachments.push(attachmentObject);
} }
const successStr = `搜索成功,短时间内剩余查询次数:${results.header.short_remaining}/${results.header.short_limit},长时间内剩余查询次数:${results.header.long_remaining}/${results.header.long_limit}`; const successStr = `搜索成功,30秒内剩余查询次数:${results.header.short_remaining}/${results.header.short_limit}24小时内剩余查询次数:${results.header.long_remaining}/${results.header.long_limit}`;
const text = attachments.length > 0 ? successStr : SauceNAOErrStr.length > 0 ? SauceNAOErrStr : '没有找到相关结果'; const text = attachments.length > 0 ? successStr : SauceNAOErrStr.length > 0 ? SauceNAOErrStr : '没有找到相关结果';
const messageBuilder = modify.getCreator().startMessage() const messageBuilder = modify.getCreator().startMessage()

View File

@ -1,6 +1,6 @@
{ {
"id": "bf4f0997-e7b7-40ff-b2b7-ba2ce5a15542", "id": "bf4f0997-e7b7-40ff-b2b7-ba2ce5a15543",
"version": "0.0.2", "version": "0.0.3",
"requiredApiVersion": "^1.44.0", "requiredApiVersion": "^1.44.0",
"iconFile": "favicon.png", "iconFile": "favicon.png",
"author": { "author": {