all files / src/ Group.js

64.29% Statements 27/42
40.74% Branches 11/27
75% Functions 3/4
64.29% Lines 27/42
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97                  15× 15×                                                                                                                          
'use strict';
 
var log = require('./Log.js').Logger('libZotero:Group');
 
module.exports = function (groupObj) {
	var group = this;
	group.instance = 'Zotero.Group';
	Eif(groupObj){
		this.parseJsonGroup(groupObj);
	}
};
 
module.exports.prototype = new Zotero.ApiObject();
 
module.exports.prototype.parseJsonGroup = function(groupObj) {
	var group = this;
	group.apiObj = groupObj;
};
 
module.exports.prototype.get = function(key) {
	var group = this;
	switch(key) {
		case 'title':
		case 'name':
			return group.apiObj.data.name;
		case 'members':
			if(!group.apiObj.data.members){
				return [];
			}
			return group.apiObj.data.members;
		case 'admins':
			if(!group.apiObj.data.admins){
				return [];
			}
			return group.apiObj.data.admins;
	}
	
	if(key in group.apiObj){
		return group.apiObj[key];
	}
	Eif(key in group.apiObj.data){
		return group.apiObj.data[key];
	}
	if(key in group.apiObj.meta){
		return group.apiObj.meta[key];
	}
	if(group.hasOwnProperty(key)){
		return group[key];
	}
	
	return null;
};
 
module.exports.prototype.isWritable = function(userID){
	var group = this;
	log.debug(group);
	let admins = group.apiObj.data.admins;
	if(!admins){
		admins = [];
	}
 
	switch(true){
		case group.get('owner') == userID:
			return true;
		case (admins.indexOf(userID) != -1):
			return true;
		case ((group.apiObj.data.libraryEditing == 'members') &&
			(group.apiObj.data.members) &&
			(group.apiObj.data.members.indexOf(userID) != -1)):
			return true;
		default:
			return false;
	}
};
 
module.exports.prototype.typeMap = {
	'Private': 'Private',
	'PublicOpen': 'Public, Open Membership',
	'PublicClosed': 'Public, Closed Membership'
};
 
module.exports.prototype.accessMap = {
	'all'     : {
		'members' : 'Anyone can view, only members can edit',
		'admins'  : 'Anyone can view, only admins can edit'
	},
	'members' : {
		'members' : 'Only members can view and edit',
		'admins'  : 'Only members can view, only admins can edit'
	},
	'admins'  : {
		'members' : 'Only admins can view, only members can edit',
		'admins'  : 'Only admins can view and edit'
	}
};