Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enable ipns over pubsub via settings menu #1739

Merged
merged 3 commits into from
Jan 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
"automaticGC": "Automatic Garbage Collection",
"ipfsCommandLineTools": "Command Line Tools",
"takeScreenshotShortcut": "Global Screenshot 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 = 'experiments.pubsubNamesys'
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: 1 addition & 1 deletion src/enable-pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const logger = require('./common/logger')
const store = require('./common/store')
const { ipcMain } = require('electron')

const CONFIG_KEY = 'pubsub'
const CONFIG_KEY = 'experiments.pubsub'
const pubsubFlag = '--enable-pubsub-experiment'
const isEnabled = flags => flags.some(f => f === pubsubFlag)

Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const setupWebUI = require('./webui')
const setupAutoLaunch = require('./auto-launch')
const setupAutoGc = require('./automatic-gc')
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 @@ -78,6 +79,7 @@ async function run () {
setupAutoLaunch(ctx),
setupAutoGc(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: AUTO_GC_KEY } = require('./automatic-gc')
const { CONFIG_KEY: IPFS_PATH_KEY } = require('./ipfs-on-path')
const { CONFIG_KEY: NPM_IPFS_KEY } = require('./npm-on-ipfs')
Expand All @@ -27,7 +28,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 @@ -136,6 +138,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