Ejemplo n.º 1
0
        public void respondToReadRequest(IWebSocketConnection socket)       //"r" operation, respond to it can be only "u" (update) or "n" (not found)
        {
            Vote vote = null;
            ServerClientEntity foundEntity = EntityRepository.GetEntityFromSetsByID(entity.Id);

            if (foundEntity != null)
            {
                try
                {
                    if (foundEntity is VotableItem)    //check if the entity we are responding with is a VotableItem or not, props to http://www.hanselman.com/blog/DoesATypeImplementAnInterface.aspx
                    {
                        vote = EntityRepository.allVotes.FirstOrDefault(x => x.subjectId == entity.Id && x.OwnerId == socket.ConnectionInfo.Cookies["user"]);
                    }
                    //var comments = new List<Comment>();

                    //comments = EntityRepository.allComments.Where(x => x.parentId == entity.Id).ToList();
                    //foreach (var comment in comments)
                    //{
                    //    entityOperation createCommentOp = new entityOperation { entity = comment, operation = 'u' };
                    //    createCommentOp.sendTo(socket);
                    //}
                }
                catch (Exception ex)
                {
                    if (ExceptionIsCriticalCheck.IsCritical(ex))
                    {
                        throw;
                    }
                }

                if (entity.version < foundEntity.version)
                {
                    //first possible outcome
                    entity = foundEntity;    //so if the version on client is current by any chance we send back the same as we received
                }
                //second possible outcome
                operation = 'u';
                //client must check whether the update he received has greater version of entity, if not, he knows he has the latest version of entity
                entity = foundEntity;
            }
            else
            {
                //third possible outcome
                operation = 'n';        //not found, nonexistent, in the entity field there is still the one that was in the request, so when it arrives back to the client he will know which request ended up not found
            }
            sendTo(socket);

            if (vote != null)   //votable Item needs to be sent to client before the vote on a votable itself
            {
                entityOperation sendVoteOp = new entityOperation {
                    entity = vote, operation = 'u'
                };
                sendVoteOp.sendTo(socket);
            }
        }
Ejemplo n.º 2
0
        private void respondToUpdateRequest(JObject obj)
        {
            var toUpdate = EntityRepository.GetEntityFromSetsByID(entity.Id);

            if (toUpdate != null)
            {
                var userId = fromUser.Id;

                if (toUpdate is Vote)
                {   // update the subject
                    Vote updatedVote = (Vote)toUpdate;
                    var  success     = true;
                    if (success)
                    {
                        var subject = EntityRepository.GetEntityFromSetsByID(updatedVote.subjectId);
                        subject.IncrementVersion();     ///we have to increment version of the subject too
                    }
                }
            }
        }
Ejemplo n.º 3
0
        internal void resolveEntityRequest(IWebSocketConnection socket, JObject receivedObj)
        {
            switch (operation)
            {
            case 'c':       //create new user generated entity
                /*
                 * //Example shows json which creates new Vote for the present user
                 * {
                 *   "msgType": "entity",
                 *   "operation": "c",
                 *   "className": "Vote",
                 *   "entity": {"subjectID": "voting/215", "Agrees": true}
                 * }
                 *
                 * // TODO implement create spam check here
                 */
            {
                try
                {
                    var  className  = (string)receivedObj["className"];
                    Type type       = Type.GetType("Dem2UserCreated." + className);
                    var  sourceJSON = receivedObj["entity"].ToString();

                    var instance = JsonConvert.DeserializeObject(sourceJSON, type, new IsoDateTimeConverter());
                    entity         = (ServerClientEntity)instance;
                    entity.OwnerId = fromUser.Id;
                    entity.Subscribe(fromUser);
                    switch (className)
                    {
                    case "Vote":
                        var newVote = (Vote)instance;              //the serialized entity must be initialized
                        newVote.InitVote(fromUser);                //the after initialization, we return the entity back
                        Console.WriteLine("Created new vote with Id {0}", newVote.Id);
                        break;

                    case "Comment":
                        var newComment = (Comment)instance;
                        EntityRepository.Add(newComment);
                        Console.WriteLine("Created new comment with Id {0}", newComment.Id);
                        break;

                    case "Listing":
                        var newListing = (Listing)instance;
                        newListing.JSONQuery.sourceJSON = receivedObj["entity"]["JSONQuery"].ToString();;
                        var added = EntityRepository.Add(newListing);
                        if (added == false)
                        {
                            entity = EntityRepository.allListings.Single(x => x.JSONQuery.sourceJSON == sourceJSON);
                            entity.Subscribe(fromUser);
                            sendTo(socket);
                        }

                        Console.WriteLine("Created new listing with Id {0}", newListing.Id);
                        break;

                    default:
                        break;
                    }

                    //var op = new entityOperation() { operation = 'c', entity = instance as ServerClientEntity };
                    //
                    Console.WriteLine("Object {0} created", instance.ToString());
                }
                catch (Exception)
                {
                    throw;
                }
            }
            break;

            case 'r':       //read an entity
                /*
                 * Example shows json for this branch
                 * {
                 * "msgType": "entity",
                 * "operation": "r",
                 * "entity":{
                 *    "Id": "user/132"
                 * }
                 * }*/
                respondToReadRequest(socket);
                break;

            case 'u':       //read an entity
                /*
                 * Example shows json for this branch
                 * {
                 * "msgType": "entity",
                 * "operation": "r",
                 * "entity":{
                 *    "Id": "user/132"
                 * }
                 * }*/
                respondToUpdateRequest(receivedObj);
                break;

            case 'd':     //delete an entity
                /*
                 * {
                 *  "msgType": "entity",
                 *  "operation": "d",
                 *  "entity": {"Id": "voting/215"}
                 * }
                 *
                 */
            {
                string IdPrefixStr = entity.Id.Split('/')[0];
                Type   type        = EntityRepository.GetRepoTypeById(IdPrefixStr);

                var instance = JsonConvert.DeserializeObject(receivedObj["entity"].ToString(), type, new IsoDateTimeConverter());

                {
                    var Id = (string)receivedObj["entity"]["Id"];
                    ServerClientEntity toDelete = EntityRepository.GetEntityFromSetsByID(Id);
                    var deletor = socket.ConnectionInfo.Cookies["user"];
                    if (deletor == toDelete.OwnerId)
                    {
                        var success = toDelete.Delete();
                        if (success)
                        {
                            if (toDelete is Vote)
                            {           // update the subject
                                Vote aVote   = (Vote)toDelete;
                                var  subject = EntityRepository.GetEntityFromSetsByID(aVote.subjectId);
                                subject.IncrementVersion();
                            }
                            var op = new entityOperation()
                            {
                                operation = 'd', entity = new ServerClientEntity(toDelete.Id, toDelete.version)
                            };
                            Dem2Hub.sendItTo(op, socket);
                        }
                    }
                    else
                    {
                        var err = new ServerError()
                        {
                            message = "sorry, you can't delete the entity id " + Id
                        };
                        Dem2Hub.sendItTo(err, socket);
                    }
                }
            }
            break;

            default:
                Console.WriteLine("Unknown type of request: {0}", operation);
                break;
            }
        }
