-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
135 lines (115 loc) · 3.54 KB
/
utils.js
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* Utilities Module
*/
const database = require('./database')
/*
* Returns an array of the most popular projects
*/
exports.getMostPopularProjects = (arr, count = undefined) => {
// Make a copy of the array to sort
let sortedArray = arr
// Sort array from highest to lowest values
sortedArray.sort((a, b) => a.collaborators.length < b.collaborators.length ? 1 : a.collaborators.length > b.collaborators.length ? -1 : 0)
if (count !== undefined) {
// Return an array of the number of the highest values requested
return sortedArray.slice(0, count - 1)
} else {
return sortedArray
}
}
/*
* Returns an array of the latest projects, users, or guilds (in order of creation date)
*/
exports.getLatest = (arr, count = undefined) => {
// Make a copy of the array to sort
let sortedArray = arr
// Sort array from the latest date to oldest
sortedArray.sort((a, b) => a.created_on < b.created_on ? 1 : a.created_on > b.created_on ? -1 : 0)
if (count !== undefined) {
// Return an array of the number of the latest objects requested
return sortedArray.slice(0, count - 1)
} else {
// Return an array of all objects in order of creation.
return sortedArray
}
}
/*
* Returns whether the user an owner of the project/guild
*/
exports.isUserOwner = (object, userObject) => {
let isOwner = false
if (userObject !== undefined) {
// Check if the user is one of the owners
for (let owner of object.owners) {
if (String(owner._id) === String(userObject._id)) {
isOwner = true
break
}
}
}
return isOwner
}
/*
* Returns whether the user a collaborator of the project
*/
exports.isUserProjectCollaborator = (projectObject, userObject) => {
let isCollaborator = false
if (userObject !== undefined) {
// Check if the user is one of the collaborators
for (let collaborator of projectObject.collaborators) {
if (String(collaborator._id) === String(userObject._id)) {
isCollaborator = true
break
}
}
}
return isCollaborator
}
/*
* Returns whether the user a member of the guild
*/
exports.isUserGuildMember = (guildObject, userObject) => {
let isMember = false
if (userObject !== undefined) {
// Check if the user is one of the members
for (let member of guildObject.members) {
if (String(member._id) === String(userObject._id)) {
isMember = true
break
}
}
}
return isMember
}
/*
* Returns whether the user is an owner or collaborator of the project
*/
exports.isUserInvolvedWithProject = (projectObject, userObject) => {
return !!(this.isUserOwner(projectObject, userObject) || this.isUserProjectCollaborator(projectObject, userObject));
}
/*
* Get all projects that the user has participated in
*/
exports.getInvolvedProjects = (userObject) => {
return database.getProjectsByOwner(userObject).then((ownedProjects) => {
return database.getProjectsByCollaborator(userObject).then((collabProjects) => {
return [...ownedProjects, ...collabProjects]
})
})
}
/*
* Returns whether the user is an owner or member of the guild
*/
exports.isUserInvolvedWithGuild = (guildObject, userObject) => {
return !!(this.isUserOwner(guildObject, userObject) || this.isUserGuildMember(guildObject, userObject));
}
/*
* Get all guilds that the user is involved in
*/
exports.getInvolvedGuilds = (userObject) => {
return database.getGuildsByOwner(userObject).then((ownedGuilds) => {
return database.getGuildsByMember(userObject).then((memberGuilds) => {
return [...ownedGuilds, ...memberGuilds]
})
})
}