Пример #1
0
        public static void Handle(Action.Context context)
        {
            // @ToDo: Check if status is valid
            // Deserialize message
            var changeStatusMessage = JsonConvert.DeserializeObject <Action.User.ChangeStatus>(context.Message);

            // Decrypt status
            var status = changeStatusMessage.Status.DecryptByTransaction(context.Transaction)?.ToUtf8String();

            if (status == null)
            {
                context.Logger.Log.LogDebug(Logger.InvalidBase64Format, "Invalid Base64 Format!");
                return;
            }

            // Get chat
            var user = context.DatabaseContext.GetUserAsync(context.Sender).GetAwaiter().GetResult();

            if (user == null)
            {
                return;
            }

            context.Logger.Log.LogDebug(Logger.HandleUserChangeStatus,
                                        "UserChangeStatus: Sender '{Address}' Status: '{Status}'!",
                                        context.Sender, status);

            user.Status          = status;
            user.StatusUpdatedAt = DateTimeOffset.FromUnixTimeMilliseconds(context.Transaction.RawData.Timestamp).DateTime;
            context.DatabaseContext.SaveChanges();

            // Notify everyone that knows this user
            context.ServiceProvider.GetService <FCMClient>()?.UpdateAddress(user);
        }
        public static void Handle(Action.Context context)
        {
            // @ToDo: Check if picture url is valid
            // Deserialize message
            var changeProfilePictureMessage = JsonConvert.DeserializeObject <Action.User.ChangeProfilePicture>(context.Message);

            // Decrypt image
            var profilePictureUrl = (changeProfilePictureMessage.Clear ? null : changeProfilePictureMessage.Image.DecryptByTransaction(context.Transaction)?.ToUtf8String());

            if (!changeProfilePictureMessage.Clear && profilePictureUrl == null)
            {
                context.Logger.Log.LogDebug(Logger.InvalidBase64Format, "Invalid Base64 Format!");
                return;
            }

            // Get user
            var user = context.DatabaseContext.GetUserAsync(context.Sender).GetAwaiter().GetResult();

            if (user == null)
            {
                return;
            }

            context.Logger.Log.LogDebug(Logger.HandleUserChangeProfilePicture,
                                        "UserChangeProfilePicture: Sender '{Address}' Image: '{Image}' Clear: {Clear}!",
                                        context.Sender, profilePictureUrl, changeProfilePictureMessage.Clear);

            user.ProfilePictureUrl = profilePictureUrl;
            context.DatabaseContext.SaveChanges();

            // Notify everyone that knows this user
            context.ServiceProvider.GetService <FCMClient>()?.UpdateAddress(user);
        }
Пример #3
0
        public static void Handle(Action.Context context)
        {
            // @ToDo: Check if nickname is valid
            // Deserialize message
            var changeNicknameMessage = JsonConvert.DeserializeObject <Action.User.ChangeNickname>(context.Message);

            // Decrypt nickname
            var nickname = changeNicknameMessage.Name.DecryptByTransaction(context.Transaction)?.ToUtf8String();

            if (nickname == null)
            {
                context.Logger.Log.LogDebug(Logger.InvalidBase64Format, "Invalid Base64 Format!");
                return;
            }

            // Get user
            var user = context.DatabaseContext.GetUserAsync(context.Sender).GetAwaiter().GetResult();

            if (user == null)
            {
                return;
            }

            context.Logger.Log.LogDebug(Logger.HandleUserChangeNickname,
                                        "UserChangeNickname: Sender '{Address}' Name: '{Name}'!",
                                        context.Sender, nickname);

            user.Nickname = nickname;
            context.DatabaseContext.SaveChanges();

            // Notify everyone that knows this user
            context.ServiceProvider.GetService <FCMClient>()?.UpdateAddress(user);
        }
