(function () { 'use strict'; angular .module('crosssec') .factory('ChatService', ChatService); /** @ngInject */ function ChatService($websocket, $log, $localStorage, UtilService, $q, $rootScope) { var service = {}; service.init = init; service.sendMessage = sendMessage; service.getAllChannel = getAllChannel; service.getMessages = getMessages; service.markChannelAsRead = markChannelAsRead; service.deleteChannel = clearChannel; service.openChannel = openChannel; service.leaveChannel = leaveChannel; service.enterChannel = enterChannel; // Open a WebSocket connection var websocket = $websocket("ws://echo.websocket.org/"); function init(userId, userHash, channelId) { $log.debug('WS - Init'); // TODO get channel id from backend _openWebSocket(userId, userHash, channelId); } function sendMessage(userId, userHash, channelId, message, timestamp) { // test data var messageObj = { userId: userId, userHash: userHash, channelId: channelId, content: message, timestamp: timestamp }; console.log(messageObj); $log.debug('REST - Save a message', messageObj); $log.debug('WS - Send a message', messageObj); _saveMessage(messageObj); _sendThroughWebsocket(messageObj); } function getAllChannel(userId, userHash) { $log.debug('REST - Get all channel id, partners and last message. If the channel is unread -> unread messages.'); var response = { "errors": null, "result": { "channels": [ { id: '2334-1234-3456', userId: '26', userHash: 'sadasdasdasdasdasd', partners: [ {id: '12', name: 'Béla', avatar: 'https://api.adorable.io/avatars/40/abott@adorable.io.png'}, {id: '10', name: 'Lilla', avatar: 'https://api.adorable.io/avatars/285/lilla%40example.com'} ], messages: [ {userId: 21, content: 'Utolsó üzenet. One must believe the thing in order to yearn the lama of atomic enlightenment. Awareness is not further in nirvana, the country of core, or heavens, but everywhere.', timestamp: ''} ], isUnread: false }, { id: '2134-5467-9812', userId: '26', userHash: 'sadasdasdasdasdasd', partners: [ {id: '21', name: 'Józsi', avatar: 'https://api.adorable.io/avatars/40/abott@adorable.io.png'}, {id: '10', name: 'Lilla', avatar: 'https://api.adorable.io/avatars/40/lilla@adorable.io.png'} ], isUnread: true, messages: [ {id: 0, userId: 385, content: 'Hello Józsi!', timestamp: new Date()}, {id: 1, userId: 385, content: 'Mi van veled?', timestamp: ''}, {id: 2, userId: 21, content: 'Semmi', timestamp: ''}, {id: 3, userId: 385, content: 'Az nem sok', timestamp: ''}, {id: 3, userId: 385, content: 'asdsadsadsadasd', timestamp: ''}, {id: 3, userId: 385, content: 'sdasdasdasdsadasdas', timestamp: ''}, {id: 3, userId: 21, content: 'Asdadasdasd', timestamp: ''}, {id: 3, userId: 21, content: 'sdadasdasdasd', timestamp: ''}, {id: 3, userId: 385, content: 'Cur tumultumque ridetis?', timestamp: ''}, {id: 3, userId: 385, content: 'Cur victrix potus? ', timestamp: ''}, {id: 3, userId: 385, content: 'Cur nutrix mori? ', timestamp: ''}, {id: 3, userId: 21, content: 'Cur demissio crescere? ', timestamp: ''}, {id: 3, userId: 385, content: 'Nunquam contactus barcas. ', timestamp: ''} ] }, { id: '1232-1111-6789', userId: '26', userHash: 'sadasdasdasdasdasd', partners: [ {id: '12', name: 'Béla', avatar: 'https://api.adorable.io/avatars/40/abott@adorable.io.png'} ], isUnread: true, messages: [ {id: 0, userId: 385, content: 'Hello Béla!', timestamp: new Date()}, {id: 1, userId: 385, content: 'Mi van veled?', timestamp: ''}, {id: 2, userId: 12, content: 'Semmi', timestamp: ''}, {id: 3, userId: 385, content: 'Cur plasmator mori? ', timestamp: ''}, {id: 3, userId: 385, content: 'Nunquam magicae abaculus. ', timestamp: ''}, {id: 3, userId: 385, content: 'asdsadsadsadasd', timestamp: ''}, {id: 3, userId: 385, content: 'sdasdasdasdsadasdas', timestamp: ''}, {id: 3, userId: 12, content: 'Asdadasdasd', timestamp: ''}, {id: 3, userId: 12, content: 'sdadasdasdasd', timestamp: ''}, {id: 3, userId: 385, content: 'Cur tumultumque ridetis?', timestamp: ''}, {id: 3, userId: 385, content: 'Cur victrix potus? ', timestamp: ''}, {id: 3, userId: 385, content: 'Cur nutrix mori? ', timestamp: ''}, {id: 3, userId: 12, content: 'Cur demissio crescere? ', timestamp: ''}, {id: 3, userId: 385, content: 'Nunquam contactus barcas. ', timestamp: ''}, {id: 3, userId: 385, content: 'Cur frondator credere.', timestamp: ''}, { id: 3, userId: 12, content: 'Cur hibrida cadunt? Neuter planetas ducunt ad triticum. Albus onuss ducunt ad messor. Ubi est fortis mortem? Cur historia trabem?', timestamp: '' }, {id: 3, userId: 385, content: 'Az nem sok', timestamp: ''}, {id: 3, userId: 12, content: 'Cur gluten experimentum? ', timestamp: ''}, {id: 3, userId: 385, content: 'Nunquam fallere barcas. Sectams nocere in alta muta!', timestamp: ''}, {id: 3, userId: 385, content: 'Cur eleates peregrinationes? Ecce, emeritis habitio!', timestamp: ''}, {id: 3, userId: 385, content: 'Cur bursa tolerare? Ecce, salvus burgus! Varius detriuss ducunt ad lura.', timestamp: ''}, {id: 3, userId: 385, content: 'Nunquam convertam glos. Brabeutas sunt calcarias de neuter equiso.', timestamp: ''}, {id: 3, userId: 385, content: 'Nunquam manifestum lacta. Fidess nocere, tanquam peritus equiso.', timestamp: ''}, { id: 3, userId: 385, content: 'Nunquam imitari adiurator. Cum amicitia peregrinationes, omnes orexises consumere grandis, magnum adelphises.', timestamp: '' }, {id: 4, userId: 12, content: 'Hát nem', timestamp: ''} ] } ] } }; // TODO if response && !response.errors if (response && !response.error) { //$localStorage.chatRooms = response.result.chatRooms; // TODO remove item from localstorage return response.result; } } function getMessages(userId, userHash, channelId, limit) { console.log('channelId'); return $q(function (resolve, reject) { setTimeout(function () { resolve({ "result": { "data": { "messages": [ {userId: 385, content: 'Going to the great unknown doesn’t experience attraction anymore than facilitating creates further issue.', timestamp: new Date()}, {userId: 385, content: 'Who can view the uniqueness and joy of a believer if he has the secret freedom of the sun?', timestamp: ''}, {userId: 12, content: 'One outer shame i give you: trap each other.', timestamp: ''}, {userId: 385, content: 'Cur plasmator mori? ', timestamp: ''}, {userId: 385, content: 'Nunquam magicae abaculus. ', timestamp: ''}, {userId: 385, content: 'asdsadsadsadasd', timestamp: ''}, {userId: 385, content: 'sdasdasdasdsadasdas', timestamp: ''}, {userId: 12, content: 'Asdadasdasd', timestamp: ''}, {userId: 12, content: 'sdadasdasdasd', timestamp: ''}, {userId: 385, content: 'Cur tumultumque ridetis?', timestamp: ''}, {userId: 385, content: 'Cur victrix potus? ', timestamp: ''}, {userId: 385, content: 'New messages from service.', timestamp: ''} ] } }, "errors": null }); }, 1000); }).then(function (response) { UtilService.handleSuccess(response); return response.result; }, UtilService.handleError); } function markChannelAsRead(userId, userHash, channelId) { $log.debug('REST - Mark channel as read.'); _fetchChannelById(userId, userHash, channelId); } function clearChannel(userId, userHash, channelId) { $log.debug('REST - Clear channel.'); _fetchChannelById(userId, userHash, channelId); } function openChannel(userId, userHash, partners) { $log.debug('REST - Create new channel'); $log.debug('WS - Create new channel'); _getChannelId(userId, userHash, partners); // If success websocket.onOpen(userId, userHash, channelId); } function leaveChannel(userId, userHash, channelId, timestamp) { $log.debug('REST - Leave channel'); } function enterChannel(userId, userHash, targetPerson, channelId, timestamp) { $log.debug('REST - User connecting to channel'); } function _fetchChannelById(userId, channelId) { $log.debug('REST - Fetch channel'); } function _getChannelId(userId, userHash, partners) { $log.debug('REST - Get channel id from backend'); } function _openWebSocket(userId, userHash, channelId) { $log.debug('WS - Open notify websocket???'); //websocket.onOpen(function (userId, userHash, channelId) { // $log.debug('Send...'); // websocket.send({userId: userId, userHash: userHash, channelId: channelId}); //}); } function _sendThroughWebsocket(message) { $log.debug('WS - Send a message', message); websocket.send(message); } function _saveMessage(messageObj) { console.log('REST- save message.', messageObj); } //function getChannelMessages(userId, userHash, channelId, limit) { // console.log(userId, userHash, channelId, limit); // if (channelId === "2334 - 1234 - 3456") { // console.log('yes'); // return { // "result": { // "data": { // "messages": [ // {userId: 385, content: 'Hello Béla!', timestamp: new Date()}, // {userId: 385, content: 'Mi van veled?', timestamp: ''}, // {userId: 12, content: 'Semmi', timestamp: ''}, // {userId: 385, content: 'Cur plasmator mori? ', timestamp: ''}, // {userId: 385, content: 'Nunquam magicae abaculus. ', timestamp: ''}, // {userId: 385, content: 'asdsadsadsadasd', timestamp: ''}, // {userId: 385, content: 'sdasdasdasdsadasdas', timestamp: ''}, // {userId: 12, content: 'Asdadasdasd', timestamp: ''}, // {userId: 12, content: 'sdadasdasdasd', timestamp: ''}, // {userId: 385, content: 'Cur tumultumque ridetis?', timestamp: ''}, // {userId: 385, content: 'Cur victrix potus? ', timestamp: ''}, // {userId: 385, content: 'Cur nutrix mori? ', timestamp: ''} // ] // } // }, // "errors": null // }; // } //} // Allandoan elerheto metodus websocket.onMessage(function (message) { $log.debug('WS - Incoming message', message); //$localStorage.notification = {id: message.channelId}; //var chatRooms = $localStorage.chatRooms; var currentUser = $localStorage.currentUser; _saveMessage($localStorage.currentUser.id, $localStorage.currentUser.hash, message); var parsed = JSON.parse(message.data); console.log(parsed); $rootScope.newMessage = parsed; //var parsedData = JSON.parse(message.data); //var targetRoom = lodash.find(chatRooms, {'id': parsedData.chatId}); //var collection; //targetRoom.messages.push(parsedData); }); return service; } })();