public async Task <JsonResult> Put(string guid, decimal percentageRead) { string currentUserId = HttpContext.User.GetUserId(); Dictionary <string, object> response = new Dictionary <string, object>(); ApplicationUser user = await ControllerHelpers.GetCurrentUserAsync(_userManager, _dataAccess, currentUserId); if (user.EvernoteCredentials == null) { response["error"] = "You must authenticate with Evernote"; return(Json(response)); } IEvernoteService evernoteService = new EvernoteServiceSDK1(user.EvernoteCredentials); Bookmark bookmark = _dataAccess.GetAutomaticBookmark(currentUserId, guid); if (bookmark == null) { bookmark = new Bookmark() { NoteGuid = guid, Type = BookmarkType.Automatic, UserId = currentUserId, Updated = DateTime.Now }; } bookmark.PercentageRead = percentageRead; bookmark.Updated = DateTime.Now; _dataAccess.SaveBookmark(bookmark); return(Json(response)); }
public async Task <JsonResult> Delete(int id) { string currentUserId = HttpContext.User.GetUserId(); Dictionary <string, object> response = new Dictionary <string, object>(); ApplicationUser user = await ControllerHelpers.GetCurrentUserAsync(_userManager, _dataAccess, currentUserId); if (user.EvernoteCredentials == null) { response["error"] = "You must authenticate with Evernote"; return(Json(response)); } IEvernoteService evernoteService = new EvernoteServiceSDK1(user.EvernoteCredentials); Bookmark bookmark = _dataAccess.GetBookmarkById(id); if (bookmark == null) { response["error"] = "Unable to delete bookmark: no such bookmark"; return(Json(response)); } if (bookmark.UserId != currentUserId) { response["error"] = "Unable to delete bookmark: user not authorised"; return(Json(response)); } _dataAccess.DeleteBookmark(bookmark); return(Json(response)); }
public async Task <IActionResult> NoteResource(string resourceGuid) { string currentUserId = HttpContext.User.GetUserId(); ApplicationUser user = await ControllerHelpers.GetCurrentUserAsync(_userManager, _dataAccess, currentUserId); if (user.EvernoteCredentials == null) { return(View("MustAuthoriseEvernote")); } IEvernoteService evernoteService = new EvernoteServiceSDK1(user.EvernoteCredentials); Resource resource; try { resource = evernoteService.GetResource(resourceGuid); } catch (EvernoteServiceSDK1AuthorisationException) { // thrown if the user's credentials are no longer valid return(View("EvernoteAuthorisationError")); } if (!resource.Mime.StartsWith("image")) { return(View("UnknownResourceTypeError")); } if (resource.Data.Size > 1024 * 512) { return(new HttpNotFoundResult()); } return(File(resource.Data.Body, resource.Mime)); }
public async Task <JsonResult> Post(string guid, string bookmarkTitle, decimal percentageRead) { string currentUserId = HttpContext.User.GetUserId(); Dictionary <string, object> response = new Dictionary <string, object>(); if (bookmarkTitle == null || bookmarkTitle.Length > 64) { response["error"] = "Bookmark title cannot be empty, and must be no more than 64 characters."; return(Json(response)); } ApplicationUser user = await ControllerHelpers.GetCurrentUserAsync(_userManager, _dataAccess, currentUserId); if (user.EvernoteCredentials == null) { response["error"] = "You must authenticate with Evernote"; return(Json(response)); } IEvernoteService evernoteService = new EvernoteServiceSDK1(user.EvernoteCredentials); // TODO: check for valid note id here? it won't matter, because bookmarks always retrieved using userId + noteGuid // if (evernoteService.GetNote(guid) == null) // { // } Bookmark bookmark = new Bookmark() { NoteGuid = guid, Type = BookmarkType.Manual, BookmarkTitle = bookmarkTitle, PercentageRead = percentageRead, UserId = currentUserId, Updated = DateTime.Now }; _dataAccess.SaveBookmark(bookmark); response["id"] = bookmark.Id; return(Json(response)); }
public async Task <JsonResult> Get(string guid) { string currentUserId = HttpContext.User.GetUserId(); Dictionary <string, object> response = new Dictionary <string, object>(); ApplicationUser user = await ControllerHelpers.GetCurrentUserAsync(_userManager, _dataAccess, currentUserId); if (user.EvernoteCredentials == null) { response["error"] = "You must authenticate with Evernote"; return(Json(response)); } IEvernoteService evernoteService = new EvernoteServiceSDK1(user.EvernoteCredentials); List <Bookmark> bookmarks = _dataAccess.GetManualBookmarks(currentUserId, guid); var bookmarksForClient = from bookmark in bookmarks select new { bookmark.Id, bookmark.BookmarkTitle, bookmark.PercentageRead }; return(Json(bookmarksForClient)); }
private async Task <IActionResult> PerformSearch(FindNotesViewModel findNotesViewModel) { ApplicationUser user = await ControllerHelpers.GetCurrentUserAsync(_userManager, _dataAccess, HttpContext.User.GetUserId()); if (user.EvernoteCredentials == null) { return(View("MustAuthoriseEvernote")); } if (ModelState.IsValid) { findNotesViewModel.SearchPerformed = true; IEvernoteService evernoteService = new EvernoteServiceSDK1(user.EvernoteCredentials); try { ISearchResults searchResults = evernoteService.GetNotesMetaList( findNotesViewModel.SearchField, (Evernote.EDAM.Type.NoteSortOrder)findNotesViewModel.SortOrder, findNotesViewModel.SortAscending, findNotesViewModel.CurrentResultsPage, findNotesViewModel.PageSize); findNotesViewModel.NumberUnfilteredResults = searchResults.NotesMetadata.Count; if (findNotesViewModel.ExcludeShortNotes) { searchResults.NotesMetadata.RemoveAll(metadata => metadata.ContentLength < (1024 * 3)); } // now we populate with tags and notebook name foreach (INoteMetadata noteMetadata in searchResults.NotesMetadata) { if (noteMetadata.TagGuids != null) { List <TagData> tags = _dataAccess.GetCachedTagData(user.Id, noteMetadata.TagGuids); noteMetadata.TagNames = new List <string>(); foreach (string tagGuid in noteMetadata.TagGuids) { TagData tag = tags.FirstOrDefault(t => t.Guid == tagGuid); if (tag == null) { // save tag Tag evernoteTag = evernoteService.GetTag(tagGuid); tag = new TagData { Guid = tagGuid, Name = evernoteTag.Name, UserId = user.Id }; _dataAccess.SaveTag(tag); } noteMetadata.TagNames.Add(tag.Name); } } noteMetadata.NotebookName = _dataAccess.GetCachedNotebookName(user.Id, noteMetadata.NotebookGuid); if (noteMetadata.NotebookName == null) { Notebook notebook = evernoteService.GetNotebook(noteMetadata.NotebookGuid); noteMetadata.NotebookName = notebook.Name; NotebookData notebookData = new NotebookData() { UserId = user.Id, Guid = noteMetadata.NotebookGuid, Name = notebook.Name }; _dataAccess.SaveNotebook(notebookData); } } findNotesViewModel.SearchResults = searchResults.NotesMetadata.ConvertAll(noteMeta => new EverReaderNodeMetadataFormatter(noteMeta)); findNotesViewModel.TotalResultsForSearch = searchResults.TotalResults; } catch (EvernoteServiceSDK1AuthorisationException) { return(View("EvernoteAuthorisationError")); } } return(View("FindNotes", findNotesViewModel)); }
public async Task <IActionResult> Read(string guid) { string currentUserId = HttpContext.User.GetUserId(); ApplicationUser user = await ControllerHelpers.GetCurrentUserAsync(_userManager, _dataAccess, currentUserId); if (user.EvernoteCredentials == null) { return(View("MustAuthoriseEvernote")); } IEvernoteService evernoteService = new EvernoteServiceSDK1(user.EvernoteCredentials); Note note; try { note = evernoteService.GetNote(guid); } catch (EvernoteServiceSDK1AuthorisationException) { // thrown if the user's credentials are no longer valid return(View("EvernoteAuthorisationError")); } catch (EvernoteServiceSDK1NoteNotFoundException) { return(View("NoteNotFoundError")); } Bookmark bookmark = _dataAccess.GetAutomaticBookmark(currentUserId, guid); if (bookmark == null) { bookmark = new Bookmark() { NoteGuid = guid, PercentageRead = 0, UserId = currentUserId, Type = BookmarkType.Automatic, Updated = DateTime.Now }; } // update all the note metadata we store bookmark.NoteTitle = note.Title; bookmark.NoteLength = note.ContentLength; bookmark.NoteCreated = EvernoteSDKHelper.ConvertEvernoteDateToDateTime(note.Created); bookmark.NoteUpdated = EvernoteSDKHelper.ConvertEvernoteDateToDateTime(note.Updated); _dataAccess.SaveBookmark(bookmark); string decodedContent = WebUtility.HtmlDecode(note.Content); // ensure that links to other Evernote notes are directed through EverReader decodedContent = Regex.Replace(decodedContent, "<a href=\"evernote:///view/[^/]+/[^/]+/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})/", "<a href=\"/Reader/Read/$1"); // convert en-media tags to img tags // get a list of image resources, indexed to the MD5SUM hash if (note.Resources != null) { Dictionary <string, string> hashGuidMapping = new Dictionary <string, string>(); foreach (Resource resource in note.Resources) { if (resource.Mime.StartsWith("image") && resource.Data.Size < 1024 * 512) { hashGuidMapping.Add(resource.Data.BodyHash.ToHexString(), resource.Guid); } } // search and replace each resource in the document foreach (string hash in hashGuidMapping.Keys) { decodedContent = Regex.Replace(decodedContent, "en-media( [^>]*)? hash=[\"'](" + hash + ")[\"']", "img $1 src=\"/Reader/NoteResource/" + hashGuidMapping[hash] + "\""); decodedContent = Regex.Replace(decodedContent, "<img([^>]*);height:auto;([^>]*)>", "<img $1;$2>"); } } ENNoteINoteMetadataAdapter erNoteMetaData = new ENNoteINoteMetadataAdapter(note); // Create view model for page ReaderViewModel readerViewModel = new ReaderViewModel() { FormattedNoteMetadata = new EverReaderNodeMetadataFormatter(erNoteMetaData), Content = decodedContent, PercentageRead = bookmark == null ? 0 : bookmark.PercentageRead }; return(View(readerViewModel)); }