private void TimerTickCleanup() { long dateNow = CurrentTime; using (var db = new RaidaContext()) { //Cleanup 'death_date' message db.Shares.Where(it => it.death_date <= dateNow) .ToList() .ForEach(delegate(Shares s) { db.Shares.Remove(s); } ); //Cleanup blocked transactions db.Transactions.Where(it => it.rollbackTime <= dateNow).ToList() .ForEach(delegate(Transactions t) { db.Transactions.Remove(t); } ); db.SaveChanges(); } }
public OutputSocketMessageWithUsers Execute(object val, string myLogin, Guid actId) { #region Тестовые данные /* { * "execFun": "OrganizationAddMember", * "data": { * "login": "******", * "password": "******", * "nickName": "Serj", * "transactionId": "80f7efc032cd4a2c97f89fca11ad3701" * } * } */ #endregion OutputSocketMessage output = new OutputSocketMessage("OrganizationAddMember", actId, true, "", new { }); OutputSocketMessageWithUsers rez = new OutputSocketMessageWithUsers(); using (var db = new RaidaContext()) { Members owner = db.Members.Include(o => o.organization).First(it => it.login.Equals(myLogin)); if (owner.organization != null && owner.organization.owner == owner) { Registration registration = new Registration(); rez = registration.Execute(val, myLogin, actId); rez.msgForOwner.callFunction = "OrganizationAddMember"; if (rez.msgForOwner.success) { try { RegistrationInfo info = DeserializeObject.ParseJSON <RegistrationInfo>(val, output, out rez); Organizations organization = db.Organizations.Include(m => m.members).First(it => it == owner.organization); Members newMember = db.Members.First(it => it.login.Equals(info.login.Trim(), StringComparison.CurrentCultureIgnoreCase)); newMember.organization = organization; organization.members.Add(newMember); db.SaveChanges(); output.data = new { newMember.login, nickName = newMember.nick_name }; rez.msgForOwner = output; } catch (Exception e) { Console.WriteLine(e.Message); } } } else { output.success = false; output.msgError = "You a not consist in organization or a not organization owner"; rez.msgForOwner = output; } } return(rez); }
public OutputSocketMessageWithUsers Execute(object val, string myLogin, Guid actId) { #region Тестовые данные /* * { * "execFun": "SetDialogPrivate", * "data": { * "publicId": "18A0CA0657DE4FB09CDC86008B2A8EBE", * "privated": "false" * } * } */ #endregion OutputSocketMessage output = new OutputSocketMessage("SetDialogPrivate", actId, true, "", new { }); OutputSocketMessageWithUsers rez = new OutputSocketMessageWithUsers(); SetDialogPrivateInfo info = DeserializeObject.ParseJSON <SetDialogPrivateInfo>(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.Groups.Any(it => it.group_id == info.publicId && it.owner == owner)) { Groups group = db.Groups.First(it => it.group_id == info.publicId && it.owner == owner); group.privated = info.privated; db.SaveChanges(); output.data = new { dialogId = group.group_id, group.privated }; List <MemberInGroup> membersInGroup = db.MemberInGroup.Include(m => m.member).Where(it => it.group == group && it.member != owner).ToList(); membersInGroup.ForEach(it => rez.forUserLogin.Add(it.member.login)); rez.msgForOther = output; } else { output.success = false; output.msgError = "This dialog not found or you not owner"; rez.msgForOwner = output; return(rez); } } rez.msgForOwner = output; return(rez); }
/* { "execFun": "registration", * "data": { * "login": "******", * "password": "******", * "nickName": "gosha", * "transactionId": "80f7efc032dd4a7c97f69fca51ad3001" * } * } */ public OutputSocketMessageWithUsers Execute(object val, string myLogin, Guid actId) { OutputSocketMessage output = new OutputSocketMessage("registration", actId, true, "", new { }); OutputSocketMessageWithUsers rez = new OutputSocketMessageWithUsers(); RegistrationInfo info = DeserializeObject.ParseJSON <RegistrationInfo>(val, output, out rez); if (info == null) { return(rez); } using (var db = new RaidaContext()) { if (db.Members.Any(it => it.login.Equals(info.login.Trim(), StringComparison.CurrentCultureIgnoreCase))) { output.success = false; output.msgError = "This login already exists"; } else { Guid privateId = Guid.NewGuid(); while (db.Members.Any(it => it.private_id == privateId)) { privateId = Guid.NewGuid(); } info.password = Argon2.Hash(info.password, 1, 512, 8); //Hashing password Members member = new Members { private_id = privateId, login = info.login.Trim().ToLower(), pass = info.password, nick_name = info.nickName, last_use = SystemClock.GetInstance().CurrentTime, description_fragment = "", photo_fragment = "", kb_bandwidth_used = 0, online = false, }; db.Members.Add(member); Transaction.saveTransaction(db, info.transactionId, Transaction.TABLE_NAME.MEMBERS, privateId.ToString(), member); db.SaveChanges(); } } rez.msgForOwner = output; return(rez); }
/// <summary> /// Remove all message above limit /// </summary> /// <param name="group">Check dialog</param> public static void removeMessageAboveLimit(Groups group) { int limit = MainConfig.LimitMsgForDialog; List <Shares> extra = group.Shares.ToList(); using (var db = new RaidaContext()) { extra.OrderByDescending(it => it.sending_date) .Skip(limit) .ToList() .ForEach(delegate(Shares s) { db.Shares.Remove(s); }); db.SaveChanges(); } }
public static void rollbackTransaction(Guid transactionId, Members owner) { //long dateNow = DateTimeOffset.Now.ToUnixTimeSeconds(); long dateNow = SystemClock.GetInstance().CurrentTime; using (var db = new RaidaContext()) { if (db.Transactions.Any(it => it.transactionId == transactionId && it.owner == owner && it.rollbackTime > dateNow)) { List <Transactions> trans = db.Transactions.Where(it => it.transactionId == transactionId && it.owner == owner && it.rollbackTime > dateNow).ToList(); trans.ForEach(delegate(Transactions t) { ReflectionRemove(db, t.tableName.Trim(), t.tableRowId.Trim()); }); db.SaveChanges(); } } }
public override void OnClientClosed(IClient client) { if (mClients.Any(it => it.client == client)) { AuthSocketInfo cl = mClients.First(it => it.client == client); using (var db = new RaidaContext()) { Members user = db.Members.Include(m => m.MemberInGroup).First(it => it.login.Equals(cl.login)); user.online = false; user.last_use = SystemClock.GetInstance().CurrentTime; db.SaveChanges(); //организовать отправку остальным клиентом SendMessageOfChangeUser(db, user); } mClients.Remove(cl); } }
/* { "execFun": "authorization", * "data": { * "login": "******", * "password": "******" * } * } */ //Замутить обновление статуса онлайн-офлайн public AuthSocketInfo Execute(object data) { AuthSocketInfo output = new AuthSocketInfo(); AuthInfo info; try { info = JsonConvert.DeserializeObject <AuthInfo>(data.ToString()); using (var db = new RaidaContext()) { Members user = db.Members.FirstOrDefault(it => it.login.Equals(info.login.Trim(), StringComparison.CurrentCultureIgnoreCase) && Argon2.Verify(it.pass, info.password, null)); if (user != null) { output.auth = true; output.login = user.login; output.nickName = user.nick_name; output.password = info.password; output.photo = user.photo_fragment; user.online = true; user.last_use = SystemClock.GetInstance().CurrentTime; db.SaveChanges(); } else { output.auth = false; } } } catch (Exception e) { Console.WriteLine(e.Message.ToString()); output.auth = false; } return(output); }
public OutputSocketMessageWithUsers Execute(object val, string myLogin, Guid actId) { #region Тестовые данные /* * { * "execFun": "sendMsg", * "data": { * "dialogId": "788FEFAD0ED24436AD73D968685110E8", * "textMsg": "test message for one user", * "guidMsg": "91D8333FA55B40AFB46CA63E214C93C8", * "curFrg":"1", * "totalFrg":"2", * "transactionId": "80f7efc032dd4a1c97f69fca51ad1100", * "deathDate": "2511450497620" * } * } */ #endregion OutputSocketMessage output = new OutputSocketMessage("sendMsg", actId, true, "", new { }); OutputSocketMessage outputOther = new OutputSocketMessage("sendMsg", Guid.Empty, true, "", new { }); OutputSocketMessageWithUsers rez = new OutputSocketMessageWithUsers(); InputMsgInfo info = DeserializeObject.ParseJSON <InputMsgInfo>(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.Groups.Any(it => it.group_id == info.dialogId && it.MemberInGroup.Any(mg => mg.group == it && mg.member == owner))) //Check created group and member in this group { if (!db.Shares.Any(it => it.id == info.msgId)) //Check exist message with Id { Groups gr = db.Groups.Include(s => s.Shares).First(it => it.group_id == info.dialogId); Console.WriteLine(SystemClock.GetInstance().CurrentTime.ToString()); Shares msg = new Shares { id = info.msgId, owner = owner, current_fragment = info.curFrg, total_fragment = info.totalFrg, file_data = Encoding.Unicode.GetBytes(info.textMsg), file_extention = "none", kb_size = 0, sending_date = SystemClock.GetInstance().CurrentTime, // DateTimeOffset.Now.ToUnixTimeSeconds(), death_date = info.deathDate > 0 ? DateConvert.validateTimestamp(SystemClock.GetInstance().CurrentTime + info.deathDate) : DateTimeOffset.Now.AddYears(2000).ToUnixTimeSeconds(), to_group = gr }; gr.Shares.Add(msg); db.Shares.Add(msg); Transaction.saveTransaction(db, info.transactionId, Transaction.TABLE_NAME.SHARES, msg.id.ToString(), owner); db.SaveChanges(); List <MemberInGroup> membersInGroup = db.MemberInGroup.Include(m => m.member) .Where(it => it.group == gr && it.member != owner).ToList(); membersInGroup.ForEach(it => rez.forUserLogin.Add(it.member.login)); string groupNameForOwner = gr.group_name_part, groupNameForOther = gr.group_name_part; if (gr.one_to_one) { groupNameForOwner = membersInGroup.First(it => it.member != owner).member.nick_name; groupNameForOther = owner.nick_name; } OneMessageInfo newMsg = new OneMessageInfo(msg.id, info.textMsg, msg.current_fragment, msg.total_fragment, msg.sending_date, owner.nick_name, msg.owner.login); output.data = new OutGetMsgInfo(gr.group_id, groupNameForOwner, gr.one_to_one, gr.privated, new List <OneMessageInfo>() { newMsg }); outputOther.data = new OutGetMsgInfo(gr.group_id, groupNameForOther, gr.one_to_one, gr.privated, new List <OneMessageInfo>() { newMsg }); Transaction.removeMessageAboveLimit(gr); } else { output.success = false; output.msgError = "Message with this ID is exist"; output.data = new { id = info.msgId }; rez.msgForOwner = output; return(rez); } } else { output.success = false; output.msgError = "Dialog is not found"; rez.msgForOwner = output; return(rez); } } rez.msgForOwner = output; rez.msgForOther = outputOther; return(rez); }
public OutputSocketMessageWithUsers Execute(object val, string myLogin, Guid actId) { #region Тестовые данные /* { * "execFun": "ChangeUserInfo", * "data": { * "name":"String", * "photo":"String", * "about":"String", * "changePass":"******", * "oldPass":"******", * "newPass":"******" * } * } */ #endregion OutputSocketMessage output = new OutputSocketMessage("changeUserInfo", actId, true, "", new { }); OutputSocketMessage outputForOther = new OutputSocketMessage("changeUserInfo", Guid.Empty, true, "", new { }); OutputSocketMessageWithUsers rez = new OutputSocketMessageWithUsers(); DTO.ChangeUserInfo info = DeserializeObject.ParseJSON <DTO.ChangeUserInfo>(val, output, out rez); if (info == null) { return(rez); } using (var db = new RaidaContext()) { Members owner = db.Members.Include(m => m.MemberInGroup).FirstOrDefault(it => it.login.Equals(myLogin)); owner.nick_name = info.Name; owner.photo_fragment = info.Photo; owner.description_fragment = info.About; if (info.ChangePass) { if (Argon2.Verify(owner.pass, info.OldPass, null)) { if (info.NewPass.Trim().Length > 5) { owner.pass = Argon2.Hash(info.NewPass, 1, 512, 8); } else { output.success = false; output.msgError = "Minimum length of password is 6 chars"; } } else { output.success = false; output.msgError = "Current password is not valid"; } } db.SaveChanges(); var user = new UserInfo(owner.login, owner.nick_name, owner.photo_fragment, owner.online); output.data = new { itself = true, user }; outputForOther.data = new { itself = false, user }; rez.forUserLogin = DeserializeObject.GetMyReferenceUser(db, owner); } rez.msgForOwner = output; rez.msgForOther = outputForOther; return(rez); }
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); }
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); }
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(); }
public OutputSocketMessageWithUsers Execute(object val, string myLogin, Guid actId) { #region Тестовые данные /* * { * "execFun": "OrganizationCreate", * "data": { * "publicId": "788FEFAD0ED24436AD73D968685110E8", * "name": "Organization Only One", * "transactionId": "80f7efc092da4a1c47f69fca51ad1100" * } * } */ #endregion OutputSocketMessage output = new OutputSocketMessage("OrganizationCreate", actId, true, "", new { }); OutputSocketMessageWithUsers rez = new OutputSocketMessageWithUsers(); OrganizationCreateInfo info = DeserializeObject.ParseJSON <OrganizationCreateInfo>(val, output, out rez); if (info == null) { return(rez); } using (var db = new RaidaContext()) { Members owner = db.Members.Include(o => o.organization).First(it => it.login.Equals(myLogin)); if (owner.organization == null) { if (!db.Organizations.Any(it => it.public_id == info.publicId)) { Guid privateId = Guid.NewGuid(); while (db.Organizations.Any(it => it.private_id == privateId)) { privateId = Guid.NewGuid(); } Organizations newOrganiz = new Organizations { private_id = privateId, public_id = info.publicId, org_name_part = info.name, kb_of_credit = 0, owner = owner }; newOrganiz.members.Add(owner); db.Organizations.Add(newOrganiz); Transaction.saveTransaction(db, info.transactionId, Transaction.TABLE_NAME.ORGANIZATIONS, newOrganiz.private_id.ToString(), owner); db.SaveChanges(); output.data = new { publicId = newOrganiz.public_id, name = newOrganiz.org_name_part }; } else { output.success = false; output.msgError = "Change the publicId"; } } else { output.success = false; output.msgError = "You a have already organization"; } } rez.msgForOwner = output; return(rez); }