Exemplo n.º 1
0
        public async Task <BindingStatus> AddBinding(string subredditName, IMessageChannel channel)
        {
            if (!(await httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Head, subredditName))).IsSuccessStatusCode)
            {
                return(BindingStatus.Error);
            }
            using (var db = new RedditContext(dbOptions))
            {
                var subreddit = new RedditBinding
                {
                    SubredditName = subredditName.ToLower(),
                    LatestPost    = DateTime.UtcNow,
                };

                if (db.RedditChannelBindings.Any(b => b.ChannelId == channel.Id && b.Subreddit.SubredditName == subreddit.SubredditName))
                {
                    return(BindingStatus.AlreadyExists);
                }

                db.RedditChannelBindings.Add(new RedditChannelBinding
                {
                    Subreddit = subreddit,
                    ChannelId = channel.Id,
                });

                await db.SaveChangesAsync();

                return(BindingStatus.Added);
            }
        }
Exemplo n.º 2
0
        public async Task <bool> RemoveBinding(IEntity <ulong> binding)
        {
            using (var db = new RedditContext(dbOptions))
            {
                var entities = db.RedditChannelBindings.Where(b => b.ChannelId == binding.Id);
                if (!entities.Any())
                {
                    return(false);
                }

                db.RedditChannelBindings.RemoveRange(entities);
                await db.SaveChangesAsync();

                return(true);
            }
        }
Exemplo n.º 3
0
        public async Task <bool> RemoveBinding(string subredditName, IMessageChannel channel)
        {
            using (var db = new RedditContext(dbOptions))
            {
                var name = subredditName.ToLower();

                var found = await db.RedditChannelBindings.FirstOrDefaultAsync(b => b.ChannelId == channel.Id && b.Subreddit.SubredditName == name);

                if (found == default(RedditChannelBinding))
                {
                    return(false);
                }

                db.RedditChannelBindings.Remove(found);
                await db.SaveChangesAsync();

                return(true);
            }
        }
 public HomeController(RedditContext context)
 {
     dbContext = context;
 }
Exemplo n.º 5
0
 public RedditController(ILogger <RedditController> logger, RedditContext context)
 {
     _logger  = logger;
     _context = context;
 }
Exemplo n.º 6
0
 public RedditRepository(RedditContext context)
 {
     this.redditContext = context;
 }
Exemplo n.º 7
0
 public UserRepository(RedditContext userContext)
 {
     this.userContext = userContext;
 }
Exemplo n.º 8
0
 public RedditBackgroundShit(RedditContext context)
 {
     _context = context;
 }
Exemplo n.º 9
0
 public BaseRepository()
 {
     _db = new RedditContext();
 }
Exemplo n.º 10
0
 public PostRepository(RedditContext postContext)
 {
     this.postContext = postContext;
 }
Exemplo n.º 11
0
 public PostCrud(RedditContext db)
 {
     this.db = db;
 }
Exemplo n.º 12
0
 public UserRepository(RedditContext context)
 {
     this.UserDb = context;
 }
