added svcapi ui and camunda code
[it/otf.git] / otf-frontend / server / src / feathers / channels.js
1 /*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
2 #                                                                              #\r
3 #   Licensed under the Apache License, Version 2.0 (the "License");            #\r
4 #   you may not use this file except in compliance with the License.           #\r
5 #   You may obtain a copy of the License at                                    #\r
6 #                                                                              #\r
7 #       http://www.apache.org/licenses/LICENSE-2.0                             #\r
8 #                                                                              #\r
9 #   Unless required by applicable law or agreed to in writing, software        #\r
10 #   distributed under the License is distributed on an "AS IS" BASIS,          #\r
11 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #\r
12 #   See the License for the specific language governing permissions and        #\r
13 #   limitations under the License.                                             #\r
14 ##############################################################################*/\r
15 \r
16 \r
17 module.exports = function (app) {\r
18         if (typeof app.channel !== 'function') {\r
19                 // If no real-time functionality has been configured just return\r
20                 return;\r
21         }\r
22 \r
23         app.on('connection', connection => {\r
24                 // On a new real-time connection, add it to the anonymous channel\r
25                 app.channel('anonymous').join(connection);\r
26                 \r
27         });\r
28 \r
29         app.on('login', (authResult, { connection }) => {\r
30                 // connection can be undefined if there is no\r
31                 // real-time connection, e.g. when logging in via REST\r
32                 if (connection) {\r
33                         // Obtain the logged in user from the connection\r
34                         // const user = connection.user;\r
35 \r
36                         // The connection is no longer anonymous, remove it\r
37                         app.channel('anonymous').leave(connection);\r
38 \r
39                         // Add it to the authenticated user channel\r
40                         app.channel('authenticated').join(connection);\r
41 \r
42                         // Channels can be named anything and joined on any condition\r
43 \r
44                         // E.g. to send real-time events only to admins use\r
45                         // if(user.isAdmin) { app.channel('admins').join(connection); }\r
46 \r
47                         // If the user has joined e.g. chat rooms\r
48                         // if(Array.isArray(user.rooms)) user.rooms.forEach(room => app.channel(`rooms/${room.id}`).join(channel));\r
49 \r
50                         // Easily organize users by email and userid for things like messaging\r
51                         // app.channel(`emails/${user.email}`).join(channel);\r
52                         // app.channel(`userIds/$(user.id}`).join(channel);\r
53                 }\r
54         });\r
55 \r
56         //eslint-disable-next-line no-unused-vars\r
57         app.publish((data, hook) => {\r
58                 // Here you can add event publishers to channels set up in `channels.js`\r
59                 // To publish only for a specific event use `app.publish(eventname, () => {})`\r
60 \r
61                 console.log('Publishing all events to all authenticated users. See `channels.js` and https://docs.feathersjs.com/api/channels.html for more information.'); // eslint-disable-line\r
62 \r
63                 // e.g. to publish all service events to all authenticated users use\r
64                 return app.channel('authenticated');\r
65         });\r
66 \r
67         // Here you can also add service specific event publishers\r
68         // e.g. the publish the `users` service `created` event to the `admins` channel\r
69         // app.service('users').publish('created', () => app.channel('admins'));\r
70 \r
71         // With the userid and email organization from above you can easily select involved users\r
72         // app.service('messages').publish(() => {\r
73         //   return [\r
74         //     app.channel(`userIds/${data.createdBy}`),\r
75         //     app.channel(`emails/${data.recipientEmail}`)\r
76         //   ];\r
77         // });\r
78 };\r