# Generated by Django 3.2.15 on 2022-08-17 14:22

import django.core.validators
import django.db.models.deletion
import django.utils.timezone
from django.conf import settings
from django.db import migrations, models

import academic_community.channels.models.groups
import academic_community.channels.models.issue
import academic_community.ckeditor5.fields


def create_links(apps, schema_editor):
    """Create links to the existing models."""
    User = apps.get_model("auth", "User")
    Group = apps.get_model("auth", "Group")

    UserMentionLink = apps.get_model("chats", "UserMentionLink")
    ManualUserMentionLink = apps.get_model("chats", "ManualUserMentionLink")
    GroupMentionLink = apps.get_model("chats", "GroupMentionLink")

    SubscribedChannelGroup = apps.get_model("chats", "SubscribedChannelGroup")
    FollowingChannelGroup = apps.get_model("chats", "FollowingChannelGroup")
    FavoritesChannelGroup = apps.get_model("chats", "FavoritesChannelGroup")

    for group in Group.objects.all():
        GroupMentionLink.objects.create(related_model=group)

    for user in User.objects.all():
        UserMentionLink.objects.create(related_model=user)
        ManualUserMentionLink.objects.create(related_model=user)

        SubscribedChannelGroup.objects.create(
            user=user,
            name="Subscribed",
            slug="subscribed",
            group_order=999,
            display_in_sidebar=False,
        )
        FollowingChannelGroup.objects.create(
            user=user, name="Following", slug="following", group_order=998
        )
        FavoritesChannelGroup.objects.create(
            user=user,
            name="Favorites",
            slug="favorites",
            group_order=0,
            expand_in_sidebar=True,
        )


def create_chat_settings(apps, schema_editor):
    """Create the subscription permission for the user model."""
    User = apps.get_model("auth", "User")
    Permission = apps.get_model("auth", "Permission")
    ContentType = apps.get_model("contenttypes", "ContentType")
    Group = apps.get_model("auth", "Group")
    ChatSettings = apps.get_model("chats", "ChatSettings")
    GroupObjectPermission = apps.get_model("guardian", "GroupObjectPermission")

    user_ct = ContentType.objects.get_for_model(User)

    permission = Permission.objects.get_or_create(
        codename="add_subscription",
        name="Can add channel subscription",
        content_type=user_ct,
    )[0]

    for user in User.objects.all():
        members_group = Group.objects.get_or_create(name="Community members")[
            0
        ]
        settings = ChatSettings.objects.create(user=user)
        settings.group_add_subscription_permission.add(members_group)
        GroupObjectPermission.objects.create(
            permission=permission,
            content_type=user_ct,
            object_pk=user.pk,
            group=members_group,
        )


def create_default_issue_statuses(apps, schema_editor):
    """Create the default issue statuses in the database."""
    IssueStatus = apps.get_model("chats", "IssueStatus")

    for name, is_closed in [
        ("New", False),
        ("In Progress", False),
        ("Resolved", False),
        ("Closed", True),
        ("Rejected", True),
        ("Finished", True),
    ]:
        IssueStatus.objects.create(name=name, is_closed=is_closed)


def create_default_issue_trackers(apps, schema_editor):
    """Create the default issue priorities in the database."""
    IssueTracker = apps.get_model("chats", "IssueTracker")

    for tracker in ["Bug", "Feature", "Support", "Task", "Subtask"]:
        IssueTracker.objects.create(name=tracker)


