Fix Prompt plugin (Closes #1063)

This commit is contained in:
Gregory Schier 2018-11-06 08:52:55 -05:00
parent 0aa842ca5f
commit 01196cf457
3 changed files with 26 additions and 4 deletions

View File

@ -44,6 +44,10 @@ export async function removeByKey(plugin: string, key: string): Promise<void> {
return db.removeWhere(type, { plugin, key });
}
export async function all(plugin: string): Promise<Array<PluginData>> {
return db.find(type, { plugin });
}
export async function removeAll(plugin: string): Promise<void> {
return db.removeWhere(type, { plugin });
}

View File

@ -19,8 +19,15 @@ export function init(plugin: Plugin) {
async removeItem(key: string): Promise<void> {
await models.pluginData.removeByKey(plugin.name, key);
},
async clear(key: string): Promise<void> {
async clear(): Promise<void> {
await models.pluginData.removeAll(plugin.name);
},
async all(): Promise<Array<{ key: string, value: string }>> {
const docs = await models.pluginData.all(plugin.name);
return docs.map(d => ({
value: d.value,
key: d.key
}));
}
}
};

View File

@ -1,7 +1,14 @@
const crypto = require('crypto');
module.exports.responseHooks = [
context => {
async context => {
const items = await context.store.all();
const requestId = context.request.getId();
const toRemove = items.filter(v => v.key.indexOf(requestId) === 0);
// Delete cached values we prompt again on the next request
context.store.clear();
for (const { key } of toRemove) {
await context.store.removeItem(key);
}
}
];
@ -53,7 +60,11 @@ module.exports.templateTags = [
// We do this because we may render the prompt multiple times per request.
// We cache it under the requestId so it only prompts once. We then clear
// the cache in a response hook when the request is sent.
const storageKey = explicitStorageKey || `${context.meta.requestId}.${title}`;
const titleHash = crypto
.createHash('md5')
.update(title)
.digest('hex');
const storageKey = explicitStorageKey || `${context.meta.requestId}.${titleHash}`;
const cachedValue = await context.store.getItem(storageKey);
if (cachedValue) {