Пример #4
0
        public static void Handle(Action.Context context)
        {
            // Deserialize send message
            var userSendMessage = JsonConvert.DeserializeObject <Action.User.SendMessage>(context.Message);

            context.Logger.Log.LogDebug(Logger.HandleUserSendMessage,
                                        "SendMessage: Sender '{Sender}' Receiver '{Receiver}'!",
                                        context.Sender, context.Receiver);

            // Get user models from sender and receiver
            var senderUser = context.DatabaseContext.GetUserAsync(context.Sender, users => users.Include(u => u.DeviceIds))
                             .GetAwaiter().GetResult();
            var receiverUser = context.DatabaseContext.GetUserAsync(context.Receiver, users => users.Include(u => u.DeviceIds))
                               .GetAwaiter().GetResult();

            if (senderUser == null || receiverUser == null)
            {
                context.Logger.Log.LogDebug(Logger.HandleUserSendMessage,
                                            "SendMessage: Sender or receiver does not exist as user! Does exist: Sender({SenderUser}), Receiver({ReceiverUser}).",
                                            senderUser != null, receiverUser != null);
                return;
            }

            // Get chat or create if not exist
            var chat = context.DatabaseContext.GetChatAsync(context.Sender, context.Receiver).GetAwaiter().GetResult() ??
                       context.DatabaseContext.CreateChat(senderUser, receiverUser, context.Transaction.RawData.Timestamp);

            // Get last sent message id
            var chatMessages          = context.DatabaseContext.ChatMessages.Where(cm => cm.Chat == chat);
            var lastMessageInternalId = (chatMessages.Any() ? chatMessages.Max(cm => cm.InternalId) : 0);

            // @ToDo: Combine with SendMessage from Groups
            // Create new chat message
            var chatMessage = new Models.ChatMessage()
            {
                InternalId = lastMessageInternalId + 1,
                Chat       = chat,

                User    = senderUser,
                Address = context.Sender,
                Message = userSendMessage.Message,

                BlockId        = context.Block.BlockHeader.RawData.Number,
                BlockCreatedAt = DateTimeOffset.FromUnixTimeMilliseconds(context.Block.BlockHeader.RawData.Timestamp).DateTime,

                TransactionHash      = context.TransactionHash,
                TransactionCreatedAt = DateTimeOffset.FromUnixTimeMilliseconds(context.Transaction.RawData.Timestamp).DateTime,

                CreatedAt = DateTime.UtcNow,
            };

            context.DatabaseContext.ChatMessages.Add(chatMessage);
            context.DatabaseContext.SaveChanges();

            context.ServiceProvider.GetService <FCMClient>()?.NotifyUserMessage(senderUser, receiverUser, chatMessage);
        }
Пример #5
0
        public static void Handle(Action.Context context)
        {
            // Deserialize message
            var kickUserMessage = JsonConvert.DeserializeObject <Action.Group.KickUser>(context.Message);

            // Decrypt kick address
            var kickAddress = kickUserMessage.Address.DecryptByTransaction(context.Transaction)
                              ?.ToUtf8String();

            if (kickAddress == null || Client.WalletAddress.Decode58Check(kickAddress) == null || kickAddress == context.Receiver)
            {
                context.Logger.Log.LogDebug(Logger.InvalidBase64Format, "Invalid Base64 Format!");
                return;
            }

            context.Logger.Log.LogDebug(Logger.HandleGroupKickUser,
                                        "UserKickedFromGroup: Sender '{Address}' GroupOwnerAddress '{OwnerAddress}' KickedUser '{KickedUser}'!",
                                        context.Sender, context.Receiver, kickAddress);

            // Get chat
            var chat = context.DatabaseContext
                       .GetChatAsync(context.Receiver, chats => chats.Include(c => c.Setting).Include(c => c.Users))
                       .GetAwaiter().GetResult();

            if (chat == null)
            {
                context.Logger.Log.LogDebug(Logger.HandleGroupKickUser, "Chat not found!");
                return;
            }

            // Get user that get kicked
            var kickChatUser = chat.GetUser(kickAddress);

            if (kickChatUser == null)
            {
                context.Logger.Log.LogDebug(Logger.HandleGroupKickUser,
                                            "User not found in chat.");
                return;
            }

            // Get user that wants to kick
            var senderChatUser = chat.GetUser(context.Sender);

            if (kickChatUser.Rank >= senderChatUser.Rank)
            {
                context.Logger.Log.LogDebug(Logger.HandleGroupKickUser,
                                            "User rank is too high");
                return;
            }

            context.DatabaseContext.ChatUsers.Remove(kickChatUser);
            context.DatabaseContext.SaveChanges();

            // Notify all members in group
            context.ServiceProvider.GetService <FCMClient>()?.RemoveUserFromGroup(chat, kickChatUser);
        }
