Пример #1
0
        public OutputSocketMessageWithUsers Execute(object val, string myLogin, Guid actId)
        {
            #region Тестовые данные

            /* {
             *      "execFun": "GetMsg",
             *      "data": {
             *          "dialogId": "788FEFAD0ED24436AD73D968685110E8",
             *          "msgCount": "2",
             *          "offset": "0"
             *      }
             *  }
             */
            #endregion

            OutputSocketMessage          output = new OutputSocketMessage("getMsg", actId, true, "", new { });
            OutputSocketMessageWithUsers rez    = new OutputSocketMessageWithUsers();

            GetMsgInfo info = DeserializeObject.ParseJSON <GetMsgInfo>(val, output, out rez);
            if (info == null)
            {
                return(rez);
            }

            using (var db = new RaidaContext())
            {
                Members owner = db.Members.First(it => it.login.Equals(myLogin));

                if (db.MemberInGroup.Include(g => g.group).Any(it => it.member.login.Equals(myLogin) && it.groupId == info.dialogId))
                {
                    MemberInGroup mg = db.MemberInGroup.Include(g => g.group).ThenInclude(s => s.Shares).Where(it => it.member.login.Equals(myLogin) && it.groupId == info.dialogId).First();

                    string groupName = mg.group.group_name_part;
                    if (mg.group.one_to_one)
                    {
                        groupName = db.MemberInGroup.Include(m => m.member).FirstOrDefault(mig => mig.group == mg.group && mig.member != owner)?.member.nick_name;
                    }
                    OutGetMsgInfo groupMsg = new OutGetMsgInfo(mg.group.group_id, groupName, mg.group.one_to_one, mg.group.privated);
                    mg.group.Shares.Where(it => it.death_date > SystemClock.GetInstance().CurrentTime)
                    .OrderBy(s => s.sending_date)
                    .Skip(info.offset)
                    .Take(info.msgCount)
                    .ToList()
                    .ForEach(msg => groupMsg.messages.Add(new OneMessageInfo(msg.id, Encoding.Unicode.GetString(msg.file_data), msg.current_fragment, msg.total_fragment, msg.sending_date, msg.owner.nick_name, msg.owner.login)));

                    output.data = groupMsg;
                }
                else
                {
                    output.success  = false;
                    output.msgError = "Dialog is not found";
                }
            }

            rez.msgForOwner = output;
            return(rez);
        }
Пример #2
0
        /// <summary>
        /// Return SHA256 hash for object
        /// </summary>
        /// <param name="obj">Input object</param>
        /// <returns>String base64</returns>
        public static string GetHash <T>(T obj)
        {
            object tmpObj;

            switch (typeof(T).FullName)
            {
            case "RAIDAChatNode.Model.Entity.Members":
                Members tmp = obj as Members;
                tmpObj = new { tmp?.login, tmp?.nick_name };
                break;

            case "RAIDAChatNode.Model.Entity.Groups":
                Groups tmp1 = obj as Groups;
                tmpObj = new { tmp1?.group_id, tmp1?.group_name_part, tmp1?.one_to_one };
                break;

            case "RAIDAChatNode.Model.Entity.MemberInGroup":
                MemberInGroup tmp2 = obj as MemberInGroup;
                tmpObj = new { tmp2?.groupId, tmp2?.member?.login };
                break;

            default:
                tmpObj = obj;
                break;
            }

            SHA256 sha = SHA256.Create();

            return(Convert.ToBase64String(
                       sha.ComputeHash(
                           Encoding.Unicode.GetBytes(
                               JsonConvert.SerializeObject(tmpObj, new JsonSerializerSettings()
            {
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore
            })
                               )
                           )
                       ));
        }
