<commit_before>express   = require 'express'
moment    = require 'moment'
debug     = require 'debug'
rongCloud = require 'rongcloud-sdk'

Utility   = require('../util/util').Utility
Config    = require Utility.getConfigPath('..')
APIResult = require('../util/util').APIResult

# 引用数据库对象和模型
[sequelize, User, Blacklist, Friendship, Group, GroupMember, GroupSync, DataVersion, VerificationCode, LoginLog] = require '../db'

FRIENDSHIP_REQUESTING = 10
FRIENDSHIP_REQUESTED  = 11
FRIENDSHIP_AGREED     = 20
FRIENDSHIP_IGNORED    = 21
FRIENDSHIP_DELETED    = 30

FRIEND_REQUEST_MESSAGE_MIN_LENGTH = 0
FRIEND_REQUEST_MESSAGE_MAX_LENGTH = 64

FRIEND_DISPLAY_NAME_MIN_LENGTH = 1
FRIEND_DISPLAY_NAME_MAX_LENGTH = 32

CONTACT_OPERATION_ACCEPT_RESPONSE = 'AcceptResponse'
CONTACT_OPERATION_REQUEST         = 'Request'

log = debug 'app:log'
logError = debug 'app:error'

# 初始化融云 Server API SDK
rongCloud.init Config.RONGCLOUD_APP_KEY, Config.RONGCLOUD_APP_SECRET

sendContactNotification = (userId, nickname, friendId, operation, message, timestamp) ->
  encodedUserId = Utility.encodeId userId
  encodedFriendId = Utility.encodeId friendId

  contactNotificationMessage =
    operation: operation
    sourceUserId: encodedUserId
    targetUserId: encodedFriendId
    message: message
    extra:
      sourceUserNickname: nickname
      version: timestamp

  rongCloud.message.private.publish encodedUserId, encodedFriendId, 'RC:ContactNtf', contactNotificationMessage,
    (err, resultText) ->
      # 暂不考虑回调的结果是否成功，后续可以考虑记录到系统错误日志中
      if err
        logError 'Error: send contact notification failed: %j', err

router = express.Router()

validator = sequelize.Validator

# 发送邀请好友
router.post '/invite', (req, res, next) -><commit_msg>BROKEN: Use system.publish instead of private.publish to send friendship notification message. MUST update client code to process the conversation type (PRIVATE to SYSTEM).<commit_after>express   = require 'express'
moment    = require 'moment'
debug     = require 'debug'
rongCloud = require 'rongcloud-sdk'

Utility   = require('../util/util').Utility
Config    = require Utility.getConfigPath('..')
APIResult = require('../util/util').APIResult

# 引用数据库对象和模型
[sequelize, User, Blacklist, Friendship, Group, GroupMember, GroupSync, DataVersion, VerificationCode, LoginLog] = require '../db'

FRIENDSHIP_REQUESTING = 10
FRIENDSHIP_REQUESTED  = 11
FRIENDSHIP_AGREED     = 20
FRIENDSHIP_IGNORED    = 21
FRIENDSHIP_DELETED    = 30

FRIEND_REQUEST_MESSAGE_MIN_LENGTH = 0
FRIEND_REQUEST_MESSAGE_MAX_LENGTH = 64

FRIEND_DISPLAY_NAME_MIN_LENGTH = 1
FRIEND_DISPLAY_NAME_MAX_LENGTH = 32

CONTACT_OPERATION_ACCEPT_RESPONSE = 'AcceptResponse'
CONTACT_OPERATION_REQUEST         = 'Request'

log = debug 'app:log'
logError = debug 'app:error'

# 初始化融云 Server API SDK
rongCloud.init Config.RONGCLOUD_APP_KEY, Config.RONGCLOUD_APP_SECRET

sendContactNotification = (userId, nickname, friendId, operation, message, timestamp) ->
  encodedUserId = Utility.encodeId userId
  encodedFriendId = Utility.encodeId friendId

  contactNotificationMessage =
    operation: operation
    sourceUserId: encodedUserId
    targetUserId: encodedFriendId
    message: message
    extra:
      sourceUserNickname: nickname
      version: timestamp

  rongCloud.message.system.publish encodedUserId, encodedFriendId, 'RC:ContactNtf', contactNotificationMessage,
    (err, resultText) ->
      # 暂不考虑回调的结果是否成功，后续可以考虑记录到系统错误日志中
      if err
        logError 'Error: send contact notification failed: %j', err

router = express.Router()

validator = sequelize.Validator

# 发送邀请好友
router.post '/invite', (req, res, next) ->
{'fraction_non_alphanumeric': 0.08655973451327434, 'fraction_numerical': 0.008849557522123894, 'mean_word_length': 4.80577849117175, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '3802405', 'n_tokens_mistral': 1246, 'n_tokens_neox': 1243, 'n_words': 266}