Пример #6
0
        public static void Handle(Action.Context context)
        {
            // Deserialize message
            var joinMessage = JsonConvert.DeserializeObject <Action.Group.Join>(context.Message);

            context.Logger.Log.LogDebug(Logger.HandleGroupJoin,
                                        "GroupJoin: Sender '{Address}' GroupOwnerAddress '{OwnerAddress}'!",
                                        context.Sender, context.Receiver);

            // Get chat
            var chat = context.DatabaseContext
                       .GetChatAsync(context.Receiver, chats => chats.Include(c => c.Setting).Include(c => c.Users))
                       .GetAwaiter().GetResult();

            if (chat == null)
            {
                context.Logger.Log.LogDebug(Logger.HandleGroupJoin, "Chat not found!");
                return;
            }

            // Can only join when chat is public
            if (!chat.Setting.IsPublic)
            {
                context.Logger.Log.LogDebug(Logger.HandleGroupJoin, "Chat not public.");
                return;
            }

            // Check if is already in group
            if (chat.HasUser(context.Sender))
            {
                context.Logger.Log.LogDebug(Logger.HandleGroupJoin, "User already in chat.");
                return;
            }

            // Add User to Group
            var joinedUser     = context.DatabaseContext.GetUserAsync(context.Sender, users => users.Include(u => u.DeviceIds)).GetAwaiter().GetResult();
            var joinedChatUser = context.DatabaseContext.CreateChatUser(chat,
                                                                        new DatabaseContext.ChatUserSetting()
            {
                User       = joinedUser,
                PrivateKey = null,
                Rank       = ChatUserRank.User
            },
                                                                        context.Transaction.RawData.Timestamp);

            context.DatabaseContext.SaveChanges();

            // Notify all members
            context.ServiceProvider.GetService <FCMClient>()?.AddUserToGroup(chat, joinedChatUser);
        }
Пример #7
0
        public static void Handle(Action.Context context)
        {
            // Deserialize message
            var userAddDeviceIdMessage = JsonConvert.DeserializeObject <Action.User.AddDeviceId>(context.Message);

            // Decrypt device id
            var deviceId = userAddDeviceIdMessage.DeviceId.DecryptByTransaction(context.Transaction)?.ToUtf8String();

            if (deviceId == null)
            {
                context.Logger.Log.LogDebug(Logger.InvalidBase64Format, "Invalid Base64 Format!");
                return;
            }

            // ToDo: Check deviceId

            context.Logger.Log.LogDebug(Logger.HandleUserSendMessage,
                                        "AddDeviceId: Sender '{Sender} DeviceId {DeviceId}'!",
                                        context.Sender, deviceId);

            // Get user id to search if device id is already registered
            var senderUser =
                context.DatabaseContext.GetUserAsync(context.Sender, users => users.Include(u => u.DeviceIds)).GetAwaiter().GetResult();
            var userDeviceId = senderUser.DeviceIds.Find(u => u.DeviceId == deviceId);

            if (userDeviceId == null)
            {
                userDeviceId = new Models.UserDeviceId()
                {
                    User      = senderUser,
                    DeviceId  = deviceId,
                    UpdatedAt = DateTime.UtcNow,
                    CreatedAt = DateTime.UtcNow,
                };

                context.DatabaseContext.UserDeviceIds.Add(userDeviceId);
            }
            else
            {
                userDeviceId.UpdatedAt = DateTime.UtcNow;
            }

            context.DatabaseContext.SaveChanges();
        }
