Example #1
0
        public void ProcessRequest(HttpContext context)
        {
            string json = "{}";

            var repo = new Repository();
            var model = repo.GetUser(context.Request["username"]);
            var user = PlainUser.FromModel(model, true);

            if (user.InviteLevel > 0)
            {
                var invitedby = PlainUser.FromModel(repo.GetUser(model.FKUserIDInvitedBy ?? 0), false);

                json = JsonConvert.SerializeObject(new
                {
                    User = user,
                    InvitedBy = invitedby
                });
            }
            else
            {
                json = JsonConvert.SerializeObject(new
                {
                    User = user
                });
            }

            SetNoCaching(context);
            context.Response.ContentType = "text/json";
            context.Response.Write(json);
        }
Example #2
0
        public void ProcessRequest(HttpContext context)
        {
            string json;

            try
            {
                var requestBody = context.Request.Form["RequestBody"];
                var requestParams = API.GetInviteRequestParams(requestBody);
                var repo = new Repository();

                var inviter = repo.GetAuthenticatedUser(requestParams.InviterNick, requestParams.InviterUser, requestParams.InviterHost);
                Invite invite = repo.GetOrCreateInvite(inviter, requestParams.InviteeNick, requestParams.InviteeUser, requestParams.InviteeHost, requestParams.ChannelID);
                                               
                json = JsonConvert.SerializeObject(new
                {
                    success = true,                    
                    SUID = invite.SUID
                });
            }
            catch (Exception ex)
            {
                json = JsonConvert.SerializeObject(new
                {
                    success = false,
                    error = "There was an exception: " + ex.Message
                });
            }

            SetNoCaching(context);
            context.Response.ContentType = "text/json";
            context.Response.Write(json);
        }
Example #3
0
        public void ProcessRequest(HttpContext context)
        {
            string json;

            try
            {
                var repo = new Repository();

                API.AttrRequestParams requestParams = API.GetAttrRequestParams(GetRequestBody(context));

                if (requestParams.SetAttr == "nsfw")
                    repo.SetURLNSFW(requestParams.URLID);
                else if (requestParams.UnsetAttr == "nsfw")
                    repo.UnsetURLNSFW(requestParams.URLID);

                json = JsonConvert.SerializeObject(new
                {
                    success = true
                });
            }
            catch (Exception ex)
            {
                json = JsonConvert.SerializeObject(new
                {
                    success = false,
                    error = "There was an exception: " + ex.Message
                });
            }

            SetNoCaching(context);
            context.Response.ContentType = "text/json";
            context.Response.Write(json);
        }
Example #4
0
        public void ProcessRequest(HttpContext context)
        {
            string json;

            try
            {                
                var repo = new Repository();
                API.LoginRequestParams requestParams = API.GetLoginRequestParams(GetRequestBody(context));

                User user = repo.Login(requestParams.Username, requestParams.Password);
                
                if (user != null)
                {
                    Token token = repo.GetOrCreateToken(user);

                    // If authorizing via PM auth command, store the UserID for this auth token
                    if (!string.IsNullOrEmpty(requestParams.SUID))
                    {
                        repo.MatchUserToAuthToken(user, requestParams.SUID);
                        repo.SaveChanges();
                    }

                    List<FirstChannelVisit> channelsVisited = repo.GetFirstChannelVisits(user.UserID);

                    // TODO: Merge this up
                    foreach (var chan in channelsVisited)
                        chan.DateVisitDisplay = chan.DateVisit.ToString();
                                        
                    json = JsonConvert.SerializeObject(new
                    {
                        success = true,
                        User = PlainUser.FromModel(user),
                        Token = PlainToken.FromModel(token),
                        ChannelsVisited = channelsVisited
                    });
                }
                else
                {
                    json = JsonConvert.SerializeObject(new
                    {
                        success = false,
                        UserMessage = "Invalid login."
                    });
                }
            }
            catch (Exception ex)
            {
                json = JsonConvert.SerializeObject(new
                {
                    success = false,
                    error = "There was an exception: " + ex.Message
                });
            }

            SetNoCaching(context);
            context.Response.ContentType = "text/json";
            context.Response.Write(json);
        }
Example #5
0
        public void ProcessRequest(HttpContext context)
        {
            string json;

            try
            {
                var channelSlug = context.Request["channel"];
                var networkSlug = context.Request["network"];
                var dataset = context.Request["dataset"];
                var repo = new Repository();

                // Top 25 chatters by line count over last 10 days
                if (dataset == "lines")
                {
                    var data = repo.StatsLineCounts(channelSlug, networkSlug, 14, 20);

                    json = JsonConvert.SerializeObject(new
                    {
                        success = true,
                        labels = data.Select(d => d.Label).ToList(),
                        data = data.Select(d => d.Value).ToList()
                    });
                }

                // Line graph of top 10 chatters by cummulative interval
                else
                {
                    var labels = new List<string>();
                    var data = repo.StatsRace(channelSlug, networkSlug, 14, 10, labels);

                    json = JsonConvert.SerializeObject(new
                    {
                        success = true,
                        labels = labels,
                        series = data
                            .Select(d => d.Label)
                            .ToList(),
                        data = data
                            .Select(d => d.Values)
                            .ToList()
                    });
                }
            }
            catch (Exception ex)
            {
                json = JsonConvert.SerializeObject(new
                {
                    success = false,
                    error = "There was an exception: " + ex.Message
                });
            }

            SetNoCaching(context);
            context.Response.ContentType = "text/json";
            context.Response.Write(json);
        }
