public async Task <IActionResult> GetItemsFromFolder(string id, string token)
        {
            try
            {
                var tokenManagerService = new TokenManagerService();
                if (tokenManagerService.ValidateToken(token, out userName, out hashKey))
                {
                    var user = await new UserService().GetUser(userName, hashKey);

                    var result = await access.GetAvailableItemsFromFolder(id, user.Id);

                    var parentFolder = await folderData.GetFolder(id);

                    var previusparent = new List <Object>()
                    {
                        new TimedModel()
                        {
                            PreviouseParent = parentFolder.ParentFolderId
                        }
                    };

                    return(Ok(result.Concat(previusparent)));
                }
                return(Unauthorized());
            }
            catch
            {
                return(BadRequest());
            }
        }
Example #2
0
        public override ClientCommandResponse Excecute()
        {
            if (!FolderClass.HasValue)
            {
                var folder = FolderService.GetFolder(StateManager.Credential, FolderId);
                FolderClass = folder.Type == eFolderType.DefaultContacts ? eFolderClass.Contacts : eFolderClass.Email;
            }
            var response = new ClientAddCommandResponse
            {
                FolderClass = FolderClass.Value,
                ClientId    = ClientId
            };

            switch (FolderClass)
            {
            case eFolderClass.Contacts:
                string serverId;
                var    contact = ((ContactAppData)ApplicationData).Contact;

                var serverAddResponse = ContactService.AddContact(StateManager.Credential, contact, out serverId);
                response.Status   = (eSyncStatus)serverAddResponse;
                response.ServerId = serverId;

                if (response.Status == eSyncStatus.Success)
                {
                    #region Load & Save Collection State

                    var collectionState = StateManager.LoadCollectionState((SyncKey)SyncKey, FolderId) ?? new CollectionState();
                    collectionState.AddItem(new SyncItemState {
                        ServerId = serverId, HashKey = ApplicationData.GenerateHash()
                    });

                    StateManager.SaveCollectionState(SyncKey, collectionState);
                    response.ServerId = serverId;

                    #endregion
                }
                break;

            case eFolderClass.Email:
                break;
            }
            return(response);
        }
        public override ClientCommandResponse Excecute()
        {
            if (!FolderClass.HasValue)
            {
                var folder = FolderService.GetFolder(StateManager.Credential, FolderId);
                FolderClass = folder.Type == eFolderType.DefaultContacts ? eFolderClass.Contacts : eFolderClass.Email;
            }
            switch (FolderClass)
            {
            case eFolderClass.Contacts:

                ContactService.DeleteContact(StateManager.Credential, ServerId);

                break;

            case eFolderClass.Email:
                break;
            }
            return(null);
        }
Example #4
0
        protected override PingResponse HandleRequest()
        {
            return(new PingResponse
            {
                Status = ePingStatus.HeartbeatExpiredWithNoChanges
            });

            var foundChanges           = false;
            var changedFolderServerIds = new List <string>();

            foreach (var pingRequestFolder in this.Folders)
            {
                SyncableFolder folder = null;
                try
                {
                    folder = FolderService.GetFolder(StateManager.Credential, pingRequestFolder.Id);
                }
                catch (Exception)
                {
                    //TODO: Add Fail Satus Collection
                    continue;
                }
                if (folder == null)
                {
                    //TODO: Add Fail Satus Collection
                    continue;
                }

                var allServerAppData    = new List <AppData>();
                var newAddedAppdata     = new List <AppData>();
                var newDeletedServerIds = new List <string>();
                var newChangedAppData   = new List <AppData>();

                if (folder.Type == eFolderType.DefaultContacts)
                {
                    var contacts = ContactService.GetContacts(StateManager.Credential, folder.Id);

                    allServerAppData.AddRange((contacts.Select(contact => new ContactAppData(contact)
                    {
                        ServerId = contact.Id
                    })));
                }
                else
                {
                    var emails = EmailService.GetEmails(StateManager.Credential, folder.Id);

                    allServerAppData.AddRange(emails.Select(email => new EmailAppData(email)
                    {
                        ServerId = email.Id
                    }));
                }
                string syncKey            = FileStateMachine.LastSyncKey;
                var    oldCollectionState = StateManager.LoadCollectionState(syncKey,
                                                                             folder.Id) ?? new CollectionState();

                newAddedAppdata     = GetNewDataItems(oldCollectionState, allServerAppData.ToArray());
                newDeletedServerIds = GetNewDeleteDataItems(oldCollectionState, allServerAppData.ToArray());
                newChangedAppData   = GetChangeDataItems(oldCollectionState, allServerAppData.ToArray());

                if (newAddedAppdata.Count > 0 || newDeletedServerIds.Count > 0 || newChangedAppData.Count > 0)
                {
                    changedFolderServerIds.Add(folder.Id);
                }
            }


            var pingResponse = new PingResponse()
            {
            };

            if (changedFolderServerIds.Count == 0)
            {
                return(pingResponse);
            }

            pingResponse.Status          = ePingStatus.ChangeOccurred;
            pingResponse.FolderServerIds = changedFolderServerIds;

            return(pingResponse);
        }
