Skip to content

Commit

Permalink
Refactor getMembers / recent authors (#3560)
Browse files Browse the repository at this point in the history
* add userIds param to getMembers

* adapt recent_authors to not fetch all members

* review
  • Loading branch information
philipperolet authored Feb 2, 2024
1 parent 681d88c commit 39816e9
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 36 deletions.
36 changes: 17 additions & 19 deletions front/lib/api/assistant/recent_authors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
} from "@dust-tt/types";
import { Sequelize } from "sequelize";

import { getMembers } from "@app/lib/api/workspace";
import type { Authenticator } from "@app/lib/auth";
import { AgentConfiguration } from "@app/lib/models";
import { safeRedisClient } from "@app/lib/redis";
Expand Down Expand Up @@ -105,35 +106,30 @@ async function populateAuthorIdsFromDb({
}

function renderAuthors(
authorIds: readonly string[],
members: UserType[],
authors: UserType[],
currentUserId?: number
): readonly string[] {
return (
authorIds
.map((id) => parseInt(id, 10))
.map((authorId) => {
authors
.map((author) => {
// If authorId is the current requester, return "Me".
if (authorId === currentUserId) {
if (author.id === currentUserId) {
return "Me";
}
return members.find((m) => m.id === authorId)?.fullName ?? null;
return author.fullName;
})
// Filter out `null` authors.
.filter((name): name is string => name !== null)
);
}

export async function getAgentRecentAuthors(
{
agent,
auth,
}: {
agent: LightAgentConfigurationType;
auth: Authenticator;
},
members: UserType[]
): Promise<AgentRecentAuthors> {
export async function getAgentRecentAuthors({
agent,
auth,
}: {
agent: LightAgentConfigurationType;
auth: Authenticator;
}): Promise<AgentRecentAuthors> {
const { sId: agentId, versionAuthorId } = agent;

const owner = auth.workspace();
Expand Down Expand Up @@ -162,9 +158,11 @@ export async function getAgentRecentAuthors(
// Populate from the database and store in Redis if the entry is not already present.
recentAuthorIds = await populateAuthorIdsFromDb({ agentId, workspaceId });
}

const authors = await getMembers(auth, {
userIds: recentAuthorIds.map((id) => parseInt(id, 10)),
});
// Consider moving this logic to the FE if we need to fetch members in different places.
return renderAuthors(recentAuthorIds, members, currentUserId);
return renderAuthors(authors, currentUserId);
}

export async function agentConfigurationWasUpdatedBy({
Expand Down
4 changes: 3 additions & 1 deletion front/lib/api/data_sources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,9 @@ async function warnPostDeletion(
switch (dataSourceProvider) {
case "github":
// get admin emails
const adminEmails = (await getMembers(auth, "admin")).map((u) => u.email);
const adminEmails = (await getMembers(auth, { role: "admin" })).map(
(u) => u.email
);
// send email to admins
for (const email of adminEmails) await sendGithubDeletionEmail(email);
break;
Expand Down
21 changes: 18 additions & 3 deletions front/lib/api/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,30 @@ export async function setInternalWorkspaceSegmentation(
*/
export async function getMembers(
auth: Authenticator,
role?: RoleType
{
role,
userIds,
}: {
role?: RoleType;
userIds?: ModelId[];
} = {}
): Promise<UserTypeWithWorkspaces[]> {
const owner = auth.workspace();
if (!owner) {
return [];
}
const whereClause = role
? { workspaceId: owner.id, role }

const whereClause: {
workspaceId: ModelId;
userId?: ModelId[];
role?: RoleType;
} = userIds
? { workspaceId: owner.id, userId: userIds }
: { workspaceId: owner.id };
if (role) {
whereClause.role = role;
}

const memberships = await Membership.findAll({
where: whereClause,
});
Expand Down
8 changes: 5 additions & 3 deletions front/pages/api/stripe/webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ async function handler(
"Couldn't get owner or subscription from `auth`."
);
}
const adminEmails = (await getMembers(auth, "admin")).map(
const adminEmails = (await getMembers(auth, { role: "admin" })).map(
(u) => u.email
);
const customerEmail = invoice.customer_email;
Expand Down Expand Up @@ -464,7 +464,7 @@ async function handler(
);

// then email admins
const adminEmails = (await getMembers(auth, "admin")).map(
const adminEmails = (await getMembers(auth, { role: "admin" })).map(
(u) => u.email
);
if (adminEmails.length === 0) {
Expand Down Expand Up @@ -675,7 +675,9 @@ async function checkStaticDatasourcesSize(auth: Authenticator) {
}
await sendOpsDowngradeTooMuchDataEmail(workspace.sId, datasourcesTooBig);
// for all admins
const adminEmails = (await getMembers(auth, "admin")).map((u) => u.email);
const adminEmails = (await getMembers(auth, { role: "admin" })).map(
(u) => u.email
);
for (const adminEmail of adminEmails)
await sendAdminDowngradeTooMuchDataEmail(adminEmail, datasourcesTooBig);
}
Expand Down
14 changes: 4 additions & 10 deletions front/pages/api/w/[wId]/assistant/agent_configurations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
getAgentConfigurations,
} from "@app/lib/api/assistant/configuration";
import { getAgentRecentAuthors } from "@app/lib/api/assistant/recent_authors";
import { getMembers } from "@app/lib/api/workspace";
import { Authenticator, getSession } from "@app/lib/auth";
import { safeRedisClient } from "@app/lib/redis";
import { apiError, withLogging } from "@app/logger/withlogging";
Expand Down Expand Up @@ -130,22 +129,17 @@ async function handler(
}

if (withAuthors === "true") {
const members = await getMembers(auth);

agentConfigurations = await Promise.all(
agentConfigurations.map(
async (
agentConfiguration
): Promise<LightAgentConfigurationType> => {
return {
...agentConfiguration,
lastAuthors: await getAgentRecentAuthors(
{
agent: agentConfiguration,
auth,
},
members
),
lastAuthors: await getAgentRecentAuthors({
agent: agentConfiguration,
auth,
}),
};
}
)
Expand Down

0 comments on commit 39816e9

Please sign in to comment.