PolyAnno: Users

This is part of my series of posts about the PolyAnno project – more here

I never really got far enough into completing the more necessary parts of the project to implement anything useful or even working with the users, but I planned to allow the users data to record favourited data (an image or a transcription or translation section), and allow filtering by favourites in the University of Edinburgh specific website (see here for more information on the filtering). Additionally, users were setup to record links within their JSONs to the data they had worked on, and information about that (created a vector for example).

Ideally, further work needs to be done investigating the design of the users structure more carefully to ensure optimum usability, but obviously this also interplays with the gamifying nature of the project that was left underdeveloped too.

Basics

The users field of the setup options of PolyAnno takes the following format:

{
  "favourites": Boolean,
  "users_url": String
}

This assumes that if you GET the users_url URL with “/username/” and a specific username, or “/id/” and specific id number, then it will recieve the user info in a JSON format. So for example, if users_url is “www.example.com/users” then Polyanno can send a GET request to “www.example.com/users/username/user_one” to recieve user_one’s info as JSON.

Similarly if you PUT the users_url URL with the specific username added to the end and the fields to be updated as body parameters then it can be updated. So for example, if users_url* is also “www.example.com/users” then Polyanno can send a PUT request to “www.example.com/users/user_one” to update user_one’s info.

It also requires that the user info has at least the following fields in the following format:

{

  "username": String,

  "docs_edited": {
    "vectors" : {
      "created" : [],
      "edited" : [],
      "deleted" : []
    },
    "transcriptions" : {
      "created" : [],
      "edited" : [],
      "deleted" : []
    },
    "translations" : {
      "created" : [],
      "edited" : [],
      "deleted" : []
    }
  },

  "favourites" : [{
    "image_id" : String,
    "the_image" : {
      "type": Boolean,
      "default": false
    },
    "transcriptions" : [],
    "translations" : [],
    "vectors" : []
  }]
}

Data Structure

The Mongoose Schema for the users were:

{

	"username": String,

	"docs_edited": {
		"vectors" : {
			"created" : [],
			"edited" : [],
			"deleted" : []
		},
		"transcriptions" : {
			"created" : [],
			"edited" : [],
			"deleted" : []
		},
		"translations" : {
			"created" : [],
			"edited" : [],
			"deleted" : []
		}
	},

	"favourites" : [{
		"image_id" : String,
		"the_image" : {
			"type": Boolean,
			"default": false
		},
		"transcriptions" : [],
		"translations" : [],
		"vectors" : []
	}]

}

And this was handled in a similar manner to the transcriptions, translations, and vectors with the main differences being in the updating functions:

////unlike annos, the users are expecting to be updated one piece of info at a time - no arrays
exports.updateOne = function(req, res) {

    var updateDoc = setup.user_model.findOne({ "username" : req.params.username }); ///////
    updateDoc.exec(function(err, theuser) {

        if (err) {res.send(err)};

        var updateDocs = function(annoType, editType) {
        	if ( !setup.isUseless(req.body.docs_edited[annoType][editType]) ) { 
        		theuser.docs_edited[annoType][editType].addToSet(req.body.docs_edited[annoType][editType]);
        	};
        };

        var updateAllEdits = function(annoType) {
        	if (!setup.isUseless(req.body.docs_edited[annoType])) {
	        	updateDocs(annoType, "created");
	        	updateDocs(annoType, "edited");
	        	updateDocs(annoType, "deleted");
        	};
        };

        if (!setup.isUseless(req.body.docs_edited)) {
        	updateAllEdits("vectors");
        	updateAllEdits("transcriptions");
        	updateAllEdits("translations");
        };

        var updateFavourite = function(theFavourite) {
        	if (!setup.isUseless(req.body.favourites.the_image)) { theFavourite.the_image = req.body.favourites.the_image };
        	if (!setup.isUseless(req.body.favourites.translations)) { theFavourite.translations = [req.body.favourites.translations] };
        	if (!setup.isUseless(req.body.favourites.transcriptions)) { theFavourite.transcriptions = [req.body.favourites.transcriptions] };
        	return theFavourite;
        };

        var createNewFavourite = function() {
        	var theNew = { "image_id" : req.body.favourites.image_id };
        	var newFavourite = updateFavourite(theNew);
        	theuser.favourites.addToSet(newFavourite);
        };

        if (!setup.isUseless(req.body.favourites)) {
        	var existingFav = false;
        	theuser.favourites.forEach(function(userFavourite) {
        		if (userFavourite.image_id == req.body.favourites.image_id) {
        			userFavourite = updateFavourite(userFavourite);
        			existingFav = true;
        		};
        	});
        	if (existingFav == false) {	createNewFavourite(); };
        };

        if (!setup.isUseless(req.body.removefavourite)) {
        	var thefavourite;
        	theuser.favourites.forEach(function(userFavourite) {
        		if (theuser.favourites.image_id == req.body.removefavourite.image_id) {	thefavourite = userFavourite;	};
        	});
        	if (!setup.isUseless(req.body.removefavourite.the_image)) { thefavourite.the_image = false; };
        	if (!setup.isUseless(req.body.removefavourite.transcriptions)) { thefavourite.transcriptions.pull(req.body.removefavourite.transcriptions); };
        	if (!setup.isUseless(req.body.removefavourite.translations)) { thefavourite.translations.pull(req.body.removefavourite.translations); };
        };

        theuser.save(function(err) {
            if (err) {res.send(err)}
            else {res.json(theuser)};
        });

    });
};

 

Next: Further Product Development 1

This is part of my series of posts about the PolyAnno project – more here

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s