// Subscribe via Guild Channel / Server Channel public async Task <bool> SubscribeNotPrivate(string searchQuery, ulong userId, ulong channelId, ulong guildId) { if (searchQuery.Count() > _max_search_query_character_size) { return(false); } e6Subscription subscription = new e6Subscription(); subscription.IsNew = true; subscription.IsActive = true; subscription.SearchQuery = searchQuery; subscription.IsPrivate = false; subscription.UserId = userId; subscription.ChannelId = channelId; subscription.GuildId = guildId; using (e6PollerBotDbContext dbcontext = new e6PollerBotDbContext()) { await dbcontext.AddAsync <e6Subscription>(subscription); await dbcontext.SaveChangesAsync(); } return(true); }
// Delete a Subscription. public async Task <bool> DeleteSubscription(int sub_to_delete, ulong userId) { if (sub_to_delete <= 0) { return(false); } sub_to_delete -= 1; bool isSuccessful = true; List <e6Subscription> subscriptions; using (e6PollerBotDbContext dbcontext = new e6PollerBotDbContext()) { subscriptions = await dbcontext.e6Subscriptions.Where(s => s.UserId == userId).Where(s => s.IsActive == true).OrderBy(s => s.e6SubscriptionId).ToListAsync(); if (sub_to_delete >= subscriptions.Count()) { isSuccessful = false; } else { try { using (IDbContextTransaction dbContextTransaction = await dbcontext.Database.BeginTransactionAsync()) { try { subscriptions[sub_to_delete].IsActive = false; dbcontext.e6Subscriptions.Update(subscriptions[sub_to_delete]); await dbcontext.SaveChangesAsync(); dbContextTransaction.Commit(); } catch (Exception e) { Console.WriteLine(e); dbContextTransaction.Rollback(); } } } catch (Exception) { isSuccessful = false; } isSuccessful = true; } } return(isSuccessful); }
// Poll e6 Periodically. private async Task PollerThread() { while (true) { try { using (e6PollerBotDbContext dbcontext = new e6PollerBotDbContext()) { List <e6Subscription> subscriptions = await dbcontext.e6Subscriptions.Where(s => s.IsActive == true).ToListAsync(); foreach (e6Subscription subscription in subscriptions) { if (subscription.IsNew) { List <e6Post> e6PostsRaw = await Querye6ByTag(subscription.SearchQuery, _query_limit); HashSet <int> e6PostIds = e6PostsRaw.Select(x => x.id).ToHashSet(); using (IDbContextTransaction dbContextTransaction = await dbcontext.Database.BeginTransactionAsync()) { try { e6Subscription tempSub = await dbcontext.e6Subscriptions.SingleOrDefaultAsync(s => s.e6SubscriptionId == subscription.e6SubscriptionId); PropertyValues propertyValues = await dbcontext.Entry(tempSub).GetDatabaseValuesAsync(); tempSub.IsActive = (bool)propertyValues["IsActive"]; tempSub.e6Posts = e6PostIds; subscription.IsNew = false; dbcontext.e6Subscriptions.Update(tempSub); await dbcontext.SaveChangesAsync(); dbContextTransaction.Commit(); } catch (Exception e) { Console.WriteLine(e); dbContextTransaction.Rollback(); } } } else { List <e6Post> e6PostsRaw = await Querye6ByTag(subscription.SearchQuery, _query_limit); HashSet <int> e6PostIds = e6PostsRaw.Select(x => x.id).ToHashSet(); IEnumerable <int> newIds = e6PostIds.Except(subscription.e6Posts); await SendUpdates(subscription : subscription, newIds : newIds); using (IDbContextTransaction dbContextTransaction = await dbcontext.Database.BeginTransactionAsync()) { try { e6Subscription tempSub = await dbcontext.e6Subscriptions.SingleOrDefaultAsync(s => s.e6SubscriptionId == subscription.e6SubscriptionId); PropertyValues propertyValues = await dbcontext.Entry(tempSub).GetDatabaseValuesAsync(); tempSub.IsActive = (bool)propertyValues["IsActive"]; tempSub.e6Posts.UnionWith(newIds); dbcontext.e6Subscriptions.Update(tempSub); await dbcontext.SaveChangesAsync(); dbContextTransaction.Commit(); } catch (Exception e) { Console.WriteLine(e); dbContextTransaction.Rollback(); } } } } } } catch (Exception e) { Console.WriteLine(e); } finally { await Task.Delay(_poller_thread_latency); } } }