public static User EnsureCreated(string controller, UserInfo user) { var db = new RepoContext(); // Поиск пользователя по айди одного из аккаунтов var existed = db.Users.FirstOrDefault(x => x.Accounts.Any(y => y.Controller == controller && y.UserId == user.Id)); if (existed != null) { return(existed); } // Ищем по юзернейму или создаем нового var usrn = user.Username != null ? db.Users.FirstOrDefault(x => x.Username == user.Username) ?? FromUsername(user.Username) : FromUsername(user.Username); // Добавляем ему аккаунт var account = new Account { Controller = controller, UserId = user.Id }; usrn.Accounts.Add(account); db.Update(usrn); db.SaveChanges(); return(usrn); }
public void DeleteIfNotUsed() { if (Chats.Count < 2) { var db = new RepoContext(); db.Remove(this); db.SaveChanges(); } }
public bool Exclude(ICollection <Chat> chats) { var db = new RepoContext(); Chats.RemoveAll(x => chats.Any(y => x.ChatId == y.Id)); if (Chats.Count < 2) { db.Remove(this); db.SaveChanges(); return(true); } db.Update(this); db.SaveChanges(); return(false); }
public void AssignPermissions(Chat chat, UserScope scope) { var db = new RepoContext(); var position = Chats.FirstOrDefault(x => x.ChatId == chat.Id); if (position == null) { position = new ChatMember(); position.User = this; position.Chat = chat; } position.Scope = scope; db.ChatMembers.Update(position); db.SaveChanges(); }
public static void EnsureContained(string controller, ChatInfo chat, User user) { var db = new RepoContext(); var existed = Resolve(controller, chat.Id); if (existed == null) { return; } if (!existed.Members.Any(x => x.UserId == user.Id)) { user.AssignPermissions(existed, UserScope.None); db.Update(user); db.SaveChanges(); } }
public static void Set(string key, string value) { var db = new RepoContext(); var val = new KeyValue { Key = key, Value = value }; if (Get(key) == null) { db.Add(val); } else { db.Update(val); } db.SaveChanges(); }
private static void BroadcastChangelog() { var gitdir = new string[] { ".git", "../.git" }.FirstOrDefault(x => Directory.Exists(Path.Combine(Directory.GetCurrentDirectory(), x))); if (gitdir == null) { return; } using var repo = new Repository(gitdir); if (repo.Tags.Count() < 2) { return; } var tags = repo.Tags.TakeLast(2).ToArray(); var(old, n) = (tags[0], tags[1]); if (KeyValue.Get("glc") == n.Target.Sha) { return; } KeyValue.Set("glc", n.Target.Sha); var db = new Models.RepoContext(); foreach (var chat in db.Chats) { if (!chat.Notifications) { continue; } var localizer = Program.Core.ResolveLocalizer(Assembly.GetExecutingAssembly(), chat.Language); var message = OutMessage.FromCode($"[{localizer["Update"]} | {old.FriendlyName} -> {n.FriendlyName}]\n{localizer["Changes"]}:\n{n.Annotation.Message}"); Program.Core .ResolveController(chat.Controller) .SendMessage(chat.ChatId, message); } }
public IEnumerable <Chat> GetDestinations() { var db = new RepoContext(); var routes = db.Routes.Where(x => x.Chats.Any(x => x.ChatId == Id)); var temp = routes .Select(x => x.Chats.AsEnumerable()); if (temp.Count() < 1) { return(new Chat[0]); } var chats = temp.ToList() .Aggregate((a, b) => a.Concat(b)) .ToList(); chats.Distinct(); chats.RemoveAll(x => x.ChatId == Id); return(chats.Select(x => x.Chat)); }
private static void CreateAdmins() { var db = new Models.RepoContext(); foreach (var admin in Options.SU) { var user = db.Users.FirstOrDefault(x => x.Username == admin); if (user == null) { user = new User { Username = admin } } ; user.IsSuperUser = true; db.Update(user); } db.SaveChanges(); }
public static (bool, Route) Sync(User owner, ICollection <Chat> chats) { var db = new RepoContext(); var route = Resolve(owner, chats); var created = route.Id == 0; foreach (var chat in chats) { if (route.Chats.Any(x => x.ChatId == chat.Id)) { continue; } route.Chats.Add(new ChatRoute { Chat = chat }); } route.Owner = owner; db.Update(route); db.SaveChanges(); return(created, route); }
public static Route Resolve(User owner, ICollection <Chat> chats) { var db = new RepoContext(); // Ищем частичное совпадение в более чем 2 маршрутах var routes = db.Routes .Where(x => x.Chats .Any(y => chats.Contains(y.Chat) && y.Route.Owner.Id == owner.Id)) .ToList(); if (routes.Any()) { routes[0].Chats = routes .Select(x => x.Chats.AsEnumerable()) .Aggregate((a, b) => a.Concat(b)) .Distinct() .ToList(); db.Update(routes[0]); foreach (var other in routes.Skip(1)) { db.Remove(other); } db.SaveChanges(); return(routes[0]); } // Или полное совпадение var route = db.Routes .FirstOrDefault(x => x.Chats.Select(y => y.Chat) .Intersect(chats).Count() == chats.Count); if (route != null) { return(route); } // Или создаем новый маршрут return(new Route()); }
public static Chat Resolve(string controller, long chatId) { var db = new RepoContext(); return(db.Chats.FirstOrDefault(x => x.Controller == controller && x.ChatId == chatId)); }
public static string Get(string key) { var db = new RepoContext(); return(db.Values.Find(key)?.Value); }