Exemplo n.º 13
0
        async Task ITimedService.Callback()
        {
            using (var db = new RedditContext(dbOptions))
            {
                foreach (var subreddit in db.RedditBindings.Include(b => b.ChannelBindings))
                {
                    var responseText = await httpClient.HttpGet($"{subreddit.SubredditName}/new/.json");

                    if (responseText == null)
                    {
                        continue;
                    }

                    dynamic responseObject     = JsonConvert.DeserializeObject(responseText);
                    dynamic posts              = responseObject["data"]["children"];
                    var     newestCreationTime = DateTimeOffset.FromUnixTimeSeconds((int)posts[0]["data"]["created_utc"]);
                    var     postStack          = new Stack <Embed>();

                    foreach (var post in posts)
                    {
                        var creationTime = DateTimeOffset.FromUnixTimeSeconds((int)post["data"]["created_utc"]);
                        if (creationTime <= subreddit.LatestPost)
                        {
                            break;
                        }

                        var    title    = WebUtility.HtmlDecode((string)post["data"]["title"]);
                        string username = post["data"]["author"];
                        string id       = post["data"]["id"];
                        string url      = post["data"]["url"];
                        string selftext = post["data"]["selftext"];

                        if (filterKeywords.Any(kw => title.ToLower().Contains(kw)))
                        {
                            continue;
                        }

                        var compact = compactKeywords.Any(kw => title.ToLower().Contains(kw));

                        string imageUrl = null;
                        try
                        {
                            imageUrl = post["data"]["preview"]["images"][0]["source"]["url"];
                        }
                        catch (Exception)
                        {
                        }

                        string thumbnailUrl = null;
                        if (post["data"]["thumbnail"] != "self" && post["data"]["thumbnail"] != "default")
                        {
                            thumbnailUrl = post["data"]["thumbnail"];
                        }

                        var embed = new EmbedBuilder()
                                    .AddField(f => f.WithName("Title").WithValue(title))
                                    .AddField(f => f.WithName("Submitted By").WithValue($"/u/{username}").WithIsInline(true))
                                    .AddField(f => f.WithName("Subreddit").WithValue($"/r/{subreddit.SubredditName}").WithIsInline(true))
                                    .AddField(f => f.WithName("Shortlink").WithValue($"http://redd.it/{id}").WithIsInline(true))
                                    .WithColor(ModuleColor);

                        if (!url.Contains("reddit.com"))
                        {
                            embed.AddField(f => f.WithName("URL").WithValue(url));
                        }

                        if (!compact)
                        {
                            if (imageUrl != null)
                            {
                                embed.ImageUrl = imageUrl;
                            }
                            else if (thumbnailUrl != null)
                            {
                                embed.ThumbnailUrl = thumbnailUrl;
                            }

                            if (selftext != string.Empty)
                            {
                                embed.AddField(f => f.WithName("Text").WithValue(selftext.Truncate(500)));
                            }
                        }

                        if (!(compact || imageUrl == null))
                        {
                            try
                            {
                                var sauce = SauceModule.GetSauce(imageUrl);
                                if (sauce.SimilarityPercentage > 90)
                                {
                                    postStack.Push(sauce.Embed);
                                }
                            }
                            catch (SauceModule.SauceNotFoundException sauceNotFoundException)
                            {
                                await Logging.LogError(sauceNotFoundException.ToString());
                            }
                        }

                        postStack.Push(embed.Build());
                    }

                    foreach (var embed in postStack)
                    {
                        foreach (var channelBinding in subreddit.ChannelBindings)
                        {
                            if (client.GetChannel(channelBinding.ChannelId) is IMessageChannel channel)
                            {
                                await channel.SendEmbedAsync(embed);
                            }
                        }
                    }

                    if (newestCreationTime > subreddit.LatestPost)
                    {
                        subreddit.LatestPost = newestCreationTime;
                    }

                    db.RedditBindings.Update(subreddit);
                }

                await db.SaveChangesAsync();
            }
        }
Exemplo n.º 14
0
 public RedditService(RedditContext redditContext)
 {
     this.redditContext = redditContext;
 }
Exemplo n.º 15
0
 public IEnumerable <RedditChannelBinding> GetBindings(IMessageChannel channel)
 {
     using (var db = new RedditContext(dbOptions))
         return(db.RedditChannelBindings.Where(b => b.ChannelId == channel.Id).Include(b => b.Subreddit).ToList());
 }
 public RedditRepository(RedditContext redditContext)
 {
     this.RedditContext = redditContext;
 }
Exemplo n.º 17
0
 public PostsController(RedditContext context)
 {
     dbContext = context;
 }
Exemplo n.º 18
0
 public Auth(RedditContext db, User currentUser)
 {
     this.db          = db;
     this.currentUser = currentUser;
 }
Exemplo n.º 19
0
 public Voter(RedditContext db)
 {
     this.db = db;
 }
Exemplo n.º 20
0
 public UserCrud(RedditContext db)
 {
     this.db = db;
 }
Exemplo n.º 21
0
 public PostRepository(RedditContext postsDb)
 {
     this.postsDb = postsDb;
 }