Пример #8
0
        public static void Handle(Action.Context context)
        {
            // @ToDo: Check if name is valid
            // Deserialize message
            var changeNameMessage = JsonConvert.DeserializeObject <Action.Group.ChangeName>(context.Message);

            // Get encrypted groupName
            var groupName = changeNameMessage.Name.DecryptByTransaction(context.Transaction)?.ToUtf8String();

            if (groupName == null)
            {
                context.Logger.Log.LogDebug(Logger.InvalidBase64Format, "Invalid Base64 Format!");
                return;
            }

            context.Logger.Log.LogDebug(Logger.HandleGroupChangeName, "ChangeGroupName: Sender '{Address}' GroupOwnerAddress '{OwnerAddress}' Name '{GroupName}'!",
                                        context.Sender, context.Receiver, groupName);

            // Get chat
            var chat = context.DatabaseContext.GetChatAsync(context.Receiver, chats => chats.Include(c => c.Setting).Include(c => c.Users)).GetAwaiter().GetResult();

            if (chat?.Setting == null)
            {
                context.Logger.Log.LogDebug(Logger.HandleGroupChangeName, "Chat not found!");
                return;
            }

            // Check user rank
            if (!chat.IsUserAdminOrHigher(context.Sender))
            {
                context.Logger.Log.LogDebug(Logger.HandleGroupChangeName,
                                            "User in Chat not found or doesn't have the permissions.");
                return;
            }

            // Change name and notify all members
            if (chat.Setting.Name != groupName)
            {
                chat.Setting.Name = groupName;
                context.DatabaseContext.SaveChanges();

                context.ServiceProvider.GetService <FCMClient>()?.UpdateGroupAddress(chat);
            }
        }
Пример #9
0
        public static void Handle(Action.Context context)
        {
            // Deserialize message
            var blockUserMessage = JsonConvert.DeserializeObject <Action.User.BlockUser>(context.Message);
            var blockedUser      = context.DatabaseContext.GetBlockedUser(context.Sender, blockUserMessage.Address);

            context.Logger.Log.LogDebug(Logger.HandleUserBlockedUser, "BlockUser: Sender '{Address}' Blocked: '{BlockedAddress}' IsBlocked: {IsBlocked}!",
                                        context.Sender, blockUserMessage.Address, blockUserMessage.IsBlocked);

            // Get user
            var user = context.DatabaseContext.GetUserAsync(context.Sender).GetAwaiter().GetResult();

            if (user == null)
            {
                context.Logger.Log.LogDebug(Logger.HandleUserBlockedUser, "BlockUser: sender does not exist as user!");
                return;
            }

            // Check if wants to block or unblock
            if (blockUserMessage.IsBlocked)
            {
                if (blockedUser != null)
                {
                    return;
                }

                blockedUser = new Models.BlockedUser()
                {
                    User           = user,
                    Address        = context.Sender,
                    BlockedAddress = blockUserMessage.Address,
                    CreatedAt      = DateTime.UtcNow,
                };

                context.DatabaseContext.BlockedUsers.Add(blockedUser);
                context.DatabaseContext.SaveChanges();
            }
            else if (blockedUser != null)
            {
                context.DatabaseContext.Remove(blockUserMessage);
                context.DatabaseContext.SaveChanges();
            }
        }
Пример #10
0
        public static void Handle(Action.Context context)
        {
            // Deserialize message
            var leaveMessage = JsonConvert.DeserializeObject <Action.Group.Leave>(context.Message);

            context.Logger.Log.LogDebug(Logger.HandleGroupLeave,
                                        "GroupLeave: Sender '{Address}' GroupOwnerAddress '{OwnerAddress}'!",
                                        context.Sender, context.Receiver);

            // Get chat
            var chat = context.DatabaseContext
                       .GetChatAsync(context.Receiver, chats => chats.Include(c => c.Setting).Include(c => c.Users))
                       .GetAwaiter().GetResult();

            if (chat == null)
            {
                context.Logger.Log.LogDebug(Logger.HandleGroupLeave, "Chat not found!");
                return;
            }

            // Check if is in group
            var leftChatUser = chat.GetUser(context.Sender);

            if (leftChatUser == null)
            {
                context.Logger.Log.LogDebug(Logger.HandleGroupLeave, "User not found.");
                return;
            }

            // Remove User from Group
            context.DatabaseContext.ChatUsers.Remove(leftChatUser);
            context.DatabaseContext.SaveChanges();

            // Notify all members
            context.ServiceProvider.GetService <FCMClient>()?.RemoveUserFromGroup(chat, leftChatUser);
        }
