From 735b49e505112798dbd4f7f6380b0cf0eecc40d8 Mon Sep 17 00:00:00 2001 From: Tim Abbott Date: Wed, 18 Oct 2017 21:21:27 -0700 Subject: [PATCH] zilencer: Fix uniqueness rules for RemotePushDeviceToken. Since a user could use the same installation of the Zulip mobile app with multiple Zulip servers, correct behavior is to allow reusing the same token with multiple Zulip servers in the RemotePushDeviceToken model. --- zerver/models.py | 2 +- ...05_remotepushdevicetoken_fix_uniqueness.py | 29 +++++++++++++++++++ zilencer/models.py | 6 +++- 3 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 zilencer/migrations/0005_remotepushdevicetoken_fix_uniqueness.py diff --git a/zerver/models.py b/zerver/models.py index 882526caaa..eaa2f3bcee 100644 --- a/zerver/models.py +++ b/zerver/models.py @@ -821,7 +821,6 @@ class AbstractPushDeviceToken(models.Model): # sent to us from each device: # - APNS token if kind == APNS # - GCM registration id if kind == GCM - token = models.CharField(max_length=4096, unique=True) # type: bytes last_updated = models.DateTimeField(auto_now=True) # type: datetime.datetime # [optional] Contains the app id of the device if it is an iOS device @@ -833,6 +832,7 @@ class AbstractPushDeviceToken(models.Model): class PushDeviceToken(AbstractPushDeviceToken): # The user who's device this is user = models.ForeignKey(UserProfile, db_index=True, on_delete=CASCADE) # type: UserProfile + token = models.CharField(max_length=4096, unique=True) # type: bytes def generate_email_token_for_stream(): # type: () -> str diff --git a/zilencer/migrations/0005_remotepushdevicetoken_fix_uniqueness.py b/zilencer/migrations/0005_remotepushdevicetoken_fix_uniqueness.py new file mode 100644 index 0000000000..9a4051ee0d --- /dev/null +++ b/zilencer/migrations/0005_remotepushdevicetoken_fix_uniqueness.py @@ -0,0 +1,29 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.5 on 2017-10-19 04:23 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('zilencer', '0004_remove_deployment_model'), + ] + + operations = [ + migrations.AlterField( + model_name='remotepushdevicetoken', + name='token', + field=models.CharField(db_index=True, max_length=4096), + ), + migrations.AlterField( + model_name='remotepushdevicetoken', + name='user_id', + field=models.BigIntegerField(db_index=True), + ), + migrations.AlterUniqueTogether( + name='remotepushdevicetoken', + unique_together=set([('server', 'token')]), + ), + ] diff --git a/zilencer/models.py b/zilencer/models.py index 47f21417a5..30816df904 100644 --- a/zilencer/models.py +++ b/zilencer/models.py @@ -21,4 +21,8 @@ class RemoteZulipServer(models.Model): class RemotePushDeviceToken(zerver.models.AbstractPushDeviceToken): server = models.ForeignKey(RemoteZulipServer) # type: RemoteZulipServer # The user id on the remote server for this device device this is - user_id = models.BigIntegerField() # type: int + user_id = models.BigIntegerField(db_index=True) # type: int + token = models.CharField(max_length=4096, db_index=True) # type: bytes + + class Meta(object): + unique_together = ("server", "token")