<commit_before>var url = require('url')
import { isEmpty } from 'lodash'

const hasReason = (regex, reasons) => reasons.some(reason => reason.match(regex))

module.exports = bookshelf.Model.extend({

  tableName: 'notifications',

  activity: function () {
    return this.belongsTo(Activity)
  },

  post: function () {
    return this.relations.activity.relations.post
  },

  comment: function () {
    return this.relations.activity.relations.comment
  },

  reader: function () {
    return this.relations.activity.relations.reader
  },

  send: function () {
    var action
    switch (this.get('medium')) {
      case Notification.MEDIUM.Push:
        action = this.sendPush()
        break
      case Notification.MEDIUM.Email:
        action = this.sendEmail()
        break
      case Notification.MEDIUM.InApp:
        action = this.incUserNewNotificationCount()
        break
    }
    if (action) {
      return action
      .then(() => this.save({'sent_at': (new Date()).toISOString()}))
    } else {
      return Promise.resolve()
    }
  },

  sendPush: function () {
    switch (Notification.priorityReason(this.relations.activity.get('meta').reasons)) {
      case 'mention':
        return this.sendPostPush('mention')
      case 'commentMention':
        return this.sendCommentPush('mention')
      case 'newComment':
        return this.sendCommentPush()
      case 'tag':
        return Promise.resolve()
      case 'newPost':
        return this.sendPostPush()
      default:
        return Promise.resolve()
    }
  },

  sendPostPush: function (version) {
    var post = this.post()
    var communityIds = Activity.communityIds(this.relations.activity)
    if (isEmpty(communityIds)) return Promise.resolve()
    return Community.find(communityIds[0])
    .then(community => {
      var path = url.parse(Frontend.Route.post(post, community)).path
      var alertText = PushNotification.textForPost(post, community, this.relations.activity.get('reader_id'), version)
      return this.reader().sendPushNotification(alertText, path)
    })
  },

  sendCommentPush: function (version) {
    var comment = this.comment()
    var communityIds = Activity.communityIds(this.relations.activity)
    if (isEmpty(communityIds)) return Promise.resolve()
    return Community.find(communityIds[0])
    .then(community => {
      var path = url.parse(Frontend.Route.post(comment.relations.post, community)).path
      var alertText = PushNotification.textForComment(comment, version, this.relations.activity.get('reader_id'))
      return this.reader().sendPushNotification(alertText, path)
    })
  },

  sendEmail: function () {
    switch (Notification.priorityReason(this.relations.activity.get('meta').reasons)) {
      case 'mention':
        return this.sendPostMentionEmail()
      case 'commentMention':
        return this.sendCommentNotificationEmail('mention')
      case 'newComment':
        return this.sendCommentNotificationEmail()
      default:
        return Promise.resolve()
    }
  },

  sendPostMentionEmail: function () {
    var post = this.post()
    var reader = this.reader()
    var user = post.relations.user
    var description = RichText.qualifyLinks(post.get('description'))
    var replyTo = Email.postReplyAddress(post.id, reader.id)

    var communityIds = Activity.communityIds(this.relations.activity)
    if (isEmpty(communityIds)) return Promise.resolve()
    return Community.find(communityIds[0])
    .then(community => reader.generateToken()
      .then(token => Email.sendPostMentionNotification({
        email: reader.get('email'),
        sender: {
          address: replyTo,
          reply_to: replyTo,
          name: format('%s (via Hylo)', user.get('name'))
        },
        data: {
          community_name: community.get('name'),
          post_user_name: user.get('name'),
          post_user_avatar_url: Frontend.Route.tokenLogin(reader, token,
            user.get('avatar_url') + '?ctt=post_mention_email'),
          post_user_profile_url: Frontend.Route.tokenLogin(reader, token,
            Frontend.Route.profile(user) + '?ctt=post_mention_email'),
          post_description: description,
          post_title: post.get('name'),
          post_type: post.get('type'),
          post_url: Frontend.Route.tokenLogin(reader, token,
            Frontend.Route.post(post) + '?ctt=post_mention_email'),
          unfollow_url: Frontend.Route.tokenLogin(reader, token,
            Frontend.Route.unfollow(post, community) + '?ctt=post_mention_email'),
          tracking_pixel_url: Analytics.pixelUrl('Mention in Post', {userId: reader.id})
        }
      })))
  },

  // version corresponds to names of versions in SendWithUs
  sendCommentNotificationEmail: function (version) {<commit_msg>don't depend on post type in post mention email<commit_after>var url = require('url')
import { isEmpty } from 'lodash'

const hasReason = (regex, reasons) => reasons.some(reason => reason.match(regex))

module.exports = bookshelf.Model.extend({

  tableName: 'notifications',

  activity: function () {
    return this.belongsTo(Activity)
  },

  post: function () {
    return this.relations.activity.relations.post
  },

  comment: function () {
    return this.relations.activity.relations.comment
  },

  reader: function () {
    return this.relations.activity.relations.reader
  },

  send: function () {
    var action
    switch (this.get('medium')) {
      case Notification.MEDIUM.Push:
        action = this.sendPush()
        break
      case Notification.MEDIUM.Email:
        action = this.sendEmail()
        break
      case Notification.MEDIUM.InApp:
        action = this.incUserNewNotificationCount()
        break
    }
    if (action) {
      return action
      .then(() => this.save({'sent_at': (new Date()).toISOString()}))
    } else {
      return Promise.resolve()
    }
  },

  sendPush: function () {
    switch (Notification.priorityReason(this.relations.activity.get('meta').reasons)) {
      case 'mention':
        return this.sendPostPush('mention')
      case 'commentMention':
        return this.sendCommentPush('mention')
      case 'newComment':
        return this.sendCommentPush()
      case 'tag':
        return Promise.resolve()
      case 'newPost':
        return this.sendPostPush()
      default:
        return Promise.resolve()
    }
  },

  sendPostPush: function (version) {
    var post = this.post()
    var communityIds = Activity.communityIds(this.relations.activity)
    if (isEmpty(communityIds)) return Promise.resolve()
    return Community.find(communityIds[0])
    .then(community => {
      var path = url.parse(Frontend.Route.post(post, community)).path
      var alertText = PushNotification.textForPost(post, community, this.relations.activity.get('reader_id'), version)
      return this.reader().sendPushNotification(alertText, path)
    })
  },

  sendCommentPush: function (version) {
    var comment = this.comment()
    var communityIds = Activity.communityIds(this.relations.activity)
    if (isEmpty(communityIds)) return Promise.resolve()
    return Community.find(communityIds[0])
    .then(community => {
      var path = url.parse(Frontend.Route.post(comment.relations.post, community)).path
      var alertText = PushNotification.textForComment(comment, version, this.relations.activity.get('reader_id'))
      return this.reader().sendPushNotification(alertText, path)
    })
  },

  sendEmail: function () {
    switch (Notification.priorityReason(this.relations.activity.get('meta').reasons)) {
      case 'mention':
        return this.sendPostMentionEmail()
      case 'commentMention':
        return this.sendCommentNotificationEmail('mention')
      case 'newComment':
        return this.sendCommentNotificationEmail()
      default:
        return Promise.resolve()
    }
  },

  sendPostMentionEmail: function () {
    var post = this.post()
    var reader = this.reader()
    var user = post.relations.user
    var description = RichText.qualifyLinks(post.get('description'))
    var replyTo = Email.postReplyAddress(post.id, reader.id)

    var communityIds = Activity.communityIds(this.relations.activity)
    if (isEmpty(communityIds)) return Promise.resolve()
    return Community.find(communityIds[0])
    .then(community => reader.generateToken()
      .then(token => Email.sendPostMentionNotification({
        email: reader.get('email'),
        sender: {
          address: replyTo,
          reply_to: replyTo,
          name: format('%s (via Hylo)', user.get('name'))
        },
        data: {
          community_name: community.get('name'),
          post_user_name: user.get('name'),
          post_user_avatar_url: Frontend.Route.tokenLogin(reader, token,
            user.get('avatar_url') + '?ctt=post_mention_email'),
          post_user_profile_url: Frontend.Route.tokenLogin(reader, token,
            Frontend.Route.profile(user) + '?ctt=post_mention_email'),
          post_description: description,
          post_title: post.get('name'),
          post_type: 'conversation',
          post_url: Frontend.Route.tokenLogin(reader, token,
            Frontend.Route.post(post) + '?ctt=post_mention_email'),
          unfollow_url: Frontend.Route.tokenLogin(reader, token,
            Frontend.Route.unfollow(post, community) + '?ctt=post_mention_email'),
          tracking_pixel_url: Analytics.pixelUrl('Mention in Post', {userId: reader.id})
        }
      })))
  },

  // version corresponds to names of versions in SendWithUs
  sendCommentNotificationEmail: function (version) {
{'fraction_non_alphanumeric': 0.1246601129470822, 'fraction_numerical': 0.0006274837900020916, 'mean_word_length': 3.0972579263067694, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '3484614', 'n_tokens_mistral': 2783, 'n_tokens_neox': 2695, 'n_words': 528}