Пример #11
0
        public static void Handle(Action.Context context)
        {
            // Deserialize message
            var setUserRankMessage = JsonConvert.DeserializeObject <Action.Group.SetUserRank>(context.Message);

            // Decrypt rank address
            var setRankAddress = setUserRankMessage.Address.DecryptByTransaction(context.Transaction)
                                 ?.ToUtf8String();

            if (setRankAddress == null || Client.WalletAddress.Decode58Check(setRankAddress) == null || setRankAddress == context.Receiver)
            {
                context.Logger.Log.LogDebug(Logger.InvalidBase64Format, "Invalid Base64 Format!");
                return;
            }

            // Check if rank is valid and not owner
            if (Enum.IsDefined(typeof(ChatUserRank), setUserRankMessage.Rank) || setUserRankMessage.Rank == ChatUserRank.Owner)
            {
                context.Logger.Log.LogDebug(Logger.HandleGroupSetUserRank, "Rank invalid/Owner not settable!");
                return;
            }

            context.Logger.Log.LogDebug(Logger.HandleGroupSetUserRank,
                                        "SetUserRankGroup: Sender '{Address}' GroupOwnerAddress '{OwnerAddress}' UserSetRank '{UserSetRank}' Rank '{Rank}'!",
                                        context.Sender, context.Receiver, setRankAddress, setUserRankMessage.Rank);

            // Get chat
            var chat = context.DatabaseContext
                       .GetChatAsync(context.Receiver, chats => chats.Include(c => c.Setting).Include(c => c.Users))
                       .GetAwaiter().GetResult();

            if (chat == null)
            {
                context.Logger.Log.LogDebug(Logger.HandleGroupSetUserRank, "Chat not found!");
                return;
            }

            // Get sender and set-rank-user
            var senderChatUser  = chat.GetUser(context.Sender);
            var setRankChatUser = chat.GetUser(setRankAddress);

            if (senderChatUser == null || setRankChatUser == null)
            {
                context.Logger.Log.LogDebug(Logger.HandleGroupSetUserRank,
                                            "User not in chat.");
                return;
            }

            // Only owner can set ranks
            if (senderChatUser.Rank != ChatUserRank.Owner)
            {
                context.Logger.Log.LogDebug(Logger.HandleGroupSetUserRank,
                                            "User rank not high enough.");
                return;
            }

            setRankChatUser.Rank = setUserRankMessage.Rank;
            context.DatabaseContext.SaveChanges();

            // Notify all members in group
            context.ServiceProvider.GetService <FCMClient>()?.SetUserGroupRank(chat, setRankChatUser);
        }
Пример #12
0
        public static void Handle(Action.Context context)
        {
            // Deserialize message
            var addUserMessage = JsonConvert.DeserializeObject <Action.Group.AddUser>(context.Message);

            // Decrypt invite address
            var inviteAddress = addUserMessage.Address.DecryptByTransaction(context.Transaction)
                                ?.ToUtf8String();

            if (inviteAddress == null || Client.WalletAddress.Decode58Check(inviteAddress) == null || inviteAddress == context.Receiver)
            {
                context.Logger.Log.LogDebug(Logger.InvalidBase64Format, "Invalid Base64 Format!");
                return;
            }

            context.Logger.Log.LogDebug(Logger.HandleGroupAddUser,
                                        "AddUserToGroup: Sender '{Address}' GroupOwnerAddress '{OwnerAddress}' InvitedUser '{InvitedUser}'!",
                                        context.Sender, context.Receiver, inviteAddress);

            // Get chat
            var chat = context.DatabaseContext
                       .GetChatAsync(context.Receiver, chats => chats.Include(c => c.Setting).Include(c => c.Users))
                       .GetAwaiter().GetResult();

            if (chat == null)
            {
                context.Logger.Log.LogDebug(Logger.HandleGroupAddUser, "Chat not found!");
                return;
            }

            // When chat is public everyone can invite, else inviter has to be admin or higher
            if (!chat.Setting.IsPublic && !chat.IsUserAdminOrHigher(context.Sender))
            {
                context.Logger.Log.LogDebug(Logger.HandleGroupAddUser,
                                            "User in Chat not found or doesn't have the permissions.");
                return;
            }

            // Check if is already in group
            if (chat.HasUser(inviteAddress))
            {
                context.Logger.Log.LogDebug(Logger.HandleGroupAddUser,
                                            "User already in chat.");
                return;
            }

            // Add User to Group
            var invitedUser     = context.DatabaseContext.CreateUserWhenNotExist(inviteAddress, users => users.Include(u => u.DeviceIds));
            var invitedChatUser = context.DatabaseContext.CreateChatUser(chat,
                                                                         new DatabaseContext.ChatUserSetting()
            {
                User       = invitedUser,
                PrivateKey = addUserMessage.PrivateKey,
                Rank       = ChatUserRank.User
            },
                                                                         context.Transaction.RawData.Timestamp);

            context.DatabaseContext.SaveChanges();

            // Notify all members
            context.ServiceProvider.GetService <FCMClient>()?.AddUserToGroup(chat, invitedChatUser);
        }