Ejemplo n.º 4
0
        public static void ResolveMessage (string message, IWebSocketConnection socket)
        {
            JObject receivedObj = JObject.Parse(message);
            User user = null;
           
            switch ((string)receivedObj["msgType"])
            {
                case "login":
                    Console.WriteLine("Login request");
                    
                    User heWhoWantsToLogin = receivedObj["theUser"].ToObject<User>();
                    
                    if (heWhoWantsToLogin != null)
                    {   //login successful
                        heWhoWantsToLogin.connection = socket;
                        if (heWhoWantsToLogin.accessToken != null)
                        {
#if IS_RUNNING_ON_SERVER
                            using (WebClient asyncWebRequest = new WebClient())
                            {

                                asyncWebRequest.DownloadDataCompleted += heWhoWantsToLogin.ProcessAccesTokenCheckResponse;
                                socket.ConnectionInfo.Cookies["authentication"] = "awaitingFBResponse";

                                Uri urlToRequest = new Uri("https://graph.facebook.com/me?access_token=" + heWhoWantsToLogin.accessToken + "&fields=id,first_name,last_name,gender,link,installed,verified,picture");
                                asyncWebRequest.DownloadDataAsync(urlToRequest);

                            }
#else
                            if (heWhoWantsToLogin.accessToken == "TESTING_TOKEN_1")
                            {
                                heWhoWantsToLogin.LogInUser("{\"id\":\"1533221860\",\"first_name\":\"Ji\\u0159\\u00ed\",\"last_name\":\"\\u0160p\\u00e1c\",\"gender\":\"male\",\"link\":\"http:\\/\\/www.facebook.com\\/Capaj\",\"installed\":true,\"verified\":true,\"picture\":{\"data\":{\"url\":\"https:\\/\\/fbcdn-profile-a.akamaihd.net\\/hprofile-ak-prn1\\/27439_1533221860_985_q.jpg\",\"is_silhouette\":false}}}");
                            }
                        }

#endif
                        
                    }
                    else
                    {    //we don't know that user
                        Console.WriteLine("Login failed");
                    }
                    break;
                case "logout":
                    Console.WriteLine("logout request");
                    User heWhoWantsToLogout = JsonConvert.DeserializeObject<User>(message);
                    break;
                case "entity":
                    user = User.getUserFromSocket(socket);
                    if (user != null)
                    {
                        entityOperation op = JsonConvert.DeserializeObject<entityOperation>(message);
                        op.fromUser = user;
                        op.resolveEntityRequest(socket, receivedObj);
                    }
                    
                    break;
                case "Subscription":
                    /*
                   {
                       "msgType": "Subscription",
                       "operation": "d",
                       "entity": {"onEntityId": "voting/215"}
                   }  
                    */
                    user = User.getUserFromSocket(socket);
                    if (user != null)
                    {
                        var subsOp = (string)receivedObj["operation"];
                        var id = (string)receivedObj["operation"]["onEntityId"];
                        var entity = EntityRepository.GetEntityFromSetsByID(id);
                        switch (subsOp)
                        {
                            case "d":
                                entity.Unsubscribe(user);
                                break;
                            case "c":
                                entity.Subscribe(user);
                                break;
                            default:
                                break;
                        }
                    }
                    break;
                default: Console.WriteLine("Unrecognized msgType");
                    break;
            }
        }