Example #6
0
        public void ProcessRequest(HttpContext context)
        {
            string json;

            try
            {
                var requestBody = context.Request.Form["RequestBody"];
                var requestParams = API.GetAuthRequestParams(requestBody);
                var repo = new Repository();

                if (repo.HaveAuth(requestParams.NetworkID, requestParams.Nick, requestParams.Username, requestParams.Host))
                {
                    json = JsonConvert.SerializeObject(new
                    {
                        success = true,
                        alreadyAuthenticated = true
                    });
                }
                else
                {
                    // Create a new auth record for the user, give him the UID for the web
                    Auth auth = new Auth
                    {
                        FKNetworkID = requestParams.NetworkID,
                        Nick = requestParams.Nick,
                        Username = requestParams.Username,
                        Host = requestParams.Host,
                        SUID = Utils.Get32ByteUID(),
                        DateIssued = DateTime.UtcNow
                    };

                    repo.AddAuth(auth);
                    repo.SaveChanges();

                    json = JsonConvert.SerializeObject(new
                    {
                        success = true,
                        ID = auth.AuthID,
                        SUID = auth.SUID
                    });
                }
            }
            catch (Exception ex)
            {
                json = JsonConvert.SerializeObject(new
                {
                    success = false,
                    error = "There was an exception: " + ex.Message
                });
            }

            SetNoCaching(context);
            context.Response.ContentType = "text/json";
            context.Response.Write(json);
        }
Example #7
0
        public void ProcessRequest(HttpContext context)
        {
            string json;
            
            try
            {
                var repo = new Repository();

                int fkChannelID = Convert.ToInt32(context.Request.Form["ChannelID"]);
                int messageLength = Convert.ToInt32(context.Request.Form["MessageLength"]);
                string nick = context.Request.Form["Nick"];

                Line line = new Line
                {
                    At = DateTime.UtcNow,
                    Nick = nick,
                    Length = messageLength,
                    FKChannelID = fkChannelID
                };

                if (!repo.OnIgnore(nick, fkChannelID))
                {
                    var lineID = repo.Line(line);

                    json = JsonConvert.SerializeObject(new
                    {
                        success = true,
                        ID = lineID
                    });
                }
                else
                {
                    json = JsonConvert.SerializeObject(new
                    {
                        success = false,
                        error = "Ignored nick"
                    });
                }
            }
            catch (Exception ex)
            {
                json = JsonConvert.SerializeObject(new
                {
                    success = false,
                    error = "There was an exception: " + ex.Message
                });
            }

            SetNoCaching(context);
            context.Response.ContentType = "text/json";
            context.Response.Write(json);
        }
Example #8
0
        public void ProcessRequest(HttpContext context)
        {
            var repo = new Repository();
            var networks = repo.GetNetworks();

            var plain = networks
                .Select(PlainNetwork.FromModelNetwork)
                .ToList();

            var json = JsonConvert.SerializeObject(new {
                Networks = plain
            });

            SetNoCaching(context);
            context.Response.ContentType = "text/json";
            context.Response.Write(json);
        }
Example #9
0
        public void ProcessRequest(HttpContext context)
        {
            string json;

            try
            {                
                var requestParams = API.GetMeRequestParams(GetRequestBody(context));
                var repo = new Repository();

                // Verify user token
                var user = repo.VerifyLoginToken(requestParams.Token);

                if (user != null)
                {
                    repo.UpdateSignature(user, requestParams.Signature);
                    repo.SaveChanges();

                    json = JsonConvert.SerializeObject(new
                    {
                        success = true
                    });
                }
                else
                {
                    json = JsonConvert.SerializeObject(new
                    {
                        success = false
                    });
                }
            }
            catch (Exception ex)
            {
                json = JsonConvert.SerializeObject(new
                {
                    success = false,
                    error = "There was an exception: " + ex.Message
                });
            }

            SetNoCaching(context);
            context.Response.ContentType = "text/json";
            context.Response.Write(json);
        }
Example #10
0
        public void ProcessRequest(HttpContext context)
        {
            _context = context;

            string json = "{}";

            try
            {
                var repo = new Repository();

                List<AutoNSFW> autoNSFW = repo.GetChannelAutoNSFWList(ChannelID);
                List<Ignore> ignores = repo.GetIgnores(ChannelID);
                
                json = JsonConvert.SerializeObject(new
                {
                    success = true,

                    autoNSFW = autoNSFW
                        .Select(PlainAutoNSFW.FromModel)
                        .ToList(),

                    ignores = ignores
                        .Select(PlainIgnore.FromModel)
                        .ToList()
                });
            }
            catch (Exception ex)
            {
                json = JsonConvert.SerializeObject(new
                {
                    success = false,
                    error = "There was an error: " + ex.Message
                });
            }

            SetNoCaching(context);
            context.Response.ContentType = "text/json";
            context.Response.Write(json);
        }