Пример #13
0
        public void Handle(TransactionExtention transaction, BlockExtention block, System.IServiceProvider serviceProvider, ref int converseTransactionCounter)
        {
            // Parse transaction contract data
            if (transaction.Transaction.RawData.Contract.Count <= 0)
            {
                return;
            }

            var contract = transaction.Transaction.RawData.Contract[0];

            if (contract.Type != Protocol.Transaction.Types.Contract.Types.ContractType.TransferAssetContract)
            {
                return;
            }

            var transferAssetContract = contract.Parameter.Unpack <TransferAssetContract>();

            if (transferAssetContract.AssetName.ToStringUtf8() != (unchecked ((ulong)block.BlockHeader.RawData.Number) <=
                                                                   _blockConfiguration.SyncUntilBlockWithTokenName ? _token.Name : _token.Id))
            {
                return;
            }

            converseTransactionCounter++;

            // Get the TRON-Public Address
            var senderAddress   = Utils.Address.FromByteString(transferAssetContract.OwnerAddress);
            var receiverAddress = Utils.Address.FromByteString(transferAssetContract.ToAddress);

            var senderUser = DatabaseContext.CreateUsersWhenNotExist(new[] { senderAddress, receiverAddress }).Find(u => u.Address == senderAddress);

            if (string.IsNullOrEmpty(senderUser.PublicKey))
            {
                var publicKey = transaction.Transaction.GetPublicKey();
                if (publicKey != null)
                {
                    senderUser.PublicKey = publicKey.EncodeBase64();
                }
            }
            DatabaseContext.SaveChanges();

            // Get message + transactionHash
            var transactionHash = Crypto.Sha256.Hash(transaction.Transaction.RawData.ToByteArray())
                                  .ToHexString()
                                  .ToLower();

            var message = transaction.Transaction.RawData.Data.ToStringUtf8();

            _logger.Log.LogDebug(Logger.NewTransaction, "Handle new Transaction with Hash '{TransactionHash}'!", transactionHash);

            try
            {
                var action = JsonConvert.DeserializeObject <Action.Action>(message);
                if (action == null)
                {
                    return;
                }

                _logger.Log.LogDebug(Logger.NewTransaction, "Handle Action " + action.Type.ToString() + "!");

                // Actions only valid when sent to propertyAddress
                if (Action.Constants.PropertyAddressTypes.Contains(action.Type))
                {
                    if (receiverAddress != Singleton.WalletClient.WalletClient.PropertyAddress?.Address)
                    {
                        _logger.Log.LogDebug(Logger.ActionPropertyAddressInvalid, "This Action needs PropertyAddress as receiver!");
                        return;
                    }
                }

                var context = new Action.Context()
                {
                    Sender   = senderAddress,
                    Receiver = receiverAddress,
                    Message  = message,

                    Transaction     = transaction.Transaction,
                    TransactionHash = transactionHash,
                    Block           = block,

                    ServiceProvider = serviceProvider,
                    DatabaseContext = DatabaseContext,
                    Logger          = _logger,
                };

                switch (action.Type)
                {
                case Action.Type.UserChangeNickname:
                    Singleton.WalletClient.ActionHandlers.UserChangeNickname.Handle(context);
                    break;

                case Action.Type.UserChangeStatus:
                    Singleton.WalletClient.ActionHandlers.UserChangeStatus.Handle(context);
                    break;

                case Action.Type.UserChangeProfilePicture:
                    Singleton.WalletClient.ActionHandlers.UserChangeProfilePicture.Handle(context);
                    break;

                case Action.Type.UserBlockUser:
                    Singleton.WalletClient.ActionHandlers.UserBlockUser.Handle(context);
                    break;

                case Action.Type.UserSendMessage:
                    Singleton.WalletClient.ActionHandlers.UserSendMessage.Handle(context);
                    break;

                case Action.Type.UserAddDeviceId:
                    Singleton.WalletClient.ActionHandlers.UserAddDeviceId.Handle(context);
                    break;

                case Action.Type.GroupCreate:
                    Singleton.WalletClient.ActionHandlers.GroupCreate.Handle(context);
                    break;

                case Action.Type.GroupChangeName:
                    Singleton.WalletClient.ActionHandlers.GroupChangeName.Handle(context);
                    break;

                case Action.Type.GroupChangeDescription:
                    Singleton.WalletClient.ActionHandlers.GroupChangeDescription.Handle(context);
                    break;

                case Action.Type.GroupChangePicture:
                    Singleton.WalletClient.ActionHandlers.GroupChangePicture.Handle(context);
                    break;

                case Action.Type.GroupAddUser:
                    Singleton.WalletClient.ActionHandlers.GroupAddUser.Handle(context);
                    break;

                case Action.Type.GroupKickUser:
                    Singleton.WalletClient.ActionHandlers.GroupKickUser.Handle(context);
                    break;

                case Action.Type.GroupSetUserRank:
                    Singleton.WalletClient.ActionHandlers.GroupSetUserRank.Handle(context);
                    break;

                case Action.Type.GroupJoin:
                    Singleton.WalletClient.ActionHandlers.GroupJoin.Handle(context);
                    break;

                case Action.Type.GroupLeave:
                    Singleton.WalletClient.ActionHandlers.GroupLeave.Handle(context);
                    break;

                case Action.Type.GroupMessage:
                    Singleton.WalletClient.ActionHandlers.GroupMessage.Handle(context);
                    break;

                //case Action.Type.GroupSetPublic:
                //	break;
                default:
                    _logger.Log.LogDebug(Logger.InvalidActionType, "Invalid ActionType({Type})!", action.Type);
                    break;
                }
            }
            catch (Newtonsoft.Json.JsonException e)
            {
                _logger.Log.LogDebug(Logger.InvalidJsonFormat, "Could not parse JSON! Error: {Message}", e.Message);
            }
        }
