Example #1
0
 def handle_cosinnus_group_reactivated(sender, group, **kwargs):
     """ Unarchive a group that gets reactivated """
     try:
         rocket = RocketChatConnection()
         rocket.groups_unarchive(group)
     except Exception as e:
         logger.exception(e)
    def handle(self, *args, **options):
        skip_inactive = options['skip_inactive']
        force_group_membership_sync = options['force_group_membership_sync']

        rocket = RocketChatConnection(stdout=self.stdout, stderr=self.stderr)
        rocket.create_missing_users(
            skip_inactive=skip_inactive,
            force_group_membership_sync=force_group_membership_sync)
Example #3
0
 def ensure_user_account_sanity(self, request, queryset):
     count = 0
     from cosinnus_message.rocket_chat import RocketChatConnection
     rocket = RocketChatConnection()
     for user in queryset:
         rocket.ensure_user_account_sanity(user)
         count += 1
     message = _('%d Users rocketchat accounts were checked.') % count
     self.message_user(request, message)
Example #4
0
 def make_user_rocket_admin(self, request, queryset):
     count = 0
     from cosinnus_message.rocket_chat import RocketChatConnection
     rocket = RocketChatConnection()
     for user in queryset:
         rocket.rocket.users_update(user_id=rocket.get_user_id(user),
                                    roles=['user', 'admin'])
         count += 1
     message = _('%d Users were made RocketChat admins.') % count
     self.message_user(request, message)
Example #5
0
 def force_redo_user_room_memberships(self, request, queryset):
     count = 0
     from cosinnus_message.rocket_chat import RocketChatConnection
     rocket = RocketChatConnection()
     for user in queryset:
         rocket.force_redo_user_room_memberships(user)
         count += 1
     message = _(
         '%d Users\' rocketchat room memberships were re-done.') % count
     self.message_user(request, message)
Example #6
0
 def handle_user_password_updated(sender, user, **kwargs):
     try:
         rocket = RocketChatConnection()
         rocket.users_update(user,
                             force_user_update=True,
                             update_password=True)
         delete_cached_rocket_connection(
             user.cosinnus_profile.rocket_username)
     except Exception as e:
         logger.exception(e)
Example #7
0
 def force_sync_rocket_user(self, request, queryset):
     count = 0
     from cosinnus_message.rocket_chat import RocketChatConnection, delete_cached_rocket_connection
     rocket = RocketChatConnection()
     for user in queryset:
         rocket.users_update(user,
                             force_user_update=True,
                             update_password=True)
         delete_cached_rocket_connection(
             user.cosinnus_profile.rocket_username)
         count += 1
     message = _('%d Users were synchronized successfully.') % count
     self.message_user(request, message)
Example #8
0
 def handle_user_updated(sender, instance, **kwargs):
     # TODO: does this hook trigger correctly?
     # this handles the user update, it is not in post_save!
     if instance.id and hasattr(instance, 'cosinnus_profile'):
         try:
             rocket = RocketChatConnection()
             old_instance = get_user_model().objects.get(pk=instance.id)
             force = any([getattr(instance, fname) != getattr(old_instance, fname) \
                             for fname in ('password', 'first_name', 'last_name', 'email')])
             password_updated = bool(
                 instance.password != old_instance.password)
             rocket.users_update(instance,
                                 force_user_update=force,
                                 update_password=password_updated)
         except Exception as e:
             logger.exception(e)
Example #9
0
    def get_rocket_chat_url(self):
        """
        Returns Rocket.Chat group URL with user is member of group,
        creates a new private group with group admins if not
        :return:
        """
        group_name = ''
        group = self.get_object()
        if not group:
            return self.base_url
        user = self.request.user
        group_name = ''
        if user and user.is_authenticated:
            rocket = RocketChatConnection()
            group_name = rocket.groups_request(group, user)

        if group_name:
            return f'{self.base_url}/group/{group_name}/'
        return self.base_url
Example #10
0
def get_unread_message_count_for_user(user):
    """ Returns the unread message count for a user, independent of which internal
        messaging system is being used (Postman, Rocketchat, etc) """
    if not user.is_authenticated:
        return 0
    if getattr(settings, 'COSINNUS_ROCKET_ENABLED', False):
        from cosinnus_message.rocket_chat import RocketChatConnection  # noqa
        unread_count = RocketChatConnection().unread_messages(user)

    else:
        from postman.models import Message
        unread_count = Message.objects.inbox_unread_count(user)
    return unread_count
