Exemplo n.º 1
0
        public static void SendData(Guid applicationId, string feedId, RealTimeAction actionName, string jsonString)
        {
            if (!RaaiVanSettings.RealTime(applicationId) || string.IsNullOrEmpty(feedId))
            {
                return;
            }

            feedId = feedId.ToLower();
            string name = actionName.ToString().ToLower();

            if (!Feeds.ContainsKey(feedId) || !Feeds[feedId].ContainsKey(name))
            {
                return;
            }

            IHubContext context = GlobalHost.ConnectionManager.GetHubContext <RaaiVanHub>();

            foreach (string connId in Feeds[feedId][name])
            {
                if (ConnectedUsers.ContainsKey(connId))
                {
                    context.Clients.Client(connId).GetData(name, jsonString);
                }
            }
        }
Exemplo n.º 2
0
        public static void SendData(Guid applicationId, List <Guid> userIds, RealTimeAction actionName, string jsonString)
        {
            if (!RaaiVanSettings.RealTime(applicationId) || userIds == null)
            {
                return;
            }
            string name = actionName.ToString().ToLower();

            IHubContext context = GlobalHost.ConnectionManager.GetHubContext <RaaiVanHub>();

            foreach (Guid uId in userIds.Distinct())
            {
                if (!UserConnectionsDic.ContainsKey(uId))
                {
                    continue;
                }

                foreach (string connId in UserConnectionsDic[uId])
                {
                    if (ConnectedUsers.ContainsKey(connId) && ConnectedUsers[connId].Events.Any(u => u == name))
                    {
                        context.Clients.Client(connId).GetData(name, jsonString);
                    }
                }
            }
        }
Exemplo n.º 3
0
        public override Task OnConnected()
        {
            Guid tenantId = Guid.Empty;

            try
            {
                tenantId = PublicMethods.get_current_tenant(Context.Request.GetHttpContext().GetOwinContext().Request,
                                                            RaaiVanSettings.Tenants).Id;
            }
            catch (Exception ex) { return(base.OnConnected()); }

            if (!RaaiVanSettings.RealTime(tenantId))
            {
                return(base.OnConnected());
            }

            Guid currentUserId = get_current_user_id();

            if (currentUserId == Guid.Empty)
            {
                return(base.OnConnected());
            }

            if (!UserConnectionsDic.ContainsKey(currentUserId))
            {
                UserConnectionsDic[currentUserId] = new List <string>();
            }

            bool added = false;

            if (!UserConnectionsDic[currentUserId].Any(u => u == Context.ConnectionId))
            {
                UserConnectionsDic[currentUserId].Add(Context.ConnectionId);
                added = true;
            }

            ConnectedUsers.Add(Context.ConnectionId,
                               new Registration(Context.ConnectionId, tenantId, currentUserId, new List <string>()));

            //if the user is now online, tell his/her friends
            if (UserConnectionsDic[currentUserId].Count == 1 && added)
            {
                prepare_user(tenantId, currentUserId);
                string userJson = _get_user_json(tenantId, currentUserId);

                RaaiVanHub.SendData(tenantId, get_friends(tenantId, currentUserId), RealTimeAction.IsOnline, userJson);
            }

            return(base.OnConnected());
        }
