Closed Bug 1036171 Opened 10 years ago Closed 9 years ago

Don't Import Contacts from .Trash

Categories

(Firefox OS Graveyard :: Gaia::Contacts, defect)

defect
Not set
normal

Tracking

(Not tracked)

RESOLVED FIXED

People

(Reporter: dp, Assigned: ShellHacker, Mentored, NeedInfo)

References

Details

(Whiteboard: [good first bug])

Attachments

(3 files, 1 obsolete file)

User Agent: Mozilla/5.0 (Windows NT 6.1; rv:24.0) Gecko/20100101 Firefox/24.0 (Beta/Release)
Build ID: 20100101

Steps to reproduce:

Tap on Contacts App, tap on the gear, tap on Import contacts, Tapo on Memory card.


Actual results:

1446 Contacts are imported


Expected results:

723 Contacts should have been imported.

Being unsure of wheter the application will look for vcf in the SD card or in the phone memory, I placed a vcf (of 723 contacts each) in both supports. When I saw that the app looked into both, I connected the phone to my computer and moved through my file system window one of the vcf to the trash. Still, the app looked into /SDCard/.Trash and imported the contacts from the trash. I don't think this should happen, as it would be a problem for most of the unexperienced users. Contacts shouldn't be imported from .Trash
Component: General → Gaia::Contacts
Mentor: sergi.mansilla, francisco
Whiteboard: [good first bug]
Hey,

I've been looking through the code base and want to work on fixing this particular problem,I already have gaia set up.

I have a few doubts,
Is the code related to import from sd card at : /shared/js/contacts/import/utilities/sdcard.js

and, does a change have to happen here by adding an exception condition for the .Trashes 
```
SdCard.retrieveFiles = function retrieveFilesContent(mimes, exts, cb) {
    var fileArray = [];

    // getDeviceStorages('sdcard') can return more than one storage unit
    // in different order, depends on the default media storage setting,
    // so we need to iterate though them to be sure we checked the proper
    // SD Card
    var storages = navigator.getDeviceStorages('sdcard');
    var numberOfStorages = storages.length;
    var currentStorage = 0;

    var cursorOnError = function() {
      cb(this.error.name);
    };

    var cursorOnSuccess = function(e) {
      var file = e.target.result;
      // If we don't have any more files on the storage...
      if (!file) {
        // ... but we have more storages...
        if (++currentStorage < numberOfStorages) {
          // ... check the next one
          cursor = storages[currentStorage].enumerate();
          cursor.onsuccess = cursorOnSuccess;
          cursor.onerror = cursorOnError;
        } else {
          cb(null, fileArray);
        }
      } else {
        if ((mimes.indexOf(file.type) === -1) &&
          file.name.search(new RegExp('.(' + exts.join('|') + ')$')) === -1) {
          cursor.continue();
        } else {
          fileArray.push(file);
          cursor.continue();
        }
      }
    };

    var cursor = storages[currentStorage].enumerate();
    cursor.onsuccess = cursorOnSuccess;
    cursor.onerror = cursorOnError;
  };
```
Flags: needinfo?(sergi.mansilla)
Flags: needinfo?(francisco)
Hi Sudheesh!

Happy that you want to take this bug, do you know how to contribute to the gaia project? 

I can help you with that.

The first thing will be clonning gaia, so having your own repo, and do a pull request to the official mozilla gaia repo.

Then you link that pull request to this bug and we help you with the review process.

I was thinking using this fix to allow define maybe more directories, perhaps we can just start forbidding the .Trash directory, but we can make it generic, like an array of forbidden directories. What do you think?

If you need help with any of the steps just let me know!

Thanks again!
Flags: needinfo?(sudheesh1995)
Flags: needinfo?(sergi.mansilla)
Flags: needinfo?(francisco)
I have cloned the gaia project and tried setting it up, it was working fine and then I encountered a weird issue [1].

I was thinking of the same thing, to have an array of forbidden directories as strings. Am I looking at the correct code segment as of comment 1, the card import path has been commented out "/shared/js/contacts/import/utilities/sdcard.js"

Am I on the right file or do I have to look somewhere else ?

[1] https://gist.github.com/sudheesh001/77cf2610f74d1edf0f23
Status: UNCONFIRMED → NEW
Ever confirmed: true
Flags: needinfo?(sudheesh1995)
(In reply to Sudheesh Singanamalla (:ShellHacker) from comment #3)
> I was thinking of the same thing, to have an array of forbidden directories
> as strings. Am I looking at the correct code segment as of comment 1, the
> card import path has been commented out
> "/shared/js/contacts/import/utilities/sdcard.js"
> 
> Am I on the right file or do I have to look somewhere else ?
> 

Yes you are in the correct file :)

If we do changes there, that shared file will be used for importing contacts from the contacts app and the first time use app.
Attached patch patch_1036171 (obsolete) — Splinter Review
Assignee: nobody → sudheesh1995
Attachment #8459148 - Flags: feedback?
I put a patch here, in case this works, I will commit and send a pull request, I am feeling slightly confused about 
```
else {
        if ((mimes.indexOf(file.type) === -1) &&
          file.name.search(new RegExp('.(' + exts.join('|') + ')$')) === -1) {
          cursor.continue();
        } 
```
Comment on attachment 8459148 [details] [diff] [review]
patch_1036171

Redirecting the feedback to Sergi, since he created the sdcard part.

