Completed
Pull Request — master (#76)
by Maxence
03:22
created

js/circles.js   B

Complexity

Conditions 1
Paths 2

Size

Total Lines 207

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
dl 0
loc 207
rs 8.2857
c 2
b 1
f 0
cc 1
nc 2
nop 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A circles.js ➔ Circles 0 3 1
B Circles.initialize 0 187 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
/*
2
 * Circles - Bring cloud-users closer together.
3
 *
4
 * This file is licensed under the Affero General Public License version 3 or
5
 * later. See the COPYING file.
6
 *
7
 * @author Maxence Lange <[email protected]>
8
 * @copyright 2017
9
 * @license GNU AGPL version 3 or any later version
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 */
25
26
/** global: OC */
27
/** global: OCA */
28
29
(function () {
30
31
32
	/**
33
	 * @constructs Circles
34
	 */
35
	var Circles = function () {
36
		this.initialize();
37
	};
38
39
	Circles.prototype = {
40
41
42
		initialize: function () {
43
44
			var self = this;
45
46
			this.createCircle = function (type, name, callback) {
47
48
				var result = {status: -1};
49
				$.ajax({
50
					method: 'PUT',
51
					url: OC.generateUrl('/apps/circles/v1/circles'),
52
					data: {
53
						type: type,
54
						name: name
55
					}
56
				}).done(function (res) {
57
					self.onCallback(callback, res);
58
				}).fail(function () {
59
					self.onCallback(callback, result);
60
				});
61
			};
62
63
64
			this.searchCircles = function (type, name, level, callback) {
65
				var result = {status: -1};
66
				$.ajax({
67
					method: 'GET',
68
					url: OC.generateUrl('/apps/circles/v1/circles'),
69
					data: {
70
						type: type,
71
						name: name,
72
						level: level
73
					}
74
				}).done(function (res) {
75
					self.onCallback(callback, res);
76
				}).fail(function () {
77
					self.onCallback(callback, result);
78
				});
79
			};
80
81
82
			this.detailsCircle = function (circleId, callback) {
83
				var result = {status: -1};
84
				$.ajax({
85
					method: 'GET',
86
					url: OC.generateUrl('/apps/circles/v1/circles/' + circleId)
87
				}).done(function (res) {
88
					self.onCallback(callback, res);
89
				}).fail(function () {
90
					self.onCallback(callback, result);
91
				});
92
			};
93
94
95
			this.addMember = function (circleId, member, callback) {
96
				var result = {status: -1};
97
				$.ajax({
98
					method: 'PUT',
99
					url: OC.generateUrl('/apps/circles/v1/circles/' + circleId + '/members'),
100
					data: {
101
						name: member
102
					}
103
				}).done(function (res) {
104
					self.onCallback(callback, res);
105
				}).fail(function () {
106
					self.onCallback(callback, result);
107
				});
108
			};
109
110
111
			this.removeMember = function (circleId, member, callback) {
112
				var result = {status: -1};
113
				$.ajax({
114
					method: 'DELETE',
115
					url: OC.generateUrl('/apps/circles/v1/circles/' + circleId + '/members'),
116
					data: {
117
						member: member
118
					}
119
				}).done(function (res) {
120
					self.onCallback(callback, res);
121
				}).fail(function () {
122
					self.onCallback(callback, result);
123
				});
124
			};
125
126
127
128
			this.levelMember = function (circleId, member, level, callback) {
129
				var result = {status: -1};
130
				$.ajax({
131
					method: 'POST',
132
					url: OC.generateUrl('/apps/circles/v1/circles/' + circleId + '/level'),
133
					data: {
134
						member: member,
135
						level: level
136
					}
137
				}).done(function (res) {
138
					self.onCallback(callback, res);
139
				}).fail(function () {
140
					self.onCallback(callback, result);
141
				});
142
			};
143
144
145
146
			this.joinCircle = function (circleId, callback) {
147
				var result = {status: -1};
148
				$.ajax({
149
					method: 'GET',
150
					url: OC.generateUrl('/apps/circles/v1/circles/' + circleId + '/join'),
151
					data: {}
152
				}).done(function (res) {
153
					self.onCallback(callback, res);
154
				}).fail(function () {
155
					self.onCallback(callback, result);
156
				});
157
			};
158
159
160
			this.leaveCircle = function (circleId, callback) {
161
				var result = {status: -1};
162
				$.ajax({
163
					method: 'GET',
164
					url: OC.generateUrl('/apps/circles/v1/circles/' + circleId + '/leave'),
165
					data: {}
166
				}).done(function (res) {
167
					self.onCallback(callback, res);
168
				}).fail(function () {
169
					self.onCallback(callback, result);
170
				});
171
			};
172
173
174
			this.destroyCircle = function (circleId, callback) {
175
				var result = {status: -1};
176
				$.ajax({
177
					method: 'DELETE',
178
					url: OC.generateUrl('/apps/circles/v1/circles/' + circleId),
179
					data: {}
180
				}).done(function (res) {
181
					self.onCallback(callback, res);
182
				}).fail(function () {
183
					self.onCallback(callback, result);
184
				});
185
			};
186
187
188
			this.shareToCircle = function (circleId, source, type, item, callback) {
189
				var result = {status: -1};
190
				$.ajax({
191
					method: 'PUT',
192
					url: OC.generateUrl('/apps/circles/v1/circles/' + circleId + '/share'),
193
					data: {
194
						source: source,
195
						type: type,
196
						item: item
197
					}
198
				}).done(function (res) {
199
					self.onCallback(callback, res);
200
				}).fail(function () {
201
					self.onCallback(callback, result);
202
				});
203
			};
204
205
206
			this.linkCircle = function (circleId, remote, callback) {
207
				var result = {status: -1};
208
				$.ajax({
209
					method: 'POST',
210
					url: OC.generateUrl('/apps/circles/v1/circles/' + circleId + '/link'),
211
					data: {
212
						remote: remote
213
					}
214
				}).done(function (res) {
215
					self.onCallback(callback, res);
216
				}).fail(function () {
217
					self.onCallback(callback, result);
218
				});
219
			};
220
221
222
			this.onCallback = function (callback, result) {
223
				if (callback && (typeof callback === "function")) {
224
					callback(result);
225
				}
226
			};
227
228
		}
229
230
	};
231
232
	OCA.Circles = Circles;
233
	OCA.Circles.api = new Circles();
234
235
})();
236
237
238