Exemplo n.º 4
0
        public void Configuration(IAppBuilder app)
        {
            RVScheduler.run_jobs();

            // Any connection or hub wire up and configuration should go here
            if (RaaiVanSettings.RealTime(null))
            {
                app.MapSignalR("/signalr", new HubConfiguration());
            }
            else
            {
                app.Map("/signalr", conf => {
                    conf.Use((context, next) =>
                    {
                        ParamsContainer paramsContainer = new ParamsContainer(HttpContext.Current);
                        paramsContainer.file_response("var daslkdjhalskfh84t94uthgk = {\"Message\":\"-)\"};",
                                                      "empty.js", "text/javascript", isAttachment: false);
                        return(next());
                    });
                });
            }

            //Ignore SSL certificate check for web requests
            ServicePointManager.ServerCertificateValidationCallback = delegate(object s,
                                                                               X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
            {
                return(true);
            };
            //end of Ignore SSL certificate check for web requests

            app.Map("/ui", spa => {
                spa.Use((context, next) => {
                    context.Request.Path = new PathString("/ui/build/index.html");
                    return(next());
                });

                spa.UseStaticFiles();
            });

            ConfigureAuth(app);
        }
Exemplo n.º 5
0
        protected void send_message(Guid?forwardedFrom, string title, string messageText, bool?isGroup,
                                    List <Guid> receiverUserIds, Guid?threadId, List <DocFileInfo> attachedFiles,
                                    Guid?groupId, string _ref, ref string responseText)
        {
            //GroupID will not be stored in the database and is just used for chat

            //Privacy Check: OK
            if (!paramsContainer.GBEdit)
            {
                return;
            }

            if (!string.IsNullOrEmpty(title) && title.Length > 490)
            {
                responseText = "{\"ErrorText\":\"" + Messages.MaxAllowedInputLengthExceeded + "\"}";
                return;
            }
            else if (!PublicMethods.is_secure_title(title))
            {
                responseText = "{\"ErrorText\":\"" + Messages.TheTextIsFormattedBadly + "\"}";
                return;
            }

            if (!isGroup.HasValue)
            {
                isGroup = false;
            }

            Guid messageId = Guid.NewGuid();

            isGroup = receiverUserIds.Count == 1 ? false : isGroup;

            bool selfChat = !isGroup.Value && (
                (receiverUserIds.Count == 1 && receiverUserIds.First() == paramsContainer.CurrentUserID.Value) ||
                (threadId == paramsContainer.CurrentUserID)
                );

            if (!threadId.HasValue && ((isGroup.Value && receiverUserIds.Count > 1) || (!isGroup.Value && receiverUserIds.Count == 1)))
            {
                threadId = isGroup.Value ? Guid.NewGuid() : (receiverUserIds.Count == 1 ? receiverUserIds.First() : threadId);
            }

            if (attachedFiles != null)
            {
                attachedFiles.ForEach(f => f.move(paramsContainer.Tenant.Id, FolderNames.TemporaryFiles, FolderNames.Attachments));
            }

            long result = MSGController.send_message(paramsContainer.Tenant.Id, messageId, forwardedFrom,
                                                     paramsContainer.CurrentUserID.Value, title, messageText, isGroup.Value, receiverUserIds, threadId, attachedFiles);

            if (result <= 0 && attachedFiles != null)
            {
                attachedFiles.ForEach(f => f.move(paramsContainer.Tenant.Id, FolderNames.Attachments, FolderNames.TemporaryFiles));
            }

            List <User> receiverUsers;
            User        senderUser = UsersController.get_user(paramsContainer.Tenant.Id, paramsContainer.CurrentUserID.Value);

            responseText = result <= 0 ? "{\"ErrorText\":\"" + Messages.OperationFailed + "\"}" :
                           "{\"Succeed\":\"" + Messages.OperationCompletedSuccessfully + "\"";

            bool sendForMany = !isGroup.Value && receiverUserIds.Count > 1;

            if (result > 0 && !sendForMany)
            {
                int msgCount = 0, sentCount = 0, notSeenCount = 0;

                MSGController.get_thread_info(paramsContainer.Tenant.Id, paramsContainer.CurrentUserID.Value,
                                              threadId.Value, ref msgCount, ref sentCount, ref notSeenCount);

                receiverUsers = UsersController.get_users(paramsContainer.Tenant.Id, receiverUserIds);

                responseText +=
                    ",\"Thread\":{" +
                    "\"ThreadID\":\"" + threadId.ToString() + "\"" +
                    ",\"IsGroup\":" + isGroup.ToString().ToLower() +
                    ",\"UsersCount\":" + receiverUserIds.Count.ToString().ToLower() +
                    ",\"MessagesCount\":" + msgCount.ToString() +
                    ",\"SentCount\":" + sentCount.ToString() +
                    ",\"NotSeenCount\":" + notSeenCount.ToString() +
                    ",\"Users\":[" + ProviderUtil.list_to_string <string>(receiverUsers.Select(
                                                                              u => "{\"UserID\":\"" + u.UserID.ToString() + "\"" +
                                                                              ",\"UserName\":\"" + Base64.encode(u.UserName) + "\"" +
                                                                              ",\"FirstName\":\"" + Base64.encode(u.FirstName) + "\"" +
                                                                              ",\"LastName\":\"" + Base64.encode(u.LastName) + "\"" +
                                                                              ",\"ProfileImageURL\":\"" + DocumentUtilities.get_personal_image_address(
                                                                                  paramsContainer.Tenant.Id, u.UserID.Value) + "\"" +
                                                                              "}").ToList()) +
                    "]" +
                    "}";

                if (threadId == senderUser.UserID || (receiverUserIds != null && receiverUserIds.Contains(senderUser.UserID.Value)))
                {
                    responseText += ",\"SenderIsReceiver\":" + true.ToString().ToLower();
                }

                for (int i = 0; i < attachedFiles.Count; ++i)
                {
                    attachedFiles[i].OwnerID = messageId;
                }

                responseText +=
                    ",\"Message\":{" +
                    "\"ID\":" + result.ToString() +
                    ",\"ReceiverID\":" + (result + 1).ToString() +
                    ",\"MessageID\":\"" + messageId.ToString() + "\"" +
                    ",\"ThreadID\":\"" + threadId.Value.ToString() + "\"" +
                    ",\"ForwardedFrom\":\"" + (!forwardedFrom.HasValue ? "" : forwardedFrom.ToString()) + "\"" +
                    ",\"IsGroup\":" + isGroup.ToString().ToLower() +
                    ",\"GroupID\":\"" + (groupId.HasValue && groupId.HasValue ? groupId.Value : Guid.NewGuid()).ToString() + "\"" +
                    ",\"SelfChat\":" + selfChat.ToString().ToLower() +
                    ",\"IsSender\":" + true.ToString().ToLower() +
                    ",\"Seen\":" + true.ToString().ToLower() +
                    ",\"Title\":\"" + string.Empty + "\"" +
                    ",\"MessageText\":\"" + Base64.encode(messageText) + "\"" +
                    ",\"SendDate\":\"" + PublicMethods.get_local_date(DateTime.Now, true) + "\"" +
                    ",\"SenderUserID\":\"" + senderUser.UserID.ToString() + "\"" +
                    ",\"SenderUserName\":\"" + Base64.encode(senderUser.UserName) + "\"" +
                    ",\"SenderFirstName\":\"" + Base64.encode(senderUser.FirstName) + "\"" +
                    ",\"SenderLastName\":\"" + Base64.encode(senderUser.LastName) + "\"" +
                    ",\"ProfileImageURL\":\"" + DocumentUtilities.get_personal_image_address(
                        paramsContainer.Tenant.Id, senderUser.UserID.Value) + "\"" +
                    ",\"AttachedFiles\":" + DocumentUtilities.get_files_json(paramsContainer.Tenant.Id, attachedFiles, true) +
                    ",\"Ref\":\"" + (string.IsNullOrEmpty(_ref) ? string.Empty : _ref) + "\"" +
                    "}";
            }

            responseText += "}";

            //Send RealTime Data
            if (result > 0 && RaaiVanSettings.RealTime(paramsContainer.Tenant.Id) && !sendForMany)
            {
                List <Guid> userIds = new List <Guid>();

                if (!isGroup.Value)
                {
                    userIds = new List <Guid>()
                    {
                        threadId.Value, senderUser.UserID.Value
                    }
                }
                ;
                else
                {
                    userIds = receiverUserIds.Count > 0 ? receiverUserIds :
                              MSGController.get_thread_users(paramsContainer.Tenant.Id, threadId.Value,
                                                             paramsContainer.CurrentUserID.Value, 1000, null).Select(u => u.UserID.Value).ToList();

                    if (receiverUserIds.Count > 0)
                    {
                        userIds.Add(senderUser.UserID.Value);
                    }
                }

                if (userIds.Count > 0)
                {
                    RaaiVanHub.SendData(paramsContainer.Tenant.Id, userIds,
                                        RaaiVanHub.RealTimeAction.NewMessage, responseText);
                }
            }
            //end of Send RealTime Data
        }