Subversion Repository Public Repository

Nextrek

@ 1352
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
const fs = require('fs');
const sharp = require('sharp');

const CHAT = Symbol.for("net.nextrek.chattrek");

var globalSymbols = Object.getOwnPropertySymbols(global);
var hasChat = (globalSymbols.indexOf(CHAT) > -1);

if (!hasChat) {
    global[CHAT] = {
        ROLES: {
            OPERATOR: 'OPERATOR',
            CUSTOMER: 'CUSTOMER'
        },

        DEFAULT_ROOMS: {
            OPERATORS: 'OPERATORS'
        },

        EVENTS: {
            CONNECTION: {
                OPEN: 'CONNECTION:OPEN',
                CLOSE: 'CONNECTION:CLOSE'
            },
            ROOM: {
                CREATED: 'ROOM:CREATED',
                JOIN: 'ROOM:JOIN',
                LEAVE: 'ROOM:LEAVE'
            },
            SEND: {
                MESSAGE: 'SEND:MESSAGE',
                IMAGE: 'SEND:IMAGE',
                ATTACHMENT: 'SEND:FILE',
                LINK: 'SEND:LINK'
            },
            OPERATORS: {
                LIST: {
                    UPDATE: 'OPERATORS:LIST:UPDATE'
                }
            },
            CUSTOMERS: {
                LIST: {
                    UPDATE: 'CUSTOMERS:LIST:UPDATE'
                }
            },
            ROOMS: {
                LIST: {
                    UPDATE: 'ROOMS:LIST:UPDATE'
                }
            }
        },

        ERRORS: {
            USER: {
                    NOT_FOUND: 'ERROR:USER:NOTFOUND'
                }
        },

        users: [],

        rooms: {
            CUSTOMER: {},
            OPERATOR: {}
        },

        isRoomAllowed(from, to) {
            if (from.role === _this.ROLES.OPERATOR) {

            }
        },

        usersMap: {},

        socketsMap: {},

        getUser(id) {
            const user = this.usersMap[id];
            return {
                room: user.room,
                name: user.name
            }
        },

        createUser(name, role, id, socket) {
            const _this = this;
            const user = {
                name: name,
                role: role,
                room: id,
            }

            this.addUser(user, socket);

            return user;
        },

        addUser(user, socket) {
            this.usersMap[user.room] = user;
            this.socketsMap[user.room] = socket;
            this.users.push(user);
            this.rooms[user.role][user.room] = [];
        },

        deleteUser(user) {
            this.users.splice(this.users.indexOf(user), 1);
            delete this.usersMap[user.room];
            delete this.socketsMap[user.room];
            return this.leaveRoom(user, user);
        },
        
        getUsersByRole(role) {
            return this.users.filter((user) => {
                return user.role === role
            });
        },
        
        countUsersByRole(role) {
            this.getUsersByRole(role).length
        },
        
        isUserInRoom(host, guest) {
            return this.rooms[host.role][host.room].indexOf(guest) > - 1;
        },

        joinRoom(host, guest) {
            if (guest && guest.role === this.ROLES.OPERATOR && this.rooms[host.role][host.room]) {
                if (!this.isUserInRoom(host, guest) && !this.isUserInRoom(guest, host)) {
                    this.rooms[host.role][host.room].push(guest);
                    return host.room;
                } else {
                    return this.rooms[host.role][host.room] ? host.room : guest.room;
                }
            }
        },

        leaveRoom(host, guest) {
            /**
             * tbd: check leaving permissions
             */
            const promises = [];
            if (host.room === guest.room) {
                // host leaving his own room means throw out guest(s)
                if (this.rooms[host.role][host.room]) {
                    this.rooms[host.role][host.room].forEach(user => {
                        if (this.socketsMap[user.room]) {
                            promises.push(new Promise(resolve => {
                                resolve(this.socketsMap[user.room]);
                            }));
                        }
                    });
                }
                delete this.rooms[host.role][host.room];
            } else {
                // operators are only allowed to leave other operators' rooms
                if (host.role === this.ROLES.OPERATOR) {
                    const ix = this.rooms[host.role][host.room].indexOf(guest);
                    this.rooms[host.role][host.room].splice(ix, 1);
                }
            }
            return promises;
        },

        storeImage(data, user) {
            return new Promise((resolve, reject) => {
                const matchExtension = /^data:image\/(\w+)/i.exec(data.content);
                if (matchExtension === null) {
                    reject('Invalid data type');
                }
    
                const extension = matchExtension[1];
                const filename = `${user.room}_${Date.now()}.${extension}`;
    
                console.log('user ' + user.room + ' saving file name: ' + filename);
    
                const dir = `./public/imagesFull/${user.room}`;
                const dir2 = `./public/imagesThumb/${user.room}`;
                if (!fs.existsSync(dir)){
                    fs.mkdirSync(dir);
                }
                if (!fs.existsSync(dir2)){
                    fs.mkdirSync(dir2);
                }
    
                const base64Data = data.content.replace(/^data:image\/\w+;base64,/, '');
    
                fs.writeFile(dir +"/"+filename, base64Data, 'base64', function(err) {
                    console.log(err);
                });
                
                let imgBuffer = Buffer.from(base64Data, 'base64');
               
                sharp(imgBuffer)
                    .resize(160, 120)
                    .toFile(dir2 + "/" + filename, (err, info) => {
                        resolve(filename);
                    });
            })
        },

        event(type, data) {
            return {
                type,
                data,
                ts: Date.now()
            }
        },

        message(data, room, from) {
            return new Promise((resolve, reject) => {
                switch (data.contentType) {
                    case 'IMAGE':
                        this.storeImage(data, from)
                        .then(filename => {
                            resolve({
                                from,
                                room: room,
                                content: filename,
                                contentType: data.contentType
                            });
                        })
                        .catch(error => {
                            data.content = error;
                        });
                        break;
                
                    default:
                        resolve({
                            from,
                            room: room,
                            content: data.content,
                            contentType: data.contentType
                        });
                        break;
                }
            });
        }
    };
}

var singleton = {};

Object.defineProperty(singleton, "instance", {
    get: function () {
        return global[CHAT];
    }
});

Object.freeze(singleton);


module.exports = singleton;

Commits for Nextrek/socketIO/chat.js

Diff revisions: vs.
Revision Author Commited Message
1130 Diff Diff GGentile picture GGentile Wed 23 Jan, 2019 23:40:05 +0000

fix disconnect when room already left

1129 Diff Diff GGentile picture GGentile Wed 23 Jan, 2019 23:23:52 +0000

room leave and notify guests

1128 Diff Diff GGentile picture GGentile Fri 18 Jan, 2019 09:29:09 +0000

room leave

1127 Diff Diff GGentile picture GGentile Wed 09 Jan, 2019 13:47:30 +0000

send image

1126 Diff Diff GGentile picture GGentile Fri 04 Jan, 2019 14:09:04 +0000
1125 Diff Diff GGentile picture GGentile Fri 04 Jan, 2019 11:31:52 +0000

event ROOM:CREATED

1124 Diff Diff GGentile picture GGentile Fri 04 Jan, 2019 11:20:58 +0000
1123 Diff Diff GGentile picture GGentile Fri 04 Jan, 2019 00:19:22 +0000
1121 Diff Diff GGentile picture GGentile Wed 02 Jan, 2019 13:10:03 +0000

chat singleton

1120 GGentile picture GGentile Tue 04 Dec, 2018 10:31:37 +0000