def create_default_issue_priorities(apps, schema_editor):
    """Create the default issue priorities in the database."""
    IssuePriority = apps.get_model("chats", "IssuePriority")

    for value, priority in enumerate(
        ["Low", "Normal", "High", "Urgent", "Immediate"], 1
    ):
        IssuePriority.objects.create(name=priority, value=value)


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        ("topics", "0011_auto_20220922_1955"),
        ("contenttypes", "0002_remove_content_type_name"),
        ("members", "0007_auto_20220206_2155"),
        ("auth", "0012_alter_user_first_name_max_length"),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ("uploaded_material", "0009_alter_material_uuid"),
        ("e2ee", "0001_initial"),
        ("programme", "0023_auto_20220922_2028"),
        ("activities", "0014_auto_20220919_1955"),
        ("guardian", "0002_generic_permissions_index"),
    ]

    operations = [
        migrations.CreateModel(
            name="ChannelGroup",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "name",
                    models.CharField(
                        help_text="The name of the group.", max_length=50
                    ),
                ),
                (
                    "slug",
                    models.SlugField(
                        help_text="The URL identifier for the group."
                    ),
                ),
                (
                    "group_order",
                    models.IntegerField(
                        default=1,
                        help_text="At what position should this group be displayed in the sidebar?",
                    ),
                ),
                (
                    "display_in_sidebar",
                    models.BooleanField(
                        blank=True,
                        choices=[
                            (True, "Yes"),
                            (False, "No"),
                            (None, "Only if not empty"),
                        ],
                        help_text="Shall the channels of this group be visible in the sidebar?",
                        null=True,
                    ),
                ),
                (
                    "expand_in_sidebar",
                    models.BooleanField(
                        default=False,
                        help_text="Expand this group by default in the sidebar",
                    ),
                ),
                (
                    "sidebar_display_option",
                    models.CharField(
                        choices=[
                            ("ALL", "All channels"),
                            ("UNREAD", "Only channels with unread comments"),
                            (
                                "FOLLOWING",
                                "Only channels that you are following",
                            ),
                            (
                                "FAVORITE",
                                "Only channels that you marked as favorite",
                            ),
                            (
                                "UNREAD_FOLLOWING",
                                "Only channels with unread comments or that you are following",
                            ),
                            (
                                "UNREAD_FAVORITE",
                                "Only channels with unread comments or that you marked as favorite",
                            ),
                            (
                                "FOLLOWING_FAVORITE",
                                "Only channels that you are following or marked as favorite",
                            ),
                            (
                                "UNREAD_FOLLOWING_FAVORITE",
                                "Only channels with unread comments, that you are following, or that you marked as favorite.",
                            ),
                        ],
                        default="ALL",
                        help_text="What channels of this group shall be displayed in the sidebar?",
                        max_length=30,
                    ),
                ),
                (
                    "user",
                    models.ForeignKey(
                        help_text="The user who defined this group",
                        on_delete=django.db.models.deletion.CASCADE,
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "ordering": ["group_order"],
            },
        ),
        migrations.CreateModel(
            name="ChannelMaterialRelation",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "pinned",
                    models.BooleanField(
                        default=False,
                        help_text="Should this material be pinned to the channel?",
                    ),
                ),
                (
                    "material",
                    models.ForeignKey(
                        help_text="The material that this relation corresponds to.",
                        on_delete=django.db.models.deletion.CASCADE,
                        to="uploaded_material.material",
                    ),
                ),
            ],
            options={
                "abstract": False,
            },
        ),
        migrations.CreateModel(
            name="Comment",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "body",
                    academic_community.ckeditor5.fields.CKEditor5Field(
                        help_text="The content of the comment."
                    ),
                ),
                (
                    "signature",
                    models.TextField(
                        blank=True,
                        help_text="Signature of the encrypted message.",
                        null=True,
                    ),
                ),
                (
                    "md5",
                    models.CharField(
                        blank=True,
                        help_text="The md5 hash of the comment.",
                        max_length=32,
                    ),
                ),
                (
                    "date_created",
                    models.DateTimeField(
                        default=django.utils.timezone.now,
                        help_text="The date when the comment has been posted",
                    ),
                ),
                (
                    "last_modification_date",
                    models.DateTimeField(
                        default=django.utils.timezone.now,
                        help_text="The date when the comment has last been edited.",
                    ),
                ),
                (
                    "encryption_key",
                    models.ForeignKey(
                        blank=True,
                        help_text="The encryption key that has been used to encrypt the body of this comment if e2ee is enabled.",
                        null=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        to="e2ee.encryptionkey",
                    ),
                ),
            ],
            options={
                "ordering": ["date_created"],
            },
        ),
        migrations.CreateModel(
            name="MentionLink",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "disabled",
                    models.BooleanField(
                        default=False,
                        help_text="Flag to enable excluding an object from being mentioned.",
                    ),
                ),
            ],
        ),
        migrations.CreateModel(
            name="Channel",
            fields=[
                (
                    "comment_ptr",
                    models.OneToOneField(
                        auto_created=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        parent_link=True,
                        primary_key=True,
                        serialize=False,
                        to="chats.comment",
                    ),
                ),
                (
                    "channel_id",
                    models.BigIntegerField(
                        blank=True,
                        help_text="The ID for the channel.",
                        unique=True,
                    ),
                ),
                (
                    "name",
                    models.CharField(
                        help_text="The subject of the channel.", max_length=200
                    ),
                ),
                (
                    "e2e_encrypted",
                    models.BooleanField(
                        default=False,
                        help_text="Enable end-to-end encryption for this channel. If true, all messages are encrypted prior to being send to the server. Note that this makes server-side searches for comments impossible.",
                        verbose_name="Enable end-to-end encryption",
                    ),
                ),
                (
                    "last_comment_modification_date",
                    models.DateTimeField(
                        default=django.utils.timezone.now,
                        help_text="The date when the channel has last been commented.",
                    ),
                ),
            ],
            options={
                "permissions": (
                    ("post_comment", "Can post comments"),
                    ("start_thread", "Can start a new thread"),
                ),
            },
            bases=("chats.comment",),
        ),
        migrations.CreateModel(
            name="CommentMentionLink",
            fields=[
                (
                    "mentionlink_ptr",
                    models.OneToOneField(
                        auto_created=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        parent_link=True,
                        primary_key=True,
                        serialize=False,
                        to="chats.mentionlink",
                    ),
                ),
            ],
            bases=("chats.mentionlink",),
        ),
        migrations.CreateModel(
            name="FollowingChannelGroup",
            fields=[
                (
                    "channelgroup_ptr",
                    models.OneToOneField(
                        auto_created=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        parent_link=True,
                        primary_key=True,
                        serialize=False,
                        to="chats.channelgroup",
                    ),
                ),
            ],
            bases=("chats.channelgroup",),
        ),
        migrations.CreateModel(
            name="FavoritesChannelGroup",
            fields=[
                (
                    "channelgroup_ptr",
                    models.OneToOneField(
                        auto_created=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        parent_link=True,
                        primary_key=True,
                        serialize=False,
                        to="chats.channelgroup",
                    ),
                ),
            ],
            bases=("chats.channelgroup",),
        ),
        migrations.CreateModel(
            name="SubscribedChannelGroup",
            fields=[
                (
                    "channelgroup_ptr",
                    models.OneToOneField(
                        auto_created=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        parent_link=True,
                        primary_key=True,
                        serialize=False,
                        to="chats.channelgroup",
                    ),
                ),
            ],
            bases=("chats.channelgroup",),
        ),
        migrations.CreateModel(
            name="Thread",
            fields=[
                (
                    "comment_ptr",
                    models.OneToOneField(
                        auto_created=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        parent_link=True,
                        primary_key=True,
                        serialize=False,
                        to="chats.comment",
                    ),
                ),
                (
                    "topic",
                    models.CharField(
                        blank=True,
                        help_text="Topic of the thread.",
                        max_length=300,
                        null=True,
                    ),
                ),
                (
                    "parent_channel",
                    models.ForeignKey(
                        help_text="The channel that this comment belongs to.",
                        on_delete=django.db.models.deletion.CASCADE,
                        to="chats.channel",
                    ),
                ),
            ],
            bases=("chats.comment",),
        ),
        migrations.CreateModel(
            name="CommentReadReport",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "unread",
                    models.BooleanField(
                        default=True,
                        help_text="Has the user read the comment?",
                    ),
                ),
                (
                    "comment",
                    models.ForeignKey(
                        help_text="The comment for this report.",
                        on_delete=django.db.models.deletion.CASCADE,
                        to="chats.comment",
                    ),
                ),
                (
                    "user",
                    models.ForeignKey(
                        help_text="The user who read the comment",
                        on_delete=django.db.models.deletion.CASCADE,
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
        ),
        migrations.AddField(
            model_name="comment",
            name="mentions",
            field=models.ManyToManyField(
                blank=True,
                help_text="Mentioned objects in this comment",
                to="chats.MentionLink",
            ),
        ),
        migrations.AddField(
            model_name="comment",
            name="user",
            field=models.ForeignKey(
                help_text="The user who posted the comment",
                on_delete=django.db.models.deletion.CASCADE,
                to=settings.AUTH_USER_MODEL,
            ),
        ),
        migrations.CreateModel(
            name="ChatSettings",
            fields=[
                (
                    "user",
                    models.OneToOneField(
                        help_text="The user these settings represent.",
                        on_delete=django.db.models.deletion.CASCADE,
                        primary_key=True,
                        serialize=False,
                        to="auth.user",
                    ),
                ),
                (
                    "notification_preferences",
                    models.CharField(
                        choices=[
                            ("ALWAYS", "On every comment"),
                            (
                                "MENTION",
                                "On every indirect or direct mention in a comment",
                            ),
                            ("DIRECTMENTION", "On direct mentions only"),
                            (
                                "NEVER",
                                "Never create any notification from a comment",
                            ),
                        ],
                        default="ALWAYS",
                        help_text="These are your global defaults that determine when we your want to receive a notification from a comment in a channel that you subscribed.",
                        max_length=20,
                        verbose_name="Default Global Notification Preferences",
                    ),
                ),
                (
                    "notify_on_all",
                    models.BooleanField(
                        default=True,
                        help_text="When enabled and someone mentions `@all` in a channel that you subscribed, you will be notified.",
                        verbose_name="Create notification when @all is mentioned",
                    ),
                ),
                (
                    "notify_on_subscription",
                    models.BooleanField(
                        default=True,
                        help_text="Check this if you want to receive a notification when someone adds you to a channel.",
                    ),
                ),
                (
                    "notify_on_subscription_removal",
                    models.BooleanField(
                        default=True,
                        help_text="Check this if you want to receive a notification when someone removes you from a channel.",
                    ),
                ),
                (
                    "follow_automatically",
                    models.BooleanField(
                        default=True,
                        help_text="Automatically follow new channels that you subscribe. If you disable this settings, you will not receive chat notifications for channels that you created or that you have been added to.",
                    ),
                ),
                (
                    "group_add_subscription_permission",
                    models.ManyToManyField(
                        blank=True,
                        related_name="add_subscription_permission",
                        to="auth.Group",
                        verbose_name="Groups whose users that can add me to a channel",
                    ),
                ),
                (
                    "user_add_subscription_permission",
                    models.ManyToManyField(
                        blank=True,
                        related_name="add_subscription_permission",
                        to=settings.AUTH_USER_MODEL,
                        verbose_name="Users that can add me to a channel",
                    ),
                ),
            ],
        ),
        migrations.RunPython(create_chat_settings, migrations.RunPython.noop),
        migrations.CreateModel(
            name="ChannelSubscription",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "following",
                    models.BooleanField(
                        default=True,
                        help_text="Do you want to receive any notifications from the channel?",
                    ),
                ),
                (
                    "favorite",
                    models.BooleanField(
                        default=False,
                        help_text="Mark this channel as favorite and let it appear in the favorites group.",
                    ),
                ),
                (
                    "notification_preferences",
                    models.CharField(
                        blank=True,
                        choices=[
                            ("ALWAYS", "On every comment"),
                            (
                                "MENTION",
                                "On every indirect or direct mention in a comment",
                            ),
                            ("DIRECTMENTION", "On direct mentions only"),
                            (
                                "NEVER",
                                "Never create any notification from a comment",
                            ),
                        ],
                        help_text="When do you want to receive notifications in this channel?",
                        max_length=20,
                        null=True,
                        verbose_name="Notification Preferences",
                    ),
                ),
                (
                    "notify_on_all",
                    models.BooleanField(
                        blank=True,
                        help_text="When enabled and someone mentions `@all` in a channel that you subscribed, you will be notified.",
                        null=True,
                        verbose_name="Create notification when @all is mentioned",
                    ),
                ),
                (
                    "date_subscribed",
                    models.DateTimeField(
                        default=django.utils.timezone.now,
                        help_text="The time when the user subscribed.",
                    ),
                ),
                (
                    "mentionlink",
                    models.ForeignKey(
                        help_text="The reason why the user subscribed.",
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        to="chats.mentionlink",
                    ),
                ),
                (
                    "user",
                    models.ForeignKey(
                        help_text="The user who subscribed.",
                        on_delete=django.db.models.deletion.CASCADE,
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
        ),
        migrations.CreateModel(
            name="GroupMentionLink",
            fields=[
                (
                    "mentionlink_ptr",
                    models.OneToOneField(
                        auto_created=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        parent_link=True,
                        primary_key=True,
                        serialize=False,
                        to="chats.mentionlink",
                    ),
                ),
                (
                    "related_model",
                    models.OneToOneField(
                        on_delete=django.db.models.deletion.CASCADE,
                        to="auth.group",
                    ),
                ),
            ],
            bases=("chats.mentionlink",),
        ),
        migrations.CreateModel(
            name="UserMentionLink",
            fields=[
                (
                    "mentionlink_ptr",
                    models.OneToOneField(
                        auto_created=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        parent_link=True,
                        primary_key=True,
                        serialize=False,
                        to="chats.mentionlink",
                    ),
                ),
                (
                    "related_model",
                    models.OneToOneField(
                        on_delete=django.db.models.deletion.CASCADE,
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "abstract": False,
            },
            bases=("chats.mentionlink",),
        ),
        migrations.CreateModel(
            name="ThreadComment",
            fields=[
                (
                    "comment_ptr",
                    models.OneToOneField(
                        auto_created=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        parent_link=True,
                        primary_key=True,
                        serialize=False,
                        to="chats.comment",
                    ),
                ),
                (
                    "parent_thread",
                    models.ForeignKey(
                        help_text="The thread that this comment belongs to.",
                        on_delete=django.db.models.deletion.CASCADE,
                        to="chats.thread",
                    ),
                ),
            ],
            bases=("chats.comment",),
        ),
        migrations.CreateModel(
            name="ManualUserMentionLink",
            fields=[
                (
                    "mentionlink_ptr",
                    models.OneToOneField(
                        auto_created=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        parent_link=True,
                        primary_key=True,
                        serialize=False,
                        to="chats.mentionlink",
                    ),
                ),
                (
                    "related_model",
                    models.OneToOneField(
                        on_delete=django.db.models.deletion.CASCADE,
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "abstract": False,
            },
            bases=("chats.mentionlink",),
        ),
        migrations.CreateModel(
            name="ManualChannelGroup",
            fields=[
                (
                    "channelgroup_ptr",
                    models.OneToOneField(
                        auto_created=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        parent_link=True,
                        primary_key=True,
                        serialize=False,
                        to="chats.channelgroup",
                    ),
                ),
                (
                    "channels",
                    models.ManyToManyField(
                        blank=True,
                        help_text="The channels in this group.",
                        to="chats.Channel",
                    ),
                ),
            ],
            bases=("chats.channelgroup",),
        ),
        migrations.AddConstraint(
            model_name="commentreadreport",
            constraint=models.UniqueConstraint(
                fields=("comment", "user"), name="unique_comment_for_user"
            ),
        ),
        migrations.AddField(
            model_name="commentmentionlink",
            name="related_model",
            field=models.OneToOneField(
                on_delete=django.db.models.deletion.CASCADE, to="chats.comment"
            ),
        ),
        migrations.AddField(
            model_name="channelsubscription",
            name="channel",
            field=models.ForeignKey(
                help_text="The channel that the user subscribed to.",
                on_delete=django.db.models.deletion.CASCADE,
                to="chats.channel",
            ),
        ),
        migrations.AddField(
            model_name="channelmaterialrelation",
            name="channel",
            field=models.ForeignKey(
                on_delete=django.db.models.deletion.CASCADE, to="chats.channel"
            ),
        ),
        migrations.AddConstraint(
            model_name="channelgroup",
            constraint=models.UniqueConstraint(
                fields=("slug", "user"), name="unique_slug_for_user"
            ),
        ),
        migrations.CreateModel(
            name="ChannelKeyword",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "name",
                    models.CharField(
                        help_text="Name of the keyword", max_length=100
                    ),
                ),
            ],
        ),
        migrations.AddField(
            model_name="channel",
            name="keywords",
            field=models.ManyToManyField(
                blank=True,
                help_text="Keywords for this channel.",
                to="chats.ChannelKeyword",
            ),
        ),
        migrations.AddField(
            model_name="channel",
            name="current_encryption_key",
            field=models.ForeignKey(
                blank=True,
                help_text="The encryption key that shall be used to encrypt comments in this channel.",
                null=True,
                on_delete=django.db.models.deletion.CASCADE,
                related_name="active_channel_keys",
                to="e2ee.encryptionkey",
            ),
        ),
        migrations.AddField(
            model_name="channel",
            name="editors",
            field=models.ManyToManyField(
                blank=True,
                help_text="The users who created this channel or can edit any content.",
                limit_choices_to={"user__isnull": False},
                to="members.CommunityMember",
            ),
        ),
        migrations.AddField(
            model_name="channel",
            name="group_post_comment_permission",
            field=models.ManyToManyField(
                blank=True,
                help_text="User Groups with explicit permissions to post comments in a thread in the channel.",
                related_name="channel_post_comment",
                to="auth.Group",
            ),
        ),
        migrations.AddField(
            model_name="channel",
            name="group_start_thread_permission",
            field=models.ManyToManyField(
                blank=True,
                help_text="User Groups with explicit permissions to start a new thread in the channel.",
                related_name="channel_start_thread",
                to="auth.Group",
            ),
        ),
        migrations.AddField(
            model_name="channel",
            name="group_view_permission",
            field=models.ManyToManyField(
                blank=True,
                help_text="User Groups with explicit view permissions on the channel.",
                related_name="channel_view",
                to="auth.Group",
            ),
        ),
        migrations.AddField(
            model_name="channel",
            name="user_post_comment_permission",
            field=models.ManyToManyField(
                blank=True,
                help_text="Users with explicit permissions to post comments in a thread in the channel.",
                related_name="channel_post_comment",
                to=settings.AUTH_USER_MODEL,
            ),
        ),
        migrations.AddField(
            model_name="channel",
            name="user_start_thread_permission",
            field=models.ManyToManyField(
                blank=True,
                help_text="Users with explicit permissions to start a new thread in the channel.",
                related_name="channel_start_thread",
                to=settings.AUTH_USER_MODEL,
            ),
        ),
        migrations.AddField(
            model_name="channel",
            name="user_view_permission",
            field=models.ManyToManyField(
                blank=True,
                help_text="Users with explicit view permissions on the channel.",
                related_name="channel_view",
                to=settings.AUTH_USER_MODEL,
            ),
        ),
        migrations.AddField(
            model_name="channel",
            name="group_edit_permission",
            field=models.ManyToManyField(
                blank=True,
                help_text="User Groups with explicit edit permissions on the channel (but not necessarily on the individual comments).",
                related_name="channel_edit",
                to="auth.Group",
            ),
        ),
        migrations.AddField(
            model_name="channel",
            name="user_edit_permission",
            field=models.ManyToManyField(
                blank=True,
                help_text="Users with explicit edit permissions on the channel (but not necessarily on the individual comments).",
                related_name="channel_edit",
                to=settings.AUTH_USER_MODEL,
            ),
        ),
        migrations.AddField(
            model_name="channel",
            name="encryption_keys",
            field=models.ManyToManyField(
                blank=True,
                help_text="Encryption keys that have been used for comments in this channel.",
                related_name="channel_keys",
                to="e2ee.EncryptionKey",
            ),
        ),
        migrations.AddConstraint(
            model_name="channelsubscription",
            constraint=models.UniqueConstraint(
                fields=("channel", "user"), name="unique_subscription_for_user"
            ),
        ),
        migrations.AddField(
            model_name="channel",
            name="subscribers",
            field=models.ManyToManyField(
                help_text="All subscribers to a channel.",
                through="chats.ChannelSubscription",
                to=settings.AUTH_USER_MODEL,
            ),
        ),
        migrations.CreateModel(
            name="Issue",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "done_ratio",
                    models.SmallIntegerField(
                        default=0,
                        help_text="How much percent of this task has been fulfilled?",
                        validators=[
                            django.core.validators.MinValueValidator(0),
                            django.core.validators.MaxValueValidator(100),
                        ],
                    ),
                ),
                (
                    "closed_on",
                    models.DateTimeField(
                        blank=True,
                        help_text="Date and time when the issue has been closed",
                        null=True,
                    ),
                ),
                (
                    "start_date",
                    models.DateField(
                        blank=True,
                        help_text="Start date of the issue.",
                        null=True,
                    ),
                ),
                (
                    "due_date",
                    models.DateField(
                        blank=True,
                        help_text="Due date of the issue.",
                        null=True,
                    ),
                ),
                (
                    "estimated_hours",
                    models.SmallIntegerField(
                        blank=True,
                        help_text="Estimated hours to fulfill this task.",
                        null=True,
                    ),
                ),
                (
                    "assignees",
                    models.ManyToManyField(
                        blank=True,
                        help_text="Community members that this issue is assigned to.",
                        to="members.CommunityMember",
                    ),
                ),
                (
                    "channel",
                    models.OneToOneField(
                        on_delete=django.db.models.deletion.CASCADE,
                        to="chats.channel",
                    ),
                ),
                (
                    "parent",
                    models.ForeignKey(
                        blank=True,
                        help_text="The parent issue.",
                        null=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        to="chats.issue",
                    ),
                ),
            ],
            options={
                "abstract": False,
            },
        ),
        migrations.CreateModel(
            name="IssuePriority",
            options={"verbose_name_plural": "Issue Priorities"},
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "name",
                    models.CharField(
                        help_text="Name of the tracker", max_length=50
                    ),
                ),
                (
                    "value",
                    models.PositiveIntegerField(
                        default=1,
                        help_text="Numeric value representing the priority. The higher the value, the higher the priority.",
                    ),
                ),
            ],
        ),
        migrations.CreateModel(
            name="IssueStatus",
            options={"verbose_name_plural": "Issue Statuses"},
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "name",
                    models.CharField(
                        help_text="Name of the status", max_length=50
                    ),
                ),
                (
                    "is_closed",
                    models.BooleanField(
                        default=False,
                        help_text="Does this status mean that the issue is closed?",
                    ),
                ),
            ],
        ),
        migrations.CreateModel(
            name="IssueTracker",
            options={"verbose_name_plural": "Issue Tracker"},
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "name",
                    models.CharField(
                        help_text="Name of the tracker", max_length=50
                    ),
                ),
            ],
        ),
        migrations.CreateModel(
            name="IssueNode",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("path", models.CharField(max_length=255, unique=True)),
                ("depth", models.PositiveIntegerField()),
                ("numchild", models.PositiveIntegerField(default=0)),
                (
                    "issue",
                    models.OneToOneField(
                        on_delete=django.db.models.deletion.CASCADE,
                        to="chats.issue",
                    ),
                ),
            ],
            options={
                "abstract": False,
            },
        ),
        migrations.AddField(
            model_name="issue",
            name="priority",
            field=models.ForeignKey(
                default=academic_community.channels.models.issue.get_normal_priority,
                help_text="Priority of the issue",
                on_delete=django.db.models.deletion.PROTECT,
                to="chats.issuepriority",
            ),
        ),
        migrations.AddField(
            model_name="issue",
            name="status",
            field=models.ForeignKey(
                default=academic_community.channels.models.issue.get_first_status,
                help_text="Status of the issue",
                on_delete=django.db.models.deletion.PROTECT,
                to="chats.issuestatus",
            ),
        ),
        migrations.AddField(
            model_name="issue",
            name="tracker",
            field=models.ForeignKey(
                default=academic_community.channels.models.issue.get_first_tracker,
                help_text="What tracker does this issue correspond to?",
                on_delete=django.db.models.deletion.PROTECT,
                to="chats.issuetracker",
            ),
        ),
        migrations.CreateModel(
            name="ForumAnswer",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "threadcomment",
                    models.OneToOneField(
                        limit_choices_to={
                            "parent_thread__parent_channel__forum__isnull": False
                        },
                        on_delete=django.db.models.deletion.CASCADE,
                        to="chats.threadcomment",
                    ),
                ),
            ],
        ),
        migrations.CreateModel(
            name="Forum",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "channel",
                    models.OneToOneField(
                        on_delete=django.db.models.deletion.CASCADE,
                        to="chats.channel",
                    ),
                ),
            ],
            options={
                "abstract": False,
            },
        ),
        migrations.CreateModel(
            name="GroupConversation",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "channel",
                    models.OneToOneField(
                        on_delete=django.db.models.deletion.CASCADE,
                        to="chats.channel",
                    ),
                ),
            ],
            options={
                "abstract": False,
            },
        ),
        migrations.CreateModel(
            name="PrivateConversation",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "channel",
                    models.OneToOneField(
                        on_delete=django.db.models.deletion.CASCADE,
                        to="chats.channel",
                    ),
                ),
            ],
            options={
                "abstract": False,
            },
        ),
        migrations.CreateModel(
            name="ChannelTypeGroup",
            fields=[
                (
                    "channelgroup_ptr",
                    models.OneToOneField(
                        auto_created=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        parent_link=True,
                        primary_key=True,
                        serialize=False,
                        to="chats.channelgroup",
                    ),
                ),
                (
                    "channel_type",
                    models.ForeignKey(
                        help_text="The channel type this group corresponds to.",
                        limit_choices_to=academic_community.channels.models.groups.limit_channel_type_choices,
                        on_delete=django.db.models.deletion.CASCADE,
                        to="contenttypes.contenttype",
                    ),
                ),
            ],
            options={
                "abstract": False,
            },
            bases=("chats.channelgroup",),
        ),
        migrations.RunPython(
            create_default_issue_statuses, migrations.RunPython.noop
        ),
        migrations.RunPython(
            create_default_issue_trackers, migrations.RunPython.noop
        ),
        migrations.RunPython(
            create_default_issue_priorities, migrations.RunPython.noop
        ),
        migrations.RunPython(create_links, migrations.RunPython.noop),
        migrations.RunSQL(
            "CREATE SEQUENCE channels_channel_channel_id MINVALUE 1",
            reverse_sql="DROP SEQUENCE IF EXISTS channels_channel_channel_id",
            elidable=False,
        ),
    ]
