Skip to content

Commit

Permalink
Correct following/followers count
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlia committed Jun 24, 2024
1 parent e9c2570 commit 553413f
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 26 deletions.
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"dependencies": {
"@aws-sdk/client-s3": "^3.577.0",
"@aws-sdk/credential-providers": "^3.577.0",
"@fedify/fedify": "^0.11.0-dev.237",
"@fedify/fedify": "0.11.0-dev.239",
"@fedify/markdown-it-hashtag": "0.2.0",
"@fedify/markdown-it-mention": "^0.1.1",
"@fedify/redis": "^0.1.1",
Expand Down
17 changes: 17 additions & 0 deletions src/api/v1/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ import * as vocab from "@fedify/fedify/vocab";
import { zValidator } from "@hono/zod-validator";
import {
and,
count,
desc,
eq,
gte,
ilike,
inArray,
isNotNull,
isNull,
lte,
or,
sql,
} from "drizzle-orm";
import { Hono } from "hono";
import { z } from "zod";
Expand Down Expand Up @@ -631,6 +634,20 @@ app.post(
}),
);
}
await db
.update(accounts)
.set({
followingCount: sql`${db
.select({ cnt: count() })
.from(follows)
.where(
and(
eq(follows.followerId, owner.id),
isNotNull(follows.approved),
),
)}`,
})
.where(eq(accounts.id, owner.id));
}
const reverse = await db.query.follows.findFirst({
where: and(eq(follows.followingId, owner.id), eq(follows.followerId, id)),
Expand Down
13 changes: 12 additions & 1 deletion src/api/v1/follow_requests.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Accept, Follow, Reject } from "@fedify/fedify";
import { and, count, eq, isNull } from "drizzle-orm";
import { and, count, eq, isNotNull, isNull, sql } from "drizzle-orm";
import { Hono } from "hono";
import db from "../../db";
import {
Expand Down Expand Up @@ -62,6 +62,17 @@ app.post(
),
)
.returning({ iri: follows.iri });
await db
.update(accounts)
.set({
followersCount: sql`${db
.select({ cnt: count() })
.from(follows)
.where(
and(eq(follows.followingId, owner.id), isNotNull(follows.approved)),
)}`,
})
.where(eq(accounts.id, owner.id));
if (result.length < 1) return c.json({ error: "Record not found" }, 404);
if (follower.owner == null) {
const fedCtx = federation.createContext(c.req.raw, undefined);
Expand Down
134 changes: 110 additions & 24 deletions src/federation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,16 @@ import {
import { RedisKvStore, RedisMessageQueue } from "@fedify/redis";
import { getLogger } from "@logtape/logtape";
import { parse } from "@std/semver";
import { and, count, eq, ilike, inArray, like, sql } from "drizzle-orm";
import {
and,
count,
eq,
ilike,
inArray,
isNotNull,
like,
sql,
} from "drizzle-orm";
import metadata from "../../package.json" with { type: "json" };
import db from "../db";
import redis, { createRedis } from "../redis";
Expand Down Expand Up @@ -125,7 +134,12 @@ federation
db
.select({ id: follows.followerId })
.from(follows)
.where(eq(follows.followingId, owner.id)),
.where(
and(
eq(follows.followingId, owner.id),
isNotNull(follows.approved),
),
),
),
filter == null
? undefined
Expand All @@ -149,27 +163,55 @@ federation
)
.setFirstCursor(async (_ctx, _handle) => "0")
.setCounter(async (_ctx, handle) => {
const result = await db
.select({ cnt: count() })
.from(follows)
.where(
eq(
follows.followingId,
const owner = await db.query.accountOwners.findFirst({
where: eq(accountOwners.handle, handle),
with: { account: true },
});
return owner == null ? 0 : owner.account.followersCount;
});

federation
.setFollowingDispatcher(
"/@{handle}/following",
async (_ctx, handle, cursor) => {
const owner = await db.query.accountOwners.findFirst({
where: eq(accountOwners.handle, handle),
});
if (owner == null || cursor == null) return null;
const offset = Number.parseInt(cursor);
if (!Number.isInteger(offset)) return null;
const following = await db.query.accounts.findMany({
where: inArray(
accounts.id,
db
.select({ id: accountOwners.id })
.from(accountOwners)
.where(eq(accountOwners.handle, handle)),
.select({ id: follows.followingId })
.from(follows)
.where(
and(
eq(follows.followerId, owner.id),
isNotNull(follows.approved),
),
),
),
);
return result.length > 0 ? result[0].cnt : 0;
offset,
orderBy: accounts.id,
limit: 41,
});
return {
items: following.slice(0, 40).map((f) => new URL(f.iri)),
nextCursor: following.length > 40 ? `${offset + 40}` : null,
};
},
)
.setFirstCursor(async (_ctx, _handle) => "0")
.setCounter(async (_ctx, handle) => {
const owner = await db.query.accountOwners.findFirst({
where: eq(accountOwners.handle, handle),
with: { account: true },
});
return owner == null ? 0 : owner.account.followingCount;
});

federation.setFollowingDispatcher("/@{handle}/following", async (_ctx, _) => {
return {
items: [], // TODO: Implement this
};
});

federation.setOutboxDispatcher("/@{handle}/outbox", async (_ctx, _) => {
return {
items: [], // TODO: Implement this
Expand Down Expand Up @@ -228,14 +270,18 @@ federation
object: follow,
}),
);
const result = await db
.select({ cnt: count() })
.from(follows)
.where(eq(follows.followingId, following.id));
await db
.update(accounts)
.set({
followersCount: result.length < 1 ? 0 : result[0].cnt,
followersCount: sql`${db
.select({ cnt: count() })
.from(follows)
.where(
and(
eq(follows.followingId, following.id),
isNotNull(follows.approved),
),
)}`,
})
.where(eq(accounts.id, following.id));
}
Expand Down Expand Up @@ -277,6 +323,26 @@ federation
eq(follows.followingId, account.id),
),
);
await db
.update(accounts)
.set({
followingCount: sql`${db
.select({ cnt: count() })
.from(follows)
.where(
and(
eq(
follows.followerId,
db
.select({ id: accounts.id })
.from(accounts)
.where(eq(accounts.iri, object.actorId.href)),
),
isNotNull(follows.approved),
),
)}`,
})
.where(eq(accounts.iri, object.actorId.href));
}
})
.on(Reject, async (ctx, reject) => {
Expand Down Expand Up @@ -314,6 +380,26 @@ federation
eq(follows.followingId, account.id),
),
);
await db
.update(accounts)
.set({
followingCount: sql`${db
.select({ cnt: count() })
.from(follows)
.where(
and(
eq(
follows.followerId,
db
.select({ id: accounts.id })
.from(accounts)
.where(eq(accounts.iri, object.actorId.href)),
),
isNotNull(follows.approved),
),
)}`,
})
.where(eq(accounts.iri, object.actorId.href));
}
})
.on(Create, async (ctx, create) => {
Expand Down

0 comments on commit 553413f

Please sign in to comment.