Skip to content

Commit ec5dc8e

Browse files
committed
[ update ] added a way to ignore update notices
1 parent cc795b0 commit ec5dc8e

2 files changed

Lines changed: 144 additions & 78 deletions

File tree

lib/announcements.js

Lines changed: 90 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,113 @@
11
const path = require("path");
22
const fs = require("fs");
3-
const {app} = require("electron")
4-
const ssbMarkdown = require("ssb-markdown")
3+
const { app } = require("electron");
4+
const ssbMarkdown = require("ssb-markdown");
55

6-
module.exports = {
6+
const announcements = {
7+
appData: (...extra) => {
8+
let result
9+
switch(process.platform) {
10+
case "win32":
11+
result = path.join(process.env.APPDATA, ...extra)
12+
break
13+
case "darwin":
14+
result = path.join(process.env.HOME, 'Library', 'Application Support', ...extra)
15+
break
16+
case "linux":
17+
result = path.join(process.env.HOME, ".config", ...extra)
18+
break
19+
default:
20+
result = path.join(process.env.HOME, ".config", ...extra)
21+
break
22+
}
23+
return result
24+
},
25+
destinationFolder: () => {
26+
return announcements.appData(
27+
"Patchwork",
28+
"announcements",
29+
);
30+
},
31+
unreadBox: () => {
32+
return path.join(announcements.destinationFolder(), "unread");
33+
},
34+
readBox: () => {
35+
return path.join(announcements.destinationFolder(), "read");
36+
},
737
copy: () => {
8-
const destinationFolder = path.join(app.getPath("appData"), "Patchwork", "announcements")
9-
const unreadBox = path.join(destinationFolder, "unread")
10-
const readBox = path.join(destinationFolder, "read")
11-
const sourceFolder = path.join(app.getAppPath(),"docs","announcements")
38+
const readBox = announcements.readBox()
39+
const unreadBox = announcements.unreadBox()
40+
41+
const sourceFolder = path.join(
42+
app.getAppPath(),
43+
"docs",
44+
"announcements",
45+
);
1246

13-
const announcementFiles = fs.readdirSync(sourceFolder)
47+
const announcementFiles = fs.readdirSync(sourceFolder);
1448

15-
fs.mkdirSync(unreadBox, {recursive: true})
16-
fs.mkdirSync(readBox, {recursive: true})
49+
fs.mkdirSync(unreadBox, { recursive: true });
50+
fs.mkdirSync(readBox, { recursive: true });
1751

18-
announcementFiles.forEach(filename => {
52+
announcementFiles.forEach((filename) => {
1953
if (!fs.existsSync(path.join(readBox, filename))) {
20-
fs.copyFileSync(path.join(sourceFolder, filename),
21-
path.join(unreadBox, filename))
54+
fs.copyFileSync(
55+
path.join(sourceFolder, filename),
56+
path.join(unreadBox, filename),
57+
);
2258

23-
console.log("copying announcement: " + filename)
59+
console.log("copying announcement: " + filename);
2460
}
25-
})
61+
});
2662
},
2763
markAsRead: () => {
28-
const destinationFolder = path.join(app.getPath("appData"), "Patchwork", "announcements")
29-
const unreadBox = path.join(destinationFolder, "unread")
30-
const readBox = path.join(destinationFolder, "read")
64+
const readBox = announcements.readBox()
65+
const unreadBox = announcements.unreadBox()
66+
const announcementFiles = fs.readdirSync(unreadBox);
3167

32-
const announcementFiles = fs.readdirSync(unreadBox)
68+
announcementFiles.forEach((filename) => {
69+
fs.renameSync(
70+
path.join(unreadBox, filename),
71+
path.join(readBox, filename),
72+
);
3373

34-
announcementFiles.forEach(filename => {
35-
fs.renameSync(
36-
path.join(unreadBox, filename),
37-
path.join(readBox, filename))
38-
39-
console.log("moving announcement: " + filename)
40-
})
74+
console.log("moving announcement: " + filename);
75+
});
4176
},
4277
available: () => {
43-
const destinationFolder = path.join(app.getPath("appData"), "Patchwork", "announcements")
44-
const unreadBox = path.join(destinationFolder, "unread")
45-
const announcementFiles = fs.readdirSync(unreadBox)
78+
const unreadBox = announcements.unreadBox()
79+
const announcementFiles = fs.readdirSync(unreadBox);
4680

47-
return announcementFiles.length > 0
81+
return announcementFiles.length > 0;
4882
},
4983
getAsHTML: () => {
50-
const destinationFolder = path.join(app.getPath("appData"), "Patchwork", "announcements")
51-
const unreadBox = path.join(destinationFolder, "unread")
52-
const announcementFiles = fs.readdirSync(unreadBox)
84+
const unreadBox = announcements.unreadBox()
85+
const announcementFiles = fs.readdirSync(unreadBox);
86+
const html = announcementFiles.map((f) => {
87+
const markdownContent = fs.readFileSync(path.join(unreadBox, f));
88+
const htmlContent = ssbMarkdown.block(markdownContent);
5389

54-
const html = announcementFiles.map(f => {
55-
const markdownContent = fs.readFileSync(path.join(unreadBox, f))
56-
const htmlContent = ssbMarkdown.block(markdownContent)
90+
return htmlContent;
91+
});
5792

58-
return htmlContent
59-
})
93+
return html.join(`---\n\n`);
94+
},
95+
markUpdateNoticeAsSeen: (version) => {
96+
const readBox = announcements.readBox()
97+
let filename = `ignore-update-${version}.ini`;
98+
let fullPath = path.join(readBox, filename);
99+
let content = `
100+
version = "${version}"
101+
`;
60102

61-
return html.join(`---\n\n`)
62-
}
103+
fs.writeFileSync(fullPath, content);
104+
},
105+
isUpdateNoticeIgnored: (version) => {
106+
const readBox = announcements.readBox()
107+
let filename = `ignore-update-${version}.ini`;
108+
let fullPath = path.join(readBox, filename);
109+
return fs.existsSync(fullPath);
110+
},
111+
};
63112