Example #11
0
 def handle_profile_updated(sender, instance, created, **kwargs):
     try:
         rocket = RocketChatConnection()
         if created:
             rocket.users_create(instance.user)
         else:
             rocket.users_update(instance.user)
     except Exception as e:
         logger.exception(e)
Example #12
0
 def sync_rocketchat_room(self, force=False):
     """ Can be safely called with force=False without re-creating rooms """
     if settings.COSINNUS_ROCKET_ENABLED and self.type in self.ROCKETCHAT_ROOM_TYPES \
             and not settings.COSINNUS_CONFERENCES_USE_COMPACT_MODE:
         if not self.rocket_chat_room_id or force:
             from cosinnus_message.rocket_chat import RocketChatConnection
             rocket = RocketChatConnection()
             room_name = f'{self.slug}-{self.group.slug}-{get_random_string(7)}'
             internal_room_id = rocket.create_private_room(
                 room_name,
                 self.creator,
                 member_users=self.group.actual_members,
                 additional_admin_users=self.group.actual_admins)
             if internal_room_id:
                 self.rocket_chat_room_id = internal_room_id
                 self.rocket_chat_room_name = room_name
                 self.save()
             else:
                 logger.error(
                     'Could not create a conferenceroom rocketchat room!',
                     extra={
                         'conference-room-id': self.id,
                         'conference-room-slug': self.slug
                     })
Example #13
0
 def handle_cosinnus_project_updated(sender, instance, **kwargs):
     try:
         rocket = RocketChatConnection()
         if instance.id:
             old_instance = CosinnusProject.objects.get(pk=instance.id)
             if instance.slug != old_instance.slug:
                 rocket.groups_rename(instance)
         else:
             rocket.groups_create(instance)
     except Exception as e:
         logger.exception(e)
Example #14
0
        def run(self):

            # everything is only real membership changes, not for non-invitations:
            if deleted or instance.status in MEMBER_STATUS:

                # assign users to the group's BBBRoom's members if one exists
                try:
                    room = instance.group.media_tag.bbb_room
                except Exception as e:
                    logger.debug(
                        'Could not access a group\'s media_tag or the tag\'s BBBRoom in MembershipUpdateHookThread. Most likely a race condition.',
                        extra={'exception': e})
                    room = None

                if room:
                    if deleted:
                        room.remove_user(instance.user)
                    else:
                        if instance.status in MEMBER_STATUS:
                            room.join_user(
                                instance.user,
                                as_moderator=bool(
                                    instance.status == MEMBERSHIP_ADMIN))

                # For group conferences:
                group = instance.group
                user = instance.user
                if group.group_is_conference:

                    # if the group is a conference and there are any ResultProjects in any conference room,
                    # mirror the membership change to those result projects
                    result_groups = group.conference_group_result_projects
                    for result_group in result_groups:
                        # apply the same membership status to the result_group as it was in the conference group
                        membership = get_object_or_None(
                            CosinnusGroupMembership,
                            group=result_group,
                            user=user)
                        if deleted:
                            if membership:
                                membership.delete()
                            continue
                        if membership and membership.status != instance.status:
                            membership.status = instance.status
                            membership.save()
                        if not membership:
                            CosinnusGroupMembership.objects.create(
                                group=result_group,
                                user=user,
                                status=instance.status)

                    # if there are any rocketchat rooms, update the rocketchat group membership for those rooms
                    if settings.COSINNUS_ROCKET_ENABLED:
                        rocket_rooms = list(
                            CosinnusConferenceRoom.objects.filter(
                                group=group,
                                type__in=CosinnusConferenceRoom.
                                ROCKETCHAT_ROOM_TYPES))
                        if len(rocket_rooms) > 0:

                            from cosinnus_message.rocket_chat import RocketChatConnection
                            rocket = RocketChatConnection()
                            # add/remove member from each rocketchat room for each conference room
                            for room in rocket_rooms:
                                try:
                                    room.sync_rocketchat_room()
                                    if not room.rocket_chat_room_id:
                                        logger.error(
                                            'Wanted to sync a user membership to a conference room, but a rocketchat room for it could not be created!',
                                            extra={'room': room.id})
                                        continue
                                    if deleted:
                                        # kicked member
                                        rocket.remove_member_from_room(
                                            user, room.rocket_chat_room_id)
                                    else:
                                        rocket.add_member_to_room(
                                            user, room.rocket_chat_room_id)
                                        # Update membership
                                        if instance.status == MEMBERSHIP_ADMIN:
                                            # Upgrade
                                            rocket.add_moderator_to_room(
                                                user, room.rocket_chat_room_id)
                                        else:
                                            # Downgrade
                                            rocket.remove_moderator_from_room(
                                                user, room.rocket_chat_room_id)
                                except Exception as e:
                                    logger.exception(e)
