index.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. const jwtSecret = '4atikJgznM'
  2. const jwt = require('jsonwebtoken')
  3. const express = require('express');
  4. const express_graphql = require('express-graphql');
  5. const { buildSchema, printSchema, GraphQLString } = require('graphql');
  6. const expand = require('mm-graphql/expand')
  7. const fs = require('fs')
  8. const uploadPath = `${__dirname}/public/images/`;
  9. const upload = require('multer')({ dest: uploadPath })
  10. ;(async () => {
  11. const {Savable, slice, getModels} = await require('./models.js')()
  12. const { jwtGQLAnon, jwtCheck } = require('mm-graphql/jwt')
  13. let schema = buildSchema(`
  14. type User {
  15. _id: ID
  16. createdAt: String
  17. login: String
  18. nick : String
  19. acl: [String]
  20. avatar: Media
  21. chats: [Chat]
  22. }
  23. input UserInput {
  24. _id: ID
  25. login: String
  26. nick : String
  27. password: String
  28. acl: [String]
  29. avatar: MediaInput
  30. chats: [ChatInput]
  31. }
  32. type Message {
  33. _id: ID
  34. createdAt: String
  35. owner: User
  36. chat: Chat
  37. text: String
  38. media: [Media]
  39. replies: [Message]
  40. replyTo: Message
  41. forwarded: Message
  42. forwardWith: [Message]
  43. }
  44. input MessageInput {
  45. _id: ID
  46. text: String
  47. chat: ChatInput
  48. media: [MediaInput]
  49. replies: [MessageInput]
  50. replyTo: MessageInput
  51. forwarded: MessageInput
  52. forwardWith: [MessageInput]
  53. }
  54. type Chat {
  55. _id: ID
  56. createdAt: String
  57. owner: User
  58. title: String
  59. members: [User]
  60. messages: [Message]
  61. avatar: Media
  62. }
  63. input ChatInput {
  64. _id: ID
  65. title: String
  66. members: [UserInput]
  67. messages: [MessageInput]
  68. }
  69. type Media {
  70. _id: ID,
  71. createdAt: String
  72. owner: User
  73. text: String,
  74. url: String,
  75. originalFileName: String,
  76. type: String
  77. userAvatar: User
  78. chatAvatar: [Chat]
  79. messages: [Message]
  80. }
  81. input MediaInput {
  82. _id: ID,
  83. text: String,
  84. userAvatar: UserInput,
  85. chatAvatar: [ChatInput]
  86. messages: [MessageInput]
  87. }
  88. `);
  89. schema = expand(schema, {
  90. login:{
  91. type: GraphQLString,
  92. args: {login: {type: GraphQLString},
  93. password: {type: GraphQLString},
  94. },
  95. async resolve(root, {login, password}, context, info){
  96. const Savable = context.models.Savable
  97. if (!login || !password) return null;
  98. const user = await Savable.m.User.findOne({login, password})
  99. console.log(user, {login, password})
  100. if (!user)
  101. return null;
  102. const token = jwt.sign({ sub: {id: user._id, login, acl: user.acl}}, jwtSecret); //подписывам токен нашим ключем
  103. return token
  104. }
  105. }
  106. })
  107. console.log(printSchema(schema))
  108. const app = express();
  109. const http = require('http').Server(app);
  110. const io = require('socket.io')(http);
  111. app.use(require('cors')())
  112. app.use(express.static('public'));
  113. app.use('/graphql', express_graphql(jwtGQLAnon({schema, createContext: decoded => getModels(decoded, messageWatcher), graphiql: true, secret: jwtSecret})))
  114. app.post('/upload', upload.single('media'), async (req, res, next) => {
  115. let decoded;
  116. if (decoded = jwtCheck(req, jwtSecret)){
  117. console.log('SOME UPLOAD', decoded, req.file)
  118. let {models: {Media }} = await getModels(decoded.sub)
  119. let media = await Media.fromFileData(req.file)
  120. res.end(JSON.stringify({_id: media._id, url: media.url}))
  121. }
  122. else {
  123. res.status(503).send('permission denied')
  124. }
  125. })
  126. app.use(express.static('public'));
  127. const sockets = {}
  128. async function messageWatcher(msg){
  129. console.log('NEW MSG', msg.text, msg.chat.title, msg.owner.login)
  130. for (let [id,{user, socket}] of Object.entries(sockets)){
  131. if (user.chats.some(chat => chat._id.toString() === msg.chat._id.toString())){
  132. let {_id, createdAt, text, chat, media, replyTo, forwarded, owner} = msg
  133. await chat;
  134. await media;
  135. await replyTo;
  136. await forwarded;
  137. owner = await owner;
  138. socket.emit('msg', {_id, createdAt, text, chat: chat && {_id: chat._id, title: chat.title},
  139. media: media && {_id: media._id, url: media.url},
  140. replyTo: replyTo && {_id: replyTo._id, text: replyTo.text},
  141. forwarded: forwarded && {_id: forwarded._id, text: forwarded.text},
  142. owner: {_id: owner._id, login: owner.login, nick: owner.nick}})
  143. }
  144. }
  145. }
  146. io.on('connection', socket => {
  147. console.log('connect', socket.id)
  148. socket.on('disconnect', () => {
  149. console.log('disconnect', socket.id)
  150. delete sockets[socket.id]
  151. })
  152. socket.on('jwt', async token =>{
  153. let decoded;
  154. try {
  155. decoded = jwt.verify(token, jwtSecret)
  156. }
  157. catch (e){
  158. socket.emit('jwt_fail', e)
  159. return
  160. }
  161. socket.emit('jwt_ok', decoded)
  162. //sockets[socket.id] = decoded
  163. sockets[socket.id] = {decoded, user: (await getModels(decoded.sub)).thisUser, socket}
  164. })
  165. })
  166. let socketPath = 5001
  167. http.listen(socketPath, () => {
  168. console.log(`Express GraphQL Server Now Running On ${socketPath}/graphql`);
  169. });
  170. })()