Пример #3
0
        public OutputSocketMessageWithUsers Execute(object val, string myLogin, Guid actId)
        {
            #region Тестовые данные

            /*
             *          {
             *              "execFun": "addMemberInGroup",
             *              "data": {
             *                  "memberLogin": "******",
             *                  "groupId": "48A0CA0657DE4FB09CDC86008B2A8EBE",
             *                   "transactionId": "80f7efc032dd4a7c97f69fca51ad1100"
             *              }
             *          }
             */
            #endregion

            OutputSocketMessage          output = new OutputSocketMessage("addMemberInGroup", actId, true, "", new { });
            OutputSocketMessageWithUsers rez    = new OutputSocketMessageWithUsers();

            AddMemberInGroupInfo info = DeserializeObject.ParseJSON <AddMemberInGroupInfo>(val, output, out rez);
            if (info == null)
            {
                return(rez);
            }

            using (var db = new RaidaContext())
            {
                if (db.Members.Any(it => it.login.Equals(info.memberLogin.Trim(), StringComparison.CurrentCultureIgnoreCase)))
                {
                    Members owner = db.Members.First(it => it.login.Equals(myLogin));
                    if (db.MemberInGroup.Any(it => it.member == owner && it.groupId == info.groupId && !it.group.one_to_one))
                    {
                        Members user = db.Members.FirstOrDefault(it => it.login.Equals(info.memberLogin.Trim(), StringComparison.CurrentCultureIgnoreCase));
                        if (db.MemberInGroup.Any(it => it.groupId == info.groupId && it.member == user))
                        {
                            output.success  = false;
                            output.msgError = "This user already exists in this group";
                        }
                        else
                        {
                            Groups group = db.Groups.First(it => it.group_id == info.groupId);

                            if (group.privated)
                            {
                                output.success  = false;
                                output.msgError = "This group is private";
                                rez.msgForOwner = output;
                                return(rez);
                            }

                            int newId = 0;
                            if (db.MemberInGroup.Any())
                            {
                                newId = db.MemberInGroup.OrderByDescending(it => it.Id).FirstOrDefault().Id + 1;
                            }

                            MemberInGroup mg = new MemberInGroup
                            {
                                Id     = newId,
                                group  = group,
                                member = user
                            };
                            db.MemberInGroup.Add(mg);

                            Transaction.saveTransaction(db, info.transactionId, Transaction.TABLE_NAME.MEMBERS_IN_GROUP, mg.Id.ToString(), owner);

                            db.SaveChanges();

                            output.data = new { itself = true, newUser = new UserInfo(user.login, user.nick_name, user.photo_fragment, user.online), groupId = group.group_id };

                            var members = db.MemberInGroup.Include(ming => ming.member)
                                          .Where(it => it.groupId.Equals(group.group_id)).Select(x => x.member.login).ToList(); //Logins of all users in dialog
                            rez.forUserLogin = members.Where(it => !it.Equals(myLogin)).ToList();                               //Send all users in dialog, but not me

                            rez.msgForOther = new
                            {
                                callFunction = "AddMemberInGroup",
                                data         = new {
                                    id       = info.groupId,
                                    name     = group.group_name_part,
                                    oneToOne = group.one_to_one,
                                    members  = members,
                                    newUser  = new UserInfo(user.login, user.nick_name, user.photo_fragment, user.online)
                                }
                            };
                        }
                    }
                    else
                    {
                        output.success  = false;
                        output.msgError = "Group is not found";
                    }
                }
                else
                {
                    output.success  = false;
                    output.msgError = "User is not found";
                }
            }

            rez.msgForOwner = output;
            return(rez);
        }
