Skip to content

Commit

Permalink
[Runner] Avatars can be seen in membership list & member detail (#2944)
Browse files Browse the repository at this point in the history
* [Runner] Avatars can be seen in membership list & member detail

Related [card](dust-tt/tasks#289) and
[discussion](https://dust4ai.slack.com/archives/C050SM8NSPK/p1702990922381299?thread_ts=1702899295.554769&cid=C050SM8NSPK)

* switch back to message context
  • Loading branch information
philipperolet authored Dec 20, 2023
1 parent b720bd7 commit 3c3c276
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 2 deletions.
2 changes: 1 addition & 1 deletion front/lib/api/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export async function getMembers(
fullName: u.firstName + (u.lastName ? ` ${u.lastName}` : ""),
firstName: u.firstName,
lastName: u.lastName,
image: null,
image: u.imageUrl,
workspaces: [{ ...owner, role }],
};
});
Expand Down
15 changes: 14 additions & 1 deletion front/lib/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,19 @@ export async function getUserFromSession(
},
});

if (session.user.image !== user.imageUrl) {
void User.update(
{
imageUrl: session.user.image,
},
{
where: {
id: user.id,
},
}
);
}

return {
id: user.id,
provider: user.provider,
Expand All @@ -413,7 +426,7 @@ export async function getUserFromSession(
firstName: user.firstName,
lastName: user.lastName,
fullName: user.firstName + (user.lastName ? ` ${user.lastName}` : ""),
image: session.user ? session.user.image : null,
image: user.imageUrl,
workspaces: workspaces.map((w) => {
const m = memberships.find((m) => m.workspaceId === w.id);
let role = "none" as RoleType;
Expand Down
5 changes: 5 additions & 0 deletions front/lib/models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export class User extends Model<
declare name: string;
declare firstName: string;
declare lastName: string | null;
declare imageUrl: string | null;

declare isDustSuperUser: CreationOptional<boolean>;
}
Expand Down Expand Up @@ -71,6 +72,10 @@ User.init(
type: DataTypes.STRING,
allowNull: true,
},
imageUrl: {
type: DataTypes.STRING,
allowNull: true,
},
isDustSuperUser: {
type: DataTypes.BOOLEAN,
defaultValue: false,
Expand Down
84 changes: 84 additions & 0 deletions front/migrations/20231219_imageUrl_backfill.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import {
AgentConfiguration,
Membership,
User,
UserMessage,
} from "@app/lib/models";

async function main() {
console.log("Starting imageUrl backfill");
const workspaceIds = (
await AgentConfiguration.findAll({
attributes: ["workspaceId"],
group: ["workspaceId"],
})
).map((a) => a.workspaceId);

console.log(`Found ${workspaceIds.length} workspaces to update`);
const chunks = [];
for (let i = 0; i < workspaceIds.length; i += 16) {
chunks.push(workspaceIds.slice(i, i + 16));
}

for (let i = 0; i < chunks.length; i++) {
console.log(`Processing workspace chunk ${i}/${chunks.length}...`);
const chunk = chunks[i];

await Promise.all(
chunk.map((wid: number) => {
return (async () => {
await backfillImageUrl(wid);
})();
})
);
}
}

async function backfillImageUrl(workspaceId: number) {
// get all users from workspace whose imageUrl is null
const users = await User.findAll({
where: {
imageUrl: null,
},
include: [
{
model: Membership,
where: {
workspaceId,
},
required: true,
},
],
});

// for each user, find the last usermessage
// and set the user's imageUrl to the usermessage's userContextProfilePictureUrl
for (const user of users) {
const userMessage = await UserMessage.findOne({
where: {
userId: user.id,
},
order: [["createdAt", "DESC"]],
});
if (!userMessage) {
console.log(
`No user messages found for user with id ${user.id} in workspace with id ${workspaceId}`
);
continue;
}

await user.update({
imageUrl: userMessage.userContextProfilePictureUrl,
});
}
}

main()
.then(() => {
console.log("Done");
process.exit(0);
})
.catch((err) => {
console.error(err);
process.exit(1);
});

0 comments on commit 3c3c276

Please sign in to comment.