1 | 'use strict'
|
2 |
|
3 | const Promise = require('bluebird')
|
4 | const _ = require('lodash')
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | class RecoveryAPI {
|
14 |
|
15 | constructor (state, transport, execUserCommand, instanceUID) {
|
16 | this.state = state
|
17 | this.transport = transport
|
18 | this.instanceUID = instanceUID
|
19 | this.execUserCommand = execUserCommand
|
20 | }
|
21 |
|
22 | checkUserSockets (user) {
|
23 | let { userName } = user
|
24 | return user.userState.getSocketsToInstance().then(sockets => {
|
25 | return Promise.each(_.toPairs(sockets), ([socket, instance]) => {
|
26 | if (instance === this.instanceUID) {
|
27 | if (!this.transport.getSocket(socket)) {
|
28 | return user.userState.removeSocket(socket)
|
29 | }
|
30 | }
|
31 | return Promise.resolve()
|
32 | })
|
33 | }).then(() => {
|
34 | return user.userState.getSocketsToRooms()
|
35 | }).then(data => {
|
36 | let args = _.values(data)
|
37 | return _.intersection(...args)
|
38 | }).then(rooms => {
|
39 | return Promise.each(rooms, roomName => {
|
40 | return this.state.getRoom(roomName)
|
41 | .then(room => room.roomState.hasInList('userlist', userName))
|
42 | .then(isPresent =>
|
43 | isPresent ? Promise.resolve() : user.removeFromRoom(roomName))
|
44 | .catchReturn()
|
45 | })
|
46 | })
|
47 | }
|
48 |
|
49 | checkRoomJoined (room) {
|
50 | let { roomName } = room
|
51 | return room.getList(null, 'userlist', true).then(userlist => {
|
52 | return Promise.each(userlist, userName => {
|
53 | return this.state.getUser(userName).then(user => {
|
54 | return user.userState.getRoomToSockets(roomName).then(sockets => {
|
55 | if (!sockets || !sockets.length) {
|
56 | return user.removeFromRoom(roomName)
|
57 | } else {
|
58 | return Promise.resolve()
|
59 | }
|
60 | }).catchReturn()
|
61 | .then(() => room.checkAcess(userName))
|
62 | .catch(() => user.removeFromRoom(roomName))
|
63 | }).catchReturn()
|
64 | })
|
65 | })
|
66 | }
|
67 |
|
68 | |
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 | userStateSync (userName, cb) {
|
77 | return this.state.getUser(userName)
|
78 | .then(user => this.checkUserSockets(user))
|
79 | .asCallback(cb)
|
80 | }
|
81 |
|
82 | |
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | roomStateSync (roomName, cb) {
|
91 | return this.state.getRoom(roomName)
|
92 | .then(room => this.checkRoomJoined(room))
|
93 | .asCallback(cb)
|
94 | }
|
95 |
|
96 | |
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 | instanceRecovery (id, cb) {
|
105 | return this.state.getInstanceSockets(id).then(sockets => {
|
106 | return Promise.each(_.toPairs(sockets), ([id, userName]) => {
|
107 | return this.state.getUser(userName)
|
108 | .then(user => user.removeSocket(id))
|
109 | .catchReturn()
|
110 | })
|
111 | }).asCallback(cb)
|
112 | }
|
113 |
|
114 | |
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 | getInstanceHeartbeat (id, cb) {
|
123 | return this.state.getInstanceHeartbeat(id).asCallback(cb)
|
124 | }
|
125 |
|
126 | }
|
127 |
|
128 | module.exports = RecoveryAPI
|