Initial attempt at setting up a Follower scheme
This commit is contained in:
parent
6e16b36d49
commit
b9c9c6bec6
|
@ -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;
|
||||
}
|
|
@ -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,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
});
|
Loading…
Reference in New Issue