Skip to content

Commit 90b1ad1

Browse files
andrewlidel
andauthored
feat: enable ipns over pubsub via settings menu (#1739)
* feat: enable ipns over pubsub via settings menu Part of #1647 Very similar approach to #1735 * refactor: move pubsub experiments to exp. section making it easier to find if someone edits config by hand Co-authored-by: Marcin Rataj <[email protected]>
1 parent 204e206 commit 90b1ad1

File tree

5 files changed

+62
-2
lines changed

5 files changed

+62
-2
lines changed

assets/locales/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@
224224
"launchOnStartup": "Launch at Login",
225225
"openWebUIAtLaunch": "Open Web UI at Launch",
226226
"pubsub": "Enable PubSub",
227+
"namesysPubsub": "Enable IPNS over PubSub",
227228
"automaticGC": "Automatic Garbage Collection",
228229
"ipfsCommandLineTools": "Command Line Tools",
229230
"takeScreenshotShortcut": "Global Screenshot Shortcut",

src/enable-namesys-pubsub.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
const createToggler = require('./utils/create-toggler')
2+
const logger = require('./common/logger')
3+
const store = require('./common/store')
4+
const { ipcMain } = require('electron')
5+
6+
const CONFIG_KEY = 'experiments.pubsubNamesys'
7+
const namesysPubsubFlag = '--enable-namesys-pubsub'
8+
const isEnabled = flags => flags.some(f => f === namesysPubsubFlag)
9+
10+
function enable () {
11+
const flags = store.get('ipfsConfig.flags', [])
12+
if (!isEnabled(flags)) {
13+
flags.push(namesysPubsubFlag)
14+
applyConfig(flags)
15+
}
16+
}
17+
18+
function disable () {
19+
let flags = store.get('ipfsConfig.flags', [])
20+
if (isEnabled(flags)) {
21+
flags = flags.filter(item => item !== namesysPubsubFlag) // remove flag
22+
applyConfig(flags)
23+
}
24+
}
25+
26+
function applyConfig (newFlags) {
27+
store.set('ipfsConfig.flags', newFlags)
28+
ipcMain.emit('ipfsConfigChanged') // trigger node restart
29+
}
30+
31+
module.exports = async function () {
32+
const activate = ({ newValue, oldValue }) => {
33+
if (newValue === oldValue) return
34+
35+
try {
36+
if (newValue === true) {
37+
enable()
38+
} else {
39+
disable()
40+
}
41+
42+
return true
43+
} catch (err) {
44+
logger.error(`[ipns over pubsub] ${err.toString()}`)
45+
46+
return false
47+
}
48+
}
49+
activate({ newValue: store.get(CONFIG_KEY, false) })
50+
createToggler(CONFIG_KEY, activate)
51+
logger.info(`[ipns over pubsub] ${store.get(CONFIG_KEY, false) ? 'enabled' : 'disabled'}`)
52+
}
53+
54+
module.exports.CONFIG_KEY = CONFIG_KEY

src/enable-pubsub.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const logger = require('./common/logger')
33
const store = require('./common/store')
44
const { ipcMain } = require('electron')
55

6-
const CONFIG_KEY = 'pubsub'
6+
const CONFIG_KEY = 'experiments.pubsub'
77
const pubsubFlag = '--enable-pubsub-experiment'
88
const isEnabled = flags => flags.some(f => f === pubsubFlag)
99

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const setupWebUI = require('./webui')
1111
const setupAutoLaunch = require('./auto-launch')
1212
const setupAutoGc = require('./automatic-gc')
1313
const setupPubsub = require('./enable-pubsub')
14+
const setupNamesysPubsub = require('./enable-namesys-pubsub')
1415
const setupDownloadCid = require('./download-cid')
1516
const setupTakeScreenshot = require('./take-screenshot')
1617
const setupAppMenu = require('./app-menu')
@@ -78,6 +79,7 @@ async function run () {
7879
setupAutoLaunch(ctx),
7980
setupAutoGc(ctx),
8081
setupPubsub(ctx),
82+
setupNamesysPubsub(ctx),
8183
setupSecondInstance(ctx),
8284
// Setup global shortcuts
8385
setupDownloadCid(ctx),

src/tray.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const { CONFIG_KEY: SCREENSHOT_KEY, SHORTCUT: SCREENSHOT_SHORTCUT, takeScreensho
1414
const { CONFIG_KEY: DOWNLOAD_KEY, SHORTCUT: DOWNLOAD_SHORTCUT, downloadCid } = require('./download-cid')
1515
const { CONFIG_KEY: AUTO_LAUNCH_KEY, isSupported: supportsLaunchAtLogin } = require('./auto-launch')
1616
const { CONFIG_KEY: PUBSUB_KEY } = require('./enable-pubsub')
17+
const { CONFIG_KEY: NAMESYS_PUBSUB_KEY } = require('./enable-namesys-pubsub')
1718
const { CONFIG_KEY: AUTO_GC_KEY } = require('./automatic-gc')
1819
const { CONFIG_KEY: IPFS_PATH_KEY } = require('./ipfs-on-path')
1920
const { CONFIG_KEY: NPM_IPFS_KEY } = require('./npm-on-ipfs')
@@ -27,7 +28,8 @@ const CONFIG_KEYS = [
2728
NPM_IPFS_KEY,
2829
SCREENSHOT_KEY,
2930
DOWNLOAD_KEY,
30-
PUBSUB_KEY
31+
PUBSUB_KEY,
32+
NAMESYS_PUBSUB_KEY
3133
]
3234

3335
function buildCheckbox (key, label) {
@@ -136,6 +138,7 @@ function buildMenu (ctx) {
136138
enabled: false
137139
},
138140
buildCheckbox(PUBSUB_KEY, 'settings.pubsub'),
141+
buildCheckbox(NAMESYS_PUBSUB_KEY, 'settings.namesysPubsub'),
139142
buildCheckbox(NPM_IPFS_KEY, 'settings.npmOnIpfs')
140143
]
141144
},

0 commit comments

Comments
 (0)