MessagesPageController.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. app.controller('MessagesPageController',['$scope','$timeout',function($scope,$timeout){
  2. // gettig of user images url
  3. $scope.userImages = {};
  4. var firebaseUsers = firebase.database().ref().child('users');
  5. firebaseUsers.on('value',function(snap){
  6. $scope.users = snap.val();
  7. $scope.userKeys = Object.keys($scope.users)
  8. for (let i = 0; i < $scope.userKeys.length; i++) {
  9. firebase.storage().ref($scope.userKeys[i]).getDownloadURL().then(function(url){
  10. $scope.userImages[$scope.userKeys[i]] = url;
  11. }).catch(function(error){
  12. console.log(error);
  13. })
  14. }
  15. $scope.selectedUser = $scope.userKeys[0];
  16. })
  17. // gettig of user images url
  18. // chat output
  19. $scope.selectUser = function(key){
  20. $scope.selectedUser = key;
  21. }
  22. // chat output
  23. //send message
  24. $scope.messageObject = {
  25. userMessage: ''
  26. };
  27. $scope.sendMessage = function(){
  28. var selectedUser = $scope.selectedUser;
  29. if($scope.messageObject.userMessage){
  30. $scope.messageObject.uid = 'uRkqD9GmNxUsdgF1XtmPicDJwAz1';
  31. var date = new Date();
  32. // $scope.messageObject.name = 'Admin';
  33. // $scope.messageObject.surname = '';
  34. $scope.messageObject.date = date.getHours() + ':' + (date.getMinutes().toString().length < 2?'0'+date.getMinutes():date.getMinutes())
  35. firebase.database().ref().child('users').child($scope.selectedUser).child('chat').push().set($scope.messageObject);
  36. $scope.messageObject.userMessage = '';
  37. $scope.selectedUser = selectedUser;
  38. }
  39. console.log($scope.userKeys.indexOf($scope.selectedUser))
  40. }
  41. //send message
  42. }])