From b9c9c6bec672d68a3ea9317f2254b4fe30b0c2d0 Mon Sep 17 00:00:00 2001 From: Robbie Antenesse Date: Mon, 20 Jan 2020 09:47:31 -0800 Subject: [PATCH] Initial attempt at setting up a Follower scheme --- server/sequelize/associations/User.js | 9 ++++ server/sequelize/models/Follow.js | 70 +++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 server/sequelize/models/Follow.js diff --git a/server/sequelize/associations/User.js b/server/sequelize/associations/User.js index 3da9c1d..47b59dc 100644 --- a/server/sequelize/associations/User.js +++ b/server/sequelize/associations/User.js @@ -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; } \ No newline at end of file diff --git a/server/sequelize/models/Follow.js b/server/sequelize/models/Follow.js new file mode 100644 index 0000000..dddb9bf --- /dev/null +++ b/server/sequelize/models/Follow.js @@ -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, + }, + }, + }, + } +}); \ No newline at end of file