Example #5
0
        protected override SyncResponse HandleRequest()
        {
            var responseCollections = new List <ResponseCollection>();

            foreach (var collection in this.Collections)
            {
                var responseCollection = new ResponseCollection
                {
                    CollectionId = collection.CollectionId,
                };

                if (collection.SyncKey == "0")//Init Sync
                {
                    responseCollection.SyncKey = StateManager.GetNewSyncKey(collection.SyncKey);

                    responseCollection.Status = eSyncStatus.Success;
                    responseCollections.Add(responseCollection);
                    continue;
                }
                else
                {
                    SyncableFolder folder;
                    try
                    {
                        folder = FolderService.GetFolder(StateManager.Credential, collection.CollectionId);
                    }
                    catch (Exception)
                    {
                        responseCollection.Status = eSyncStatus.FolderHierarchyHasChanged;
                        responseCollections.Add(responseCollection);
                        continue;
                    }
                    if (folder == null)
                    {
                        responseCollection.Status = eSyncStatus.FolderHierarchyHasChanged;
                        responseCollections.Add(responseCollection);
                        continue;
                    }

                    #region Load Collection State

                    var collectionState = StateManager.LoadCollectionState((SyncKey)collection.SyncKey, collection.CollectionId) ?? new CollectionState()
                    {
                        FolderId = collection.CollectionId
                    };

                    #endregion

                    #region Run Client Commands

                    foreach (var commandResponse in collection.ClientCommands.Select(clientCommand => clientCommand.Excecute()))
                    {
                        if (commandResponse != null)
                        {
                            responseCollection.Responses.Add(commandResponse);
                        }
                    }

                    #endregion

                    var allServerAppData = new List <AppData>();
                    var folderClass      = eFolderClass.Email;

                    if (folder.Type == eFolderType.DefaultContacts)
                    {
                        folderClass = eFolderClass.Contacts;
                        var contacts = ContactService.GetContacts(StateManager.Credential, collection.CollectionId);

                        allServerAppData.AddRange((contacts.Select(contact => new ContactAppData(contact)
                        {
                            ServerId = contact.Id
                        })));
                    }
                    else
                    {
                        var emails          = EmailService.GetEmails(StateManager.Credential, collection.CollectionId);
                        var bodyContentType = eBodyContentType.HTML;
                        if (collection.Options != null && collection.Options.BodyPreference != null)
                        {
                            bodyContentType = collection.Options.BodyPreference.Type;
                        }

                        allServerAppData.AddRange(emails.Select(email => new EmailAppData(email)
                        {
                            ServerId = email.Id, BodyContentType = bodyContentType
                        }));
                    }

                    responseCollection.SyncKey = collection.SyncKey;

                    var newAddedAppdata     = GetNewDataItems(collectionState, allServerAppData.ToArray());
                    var newDeletedServerIds = GetNewDeleteDataItems(collectionState, allServerAppData.ToArray());
                    var newChangedAppData   = GetChangeDataItems(collectionState, allServerAppData.ToArray());
                    //var newSoftDeleteAppData = GetNewSoftDeleteDataItems(oldCollectionState, allServerAppData.ToArray());

                    responseCollection.Commands = new List <ServerCommand>();
                    foreach (var appData in newAddedAppdata)
                    {
                        responseCollection.Commands.Add(new AddServerCommand
                        {
                            ServerId = appData.ServerId,
                            AppData  = appData
                        });

                        collectionState.AddItem(new SyncItemState
                        {
                            ServerId = appData.ServerId,
                            HashKey  = appData.GenerateHash()
                        });
                    }
                    foreach (var deletedServerId in newDeletedServerIds)
                    {
                        responseCollection.Commands.Add(new DeleteServerCommand()
                        {
                            ServerId    = deletedServerId,
                            FolderClass = folderClass
                        });
                        collectionState.DeleteItem(deletedServerId);
                    }
                    foreach (var appData in newChangedAppData)
                    {
                        responseCollection.Commands.Add(new ChangeServerCommand()
                        {
                            ServerId = appData.ServerId,
                            AppData  = appData
                        });
                        collectionState.UpdateItem(appData.ServerId, appData.GenerateHash());
                    }
                    //foreach (var appData in newSoftDeleteAppData)
                    //{
                    //    responseCollection.Commands.Add(new SoftDeleteServerCommand()
                    //    {
                    //        ServerId = appData.ServerId
                    //    });
                    //    collectionState.DeleteItem(appData.ServerId);
                    //}
                    responseCollection.Status = eSyncStatus.Success;

                    StateManager.SaveCollectionState(responseCollection.SyncKey, collectionState);

                    //if (responseCollection.Commands.Count > 0)
                    responseCollections.Add(responseCollection);
                }
            }

            var syncResponse = new SyncResponse
            {
                Status      = eSyncStatus.Success,
                Collections = responseCollections
            };

            return(syncResponse);
        }