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

Add csv export for bookmarks #118

Merged
merged 5 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"chalk": "^5.2.0",
"connect-sqlite3": "^0.9.13",
"cors": "^2.8.5",
"csv-stringify": "^6.4.2",
"dotenv": "^16.0.3",
"es6-promisify": "^7.0.0",
"express": "^4.18.2",
Expand Down
14 changes: 14 additions & 0 deletions src/bookmarks-db.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,20 @@ export async function getBookmarks(limit = 10, offset = 0) {
return undefined;
}

export async function getBookmarksForCSVExport() {
// We use a try catch block in case of db errors
try {
const results = await db.all('SELECT title,url,description,tags,created_at,updated_at from bookmarks');
// These titles should be updated if the query changes above.
const columnTitles = { title: 'title', url: 'url', description: 'description', tags: 'tags', created_at: 'created_at', updated_at: 'updated_at' };
return [columnTitles].concat(results);
} catch (dbError) {
// Database connection error
console.error(dbError);
}
return undefined;
}

export async function getBookmarkCountForTags(tags) {
const tagClauses = tags.map(() => `(tags like ? OR tags like ?)`).join(' AND ');
const tagParams = tags.map((tag) => [`%${tag}% `, `%${tag}%`]).flat();
Expand Down
9 changes: 6 additions & 3 deletions src/pages/admin/data.hbs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
<h3>
Download bookmarks
</h3>
<p>
You can download your bookmarks sqlite db
<a href="/admin/bookmarks.db">here</a>.
<p style='margin: 0'>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a margin change here just because I thought there was a lot of space 🤷 . Can definitely remove if desired.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no problem with this, the only comment I have is that to be consistent with the rest of the HTML code, switch over to " instead of ' (and throw a ; on the end of the style value)

<p style="margin: 0;">

I think overall the web UI piece needs a lot more work, but this is fine from my POV.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, the web UI is definitely still rough. I have a couple talented friends who have offered to help out with this if they can find the time, but obviously we're all volunteering what we have to give here (and I'm still honored by how much time y'all have spent on this already). I'm fine with little stopgaps here and there with the understanding that we're probably going to blow up these templates and rebuild them.

You can download your bookmarks as:
</p>
<ul>
<li><a href="/admin/bookmarks.db">sqlite database</a></li>
<li><a href="/admin/bookmarks.csv">csv</a></li>
</ul>
<p>
It's less likely you'll need this, but you can also download your
activitypub.db
Expand Down
14 changes: 14 additions & 0 deletions src/routes/admin.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import express from 'express';
/* eslint-disable import/no-unresolved, node/no-missing-import */
import { stringify as csvStringify } from 'csv-stringify/sync'; // https://github.com/adaltas/node-csv/issues/323
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These eslint issues seem like a known issue. I tried using the csv package instead, but that didn't work either 😞

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a similar issue with string-strip-html here-- if anyone reading this knows why this happens, I'd love to know!

/* eslint-enable */
import { domain, actorInfo, parseJSON } from '../util.js';
import { isAuthenticated } from '../session-auth.js';
import { lookupActorInfo, createFollowMessage, createUnfollowMessage, signAndSend, getInboxFromActorProfile } from '../activitypub.js';
Expand Down Expand Up @@ -91,6 +94,17 @@ router.get('/bookmarks.db', isAuthenticated, async (req, res) => {
res.download(filePath);
});

router.get('/bookmarks.csv', isAuthenticated, async (req, res) => {
const bookmarksDb = req.app.get('bookmarksDb');
const bookmarks = await bookmarksDb.getBookmarksForCSVExport();
const result = csvStringify(bookmarks);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

csv-stringify does also have an streaming API such that we could asynchronously stringify a row and then write that to the client. I didn't do that because this was simpler and we probably don't need to worry about memory ballooning during an export right? That would be so many bookmarks 😬

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I definitely am in favor of keeping things as simple as possible for now. If we start getting reports that memory limits are happening (especially on the Glitch containers), we can revisit this.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this needs a tweak

const result = csvStringify(bookmarks, {quoted: true});

I had some bookmarks in my database with null values in some columns (from a batch import), and they caused the CSV generation to go awry until I added this option to ensure that they were correctly handled.


res.setHeader('Content-Type', 'text/csv');
res.setHeader('Content-Disposition', 'attachment; filename="bookmarks.csv"');

res.send(result);
});

router.get('/activitypub.db', isAuthenticated, async (req, res) => {
const filePath = `${DATA_PATH}/activitypub.db`;

Expand Down