From a41c331156cff2563542a991e49906234ae62ed3 Mon Sep 17 00:00:00 2001 From: jonybang Date: Mon, 24 Jun 2019 19:56:56 +0200 Subject: [PATCH] fix content linking #49 and prepare contents data base for fulltext search #50 --- src/background.ts | 4 ++-- src/backgroundServices/database.ts | 8 +++++++- .../CyberDCabinet/LinkContent/LinkContent.ts | 15 ++++++++++++--- .../SaveAndLinkContent/SaveAndLinkContent.ts | 4 ++-- 4 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/background.ts b/src/background.ts index eec1747..558c0d5 100644 --- a/src/background.ts +++ b/src/background.ts @@ -153,7 +153,7 @@ async function restoreExtensionDataFromIpld(backupIpld) { setInterval(() => { saveExtensionDataAndBindToIpns(); -}, 1000 * 60); //* 10 +}, 1000 * 60 * 5); fetchCurrentTab(); @@ -209,7 +209,7 @@ onMessage(async (request, sender, sendResponse) => { .then(async data => { setAction(null); - if (data.mimeType === 'text/html') { + if (data.mimeType === 'text/html' && !data.iconHash) { const contentData = await ipfsService.getContent(data.contentHash); const $ = cheerio.load(contentData); diff --git a/src/backgroundServices/database.ts b/src/backgroundServices/database.ts index d3e7464..030cd52 100644 --- a/src/backgroundServices/database.ts +++ b/src/backgroundServices/database.ts @@ -29,13 +29,19 @@ const databaseService = { db.version(1).stores({ content: - '++id,contentHash,manifestHash,description,keywords,size,mimeType,previewHash,previewMimeType,iconHash,iconMimeType,view,extension,previewExtension,createdAt,updatedAt', + '++id,contentHash,manifestHash,description,keywords,size,mimeType,previewHash,previewMimeType,iconHash,iconMimeType,view,extension,previewExtension,fullText,createdAt,updatedAt', settings: 'name,value', }); }, async saveContent(contentObj) { const existingContent = await this.getContentByHash(contentObj.contentHash); + + contentObj.fullText = (contentObj.description || (existingContent || {}).description || '').split(/[ ,.]+/); + contentObj.fullText = contentObj.fullText.concat(contentObj.keywords || (existingContent || {}).keywords || []); + contentObj.fullText = contentObj.fullText.filter(item => !item.match(/^[ ,.\-_/\\\(\)"'?]$/) && item != ''); + if (existingContent) { + console.log('updateContentByHash', contentObj.contentHash, contentObj); return this.updateContentByHash(contentObj.contentHash, contentObj).then(() => this.getContentByHash(contentObj.contentHash)); } contentObj.createdAt = Helper.now(); diff --git a/src/popup/pages/Cabinet/CyberDCabinet/LinkContent/LinkContent.ts b/src/popup/pages/Cabinet/CyberDCabinet/LinkContent/LinkContent.ts index c56d062..784dce2 100644 --- a/src/popup/pages/Cabinet/CyberDCabinet/LinkContent/LinkContent.ts +++ b/src/popup/pages/Cabinet/CyberDCabinet/LinkContent/LinkContent.ts @@ -1,6 +1,6 @@ import { AppWallet, StorageVars } from '../../../../../services/data'; import { CyberD } from '../../../../../services/cyberd'; -import { addIpfsContentArray } from '../../../../../services/backgroundGateway'; +import { addIpfsContentArray, saveContent } from '../../../../../services/backgroundGateway'; import ContentDetails from '../../../../directives/ContentDetails/ContentDetails'; const pIteration = require('p-iteration'); @@ -13,6 +13,10 @@ export default { }, methods: { async link() { + console.log('this.inputKeywordsStr', this.inputKeywordsStr); + console.log('this.keywords', this.keywords); + console.log('this.resultKeywords', this.resultKeywords); + console.log('this.$route.query.keywords', this.$route.query.keywords); const keywordHashes = await addIpfsContentArray(this.resultKeywords); const results = await pIteration.mapSeries(keywordHashes, async keywordHash => { @@ -26,6 +30,11 @@ export default { ); }); + await saveContent({ + contentHash: this.resultContentHash, + keywords: this.resultKeywords, + }); + console.log('link results', results); this.$notify({ @@ -41,13 +50,13 @@ export default { return this.contentHash || this.inputContentHash; }, resultKeywords() { - return this.keywords || this.inputKeywordsStr.split(/[ ,]+/); + return this.inputKeywordsStr.split(/[ ,]+/); }, contentHash() { return this.$route.query.contentHash; }, keywords() { - return _.isArray(this.$route.query.keywords) ? this.$route.query.keywords : (this.$route.query.keywords || '').split(/[ ,]+/); + return _.isArray(this.$route.query.keywords) ? this.$route.query.keywords : this.$route.query.keywords ? this.$route.query.keywords.split(/[ ,]+/) : null; }, keywordsStr() { return this.keywords ? this.keywords.join(', ') : ''; diff --git a/src/popup/pages/Cabinet/CyberDCabinet/SaveAndLinkContent/SaveAndLinkContent.ts b/src/popup/pages/Cabinet/CyberDCabinet/SaveAndLinkContent/SaveAndLinkContent.ts index 1658f9e..b03c2d2 100644 --- a/src/popup/pages/Cabinet/CyberDCabinet/SaveAndLinkContent/SaveAndLinkContent.ts +++ b/src/popup/pages/Cabinet/CyberDCabinet/SaveAndLinkContent/SaveAndLinkContent.ts @@ -51,13 +51,13 @@ export default { return this.contentHash || this.inputContentHash; }, resultKeywords() { - return this.keywords || this.inputKeywordsStr.split(/[ ,]+/); + return this.inputKeywordsStr.split(/[ ,]+/); }, contentHash() { return this.$route.query.contentHash; }, keywords() { - return _.isArray(this.$route.query.keywords) ? this.$route.query.keywords : (this.$route.query.keywords || '').split(/[ ,]+/); + return _.isArray(this.$route.query.keywords) ? this.$route.query.keywords : this.$route.query.keywords ? this.$route.query.keywords.split(/[ ,]+/) : null; }, keywordsStr() { return this.keywords ? this.keywords.join(', ') : '';