Example #15
0
 def run(self):
     try:
         rocket = RocketChatConnection()
         rocket.ensure_user_account_sanity(user)
     except Exception as e:
         logger.exception(e)
 def handle(self, *args, **options):
     rocket = RocketChatConnection(stdout=self.stdout, stderr=self.stderr)
     rocket.settings_update()
Example #17
0
 def handle_note_deleted(sender, profile, **kwargs):
     """ Called when a user deletes their account. Completely deletes the user's rocket profile """
     rocket = RocketChatConnection()
     rocket.users_delete(profile.user)
Example #18
0
 def handle_note_deleted(sender, instance, **kwargs):
     rocket = RocketChatConnection()
     rocket.notes_delete(instance)
Example #19
0
 def handle_app_authorized(sender, request, token, **kwargs):
     rocket = RocketChatConnection()
     rocket.users_create_or_update(token.user, request=request)
Example #20
0
 def handle_membership_deleted(sender, instance, **kwargs):
     try:
         rocket = RocketChatConnection()
         rocket.groups_kick(instance)
     except Exception as e:
         logger.exception(e)
Example #21
0
    def handle_membership_updated(sender, instance, **kwargs):
        try:
            rocket = RocketChatConnection()
            is_pending = instance.status in (MEMBERSHIP_PENDING,
                                             MEMBERSHIP_INVITED_PENDING)
            if instance.id:
                old_instance = CosinnusGroupMembership.objects.get(
                    pk=instance.id)
                was_pending = old_instance.status in (
                    MEMBERSHIP_PENDING, MEMBERSHIP_INVITED_PENDING)
                user_changed = instance.user_id != old_instance.user_id
                group_changed = instance.group_id != old_instance.group_id
                is_moderator_changed = instance.status != old_instance.status and \
                        (instance.status == MEMBERSHIP_ADMIN or old_instance.status == MEMBERSHIP_ADMIN)

                # Invalidate old membership
                if (is_pending
                        and not was_pending) or user_changed or group_changed:
                    rocket.groups_kick(old_instance)

                # Create new membership
                if (was_pending
                        and not is_pending) or user_changed or group_changed:
                    rocket.groups_invite(instance)

                # Update membership
                if not is_pending and is_moderator_changed:
                    # Upgrade
                    if not old_instance.status == MEMBERSHIP_ADMIN and instance.status == MEMBERSHIP_ADMIN:
                        rocket.groups_add_moderator(instance)
                    # Downgrade
                    elif old_instance.status == MEMBERSHIP_ADMIN and not instance.status == MEMBERSHIP_ADMIN:
                        rocket.groups_remove_moderator(instance)
            elif not is_pending:
                # Create new membership
                rocket.groups_invite(instance)
                if instance.status == MEMBERSHIP_ADMIN:
                    rocket.groups_add_moderator(instance)
        except Exception as e:
            logger.exception(e)
Example #22
0
 def handle_cosinnus_conference_deleted(sender, instance, **kwargs):
     try:
         rocket = RocketChatConnection()
         rocket.groups_delete(instance)
     except Exception as e:
         logger.exception(e)
Example #23
0
    def handle(self, *args, **options):
        skip_update = options['skip_update']

        rocket = RocketChatConnection(stdout=self.stdout, stderr=self.stderr)
        rocket.users_sync(skip_update=skip_update)
 def handle(self, *args, **options):
     rocket = RocketChatConnection(stdout=self.stdout, stderr=self.stderr)
     rocket.groups_sync()