Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: thread save incorrectly rejects when no dialogue exists #247

Merged
merged 13 commits into from
Aug 1, 2017
Merged

Fix: thread save incorrectly rejects when no dialogue exists #247

merged 13 commits into from
Aug 1, 2017

Conversation

Minh-Ng
Copy link
Contributor

@Minh-Ng Minh-Ng commented Jul 26, 2017

Promise would reject upon successful creation of annotation on the backend due to attempted dereferences on null/undefined objects.

Might have future issues with
this.thread = this.thread || savedAnnotation.thread;
if create(annotationData) returns a null annotation object.
This is not likely, I just haven't read through all of AnnotationService.

For
this.annotations[tempIdx] = savedAnnotation;,
let me know if this.annotations[-1] is actually intentional. I saw that ImageAnnotator uses threads.page[-1] as a valid index.

@pramodsum
Copy link
Contributor

The line this.thread = this.thread || savedAnnotation.thread; was added so that either the thread will always have an associated threadNumber. If the thread has annotations already in it, then it will set the threadNumber to the one associated with all those annotations. If the thread was just created, then it'll set it as the threadNumber for the newly created annotation aka savedAnnotation

@pramodsum
Copy link
Contributor

pramodsum commented Jul 26, 2017

ImageAnnotator uses -1 to indicate that it is a single paged image. I have a separate PR (#249) to change page -1 to page 1 for images. Having -1 pages was just how it was implemented beforehand to indicate that we didn't care for pages in images, but that definitely changed once we added the multi-page image annotations.

@JustinHoldstock
Copy link
Contributor

After @pramodsum and @minhhnguyen's discussion, is this still a valid PR?

@Minh-Ng
Copy link
Contributor Author

Minh-Ng commented Jul 27, 2017

@JustinHoldstock
Yes it is still valid, if the code is left as is, newly created annotations without dialogues will be created on the server but throw an error on the client side. Please let me know if you see any unforseen errors that can occur with the changes.

@jeremypress
Copy link
Contributor

LGTM but someone with more annotations expertise should verify

Copy link
Contributor

@JustinHoldstock JustinHoldstock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you write a test for this?

@Minh-Ng
Copy link
Contributor Author

Minh-Ng commented Jul 28, 2017

tests incoming


if (this.dialog) {
// Add thread number to associated dialog and thread
if (this.dialog.element && this.dialog.element.dataset) {
this.dialog.element.dataset.threadNumber = this.thread;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be setting the dialog thread number to null
this.dialog.element.dataset.threadNumber = this.threadNumber;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pramodsum tests in and variable name updated

this.saveAnnotationToThread(savedAnnotation);
} else {
// Otherwise, replace temporary annotation with annotation saved to server
this.annotations[tempIdx] = savedAnnotation;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible that this.annotations[-1] would have tempAnnotation in it? Would we need to replace it with the correct one no matter what?

Copy link
Contributor

@pramodsum pramodsum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment above about setting the dialog thread number

Copy link
Contributor

@pramodsum pramodsum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some small cleanup and looks good to me!

* @param {Annotation} localAnnotation - The temporary annotation holding place of the server validated annotation
* @return {void}
*/
updateLocalAnnotationFromServer(serverAnnotation, localAnnotation) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for moving this out and writing tests! 👍

this.dialog.removeAnnotation(tempAnnotationID);
}
})
.then((savedAnnotation) => this.updateLocalAnnotationFromServer(savedAnnotation, tempAnnotation))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to keep the naming consistent. Can we make this method either updateTempAnnotation(...) or change tempAnnotation, tempIdx, and other temp variables to localAnnotation, etc?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changing all localAnnotation variable names to tempAnnotation

*
* @private
* @param {Annotation} serverAnnotation - The annotation returned by the backend. Use this as the source of truth
* @param {Annotation} localAnnotation - The temporary annotation holding place of the server validated annotation
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The temporary annotation stored locally holding place of the server validated annotation

* @return {void}
*/
updateLocalAnnotationFromServer(serverAnnotation, localAnnotation) {
const tempIdx = this.annotations.indexOf(localAnnotation);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

choose temp or local

this.dialog.element.dataset.threadNumber = this.threadNumber;
}

this.dialog.addAnnotation(serverAnnotation);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// Removes temporary/local annotation and replaces it with the annotation saved to server

stubs.saveAnnotationToThread = sandbox.stub(thread, 'saveAnnotationToThread');
thread.annotations.push(serverAnnotation)
thread.updateLocalAnnotationFromServer(replacementAnnotation, serverAnnotation);
thread.on('annotationcreateerror', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure the check on 'annotationcreateerror' needs to be in the unit tests for updateLocalAnnotationFromServer(). Might make sense to move the portion of code called in the catch block into it's own method and move this check there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I've removed this portion from the tests.

@pramodsum
Copy link
Contributor

Just make sure you test this thoroughly with all the different annotation types just to be sure 👍

@Minh-Ng
Copy link
Contributor Author

Minh-Ng commented Aug 1, 2017

I'll go through a quick sanity check prior to merging this

@Minh-Ng Minh-Ng merged commit 6b361ed into box:master Aug 1, 2017
@Minh-Ng Minh-Ng deleted the branch/rebaseToRemote branch August 1, 2017 16:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants