-angular.module('loomioApp').directive 'discussionsCard', ($location, $timeout, Records, RecordLoader, ModalService, DiscussionModal, ThreadQueryService,  KeyEventService, LoadingService, AbilityService) ->
+angular.module('loomioApp').directive 'discussionsCard', ($q, $location, $timeout, Records, RecordLoader, ModalService, DiscussionModal, ThreadQueryService,  KeyEventService, LoadingService, AbilityService) ->
   scope: {group: '='}
   restrict: 'E'
   templateUrl: 'generated/components/group_page/discussions_card/discussions_card.html'

      $timeout -> document.querySelector('.discussions-card__search input').focus()

    $scope.closeSearch = ->
      $scope.searchOpen = false

    $scope.canStartThread = ->

    $scope.$on 'subgroupsLoaded', -> $scope.init($scope.filter)

    $scope.searchThreads = ->
      return $q.when() unless $scope.fragment
      Records.discussions.search($scope.group.key, $scope.fragment, per: 10).then (data) ->
        $scope.searched = ThreadQueryService.queryFor
          name: "group_#{$scope.group.key}_searched"
          group: $scope.group
          ids: _.pluck(data.discussions, 'id')
          overwrite: true
    LoadingService.applyLoadingFunction $scope, 'searchThreads'

    $scope.openDiscussionModal = ->
      ModalService.open DiscussionModal, discussion: -> Records.discussions.build(groupId: $scope.group.id)