Пример #14
0
        public static void Handle(Action.Context context)
        {
            // Deserialize message
            var groupMessage = JsonConvert.DeserializeObject <Action.Group.SendMessage>(context.Message);

            context.Logger.Log.LogDebug(Logger.HandleGroupMessage,
                                        "GroupMessage: Sender '{Address}' GroupOwnerAddress '{OwnerAddress}' Message '{Message}'!",
                                        context.Sender, context.Receiver, groupMessage.Message);

            // Get chat
            var chat = context.DatabaseContext
                       .GetChatAsync(context.Receiver, chats => chats.Include(c => c.Setting).Include(c => c.Users).Include(c => c.Messages))
                       .GetAwaiter().GetResult();

            if (chat == null)
            {
                context.Logger.Log.LogDebug(Logger.HandleGroupMessage, "Chat not found!");
                return;
            }

            // Check if is in group
            var senderUser = context.DatabaseContext.GetUserAsync(context.Sender).GetAwaiter().GetResult();

            if (!chat.HasUser(context.Sender))
            {
                context.Logger.Log.LogDebug(Logger.HandleGroupMessage, "User is not in chat.");
                return;
            }

            // Get last sent message id
            var lastMessageInternalId = (chat.Messages.Any() ? chat.Messages.Max(cm => cm.InternalId) : 0);

            // @ToDo: Combine with SendMessage for PrivateChats
            // Create new chat message
            var chatMessage = new Models.ChatMessage()
            {
                InternalId = lastMessageInternalId + 1,
                Chat       = chat,

                User    = senderUser,
                Address = context.Sender,
                Message = groupMessage.Message,

                BlockId        = context.Block.BlockHeader.RawData.Number,
                BlockCreatedAt = DateTimeOffset.FromUnixTimeMilliseconds(context.Block.BlockHeader.RawData.Timestamp)
                                 .DateTime,

                TransactionHash      = context.TransactionHash,
                TransactionCreatedAt = DateTimeOffset.FromUnixTimeMilliseconds(context.Transaction.RawData.Timestamp)
                                       .DateTime,

                CreatedAt = DateTime.UtcNow,
            };

            context.DatabaseContext.ChatMessages.Add(chatMessage);
            context.DatabaseContext.SaveChanges();


            // Notify all members
            context.ServiceProvider.GetService <FCMClient>()?.NotifyGroupMessage(chat, chatMessage);
        }
