Skip to content

Commit

Permalink
Import data
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlia committed Dec 10, 2024
1 parent 73bde94 commit 5c7a786
Show file tree
Hide file tree
Showing 5 changed files with 406 additions and 51 deletions.
Binary file modified bun.lockb
Binary file not shown.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@logtape/sentry": "^0.1.0",
"@sentry/bun": "^8.41.0",
"@sentry/core": "^8.41.0",
"@supercharge/promise-pool": "^3.2.0",
"cheerio": "^1.0.0",
"csv-writer-portable": "^1.7.6",
"drizzle-orm": "^0.30.10",
Expand All @@ -36,6 +37,7 @@
"markdown-it": "^14.1.0",
"markdown-it-replace-link": "^1.2.1",
"mime": "^4.0.4",
"neat-csv": "^7.0.0",
"open-graph-scraper": "^6.5.1",
"otpauth": "^9.3.4",
"postgres": "^3.4.5",
Expand Down
23 changes: 3 additions & 20 deletions src/api/v1/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ import { getPostRelations, serializePost } from "../../entities/status";
import { federation } from "../../federation";
import {
REMOTE_ACTOR_FETCH_POSTS,
blockAccount,
followAccount,
persistAccount,
persistAccountPosts,
removeFollower,
unfollowAccount,
} from "../../federation/account";
import { type Variables, scopeRequired, tokenRequired } from "../../oauth";
Expand Down Expand Up @@ -1037,25 +1037,8 @@ app.post(
with: { owner: true },
});
if (acct == null) return c.json({ error: "Record not found" }, 404);
await db.insert(blocks).values({
accountId: owner.id,
blockedAccountId: id,
});
if (acct.owner == null) {
const fedCtx = federation.createContext(c.req.raw, undefined);
await unfollowAccount(db, fedCtx, { ...owner.account, owner }, acct);
await removeFollower(db, fedCtx, { ...owner.account, owner }, acct);
await fedCtx.sendActivity(
{ username: owner.handle },
{ id: new URL(acct.iri), inboxId: new URL(acct.inboxUrl) },
new Block({
id: new URL(`#block/${acct.id}`, owner.account.iri),
actor: new URL(owner.account.iri),
object: new URL(acct.iri),
}),
{ excludeBaseUris: [new URL(fedCtx.url)] },
);
}
const fedCtx = federation.createContext(c.req.raw, undefined);
await blockAccount(db, fedCtx, owner, acct);
const result = await db.query.accounts.findFirst({
where: eq(accounts.id, id),
with: {
Expand Down
48 changes: 48 additions & 0 deletions src/federation/account.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
type Actor,
Announce,
Block,
type Context,
Create,
type DocumentLoader,
Expand Down Expand Up @@ -495,3 +496,50 @@ export async function removeFollower(
);
return result[0];
}

export async function blockAccount(
db: PgDatabase<
PostgresJsQueryResultHKT,
typeof schema,
ExtractTablesWithRelations<typeof schema>
>,
ctx: Context<unknown>,
blocker: schema.AccountOwner & { account: schema.Account },
blockee: schema.Account & { owner: schema.AccountOwner | null },
): Promise<schema.Block | null> {
const result = await db
.insert(schema.blocks)
.values({
accountId: blocker.id,
blockedAccountId: blockee.id,
})
.returning();
if (result.length < 1) return null;
if (blockee.owner == null) {
await unfollowAccount(
db,
ctx,
{ ...blocker.account, owner: blocker },
blockee,
);
await removeFollower(
db,
ctx,
{ ...blocker.account, owner: blocker },
blockee,
);
await ctx.sendActivity(
{ username: blocker.handle },
{ id: new URL(blockee.iri), inboxId: new URL(blockee.inboxUrl) },
new Block({
id: new URL(`#block/${blockee.id}`, blocker.account.iri),
actor: new URL(blocker.account.iri),
object: new URL(blockee.iri),
}),
{ excludeBaseUris: [new URL(ctx.origin)] },
);
}
return result[0];
}

// TODO: define unblockAccount()
Loading

0 comments on commit 5c7a786

Please sign in to comment.