This repository has been archived by the owner on May 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
usergroups.js
125 lines (107 loc) · 3.35 KB
/
usergroups.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
const localPrefs = require("./localprefs")
// Groups are stored as:
// {
// id: (String, with the format not to be relied upon outside of this module)
// name: 'Name of group',
// description: 'Description of group in Markdown format (optional)',
// members: [
// '@id_of_a_user',
// '@id_of_another_user'
// ]
// }
function getFullLocalGroupInfo() {
return localPrefs.getUserGroups()
}
function setFullLocalGroupInfo(groups) {
localPrefs.setUserGroups(groups)
}
// Gets the names and IDs of groups, but not their members, in case that becomes a more time-consuming search later.
// This is a little roundabout for the way it's implemented right now, but I expect it will yield performance benefits if/when we persist this to SSB.
exports.getGroups = function(cb) {
const groups = getFullLocalGroupInfo().map((x) => { return { name: x.name, id: x.id, description: x.description || '' } })
cb(null, groups)
}
exports.addGroup = function(groupInfo, cb) {
if (!groupInfo.name || groupInfo.name.trim() == '') {
cb("Group must have a name")
}
// Good enough for now, until we find a way to persist this.
// This is purely to make sure the API references things by ID instead of by name, so we can swap out the implementation later.
var id = (new Date()).getTime()
var groups = getFullLocalGroupInfo()
var newGroup = { name: groupInfo.name, id: id, members: [] }
if (groupInfo.description)
newGroup.description = groupInfo.description
groups.push(newGroup)
setFullLocalGroupInfo(groups)
cb(null, id)
}
exports.updateGroup = function(groupId, groupInfo, cb) {
var groups = getFullLocalGroupInfo()
var updated = false
for (g in groups) {
if (groups[g].id == groupId) {
if (groupInfo.name && groupInfo.name.trim() != '') {
groups[g].name = groupInfo.name
updated = true
}
if (groupInfo.description) {
groups[g].description = groupInfo.description
updated = true
}
}
}
if (!updated)
cb("Group not found")
else {
setFullLocalGroupInfo(groups)
cb(null, true)
}
}
exports.deleteGroup = function(groupId, cb) {
var groups = getFullLocalGroupInfo()
groups = groups.filter((x) => { return x.id != groupId })
setFullLocalGroupInfo(groups)
cb(null, true)
}
exports.getMembers = function(groupId, cb) {
var groups = getFullLocalGroupInfo()
for (g in groups)
if (groups[g].id == groupId) {
cb(null, groupId, groups[g].members)
return
}
cb("Group not found")
}
exports.addMember = function(groupId, feedId, cb) {
var groups = getFullLocalGroupInfo()
for (g in groups)
if (groups[g].id == groupId) {
if (groups[g].members.indexOf(feedId) >= 0) {
cb("Group already has member")
return
} else {
groups[g].members.push(feedId)
setFullLocalGroupInfo(groups)
cb(null, true)
return
}
}
cb("Group not found")
}
exports.removeMember = function(groupId, feedId, cb) {
var groups = getFullLocalGroupInfo()
for (g in groups)
if (groups[g].id == groupId) {
if (groups[g].members.indexOf(feedId) < 0) {
cb("Group does not have member")
return
} else {
groups[g].members = groups[g].members.filter((x) => { return x != feedId })
setFullLocalGroupInfo(groups)
cb(null, true)
return
}
}
cb("Group not found")
}