64-
}
113+
module.exports = announcements

lib/latest-update.js

Lines changed: 54 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,79 @@
1-
const https = require('https')
2-
const packageInfo = require('../package.json')
3-
const compareVersion = require('compare-version')
4-
const Value = require('mutant/value')
5-
const computed = require('mutant/computed')
1+
const https = require("https");
2+
const packageInfo = require("../package.json");
3+
const compareVersion = require("compare-version");
4+
const Value = require("mutant/value");
5+
const computed = require("mutant/computed");
6+
const announcements = require("./announcements.js");
67

78
module.exports = function () {
8-
const update = Value()
9-
const hidden = Value(false)
10-
update.sync = Value(false)
11-
const version = packageInfo.version
9+
const update = Value();
10+
const hidden = Value(false);
11+
update.sync = Value(false);
12+
const version = packageInfo.version;
13+
let newVersion = packageInfo.version
14+
1215
const checkForUpdate = () => {
1316
// Check whether notification has been hidden.
1417
if (hidden() === true) {
1518
// If so, stop the interval timer.
16-
clearInterval(updateCheckInterval)
19+
clearInterval(updateCheckInterval);
1720
// And `return` to quit this function.
18-
return
21+
return;
1922
}
2023

2124
https.get({
22-
host: 'api.github.com',
23-
path: '/repos/soapdog/patchwork/releases/latest',
25+
host: "api.github.com",
26+
path: "/repos/soapdog/patchwork/releases/latest",
2427
headers: {
25-
'user-agent': `Poncho Wonky v${version}`
26-
}
28+
"user-agent": `Poncho Wonky v${version}`,
29+
},
2730
}, function (res) {
2831
if (res.statusCode === 200) {
29-
let result = ''
30-
res.on('data', (x) => {
31-
result += x
32-
}).on('end', () => {
33-
const info = JSON.parse(result)
34-
console.log("Current patchwork version", version)
35-
console.log("Most recent version on Github", info.tag_name.slice(1))
36-
if (compareVersion(info.tag_name.slice(1), version) > 0) {
37-
update.set(info.tag_name.slice(1))
32+
let result = "";
33+
res.on("data", (x) => {
34+
result += x;
35+
}).on("end", () => {
36+
const info = JSON.parse(result);
37+
newVersion = info.tag_name.slice(1)
38+
console.log("Current patchwork version", version);
39+
console.log("Most recent version on Github", newVersion);
40+
if (compareVersion(newVersion, version) > 0) {
41+
if (announcements.isUpdateNoticeIgnored(newVersion)) {
42+
hidden.set(true)
43+
} else {
44+
update.set(newVersion);
45+
}
3846
}
39-
update.sync.set(true)
40-
})
47+
update.sync.set(true);
48+
});
4149
}
4250
// You must handle the error here otherwise you get an unhandled error exception which stops the whole app.
43-
}).on('error', function (error) {
44-
console.log('error trying to reach github to check for latest poncho wonky version: ', error)
45-
})
46-
}
51+
}).on("error", function (error) {
52+
console.log(
53+
"error trying to reach github to check for latest poncho wonky version: ",
54+
error,
55+
);
56+
});
57+
};
4758

4859
// Retry update check every 24 hours.
49-
const millisecondsPerDay = 1000 * 60 * 60 * 24
60+
const millisecondsPerDay = 1000 * 60 * 60 * 24;
5061

5162
// Previously we only checked for updates on startup, but some people keep
5263
// Patchwork running in the background all the time and they should get
5364
// update notifications too.
54-
const updateCheckInterval = setInterval(checkForUpdate, millisecondsPerDay)
65+
const updateCheckInterval = setInterval(checkForUpdate, millisecondsPerDay);
5566

5667
// Check for update immediately.
57-
checkForUpdate()
68+
checkForUpdate();
5869

59-
const obs = computed([update, hidden], (update, hidden) => update && !hidden ? update : false)
60-
obs.ignore = () => hidden.set(true)
61-
return obs
62-
}
70+
const obs = computed(
71+
[update, hidden],
72+
(update, hidden) => update && !hidden ? update : false,
73+
);
74+
obs.ignore = () => {
75+
hidden.set(true);
76+
announcements.markUpdateNoticeAsSeen(newVersion)
77+
}
78+
return obs;
79+
};

0 commit comments

Comments
 (0)