Skip to content

Commit

Permalink
feat: enable ipns over pubsub via settings menu
Browse files Browse the repository at this point in the history
Part of ipfs#1647

Very similar approach to ipfs#1735
  • Loading branch information
andrew committed Jan 20, 2021
1 parent 6dbfa13 commit 1ec3ff6
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
1 change: 1 addition & 0 deletions assets/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@
"launchOnStartup": "Launch at Login",
"openWebUIAtLaunch": "Open Web UI at Launch",
"pubsub": "Enable PubSub",
"namesysPubsub": "Enable IPNS over PubSub",
"ipfsCommandLineTools": "Command Line Tools",
"takeScreenshotShortcut": "Global Screenshot Shortcut",
"downloadHashShortcut": "Global Download Shortcut",
Expand Down
54 changes: 54 additions & 0 deletions src/enable-namesys-pubsub.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const createToggler = require('./utils/create-toggler')
const logger = require('./common/logger')
const store = require('./common/store')
const { ipcMain } = require('electron')

const CONFIG_KEY = 'namesys-pubsub'
const namesysPubsubFlag = '--enable-namesys-pubsub'
const isEnabled = flags => flags.some(f => f === namesysPubsubFlag)

function enable () {
const flags = store.get('ipfsConfig.flags', [])
if (!isEnabled(flags)) {
flags.push(namesysPubsubFlag)
applyConfig(flags)
}
}

function disable () {
let flags = store.get('ipfsConfig.flags', [])
if (isEnabled(flags)) {
flags = flags.filter(item => item !== namesysPubsubFlag) // remove flag
applyConfig(flags)
}
}

function applyConfig (newFlags) {
store.set('ipfsConfig.flags', newFlags)
ipcMain.emit('ipfsConfigChanged') // trigger node restart
}

module.exports = async function () {
const activate = ({ newValue, oldValue }) => {
if (newValue === oldValue) return

try {
if (newValue === true) {
enable()
} else {
disable()
}

return true
} catch (err) {
logger.error(`[ipns over pubsub] ${err.toString()}`)

return false
}
}
activate({ newValue: store.get(CONFIG_KEY, false) })
createToggler(CONFIG_KEY, activate)
logger.info(`[ipns over pubsub] ${store.get(CONFIG_KEY, false) ? 'enabled' : 'disabled'}`)
}

module.exports.CONFIG_KEY = CONFIG_KEY
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const setupDaemon = require('./daemon')
const setupWebUI = require('./webui')
const setupAutoLaunch = require('./auto-launch')
const setupPubsub = require('./enable-pubsub')
const setupNamesysPubsub = require('./enable-namesys-pubsub')
const setupDownloadCid = require('./download-cid')
const setupTakeScreenshot = require('./take-screenshot')
const setupAppMenu = require('./app-menu')
Expand Down Expand Up @@ -76,6 +77,7 @@ async function run () {
setupArgvFilesHandler(ctx),
setupAutoLaunch(ctx),
setupPubsub(ctx),
setupNamesysPubsub(ctx),
setupSecondInstance(ctx),
// Setup global shortcuts
setupDownloadCid(ctx),
Expand Down
5 changes: 4 additions & 1 deletion src/tray.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const { CONFIG_KEY: SCREENSHOT_KEY, SHORTCUT: SCREENSHOT_SHORTCUT, takeScreensho
const { CONFIG_KEY: DOWNLOAD_KEY, SHORTCUT: DOWNLOAD_SHORTCUT, downloadCid } = require('./download-cid')
const { CONFIG_KEY: AUTO_LAUNCH_KEY, isSupported: supportsLaunchAtLogin } = require('./auto-launch')
const { CONFIG_KEY: PUBSUB_KEY } = require('./enable-pubsub')
const { CONFIG_KEY: NAMESYS_PUBSUB_KEY } = require('./enable-namesys-pubsub')
const { CONFIG_KEY: IPFS_PATH_KEY } = require('./ipfs-on-path')
const { CONFIG_KEY: NPM_IPFS_KEY } = require('./npm-on-ipfs')
const { CONFIG_KEY: AUTO_LAUNCH_WEBUI_KEY } = require('./webui')
Expand All @@ -25,7 +26,8 @@ const CONFIG_KEYS = [
NPM_IPFS_KEY,
SCREENSHOT_KEY,
DOWNLOAD_KEY,
PUBSUB_KEY
PUBSUB_KEY,
NAMESYS_PUBSUB_KEY
]

function buildCheckbox (key, label) {
Expand Down Expand Up @@ -133,6 +135,7 @@ function buildMenu (ctx) {
enabled: false
},
buildCheckbox(PUBSUB_KEY, 'settings.pubsub'),
buildCheckbox(NAMESYS_PUBSUB_KEY, 'settings.namesysPubsub'),
buildCheckbox(NPM_IPFS_KEY, 'settings.npmOnIpfs')
]
},
Expand Down

0 comments on commit 1ec3ff6

Please sign in to comment.