Example #11
0
        public void ProcessRequest(HttpContext context)
        {
            string json;

            try
            {
                var repo = new Repository();
                API.RegisterRequestParams requestParams = API.GetRegisterRequestParams(GetRequestBody(context));

                string username = requestParams.Username.Trim();

                if (!Irception.IsValidUsername(username))
                {
                    json = JsonConvert.SerializeObject(new
                    {
                        success = false,
                        UserMessage = "Usernames consist of a-z 0-9 _ -"
                    });
                }                
                else if (!repo.UsernameAvailable(username))
                {
                    json = JsonConvert.SerializeObject(new
                    {
                        success = false,
                        UserMessage = "Username taken."
                    });
                }
                else if (repo.InviteAccepted(requestParams.SUID))
                {
                    json = JsonConvert.SerializeObject(new
                    {
                        success = false,
                        UserMessage = "Invite already accepted."
                    });
                }
                else
                {
                    User user = repo.Register(username, requestParams.Password, requestParams.SUID);

                    if (user == null)
                        throw new Exception("Error during registration.");

                    Token token = repo.GetOrCreateToken(user);

                    List<FirstChannelVisit> channelsVisited = repo.GetFirstChannelVisits(user.UserID);

                    json = JsonConvert.SerializeObject(new
                    {
                        success = true,
                        User = PlainUser.FromModel(user),
                        Token = PlainToken.FromModel(token),
                        ChannelsVisited = channelsVisited
                    });
                }                
            }
            catch (Exception ex)
            {
                json = JsonConvert.SerializeObject(new
                {
                    success = false,
                    error = "There was an exception: " + ex.Message
                });
            }

            SetNoCaching(context);
            context.Response.ContentType = "text/json";
            context.Response.Write(json);
        }
Example #12
0
        public void ProcessRequest(HttpContext context)
        {
            _context = context;

            List<URL> urls = new List<URL>();
            string json = "{}";

            try
            {
                var repo = new Repository();
                Channel channel = repo.GetChannelBySlug(context.Request["network"], RequestChannel);

                if (channel == null)
                    throw new Exception("Channel doesn't exist.");

                var lastUrlID = URLUpdateHistoryID;                

                if (lastUrlID == 0)
                    urls = repo.GetURLs(channel.ChannelID);
                else
                {
                    var ids = repo.GetUpdatedURLs(channel.ChannelID, lastUrlID);

                    urls = repo.GetURLs(ids);
                }

                long lastURLUpdateHistoryID = repo.GetLastURLUpdateHistoryID();

                List<PlainURL> plains = urls
                    .Select(PlainURL.FromModel)
                    .ToList();

                if (lastUrlID == 0)
                {
                    json = JsonConvert.SerializeObject(new
                    {
                        success = true,
                        URLs = plains,
                        luuhid = lastURLUpdateHistoryID,
                        Channel = PlainChannel.FromModel(channel),
                        NetworkSlug = channel.Network.Slug,
                        NetworkName = channel.Network.Name,
                        ChannelSlug = channel.Slug
                    });
                }
                else
                {
                    json = JsonConvert.SerializeObject(new
                    {
                        success = true,
                        URLs = plains,
                        luuhid = lastURLUpdateHistoryID
                    });
                }
            }
            catch (Exception ex)
            {
                json = JsonConvert.SerializeObject(new
                {
                    success = false,
                    error = "There was an error: " + ex.Message
                });
            }

            SetNoCaching(context);
            context.Response.ContentType = "text/json";
            context.Response.Write(json);
        }
Example #13
0
        public void ProcessRequest(HttpContext context)
        {
            string json;

            try
            {
                var requestBody = context.Request.Form["RequestBody"];
                var requestParams = API.GetURLRequestParams(requestBody);
                var repo = new Repository();

                if (!repo.OnIgnore(requestParams.Nick, requestParams.ChannelID))
                {
                    URL url = new URL
                    {
                        At = DateTime.UtcNow,
                        Nick = requestParams.Nick,
                        URL1 = requestParams.URL,
                        FKChannelID = requestParams.ChannelID,
                        VoteCount = 0
                    };

                    if (requestParams.YouTube != null)
                    {
                        try
                        {
                            API.DecorateURLForYouTube(url, requestParams.YouTube);
                        }
                        catch (Exception)
                        {
                        }
                    }

                    int urlID = repo.URL(url);

                    json = JsonConvert.SerializeObject(new
                    {
                        success = true,
                        ID = urlID
                    });
                }
                else
                {
                    json = JsonConvert.SerializeObject(new
                    {
                        success = false,
                        error = "Ignored nick"
                    });
                }
            }
            catch (Exception ex)
            {
                json = JsonConvert.SerializeObject(new
                {
                    success = false,
                    error = "There was an exception: " + ex.Message
                });
            }

            SetNoCaching(context);
            context.Response.ContentType = "text/json";
            context.Response.Write(json);
        }