Пример #15
0
        public static void Handle(Action.Context context)
        {
            // Deserialize message
            var createGroupMessage = JsonConvert.DeserializeObject <Action.Group.Create>(context.Message);

            context.Logger.Log.LogDebug(Logger.HandleGroupCreate, "CreateGroup: Sender '{Address}' GroupOwnerAddress '{OwnerAddress}'!",
                                        context.Sender, createGroupMessage.Address);

            // Get public key
            var publicKey = context.Transaction.GetPublicKey();

            if (publicKey == null)
            {
                context.Logger.Log.LogDebug("Invalid PublicKey!");
                return;
            }

            // Decrypt group address
            var groupAddress = createGroupMessage.Address.DecryptByPublicKey(publicKey)?.ToUtf8String();

            if (groupAddress == null)
            {
                context.Logger.Log.LogDebug(Logger.InvalidBase64Format, "Invalid Base64 Format!");
                return;
            }

            // Decrypt group public key
            var groupPublicKey = createGroupMessage.PublicKey.DecryptByPublicKey(publicKey)?.EncodeBase64();

            if (groupPublicKey == null)
            {
                context.Logger.Log.LogDebug(Logger.InvalidBase64Format, "Invalid Base64 Format!");
                return;
            }

            // Decrypt group private key
            var publicGroupPrivateKey = (createGroupMessage.IsPublic ? createGroupMessage.PrivateKey.DecryptByPublicKey(publicKey)?.EncodeBase64() : null);

            if (createGroupMessage.IsPublic && createGroupMessage.PrivateKey == null)
            {
                context.Logger.Log.LogDebug(Logger.InvalidBase64Format, "Invalid Base64 Format!");
                return;
            }

            // Decrypt group data
            var groupName        = createGroupMessage.Name?.DecryptByPublicKey(publicKey)?.ToUtf8String();
            var groupDescription = createGroupMessage.Description?.DecryptByPublicKey(publicKey)?.ToUtf8String();
            var groupImage       = createGroupMessage.Image?.DecryptByPublicKey(publicKey)?.ToUtf8String();

            if (groupName == null)
            {
                groupName = groupAddress;
            }


            // Check if address is valid
            if (Client.WalletAddress.Decode58Check(groupAddress) == null)
            {
                context.Logger.Log.LogDebug("Invalid Address!");
                return;
            }

            // Get user
            var senderUser = context.DatabaseContext.GetUserAsync(context.Sender, users => users.Include(u => u.DeviceIds)).GetAwaiter().GetResult();

            if (senderUser == null)
            {
                context.Logger.Log.LogDebug("User not found!");
                return;
            }

            // Check if address already exists
            if (context.DatabaseContext.ChatSettings.SingleOrDefault(cs => cs.Address == groupAddress) != null)
            {
                context.Logger.Log.LogDebug("Address already in use as a group!");
                return;
            }

            // Transfer a token to the group address to register it in tron
            TransferToGroup(context.ServiceProvider, groupAddress);

            // Create chat
            var chat = context.DatabaseContext.CreateGroupChat(senderUser, createGroupMessage.PrivateKey,
                                                               new DatabaseContext.ChatGroupInfo()
            {
                Address     = groupAddress,
                Name        = groupName,
                Description = groupDescription,
                Image       = groupImage,
                PublicKey   = groupPublicKey,
                PrivateKey  = publicGroupPrivateKey,
                IsPublic    = createGroupMessage.IsPublic,
            }, context.Transaction.RawData.Timestamp);

            context.DatabaseContext.SaveChanges();

            // Notify user that chat is created
            context.ServiceProvider.GetService <FCMClient>()?.GroupCreated(senderUser.DeviceIds, chat);
        }