Guest User

Untitled

a guest
Jun 18th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.40 KB | None | 0 0
  1. // Push при удалении фотографии в админке у пользователя
  2. var sendPhotoRemovePush = function (user) {
  3. var message = {
  4. to: user.gcmId,
  5. notification: {
  6. title: user.locale == 'en' ? 'Photo deleted (No face)' : 'Фото удалено (Без лица)',
  7. body: user.locale == 'en' ? 'Upload your photo with a face. Your profile will be deleted with in 24 hours' :
  8. 'Загрузите своё фото с лицом. Ваш профиль будет удалён в течении 24 часа',
  9. sound: 'obyjvlenie.mp3'
  10. }
  11. };
  12. sendPush(message);
  13. };
  14.  
  15. //db.notices.find({name: "sendPhotoRemovePush"},{titleEn: 1, _id:0})
  16.  
  17. {
  18. "_id" : ObjectId("5b1055bab772d4a8c93dc0cf"),
  19. "name" : "sendPhotoRemovePush",
  20. "titleEn" : "Photo deleted (No face)",
  21. "titleRu" : "Фото удалено (Без лица)",
  22. "bodyEn" : "Upload your photo with a face. Your profile will be deleted with in 24 hours",
  23. "bodyRu" : "Загрузите своё фото с лицом. Ваш профиль будет удалён в течении 24 часа",
  24. "sound" : "obyjvlenie.mp3",
  25. "createdAt" : ISODate("2018-05-31T21:02:39.547Z"),
  26. "updatedAt" : ISODate("2018-06-01T07:09:29.791Z")
  27. }
  28.  
  29. {
  30. (function () {
  31. var path = require('path');
  32. var config = require('nconf');
  33. var FCM = require('fcm-push');
  34. var mongoose = require('mongoose');
  35. require('../models');
  36. var User = mongoose.model('User');
  37. var Notice = mongoose.model('Notice'); // подключаем коллекцию Notice
  38.  
  39. config.file({
  40. file: path.join(__dirname, '../../config/google-services.json')
  41. });
  42.  
  43. var gcmApiKey = config.get('client:0:api_key:0:current_key');
  44. var fcmSender = new FCM(gcmApiKey);
  45.  
  46. var getUser = function (userId, callback) {
  47. User.findOne({_id: userId}).exec(function (err, user) {
  48. callback(user);
  49. });
  50. };
  51.  
  52. var sendPush = function (message) {
  53. fcmSender.send(message, function (err, response) {
  54. console.log(message);
  55. if (err) {
  56. console.log("sendFcmPush ---> Error:", err);
  57. } else {
  58. console.log("sendFcmPush ---> Success: ", response);
  59. }
  60. });
  61. };
  62.  
  63. // Push при удалении фотографии в админке у пользователя
  64. var sendPhotoRemovePush = function (user) {
  65. var message = {
  66. to: user.gcmId,
  67. notification: {
  68. title: user.locale == 'en' ? 'Photo deleted (No face)' : 'Фото удалено (Без лица)',
  69. body: user.locale == 'en' ? 'Upload your photo with a face. Your profile will be deleted with in 24 hours' :
  70. 'Загрузите своё фото с лицом. Ваш профиль будет удалён в течении 24 часа',
  71. sound: 'obyjvlenie.mp3'
  72. }
  73. };
  74. sendPush(message);
  75. };
  76.  
  77. // Push о напоминании что нужно поставить фотографию в профиле
  78. var sendAboutProfilePhoto = function (user) {
  79. var message = {
  80. to: user.gcmId,
  81. notification: {
  82. title: user.locale == 'en' ? 'Profile will be deleted' : 'Профиль будет удалён',
  83. body: user.locale == 'en' ? 'You did not put your photo with a face' : 'Вы не поставили своё фото с лицом',
  84. sound: 'obyjvlenie.mp3'
  85. }
  86. };
  87. sendPush(message);
  88. };
  89.  
  90. // 1. Push - это находка целей когда происходит запись в БД в hunts
  91. var sendHunterSignal = function (user) {
  92. var message = {
  93. to: user.gcmId,
  94. notification: {
  95. title: user.locale == 'en' ? 'Catch or release!' : 'Поймай или отпусти',
  96. body: user.locale == 'en' ? 'The goal for dating is found!' : 'Найдена цель для знакомства!',
  97. sound: 'hunterlove.mp3',
  98. screenState: 'hunter_signal'
  99. }
  100. };
  101. sendPush(message);
  102. };
  103.  
  104. // 2. Заявка на чат кто рядом
  105. var sendChatInvite = function (userId) {
  106. getUser(userId, function (user) {
  107. var message = {
  108. to: user.gcmId,
  109. notification: {
  110. title: user.locale == 'en' ? 'Application for dating!' : 'Заявка на знакомства',
  111. body: user.locale == 'en' ? 'They want to meet you!' : 'С тобой хотят познакомиться!',
  112. sound: 'obyjvlenie.mp3',
  113. screenState: 'invite_near'
  114. }
  115. };
  116. sendPush(message);
  117. });
  118. };
  119.  
  120. // 3.1 Принятие заявки
  121. var sendAcceptInvite = function (userId) {
  122. getUser(userId, function (user) {
  123. var message = {
  124. to: user.gcmId,
  125. notification: {
  126. title: user.locale == 'en' ? 'Rather, in a chat dating!' : 'Скорее в чат знакомств',
  127. body: user.locale == 'en' ? 'Your application for dating is accepted!' : 'Твоя заявка на знакомства принята!',
  128. sound: 'obyjvlenie.mp3',
  129. screenState: 'newnear_message'
  130. }
  131. };
  132. sendPush(message);
  133. });
  134. };
  135.  
  136. // 3.2 Отказ от чата
  137. var sendDeclineInvite = function (userId) {
  138. getUser(userId, function (user) {
  139. var message = {
  140. to: user.gcmId,
  141. notification: {
  142. title: user.locale == 'en' ? 'LoveHunting!' : 'Охота любви',
  143. body: user.locale == 'en' ? 'Your application for dating has been deported!' : 'Твоя заявка на знакомства отклонена!',
  144. sound: 'obyjvlenie.mp3',
  145. screenState: null
  146. }
  147. };
  148. sendPush(message);
  149. });
  150. };
  151.  
  152. // 4. Новое сообщение в чате с кем были встречи
  153. var newMessageInChat = function (userId) {
  154. getUser(userId, function (user) {
  155. var message = {
  156. to: user.gcmId,
  157. notification: {
  158. title: user.locale == 'en' ? 'LoveHunting!' : 'Охота любви',
  159. body: user.locale == 'en' ? 'You have messages from chat!' : 'Тебе сообщения из чата!',
  160. sound: 'sms.mp3',
  161. screenState: 'new_message'
  162. }
  163. };
  164. sendPush(message);
  165. });
  166. }
  167.  
  168. // 5. Новое сообщение в чате "Кто рядом"
  169. var newMessageInChatNear = function (userId) {
  170. getUser(userId, function (user) {
  171. var message = {
  172. to: user.gcmId,
  173. notification: {
  174. title: user.locale == 'en' ? 'LoveHunting!' : 'Охота любви',
  175. body: user.locale == 'en' ? 'You have messages from chat!' : 'Тебе сообщения из чата!',
  176. sound: 'sms.mp3',
  177. screenState: 'newnear_message'
  178. }
  179. };
  180. sendPush(message);
  181. });
  182. }
  183.  
  184. // 6. Создание видеочата
  185. var createVideoChat = function () {
  186. var message = {
  187. to: '/topic/all', // Как отправить всем?
  188. notification: {
  189. title:'LoveHunting! / Охота любви',
  190. body: 'Meet the video chat! / Успей познакомиться в видеочате!',
  191. sound: 'default',
  192. screenState: 'video_chat'
  193. }
  194. };
  195. sendPush(message);
  196. }
  197.  
  198. module.exports = {
  199. sendPhotoRemovePush: sendPhotoRemovePush,
  200. sendAboutProfilePhoto: sendAboutProfilePhoto,
  201. sendHunterSignal: sendHunterSignal,
  202. sendChatInvite: sendChatInvite,
  203. sendAcceptInvite: sendAcceptInvite,
  204. sendDeclineInvite: sendDeclineInvite,
  205. newMessageInChat: newMessageInChat,
  206. newMessageInChatNear: newMessageInChatNear,
  207. createVideoChat: createVideoChat,
  208. };
  209.  
  210. const express = require('express');
  211. const router = express.Router();
  212.  
  213. router.route('/photo-remove-push')
  214. .delete((req, res) => {
  215. db.notices.find({name: %REQ_PARAMS%},{titleEn: 1, _id:0}, (err, mess) => {
  216. if (err) return res.status(200).json(err).end();
  217.  
  218. const {titleEn, titleRu, bodyEn, bodyRu} = mess;
  219. sendPhotoRemovePush(titleEn, titleRu, bodyEn, bodyRu)
  220. res.status(200).json(mess).end();
  221. });
  222. });
  223.  
  224. sendPhotoRemovePush (...args) {
  225. const message = {
  226. to: user.gcmId,
  227. notification: {
  228. title: user.locale == 'en' ? titleEn : titleRu,
  229. body: user.locale == 'en' ? bodyEn : bodyRu,
  230. sound: 'obyjvlenie.mp3'
  231. }
  232. };
  233. sendPush(message);
  234. };
Add Comment
Please, Sign In to add comment