X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=otf-frontend%2Fserver%2Fsrc%2Ffeathers%2Fchannels.js;fp=otf-frontend%2Fserver%2Fsrc%2Ffeathers%2Fchannels.js;h=a896480a7a6f00e0245867fa03c4184aa2e7f0d7;hb=6f7e46b4562b31f748670be8947d315963240ddd;hp=0000000000000000000000000000000000000000;hpb=f49bd1efeaaddd4891c1f329b18d8cfb28b3e75b;p=it%2Fotf.git diff --git a/otf-frontend/server/src/feathers/channels.js b/otf-frontend/server/src/feathers/channels.js new file mode 100644 index 0000000..a896480 --- /dev/null +++ b/otf-frontend/server/src/feathers/channels.js @@ -0,0 +1,78 @@ +/* Copyright (c) 2019 AT&T Intellectual Property. # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); # +# you may not use this file except in compliance with the License. # +# You may obtain a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +##############################################################################*/ + + +module.exports = function (app) { + if (typeof app.channel !== 'function') { + // If no real-time functionality has been configured just return + return; + } + + app.on('connection', connection => { + // On a new real-time connection, add it to the anonymous channel + app.channel('anonymous').join(connection); + + }); + + app.on('login', (authResult, { connection }) => { + // connection can be undefined if there is no + // real-time connection, e.g. when logging in via REST + if (connection) { + // Obtain the logged in user from the connection + // const user = connection.user; + + // The connection is no longer anonymous, remove it + app.channel('anonymous').leave(connection); + + // Add it to the authenticated user channel + app.channel('authenticated').join(connection); + + // Channels can be named anything and joined on any condition + + // E.g. to send real-time events only to admins use + // if(user.isAdmin) { app.channel('admins').join(connection); } + + // If the user has joined e.g. chat rooms + // if(Array.isArray(user.rooms)) user.rooms.forEach(room => app.channel(`rooms/${room.id}`).join(channel)); + + // Easily organize users by email and userid for things like messaging + // app.channel(`emails/${user.email}`).join(channel); + // app.channel(`userIds/$(user.id}`).join(channel); + } + }); + + //eslint-disable-next-line no-unused-vars + app.publish((data, hook) => { + // Here you can add event publishers to channels set up in `channels.js` + // To publish only for a specific event use `app.publish(eventname, () => {})` + + 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 + + // e.g. to publish all service events to all authenticated users use + return app.channel('authenticated'); + }); + + // Here you can also add service specific event publishers + // e.g. the publish the `users` service `created` event to the `admins` channel + // app.service('users').publish('created', () => app.channel('admins')); + + // With the userid and email organization from above you can easily select involved users + // app.service('messages').publish(() => { + // return [ + // app.channel(`userIds/${data.createdBy}`), + // app.channel(`emails/${data.recipientEmail}`) + // ]; + // }); +};