<commit_before>class CommentSubscription < ActiveRecord::Base
  attr_accessible :course_id, :topic_id, :topic_type, :user_course_id

  belongs_to :topic, polymorphic: true
  belongs_to :course
  belongs_to :user_course

  def self.populate_subscription(comment)
    # add the commentator to the subscription list
    commentable = comment.commentable
    commentator = comment.user_course
    CommentSubscription.subscribe(commentable, commentator)
    if commentable.respond_to?(:user_course)
      # add the owner of the topic to the subscription list
      CommentSubscription.subscribe(commentable, commentable.user_course)
    end
    if commentator.is_student?
      # add the ta to the subscription list
      commentator.get_my_tutors.each do |uc|
        CommentSubscription.subscribe(commentable, uc)
      end
    end
  end

  def self.subscribe(topic, user_course)
    if !topic || !user_course
      return
    end

    old_cs = CommentSubscription.where(
      user_course_id: user_course.id,
      topic_id: topic.id,
      topic_type: topic.class).count

    if old_cs == 0
      cs = CommentSubscription.new
      cs.topic = topic
      cs.user_course = user_course
      cs.course = user_course.course
      cs.save
      puts cs.to_json
    end
  end

  def self.unsubscribe(topic, user_course)
    cs = CommentSubscription.where(
        user_course_id: user_course.id,
        topic_id: topic.id,<commit_msg>avoid avoid subscribe to deleted topics 2<commit_after>class CommentSubscription < ActiveRecord::Base
  attr_accessible :course_id, :topic_id, :topic_type, :user_course_id

  belongs_to :topic, polymorphic: true
  belongs_to :course
  belongs_to :user_course

  def self.populate_subscription(comment)
    # add the commentator to the subscription list
    commentable = comment.commentable
    commentator = comment.user_course

    if !commentable || !commentator
      return
    end

    CommentSubscription.subscribe(commentable, commentator)
    if commentable.respond_to?(:user_course)
      # add the owner of the topic to the subscription list
      CommentSubscription.subscribe(commentable, commentable.user_course)
    end
    if commentator.is_student?
      # add the ta to the subscription list
      commentator.get_my_tutors.each do |uc|
        CommentSubscription.subscribe(commentable, uc)
      end
    end
  end

  def self.subscribe(topic, user_course)

    old_cs = CommentSubscription.where(
      user_course_id: user_course.id,
      topic_id: topic.id,
      topic_type: topic.class).count

    if old_cs == 0
      cs = CommentSubscription.new
      cs.topic = topic
      cs.user_course = user_course
      cs.course = user_course.course
      cs.save
      puts cs.to_json
    end
  end

  def self.unsubscribe(topic, user_course)
    cs = CommentSubscription.where(
        user_course_id: user_course.id,
        topic_id: topic.id,
{'fraction_non_alphanumeric': 0.06841741534208708, 'fraction_numerical': 0.0010366275051831375, 'mean_word_length': 3.2139737991266375, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4764467', 'n_tokens_mistral': 870, 'n_tokens_neox': 856, 'n_words': 213}