Initial attempt at setting up a Follower scheme

This commit is contained in:
Robbie Antenesse 2020-01-20 09:47:31 -08:00
parent 6e16b36d49
commit b9c9c6bec6
2 changed files with 79 additions and 0 deletions

View File

@ -6,6 +6,7 @@ module.exports = models => {
Status,
Review,
Recommendation,
Follow,
} = models;
User.belongsTo(PermissionLevel, {
@ -18,6 +19,14 @@ module.exports = models => {
User.hasMany(Recommendation, {
foreignKey: 'toUser',
});
User.hasMany(Follow, {
foreignKey: 'follower',
as: 'Followers',
});
User.belongsTo(Follow.scope('internal'), {
foreignKey: 'following',
as: 'Follows',
});
return User;
}

View File

@ -0,0 +1,70 @@
const Sequelize = require('sequelize');
module.exports = sequelize => sequelize.define('Follow', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
},
follower: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: sequelize.models.User,
key: 'id',
deferrable: Sequelize.Deferrable.INITIALLY_IMMEDIATE,
},
},
followerDomain: {
type: Sequelize.TEXT,
allowNull: true,
comment: 'A null value means that the following id is on this server, otherwise it is external.',
},
following: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: sequelize.models.User,
key: 'id',
deferrable: Sequelize.Deferrable.INITIALLY_IMMEDIATE,
},
},
followingDomain: {
type: Sequelize.TEXT,
allowNull: true,
comment: 'A null value means that the following id is on this server, otherwise it is external.',
},
// Timestamps
createdAt: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.NOW,
},
updatedAt: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.NOW,
},
}, {
indexes: [
{
fields: ['follower'],
},
{
fields: ['following', 'domain'],
},
],
scopes: {
internal: {
where: {
followerDomain: {
[Sequelize.Op.is]: null,
},
followingDomain: {
[Sequelize.Op.is]: null,
},
},
},
}
});