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 import actor profile #3

Merged
merged 2 commits into from
Nov 12, 2024
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 build-dist.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mkdir ./dist/esm
cat >dist/esm/index.js <<!EOF
import cjsModule from "../index.js";
export const exportActorProfile = cjsModule.exportActorProfile;
export const importActorProfile = cjsModule.importActorProfile;
!EOF

cat >dist/esm/package.json <<!EOF
Expand Down
95 changes: 86 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import * as tar from 'tar-stream'
import { type Pack } from 'tar-stream'
import YAML from 'yaml'
import { Readable } from 'stream'

export type ActorProfileOptions = {
actorProfile?: any
Expand All @@ -18,9 +19,17 @@ export type ActorProfileOptions = {
mutedAccounts?: any
}

export function exportActorProfile ({
actorProfile, outbox, followers, followingAccounts, lists, bookmarks, likes,
blockedAccounts, blockedDomains, mutedAccounts
export function exportActorProfile({
actorProfile,
outbox,
followers,
followingAccounts,
lists,
bookmarks,
likes,
blockedAccounts,
blockedDomains,
mutedAccounts
}: ActorProfileOptions): tar.Pack {
const pack: Pack = tar.pack() // pack is a stream

Expand Down Expand Up @@ -53,39 +62,54 @@ export function exportActorProfile ({
manifest.contents.activitypub.contents['actor.json'] = {
url: 'https://www.w3.org/TR/activitypub/#actor-objects'
}
pack.entry({ name: 'activitypub/actor.json' }, JSON.stringify(actorProfile, null, 2))
pack.entry(
{ name: 'activitypub/actor.json' },
JSON.stringify(actorProfile, null, 2)
)
}

if (outbox) {
// ActivityStreams OrderedCollection representing the contents of the actor's Outbox
manifest.contents.activitypub.contents['outbox.json'] = {
url: 'https://www.w3.org/TR/activitystreams-core/#collections'
}
pack.entry({ name: 'activitypub/outbox.json' }, JSON.stringify(outbox, null, 2))
pack.entry(
{ name: 'activitypub/outbox.json' },
JSON.stringify(outbox, null, 2)
)
}

if (followers) {
// ActivityStreams OrderedCollection representing the actor's Followers
manifest.contents.activitypub.contents['followers.json'] = {
url: 'https://www.w3.org/TR/activitystreams-core/#collections'
}
pack.entry({ name: 'activitypub/followers.json' }, JSON.stringify(followers, null, 2))
pack.entry(
{ name: 'activitypub/followers.json' },
JSON.stringify(followers, null, 2)
)
}

if (likes) {
// ActivityStreams OrderedCollection representing Activities and Objects the actor liked
manifest.contents.activitypub.contents['likes.json'] = {
url: 'https://www.w3.org/TR/activitystreams-core/#collections'
}
pack.entry({ name: 'activitypub/likes.json' }, JSON.stringify(likes, null, 2))
pack.entry(
{ name: 'activitypub/likes.json' },
JSON.stringify(likes, null, 2)
)
}

if (bookmarks) {
// ActivityStreams OrderedCollection representing the actor's Bookmarks
manifest.contents.activitypub.contents['bookmarks.json'] = {
url: 'https://www.w3.org/TR/activitystreams-core/#collections'
}
pack.entry({ name: 'activitypub/bookmarks.json' }, JSON.stringify(bookmarks, null, 2))
pack.entry(
{ name: 'activitypub/bookmarks.json' },
JSON.stringify(bookmarks, null, 2)
)
}

if (followingAccounts) {
Expand All @@ -94,7 +118,10 @@ export function exportActorProfile ({
manifest.contents.activitypub.contents['following_accounts.csv'] = {
url: 'https://docs.joinmastodon.org/user/moving/#export'
}
pack.entry({ name: 'activitypub/following_accounts.csv' }, followingAccounts)
pack.entry(
{ name: 'activitypub/following_accounts.csv' },
followingAccounts
)
}

if (lists) {
Expand Down Expand Up @@ -130,3 +157,53 @@ export function exportActorProfile ({
return pack
}

export async function importActorProfile(tarBuffer: Buffer): Promise<any> {
const extract = tar.extract()
const result: Record<string, any> = {}

return new Promise((resolve, reject) => {
extract.on('entry', async (header, stream, next) => {
let content = ''

stream.on('data', (chunk) => {
content += chunk.toString()
})

stream.on('end', () => {
// Handle JSON files
if (header.name.endsWith('.json')) {
try {
result[header.name] = JSON.parse(content)
} catch (error) {
console.error(`Error parsing JSON from ${header.name}:`, error)
}
}
// Handle YAML files
else if (
header.name.endsWith('.yaml') ||
header.name.endsWith('.yml')
) {
try {
result[header.name] = YAML.parse(content)
} catch (error) {
console.error(`Error parsing YAML from ${header.name}:`, error)
}
}
// Handle CSV files
else if (header.name.endsWith('.csv')) {
result[header.name] = content // Return raw CSV as a string or implement CSV parsing here if needed
}

next() // Process the next file in the tar archive
})

stream.on('error', (error) => reject(error))
})

extract.on('finish', () => resolve(result))

// Stream the buffer data into the tar extractor
const stream = Readable.from(tarBuffer)
stream.pipe(extract)
})
}
Binary file added test/fixtures/account2.tar
Binary file not shown.
21 changes: 18 additions & 3 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
// import { expect } from 'chai'
import * as fs from 'node:fs'

import { exportActorProfile } from '../src'
import * as path from 'path'
import { exportActorProfile, importActorProfile } from '../src'
import { outbox } from './fixtures/outbox'
import { actorProfile } from './fixtures/actorProfile'
import { expect } from 'chai'

describe('exportActorProfile', () => {
it('calls function', async () => {
const filename = 'out/test-export-2024-01-01.tar'
const tarball = fs.createWriteStream(filename)

const packStream = exportActorProfile({ actorProfile, outbox })

packStream.pipe(tarball)
})
})

describe('importActorProfile', () => {
it('extracts and verifies contents from account2.tar', async () => {
// Load the tar file as a buffer
const tarBuffer = fs.readFileSync('test/fixtures/account2.tar')

// Use the importActorProfile function to parse the tar contents
const importedData = await importActorProfile(tarBuffer)

// Log or inspect the imported data structure
console.log('Imported Data:', importedData)

// Example assertions to check specific files and content
expect(importedData).to.have.property('activitypub/actor.json')
})
})
Loading