Sergi could you provide the feedback on Sudheesh work?
Attachment #8459148 - Flags: feedback? → feedback?(sergi.mansilla)
Comment on attachment 8459148 [details] [diff] [review]
patch_1036171

Hi sudheesh,

In your code, the line

var forbiddenLength = forbiddenDirectories.length;

should be

```
var forbiddenLength = setOfValues.length;
```

Otherwise this function would operate with two different base arrays (setOfValues and forbiddenDirectories), and since you want to make a generic function to find a value in an Array, that would yield incorrect values in some cases.

Moreover, the whole function `inArray` is unnecessary. You should be using Array.prototype.indexOf, which tells you if an array contains a particular item. In your case you would use it like this:

```
if (forbiddenDirectories.indexOf(file.name) === -1){
  fileArray.push(file);
  cursor.continue();
}
```

Also notice that you should check for `file.name`, and not `file` because we are dealing with strings.

About the snippet of code you are confused about, it is checking that the file type is NOT in `mimes` and that it has NOT any of the extensions passed as parameters. The code is admittedly confusing, and compiles a RegExp every time, which is inefficient. You could add the following changes to your patch in order to improve it:


diff --git a/shared/js/contacts/import/utilities/sdcard.js b/shared/js/contacts/import/utilities/sdcard.js
index c8f363c..94f3af2 100644
--- a/shared/js/contacts/import/utilities/sdcard.js
+++ b/shared/js/contacts/import/utilities/sdcard.js
@@ -101,6 +101,8 @@ if (!utils.sdcard) {
       cb(this.error.name);
     };

+    // RegExp that checks if a string contains any of the extensions.
+    var reExt = new RegExp('.(' + exts.join('|') + ')$');
     var cursorOnSuccess = function(e) {
       var file = e.target.result;
       // If we don't have any more files on the storage...
@@ -115,8 +117,7 @@ if (!utils.sdcard) {
           cb(null, fileArray);
         }
       } else {
-        if ((mimes.indexOf(file.type) === -1) &&
-          file.name.search(new RegExp('.(' + exts.join('|') + ')$')) === -1) {
+        if ((mimes.indexOf(file.type) === -1) && !reExt.test(file.name)) {
           cursor.continue();
         } else {
           fileArray.push(file);


Please let me know if that makes sense to you.
Attachment #8459148 - Flags: feedback?(sergi.mansilla) → feedback-
Thanks, I hope this patch works ?
Attachment #8459148 - Attachment is obsolete: true
Attachment #8460275 - Flags: feedback?(sergi.mansilla)
Can I know the status of this ? Should I send this as a PR ?
Comment on attachment 8460275 [details] [diff] [review]
Patch 1036171 Update

Asking Sergi for second round of reviews :)

Didn't do any review, but could we add unit tests to this? Sudheesh, if you don't know how, we can fill a follow up bug to add the test to this bug and land the code.
Attachment #8460275 - Flags: review?(sergi.mansilla)
I've never worked with test in gaia, maybe I could look through this if a new bug is filed for that and assigned :)
Yes,

unit test are mandatory for gaia code, but also we understand that are hard to learn. So as we like to get new contributors on board and help them to learn how it's the process, we can do that.

We create a new bug, we call it, Follow up bug 1036171 Add unit tests to 'Dont import contacts from .Trash' and we reference this bug in the new one.

So you have another bug to learn how to do the unit test, and you'll have learn everything you need :)
Blocks: 1045422
I've made the bug to track the unit test, I've been looking through the test_contacts.py which tests a mock contact data. I also understand that we have to write a new function

def test_retrieve_contact_sdcard():
Hi Sudheesh,

those are integration tests, we will need to add unit tests, since we will need to mock the use of a sdcard. You can take a look to the sdcard test file here:
https://github.com/mozilla-b2g/gaia/blob/master/apps/communications/contacts/test/unit/import_vcard_test.js

Thanks!
I think I understand whats happening there, Do I have to make a new multiple vCard for testing with the .Trash ?

This could be called vCardTrash = 'Dont Import' and the suiteSetup() function doesn't take place. ?
Hei Sergi,

any news with this review?
Flags: needinfo?(sergi.mansilla)
Comment on attachment 8460275 [details] [diff] [review]
Patch 1036171 Update

Changing the review to myself, since sergi seems to be not active.
Attachment #8460275 - Flags: review?(sergi.mansilla)
Attachment #8460275 - Flags: review?(francisco)
Attachment #8460275 - Flags: feedback?(sergi.mansilla)
Comment on attachment 8460275 [details] [diff] [review]
Patch 1036171 Update

LGTM, the only problem is the ".Trash", double quoted string, should be '.Trash', singled quoted.

Sudheesh, could you do a PR against gaia and assign to me the review?
Flags: needinfo?(sudheesh1995)
Attachment #8460275 - Flags: review?(francisco) → review+
Attached file Patch_1036171
Francisco, I've made the required changes and kept the patch upto date with master. Could you please review this now.

Thanks
Flags: needinfo?(sudheesh1995) → needinfo?(francisco)
Attachment #8682313 - Flags: review?(francisco)
Comment on attachment 8682313 [details] [review]
Patch_1036171

Looking perfect thanks!
Flags: needinfo?(francisco)
Attachment #8682313 - Flags: review?(francisco) → review+
Landed:

https://github.com/mozilla-b2g/gaia/commit/347b22c572fee1bfe97a455679a37c373c37a0e3

And nga branch:

5cc476b
Status: NEW → RESOLVED
Closed: 9 years ago
Resolution: --- → FIXED
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: