Ejemplo n.º 1
0
        public List <string> statistics()
        {
            WebOperationContext.Current.OutgoingResponse.Headers.Set("Access-Control-Allow-Origin", "*");
            List <string> myList = new List <string>();

            myList.Add("UID\tTCount\tMSGCount\tLikesCount");
            List <UserDetais> uList = new List <UserDetais>();

            foreach (string key in hashUserDetails.Keys)
            {
                uList.Add((UserDetais)hashUserDetails[key]);
            }
            UserDetais[] ud   = uList.ToArray();
            UserDetais   temp = new UserDetais();

            for (int j = 0; j < ud.Length; j++)
            {
                for (int i = 0; i < ud.Length - 1; i++)
                {
                    if (ud[i].points < ud[i + 1].points)
                    {
                        temp      = ud[i];
                        ud[i]     = ud[i + 1];
                        ud[i + 1] = temp;
                    }
                }
            }
            for (int i = 0; i < 3 && i < ud.Length; i++)
            {
                string st = ud[i].userId + "\t" + ud[i].tweetCount + "\t" + ud[i].msgCount + "\t" + ud[i].likeCount;
                myList.Add(st);
            }
            return(myList);
        }
Ejemplo n.º 2
0
        public string addMessage(string msg)
        {
            WebOperationContext.Current.OutgoingResponse.Headers.Set("Access-Control-Allow-Origin", "*");
            if (!(hashAuthenticateUsers.ContainsKey(getUserName())))
            {
                return("you are not authenticated yet");
            }
            string userId  = getUserName();
            int    tweetId = (int)(hashTweetUsers[userId]);
            Tweet  tweet   = (Tweet)hashTweet[tweetId];

            if (tweet.message.Count <= 1001)
            {
                Message g_idObject = (Message)tweet.message[0];
                int     g_id       = g_idObject.messageId;
                g_id++;
                Message newObject = new Message(tweetId, g_id, msg, userId, true, 0, 0, false);
                tweet.noOfMessages++;
                tweet.message.Add(g_id, newObject);
                tweet.message.Remove(0);
                Message g_Object = new Message(tweetId, g_id, "", "", false, 0, 0, false);
                tweet.message.Add(0, g_Object);
                UserDetais ud = (UserDetais)hashUserDetails[userId];
                ud.msgCount++;
                ud.points = ud.points + 3;
                writeToFileUserDetails("D:\\Users.xml");
                writeToFileTweet("D:\\Tweets.xml");
                writeToFileMessage("D:\\Message.xml");
                return("message Added successfully with id:" + g_id);
            }
            else
            {
                return("Max Message Limit exceeded");
            }
        }
Ejemplo n.º 3
0
        //this is used to delete a tweet

        public string deleteTweet(string tweetId)
        {
            WebOperationContext.Current.OutgoingResponse.Headers.Set("Access-Control-Allow-Origin", "*");
            if (!(hashAuthenticateUsers.ContainsKey(getUserName())))
            {
                return("you are not authenticated yet");
            }
            int    id     = int.Parse(tweetId);
            string userId = getUserName();

            if (hashTweet.ContainsKey(id))
            {
                Tweet tweet = (Tweet)hashTweet[id];
                if (tweet.userId == userId)
                {
                    hashTweet.Remove(id);
                    UserDetais ud = (UserDetais)hashUserDetails[userId];
                    ud.tweetCount--;
                    ud.points = ud.points - 5;
                    writeToFileUserDetails("D:\\Users.xml");
                    writeToFileTweet("D:\\Tweets.xml");
                    writeToFileMessage("D:\\Message.xml");
                    return("Deleted Successfully");
                }
                else
                {
                    return("you are not authenticated to delete the tweet");
                }
            }
            else
            {
                return("TweetId doesn't exists");
            }
        }
Ejemplo n.º 4
0
        // create Tweet function


        public string createTweet(string tweetName)
        {
            WebOperationContext.Current.OutgoingResponse.Headers.Set("Access-Control-Allow-Origin", "*");
            if (!(hashAuthenticateUsers.ContainsKey(getUserName())))
            {
                return("you are not authenticated yet");
            }
            string userId = getUserName();

            if (hashTweet.Count <= 50)
            {
                Tweet obj     = (Tweet)hashTweet[0];
                int   tweetid = obj.tweetId;
                tweetid++;
                obj.tweetId++;
                Tweet   newTweet      = new Tweet(tweetid, tweetName, 0, userId, false);
                Message messageObject = new Message(tweetid, 0, "", "", false, 0, 0, false);
                newTweet.noOfMessages++;
                newTweet.message.Add(0, messageObject);
                hashTweet.Add(tweetid, newTweet);
                UserDetais ud = (UserDetais)hashUserDetails[userId];
                ud.tweetCount++;
                ud.points = ud.points + 5;
                writeToFileUserDetails("D:\\Users.xml");
                writeToFileTweet("D:\\Tweets.xml");
                writeToFileMessage("D:\\Message.xml");
                return("tweet created with ID :" + tweetid);
            }
            return("Exceeds Max Tweet Limit");
        }
Ejemplo n.º 5
0
        //this is the function that authenticates the user

        public string authenticate(string userId)
        {
            if (serverStarted)
            {
                //Tweet tweet1 = new Tweet(0, "", 0, "", false);
                //hashTweet.Add(0, tweet1);
                //writeToFileTweet("F:\\Tweets.xml");
                readFromFile();
                serverStarted = false;
            }
            if (isValid(userId.ToLower()))
            {
                if (!hashAuthenticateUsers.ContainsKey(userId.ToLower()))
                {
                    if (hashAuthenticateUsers.ContainsKey(userId))
                    {
                        return("Already a user logged on from the remote system,Please Logout and then login again");
                    }
                    hashAuthenticateUsers[userId.ToLower()] = true;
                    if (!hashUserDetails.ContainsKey(userId))
                    {
                        hashUserDetails[userId] = new UserDetais(userId, 0, 0, 0, 0);
                    }
                    hashTweetUsers[userId.ToLower()] = null;
                    WebOperationContext.Current.OutgoingResponse.Headers.Set("Access-Control-Allow-Origin", "*");
                    return("authenticated");
                }
            }

            return("not authenticated");
        }
Ejemplo n.º 6
0
        // Delete a Message

        public string deleteMessage(string msgId)
        {
            WebOperationContext.Current.OutgoingResponse.Headers.Set("Access-Control-Allow-Origin", "*");
            if (!(hashAuthenticateUsers.ContainsKey(getUserName())))
            {
                return("you are not authenticated yet");
            }
            string userId    = getUserName();
            int    tweetId   = (int)(hashTweetUsers[userId]);
            Tweet  tweet     = (Tweet)hashTweet[tweetId];
            int    id        = int.Parse(msgId);
            int    msg_count = tweet.noOfMessages;

            if (tweet.message.ContainsKey(id))
            {
                Message msg = (Message)tweet.message[id];
                if (msg.userId == userId)
                {
                    tweet.message.Remove(id);
                    UserDetais ud = (UserDetais)hashUserDetails[userId];
                    ud.msgCount--;
                    ud.points = ud.points - 3;
                    tweet.noOfMessages--;
                    for (int i = 0; i < msg_count; i++)
                    {
                        foreach (int key in tweet.message.Keys)
                        {
                            Message msg1 = (Message)tweet.message[key];
                            if (msg1.replyTo == id)
                            {
                                tweet.message.Remove(msg1.messageId);
                                ud = (UserDetais)hashUserDetails[msg1.userId];
                                ud.msgCount--;
                                ud.points = ud.points - 3;
                                tweet.noOfMessages--;
                                break;
                            }
                        }
                    }
                    writeToFileUserDetails("D:\\Users.xml");
                    writeToFileTweet("D:\\Tweets.xml");
                    writeToFileMessage("D:\\Message.xml");
                    return("Message successfully Deleted");
                }
                else
                {
                    return("you are not autherized to delete the message");
                }
            }
            return("Message Id doesn't Exists");
        }
Ejemplo n.º 7
0
        //reply to a Message

        public string replyToMessage(string msgId, string msg)
        {
            WebOperationContext.Current.OutgoingResponse.Headers.Set("Access-Control-Allow-Origin", "*");

            if (!(hashAuthenticateUsers.ContainsKey(getUserName())))
            {
                return("you are not authenticated yet");
            }
            string userId  = getUserName();
            int    tweetId = (int)(hashTweetUsers[userId]);
            Tweet  tweet   = (Tweet)hashTweet[tweetId];
            int    id      = int.Parse(msgId);

            if (tweet.message.ContainsKey(id))
            {
                Message msgObject = (Message)tweet.message[id];
                if (msgObject.isReply)
                {
                    if (tweet.message.Count <= 1001)
                    {
                        Message g_idObject = (Message)tweet.message[0];
                        int     g_id       = g_idObject.messageId;
                        g_id++;
                        Message newObject = new Message(tweetId, g_id, msg, userId, false, id, 0, false);
                        tweet.message.Add(g_id, newObject);
                        tweet.noOfMessages++;
                        tweet.message.Remove(0);
                        Message g_Object = new Message(tweetId, g_id, "", "", false, 0, 0, false);
                        tweet.message.Add(0, g_Object);
                        UserDetais ud = (UserDetais)hashUserDetails[userId];
                        ud.msgCount++;
                        ud.points = ud.points + 3;
                        writeToFileUserDetails("D:\\Users.xml");
                        writeToFileTweet("D:\\Tweets.xml");
                        writeToFileMessage("D:\\Message.xml");
                        return("reply Added successfully to id:" + id);
                    }
                    else
                    {
                        return("Max Message Limit exceeded");
                    }
                }
                else
                {
                    return("you cannot reply to this because it is a reply");
                }
            }
            return("Id doesn't Exists");
        }
Ejemplo n.º 8
0
        public void writeToFileUserDetails(string fileName)
        {
            FileStream        writer = new FileStream(fileName, FileMode.Create);
            List <UserDetais> myList = new List <UserDetais>();

            foreach (string key in hashUserDetails.Keys)
            {
                UserDetais user = (UserDetais)hashUserDetails[key];
                myList.Add(user);
            }
            DataContractSerializer ser =
                new DataContractSerializer(typeof(List <UserDetais>));
            DataContractJsonSerializer ser1 = new DataContractJsonSerializer(typeof(List <UserDetais>));

            ser.WriteObject(writer, myList);
            writer.Close();
        }
Ejemplo n.º 9
0
        //Like message
        public string likeMessage(string msgId)
        {
            WebOperationContext.Current.OutgoingResponse.Headers.Set("Access-Control-Allow-Origin", "*");
            string        userId        = getUserName();
            int           tweetId       = (int)(hashTweetUsers[userId]);
            Tweet         tweet         = (Tweet)hashTweet[tweetId];
            int           id            = int.Parse(msgId);
            List <string> myList        = new List <string>();
            Message       messageObject = (Message)tweet.message[id];

            myList = messageObject.userList;
            string[] users = myList.ToArray();
            for (int i = 0; i < users.Length; i++)
            {
                if (users[i] == userId)
                {
                    return("The user Already Liked the message");
                }
            }
            //IEnumerator ie = myList.GetEnumerator();
            //while (ie.MoveNext())
            //{
            //    if (ie.ToString() == userId)
            //    {
            //        return "The user Already Liked the message";
            //    }
            //}
            UserDetais ud = (UserDetais)hashUserDetails[userId];

            ud.likeCount++;
            ud.points = ud.points + 1;
            myList.Add(userId);
            messageObject.userList = myList;
            messageObject.likes++;
            //tweet.message.Remove(id);
            //tweet.message.Add(id, messageObject);
            writeToFileUserDetails("D:\\Users.xml");
            writeToFileTweet("D:\\Tweets.xml");
            writeToFileMessage("D:\\Message.xml");
            return("Message Successfully Liked");
        }
Ejemplo n.º 10
0
        //Reading from file

        public void readFromFile()
        {
            //Console.WriteLine("Deserializing an instance of the object.");
            FileStream fs;

            try
            {
                fs = new FileStream("D:\\Tweets.xml", FileMode.Open);
                //FileStream fs1 = new FileStream("object.json", FileMode.Open);

                XmlDictionaryReader reader =
                    XmlDictionaryReader.CreateTextReader(fs, new XmlDictionaryReaderQuotas());

                //JsonReaderWriterFactory reader1 =
                //    JsonReaderWriterFactory.CreateJsonReader(fs1, new XmlDictionaryReaderQuotas());
                List <Tweet>           myList = new List <Tweet>();
                DataContractSerializer ser    = new DataContractSerializer(typeof(List <Tweet>));
                //DataContractJsonSerializer ser1 = new DataContractJsonSerializer(typeof(List<Tweet>));

                // Deserialize the data and read it from the instance.

                List <Tweet> deserializedTweet =
                    (List <Tweet>)ser.ReadObject(reader);
                //hashTweet.Add(deserializedTweet.tweetId, deserializedTweet);
                //IEnumerator ie = deserializedTweet.GetEnumerator();
                Tweet[] tweets = deserializedTweet.ToArray();
                for (int i = 0; i < tweets.Length; i++)
                {
                    //Tweet tweet = (Tweet)ie;
                    Tweet newTweet = new Tweet(tweets[i].tweetId, tweets[i].tweetName, tweets[i].noOfMessages, tweets[i].userId, tweets[i].isOffensive);
                    if (tweets[i].tweetName == "")
                    {
                        hashTweet.Add(0, newTweet);
                        continue;
                    }
                    hashTweet.Add(tweets[i].tweetId, newTweet);
                }

                reader.Close();
                fs.Close();
            }
            catch (FileNotFoundException)
            {
                fs = new FileStream("D:\\Tweets.xml", FileMode.Create);
                Tweet        newObject = new Tweet(0, "", 0, "", false);
                List <Tweet> myList    = new List <Tweet>();
                myList.Add(newObject);
                DataContractSerializer ser =
                    new DataContractSerializer(typeof(List <Tweet>));
                DataContractJsonSerializer ser1 = new DataContractJsonSerializer(typeof(List <Tweet>));
                ser.WriteObject(fs, myList);
                hashTweet.Add(0, newObject);
                fs.Close();
            }
            FileStream fs_msg;

            try
            {
                // To read Messages
                fs_msg = new FileStream("D:\\Message.xml", FileMode.Open);
                if (fs_msg.Length != 0)
                {
                    XmlDictionaryReader reader_msg =
                        XmlDictionaryReader.CreateTextReader(fs_msg, new XmlDictionaryReaderQuotas());

                    DataContractSerializer ser_msg = new DataContractSerializer(typeof(List <Message>));
                    //DataContractJsonSerializer ser1 = new DataContractJsonSerializer(typeof(List<Tweet>));
                    //List<Message>
                    List <Message> deserializedMessage =
                        (List <Message>)ser_msg.ReadObject(reader_msg);

                    Message[] message = deserializedMessage.ToArray();
                    for (int i = 0; i < message.Length; i++)
                    {
                        Message msg   = new Message(message[i].tweetId, message[i].messageId, message[i].message, message[i].userId, message[i].isReply, message[i].replyTo, message[i].likes, message[i].isOffensive);
                        Tweet   tweet = (Tweet)hashTweet[message[i].tweetId];
                        if (message[i].userId == "")
                        {
                            tweet.message.Add(0, msg);
                            continue;
                        }
                        string[] users = message[i].userList.ToArray();
                        for (int j = 0; j < users.Length; j++)
                        {
                            msg.userList.Add(users[j]);
                        }
                        tweet.message.Add(message[i].messageId, msg);
                    }
                    //FileStream fs1 = new FileStream("object.json", FileMode.Open);
                    fs_msg.Close();
                    reader_msg.Close();
                }
            }
            catch (FileNotFoundException)
            {
                fs_msg = new FileStream("D:\\Message.xml", FileMode.Create);
                fs_msg.Close();
            }
            FileStream fs_user;

            try
            {
                fs_user = new FileStream("D:\\Users.xml", FileMode.Open);
                if (fs_user.Length != 0)
                {
                    XmlDictionaryReader reader_user =
                        XmlDictionaryReader.CreateTextReader(fs_user, new XmlDictionaryReaderQuotas());
                    DataContractSerializer ser_user          = new DataContractSerializer(typeof(List <UserDetais>));
                    List <UserDetais>      deserializedUsers =
                        (List <UserDetais>)ser_user.ReadObject(reader_user);
                    UserDetais[] user = deserializedUsers.ToArray();
                    for (int i = 0; i < user.Length; i++)
                    {
                        UserDetais userObject = new UserDetais(user[i].userId, user[i].tweetCount, user[i].msgCount, user[i].likeCount, user[i].points);
                        hashUserDetails.Add(user[i].userId, userObject);
                    }
                }
                fs_user.Flush();
                fs_user.Close();
            }
            catch (FileNotFoundException)
            {
                fs_user = new FileStream("D:\\Users.xml", FileMode.Create);
                fs_user.Close();
            }
            //Tweet des = (Tweet)ser1.ReadObject(fs1);
            //Console.WriteLine(String.Format("{0} {1} {2} {3} {4}",
            //deserializedPerson.tweetId, deserializedPerson.tweetName,
            //deserializedPerson.noOfMessages, deserializedPerson.isOffensive, deserializedPerson.userId));
            //Console.WriteLine(String.Format("{0} {1}, ID: {2}", des.FirstName, des.LastName, des.ID));
        }