This commit is contained in:
Paul DeVito 2026-05-06 13:51:21 +02:00 committed by GitHub
commit 1122be3399
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 744 additions and 5 deletions

View file

@ -357,7 +357,7 @@ class User extends Model {
[sequelize.Op.like]: username
}
},
include: this.sequelize.models.mediaProgress
include: [this.sequelize.models.mediaProgress, this.sequelize.models.userSeriesFollow]
})
if (user) userCache.set(user)
@ -382,7 +382,7 @@ class User extends Model {
[sequelize.Op.like]: email
}
},
include: this.sequelize.models.mediaProgress
include: [this.sequelize.models.mediaProgress, this.sequelize.models.userSeriesFollow]
})
if (user) userCache.set(user)
@ -402,7 +402,7 @@ class User extends Model {
if (cachedUser) return cachedUser
const user = await this.findByPk(userId, {
include: this.sequelize.models.mediaProgress
include: [this.sequelize.models.mediaProgress, this.sequelize.models.userSeriesFollow]
})
if (user) userCache.set(user)
@ -426,7 +426,7 @@ class User extends Model {
where: {
[sequelize.Op.or]: [{ id: userId }, { 'extraData.oldUserId': userId }]
},
include: this.sequelize.models.mediaProgress
include: [this.sequelize.models.mediaProgress, this.sequelize.models.userSeriesFollow]
})
if (user) userCache.set(user)
@ -447,7 +447,7 @@ class User extends Model {
const user = await this.findOne({
where: sequelize.where(sequelize.literal(`extraData->>"authOpenIDSub"`), sub),
include: this.sequelize.models.mediaProgress
include: [this.sequelize.models.mediaProgress, this.sequelize.models.userSeriesFollow]
})
if (user) userCache.set(user)
@ -506,6 +506,17 @@ class User extends Model {
}
}
/**
* Invalidate cached user when series follows change
* @param {string} userId
*/
static seriesFollowChanged(userId) {
const cachedUser = userCache.getById(userId)
if (cachedUser) {
userCache.delete(userId)
}
}
/**
* Initialize model
* @param {import('../Database').sequelize} sequelize
@ -621,6 +632,7 @@ class User extends Model {
isOldToken: this.isOldToken,
mediaProgress: this.mediaProgresses?.map((mp) => mp.getOldMediaProgress()) || [],
seriesHideFromContinueListening: [...seriesHideFromContinueListening],
seriesFollowing: this.userSeriesFollows?.map((f) => f.seriesId) || [],
bookmarks: this.bookmarks?.map((b) => ({ ...b })) || [],
isActive: this.isActive,
isLocked: this.isLocked,

View file

@ -0,0 +1,84 @@
const { DataTypes, Model } = require('sequelize')
class UserSeriesFollow extends Model {
constructor(values, options) {
super(values, options)
/** @type {UUIDV4} */
this.id
/** @type {UUIDV4} */
this.userId
/** @type {UUIDV4} */
this.seriesId
/** @type {Date} */
this.createdAt
}
/**
* Get array of series IDs that a user is following
* @param {string} userId
* @returns {Promise<string[]>}
*/
static async getFollowedSeriesIdsForUser(userId) {
const follows = await this.findAll({
where: { userId },
attributes: ['seriesId']
})
return follows.map((f) => f.seriesId)
}
/**
* Initialize model
* @param {import('../Database').sequelize} sequelize
*/
static init(sequelize) {
super.init(
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true
}
},
{
sequelize,
modelName: 'userSeriesFollow',
timestamps: true,
updatedAt: false,
indexes: [
{
name: 'user_series_follows_userId',
fields: ['userId']
},
{
name: 'user_series_follows_unique',
fields: ['userId', 'seriesId'],
unique: true
},
{
name: 'user_series_follows_seriesId',
fields: ['seriesId']
}
]
}
)
// Super Many-to-Many
// ref: https://sequelize.org/docs/v6/advanced-association-concepts/advanced-many-to-many/#the-best-of-both-worlds-the-super-many-to-many-relationship
const { user, series } = sequelize.models
user.belongsToMany(series, { through: UserSeriesFollow, as: 'followedSeries' })
series.belongsToMany(user, { through: UserSeriesFollow, as: 'followers' })
user.hasMany(UserSeriesFollow, {
onDelete: 'CASCADE'
})
UserSeriesFollow.belongsTo(user)
series.hasMany(UserSeriesFollow, {
onDelete: 'CASCADE'
})
UserSeriesFollow.belongsTo(series)
}
}
module.exports = UserSeriesFollow