Split follows, follow requests and blocks into separate tables.

As discussed in issue #82
This commit is contained in:
Adam Kelly 2020-03-11 12:20:19 +00:00
parent e8cfa99cd9
commit e050704c2c
2 changed files with 151 additions and 8 deletions

View file

@ -0,0 +1,115 @@
# Generated by Django 3.0.3 on 2020-03-11 12:12
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('fedireads', '0014_status_remote_id'),
]
operations = [
migrations.CreateModel(
name='UserBlocks',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created_date', models.DateTimeField(auto_now_add=True)),
('updated_date', models.DateTimeField(auto_now=True)),
('relationship_id', models.CharField(max_length=100)),
],
options={
'abstract': False,
},
),
migrations.CreateModel(
name='UserFollowRequest',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created_date', models.DateTimeField(auto_now_add=True)),
('updated_date', models.DateTimeField(auto_now=True)),
('relationship_id', models.CharField(max_length=100)),
],
options={
'abstract': False,
},
),
migrations.CreateModel(
name='UserFollows',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created_date', models.DateTimeField(auto_now_add=True)),
('updated_date', models.DateTimeField(auto_now=True)),
('relationship_id', models.CharField(max_length=100)),
],
options={
'abstract': False,
},
),
migrations.RemoveField(
model_name='user',
name='followers',
),
migrations.DeleteModel(
name='UserRelationship',
),
migrations.AddField(
model_name='userfollows',
name='user_object',
field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='userfollows_user_object', to=settings.AUTH_USER_MODEL),
),
migrations.AddField(
model_name='userfollows',
name='user_subject',
field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='userfollows_user_subject', to=settings.AUTH_USER_MODEL),
),
migrations.AddField(
model_name='userfollowrequest',
name='user_object',
field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='userfollowrequest_user_object', to=settings.AUTH_USER_MODEL),
),
migrations.AddField(
model_name='userfollowrequest',
name='user_subject',
field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='userfollowrequest_user_subject', to=settings.AUTH_USER_MODEL),
),
migrations.AddField(
model_name='userblocks',
name='user_object',
field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='userblocks_user_object', to=settings.AUTH_USER_MODEL),
),
migrations.AddField(
model_name='userblocks',
name='user_subject',
field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='userblocks_user_subject', to=settings.AUTH_USER_MODEL),
),
migrations.AddField(
model_name='user',
name='blocks',
field=models.ManyToManyField(related_name='blocked_by', through='fedireads.UserBlocks', to=settings.AUTH_USER_MODEL),
),
migrations.AddField(
model_name='user',
name='follow_requests',
field=models.ManyToManyField(related_name='follower_requests', through='fedireads.UserFollowRequest', to=settings.AUTH_USER_MODEL),
),
migrations.AddField(
model_name='user',
name='following',
field=models.ManyToManyField(related_name='followers', through='fedireads.UserFollows', to=settings.AUTH_USER_MODEL),
),
migrations.AddConstraint(
model_name='userfollows',
constraint=models.UniqueConstraint(fields=('user_subject', 'user_object'), name='userfollows_unique'),
),
migrations.AddConstraint(
model_name='userfollowrequest',
constraint=models.UniqueConstraint(fields=('user_subject', 'user_object'), name='userfollowrequest_unique'),
),
migrations.AddConstraint(
model_name='userblocks',
constraint=models.UniqueConstraint(fields=('user_subject', 'user_object'), name='userblocks_unique'),
),
]

View file

@ -34,12 +34,26 @@ class User(AbstractUser):
# name is your display name, which you can change at will
name = models.CharField(max_length=100, blank=True, null=True)
avatar = models.ImageField(upload_to='avatars/', blank=True, null=True)
followers = models.ManyToManyField(
following = models.ManyToManyField(
'self',
symmetrical=False,
through='UserRelationship',
through_fields=('user_object', 'user_subject'),
related_name='following'
through='UserFollows',
through_fields=('user_subject', 'user_object'),
related_name='followers'
)
follow_requests = models.ManyToManyField(
'self',
symmetrical=False,
through='UserFollowRequest',
through_fields=('user_subject', 'user_object'),
related_name='follower_requests'
)
blocks = models.ManyToManyField(
'self',
symmetrical=False,
through='UserBlocks',
through_fields=('user_subject', 'user_object'),
related_name='blocked_by'
)
favorites = models.ManyToManyField(
'Status',
@ -65,22 +79,22 @@ class UserRelationship(FedireadsModel):
user_subject = models.ForeignKey(
'User',
on_delete=models.PROTECT,
related_name='user_subject'
related_name='%(class)s_user_subject'
)
user_object = models.ForeignKey(
'User',
on_delete=models.PROTECT,
related_name='user_object'
related_name='%(class)s_user_object'
)
# follow or follow_request for pending TODO: blocking?
status = models.CharField(max_length=100, default='follows', null=True)
relationship_id = models.CharField(max_length=100)
class Meta:
abstract = True
constraints = [
models.UniqueConstraint(
fields=['user_subject', 'user_object'],
name='followers_unique'
name='%(class)s_unique'
)
]
@ -90,6 +104,20 @@ class UserRelationship(FedireadsModel):
base_path = self.user_subject.absolute_id
return '%s#%s/%d' % (base_path, self.status, self.id)
class UserFollows(UserRelationship):
@property
def status(self):
return 'follows'
class UserFollowRequest(UserRelationship):
@property
def status(self):
return 'follow_request'
class UserBlocks(UserRelationship):
@property
def status(self):
return 'blocks'
class FederatedServer(FedireadsModel):
''' store which server's we federate with '''