Exemplo n.º 1
0
        public async Task <List <string> > SubscribeHives(List <string> hives)
        {
            var results = new List <string>();

            if (hives.IsNullOrEmpty())
            {
                return(results);
            }

            var handle         = GetUserHandle();
            var throttleResult = await ThrottleStore.CheckThrottle(ThrottleAction.SubscribeHives, handle, IsAuthenticated);

            if (throttleResult.ThrottleRequest)
            {
                await SendErrorMessage("Rate Limit", throttleResult.Message);

                return(results);
            }

            foreach (var hive in hives)
            {
                var hiveName = HiveValidation.GetHiveName(hive, true);
                if (string.IsNullOrEmpty(hiveName))
                {
                    continue;
                }

                await Subscribe(handle, hiveName);

                results.Add(hiveName);
            }
            return(results);
        }
Exemplo n.º 2
0
        public async Task <IEnumerable <object> > GetTrending()
        {
            var handle         = GetUserHandle();
            var throttleResult = await ThrottleStore.CheckThrottle(ThrottleAction.JoinHive, handle, IsAuthenticated);

            if (throttleResult.ThrottleRequest)
            {
                await SendErrorMessage("Rate Limit", throttleResult.Message);

                return(Enumerable.Empty <object>());
            }
            return(await ConnectionStore.GetPopularHives(15));
        }
Exemplo n.º 3
0
        public async Task <object> SubscribeFollowers(List <string> followers)
        {
            var handle         = GetUserHandle();
            var throttleResult = await ThrottleStore.CheckThrottle(ThrottleAction.SubscribeFollowers, handle, IsAuthenticated);

            if (throttleResult.ThrottleRequest)
            {
                return(await SendErrorMessage("Rate Limit", throttleResult.Message));
            }

            var following = await FollowerStore.GetFollowers(handle);

            if (followers.IsNullOrEmpty())
            {
                return new
                       {
                           Followers = following,
                           Following = new List <string>()
                       }
            }
            ;

            var result = new List <string>();

            foreach (var follower in followers.Select(x => x.Trim()).Distinct())
            {
                if (!HiveValidation.ValidateUserHandle(follower))
                {
                    continue;
                }

                await FollowerStore.FollowHandle(handle, follower);

                await SendFollowUpdate(follower, handle, false);

                result.Add(follower);
            }

            return(new
            {
                Followers = following,
                Following = result
            });
        }
Exemplo n.º 4
0
        public async Task <bool> LeaveHive(string hive)
        {
            var hiveName = HiveValidation.GetHiveName(hive, true);

            if (string.IsNullOrEmpty(hiveName))
            {
                return(await SendErrorMessage("Validation Error", "Invalid Hive name"));
            }

            var handle         = GetUserHandle();
            var throttleResult = await ThrottleStore.CheckThrottle(ThrottleAction.LeaveHive, handle, IsAuthenticated);

            if (throttleResult.ThrottleRequest)
            {
                return(await SendErrorMessage("Rate Limit", throttleResult.Message));
            }

            await Unsubscribe(handle, hiveName);

            return(true);
        }
Exemplo n.º 5
0
        public async Task <bool> UnfollowUser(string userToUnfollow)
        {
            if (!HiveValidation.ValidateUserHandle(userToUnfollow))
            {
                return(await SendErrorMessage("Validation Error", "Invalid name format"));
            }

            var handle         = GetUserHandle();
            var throttleResult = await ThrottleStore.CheckThrottle(ThrottleAction.UnfollowUser, handle, IsAuthenticated);

            if (throttleResult.ThrottleRequest)
            {
                return(await SendErrorMessage("Rate Limit", throttleResult.Message));
            }

            await FollowerStore.UnfollowHandle(handle, userToUnfollow);

            await SendFollowUpdate(userToUnfollow, handle, true);

            return(true);
        }
Exemplo n.º 6
0
        public async Task <bool> SendMessage(string sender, string messageInput, string hiveTargets)
        {
            var senderId       = GetUserHandle();
            var senderName     = GetSenderName(sender, senderId);
            var throttleResult = await ThrottleStore.CheckThrottle(ThrottleAction.SendMessage, senderId, IsAuthenticated);

            if (throttleResult.ThrottleRequest)
            {
                return(await SendErrorMessage("Rate Limit", throttleResult.Message));
            }

            var message   = HiveValidation.ValidateMessage(messageInput);
            var timestamp = DateTime.UtcNow.ToJavaTime();
            var hives     = HiveValidation.GetHives(message, hiveTargets);
            var messageId = HiveValidation.GetMessageId(senderId, message, timestamp);

            var hivers = await ConnectionStore.GetHiveUsers(hives);

            var mentions  = HiveValidation.GetMentions(message);
            var followers = await GetFollowers(senderId);

            var messages = new Dictionary <string, HiveMessage>();

            // Messages to users in hives
            if (!hivers.IsNullOrEmpty())
            {
                foreach (var userHandle in hivers)
                {
                    if (messages.ContainsKey(userHandle))
                    {
                        continue;
                    }

                    messages.Add(userHandle, new HiveMessage
                    {
                        Id               = messageId,
                        Sender           = senderId,
                        SenderName       = senderName,
                        IsSenderVerified = IsAuthenticated,
                        Message          = message,
                        Timestamp        = timestamp,
                        Hives            = new HashSet <string>(hives),
                        MessageType      = new HashSet <string> {
                            "Feed"
                        }
                    });
                }
            }

            // Messages to users mentioned in message
            if (!mentions.IsNullOrEmpty())
            {
                foreach (var userHandle in mentions)
                {
                    if (messages.ContainsKey(userHandle))
                    {
                        messages[userHandle].MessageType.Add("Mention");
                        continue;
                    }

                    messages.Add(userHandle, new HiveMessage
                    {
                        Id               = messageId,
                        Sender           = senderId,
                        SenderName       = senderName,
                        IsSenderVerified = IsAuthenticated,
                        Message          = message,
                        Timestamp        = timestamp,
                        Hives            = new HashSet <string>(hives),
                        MessageType      = new HashSet <string> {
                            "Mention"
                        }
                    });
                }
            }

            // Messages to anyone following the sender
            if (!followers.IsNullOrEmpty())
            {
                foreach (var userHandle in followers)
                {
                    if (messages.ContainsKey(userHandle))
                    {
                        messages[userHandle].MessageType.Add("Follow");
                        continue;
                    }

                    messages.Add(userHandle, new HiveMessage
                    {
                        Id               = messageId,
                        Sender           = senderId,
                        SenderName       = senderName,
                        IsSenderVerified = IsAuthenticated,
                        Message          = message,
                        Timestamp        = timestamp,
                        Hives            = new HashSet <string>(hives),
                        MessageType      = new HashSet <string> {
                            "Follow"
                        }
                    });
                }
            }

            foreach (var item in messages)
            {
                await Clients.User(item.Key).OnMessage(item.Value);
            }
            return(true);
        }