Refactor: Remove sourceAvatarUrl field and use hash-based comparison on avatarUrl

- Remove sourceAvatarUrl field from User model
- Delete database migration for sourceAvatarUrl
- Update userProvisioner to schedule avatar sync directly instead of via event
- Remove AvatarSyncProcessor (no longer needed)
- Simplify UploadUserAvatarTask to not set sourceAvatarUrl
- Avatar change detection now uses hash comparison on existing avatarUrl field
This commit is contained in:
codegen-sh[bot] committed 2025-11-24 11:41:09 +00:00
1 parent 19283451dd
commit 315c2ea327
5 files changed
+10 -72

No files matched your search

+10 -2
View File
@@ -9,6 +9,7 @@ import {
} from "@server/errors";
import Logger from "@server/logging/Logger";
import { Team, User, UserAuthentication } from "@server/models";
import UploadUserAvatarTask from "@server/queues/tasks/UploadUserAvatarTask";
import { sequelize } from "@server/storage/database";
import { APIContext } from "@server/types";
@@ -139,7 +140,6 @@ export default async function userProvisioner(
{
name,
avatarUrl,
sourceAvatarUrl: avatarUrl,
lastActiveAt: new Date(),
lastActiveIp: ctx.ip,
},
@@ -162,6 +162,15 @@ export default async function userProvisioner(
);
});
// Schedule avatar sync task if user has an avatar URL and hasn't manually changed it
if (avatarUrl && !existingUser.getFlag("avatarChanged")) {
await new UploadUserAvatarTask().schedule({
userId: existingUser.id,
avatarUrl,
isSync: true,
});
}
if (isInvite) {
const inviter = await existingUser.$get("invitedBy");
if (inviter) {
@@ -219,7 +228,6 @@ export default async function userProvisioner(
role: role ?? team?.defaultUserRole,
teamId,
avatarUrl,
sourceAvatarUrl: avatarUrl,
authentications: authentication ? [authentication] : [],
lastActiveAt: new Date(),
lastActiveIp: ctx.ip,
@@ -1,15 +0,0 @@
"use strict";
/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.addColumn("users", "sourceAvatarUrl", {
type: Sequelize.STRING(4096),
allowNull: true,
});
},
async down(queryInterface, Sequelize) {
await queryInterface.removeColumn("users", "sourceAvatarUrl");
},
};
-9
View File
@@ -223,15 +223,6 @@ class User extends ParanoidModel<
this.setDataValue("avatarUrl", value);
}
@AllowNull
@IsUrlOrRelativePath
@Length({
max: 4096,
msg: "sourceAvatarUrl must be less than 4096 characters",
})
@Column(DataType.STRING)
sourceAvatarUrl: string | null;
// associations
@BelongsTo(() => User, "suspendedById")
suspendedBy: User | null;
@@ -1,45 +0,0 @@
import { User } from "@server/models";
import { UserEvent } from "@server/types";
import UploadUserAvatarTask from "../tasks/UploadUserAvatarTask";
import BaseProcessor from "./BaseProcessor";
/**
* Processor to handle avatar synchronization on user signin events.
* This checks if the user's avatar from their identity provider has changed
* and updates it if necessary, unless the user has manually changed their avatar.
*/
export default class AvatarSyncProcessor extends BaseProcessor {
static applicableEvents: UserEvent["name"][] = ["users.signin"];
async perform(event: UserEvent) {
switch (event.name) {
case "users.signin": {
const user = await User.findByPk(event.userId, {
rejectOnEmpty: true,
});
// Only sync if user has a source avatar URL to compare against
if (!user.sourceAvatarUrl) {
return;
}
// Check if user has manually changed their avatar
if (user.getFlag("avatarChanged")) {
return; // Don't override user's manual avatar choice
}
// The sourceAvatarUrl has been updated during the authentication process
// in userProvisioner. Now we need to check if it differs from the current
// stored avatar and update if necessary.
await new UploadUserAvatarTask().schedule({
userId: user.id,
avatarUrl: user.sourceAvatarUrl,
isSync: true,
});
break;
}
default:
}
}
}
@@ -51,7 +51,6 @@ export default class UploadUserAvatarTask extends BaseTask<Props> {
if (res?.url) {
await user.update({
avatarUrl: res.url,
sourceAvatarUrl: props.avatarUrl,
});
}
}