Пример #4
0
        public OutputSocketMessageWithUsers Execute(object val, string myLogin, Guid actId)
        {
            #region Тестовые данные

            /*
             * {
             *  "execFun": "createDialog",
             *  "data": {
             *      "name": "MeCloseGroup",
             *      "publicId": "48A0CA0657DE4FB09CDC86008B2A8EBE",
             *      "oneToOne": "false",
             *      "transactionId": "80f7efc032dd4a7c97f69fca51ad3100"
             *  }
             * }
             */
            #endregion

            OutputSocketMessage          output = new OutputSocketMessage("createDialog", actId, true, "", new { });
            OutputSocketMessageWithUsers rez    = new OutputSocketMessageWithUsers();

            GroupInfo info = DeserializeObject.ParseJSON <GroupInfo>(val, output, out rez);
            if (info == null)
            {
                return(rez);
            }

            using (var db = new RaidaContext())
            {
                if (db.Groups.Any(it => it.group_id == info.publicId))
                {
                    output.success  = false;
                    output.msgError = "Change the publicId";
                    rez.msgForOwner = output;
                    return(rez);
                }

                Members owner = db.Members.First(it => it.login.Equals(myLogin));

                Groups group = new Groups
                {
                    group_id        = info.publicId,
                    group_name_part = info.name,
                    owner           = owner,
                    photo_fragment  = "",
                    one_to_one      = info.oneToOne,
                    privated        = false
                };

                int newId = 1;
                if (db.MemberInGroup.Count() > 0)
                {
                    newId = db.MemberInGroup.OrderByDescending(it => it.Id).FirstOrDefault().Id + 1;
                }


                if (info.oneToOne)
                {
                    if (db.Members.Any(it => it.login.Equals(info.name.Trim().ToLower())))
                    {
                        group.group_name_part = "OneToOne";
                        Members rec = db.Members.First(it => it.login.Equals(info.name.Trim().ToLower()));

                        if (!db.MemberInGroup.Any(it => it.member == owner && db.MemberInGroup.Any(mg => mg.member == rec && it.group == mg.group && mg.group.one_to_one)))
                        {
                            MemberInGroup mg1 = new MemberInGroup
                            {
                                Id     = newId,
                                group  = group,
                                member = rec
                            };
                            db.MemberInGroup.Add(mg1);

                            Transaction.saveTransaction(db, info.transactionId, Transaction.TABLE_NAME.MEMBERS_IN_GROUP, mg1.Id.ToString(), owner);

                            newId++;
                            //rez.usersId.Add(info.login); Придумать ответ пользователю, которого добавили - а может он и не нужен
                        }
                        else
                        {
                            output.success  = false;
                            output.msgError = "This dialog exists";
                            rez.msgForOwner = output;
                            return(rez);
                        }
                    }
                    else
                    {
                        output.success  = false;
                        output.msgError = "User is not found";
                        rez.msgForOwner = output;
                        return(rez);
                    }
                }
                db.Groups.Add(group);
                MemberInGroup OwnerInMg = new MemberInGroup
                {
                    Id     = newId,
                    group  = group,
                    member = owner
                };
                db.MemberInGroup.Add(OwnerInMg);
                try {
                    db.SaveChanges();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                Transaction.saveTransaction(db, info.transactionId, Transaction.TABLE_NAME.GROUPS, info.publicId.ToString(), owner);
                Transaction.saveTransaction(db, info.transactionId, Transaction.TABLE_NAME.MEMBERS_IN_GROUP, OwnerInMg.Id.ToString(), owner);
            }
            output.data     = new { id = info.publicId, info.name, info.oneToOne };
            rez.msgForOwner = output;
            return(rez);
        }
Пример #5
0
        private void SyncronizeBase(InputInfo correctData, RaidaContext db)
        {
            //Save\Update members table
            correctData.MemberInfo.NewRows.ForEach(it =>
            {
                correctData.MemberInfo.Actual.Add(GetHash(new { login = it.Login, nick_name = it.NickName }));
                if (db.Members.Any(m => m.login.Equals(it.Login)))
                {
                    //UPDATE
                    Members member              = db.Members.First(m => m.login.Equals(it.Login));
                    member.nick_name            = it.NickName;
                    member.description_fragment = it.Descript;
                    member.last_use             = it.LastUse;
                    member.online         = it.Online;
                    member.photo_fragment = it.Photo;
                }
                else
                {
                    //INSERT NEW
                    Guid privateId = Guid.NewGuid();
                    while (db.Members.Any(memb => memb.private_id == privateId))
                    {
                        privateId = Guid.NewGuid();
                    }

                    Members member = new Members
                    {
                        private_id           = privateId,
                        login                = it.Login.Trim().ToLower(),
                        pass                 = it.Password,
                        nick_name            = it.NickName,
                        last_use             = it.LastUse,
                        description_fragment = it.Descript,
                        photo_fragment       = it.Photo,
                        kb_bandwidth_used    = 0,
                        online               = it.Online,
                    };
                    db.Members.Add(member);
                }
            });
            db.SaveChanges();


            //Save\Update groups table
            correctData.GroupInfo.NewRows.ForEach(it =>
            {
                correctData.GroupInfo.Actual.Add(GetHash(new { group_id = it.Id, group_name_part = it.Name, one_to_one = it.PeerToPeer }));
                Members owner = db.Members.First(m => m.login.Equals(it.Owner));
                if (db.Groups.Any(g => g.group_id == it.Id))
                {
                    Groups group          = db.Groups.First(g => g.group_id == it.Id);
                    group.group_name_part = it.Name;
                    group.one_to_one      = it.PeerToPeer;
                    group.photo_fragment  = it.Photo;
                    group.privated        = it.Privated;
                    group.owner           = owner;
                }
                else
                {
                    Groups newGroup = new Groups()
                    {
                        group_id        = it.Id,
                        group_name_part = it.Name,
                        owner           = owner,
                        photo_fragment  = it.Photo,
                        one_to_one      = it.PeerToPeer,
                        privated        = it.Privated
                    };
                    db.Groups.Add(newGroup);
                }
            });
            db.SaveChanges();

            //Save member in groups
            correctData.MinGInfo.NewRows.ForEach(it =>
            {
                correctData.MinGInfo.Actual.Add(GetHash(new { groupId = it.GroupId, login = it.Login }));
                if (db.Members.Any(m => m.login.Equals(it.Login) && db.Groups.Any(g => g.group_id == it.GroupId)))
                {
                    Groups group   = db.Groups.First(gr => gr.group_id == it.GroupId);
                    Members member = db.Members.First(m => m.login.Equals(it.Login));

                    if (!db.MemberInGroup.Any(mig => mig.member.Equals(member) && mig.group.Equals(group))) //Find duplicate
                    {
                        int newId = 0;
                        if (db.MemberInGroup.Any())
                        {
                            newId = db.MemberInGroup.OrderByDescending(mig => mig.Id).FirstOrDefault().Id + 1;
                        }

                        MemberInGroup mg = new MemberInGroup
                        {
                            Id     = newId,
                            group  = group,
                            member = member
                        };
                        db.MemberInGroup.Add(mg);
                    }
                }
            });
            db.SaveChanges();

            //Remove not actual data in tables
            db.Members.ForEachAsync(it =>
            {
                if (!correctData.MemberInfo.Actual.Contains(GetHash(it)))
                {
                    db.Members.Remove(it);
                }
            });

            db.Groups.ForEachAsync(it =>
            {
                if (!correctData.GroupInfo.Actual.Contains(GetHash(it)))
                {
                    db.Groups.Remove(it);
                }
            });

            db.MemberInGroup.ForEachAsync(it =>
            {
                if (!correctData.MinGInfo.Actual.Contains(GetHash(it)))
                {
                    db.MemberInGroup.Remove(it);
                }
            });
            db.SaveChangesAsync();
        }