Esempio n. 1
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
                    }
                }
            }
        }
Esempio n. 2
0
 public virtual bool RegisterVote(Vote vote)
 {
     return(EntityRepository.Add(vote));
 }
Esempio 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;
            }
        }
Esempio 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;
            }
        }