Skip to content

Commit 7aa3cb7

Browse files
committed
dev: puter.js support for group management
1 parent f0a6e49 commit 7aa3cb7

File tree

3 files changed

+64
-6
lines changed

3 files changed

+64
-6
lines changed

src/backend/src/entities/Group.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ module.exports = SimpleEntity({
2828
}
2929
},
3030
methods: {
31-
async get_client_value () {
32-
await this.fetch_members();
31+
async get_client_value (options = {}) {
32+
if ( options.members ) {
33+
await this.fetch_members();
34+
}
3335
const group = {
3436
uid: this.values.uid,
3537
metadata: this.values.metadata,
36-
members: this.values.members,
38+
...(options.members ? { members: this.values.members } : {}),
3739
};
3840
return group;
3941
}

src/backend/src/services/PermissionAPIService.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,10 @@ class PermissionAPIService extends BaseService {
105105
const svc_group = this.services.get('group');
106106
const uid = await svc_group.create({
107107
owner_user_id,
108-
// TODO: allow specifying these in request
108+
// TODO: includeslist for allowed 'extra' fields
109109
extra: {},
110-
metadata: {},
110+
// Metadata can be specified in request
111+
metadata: metadata ?? {},
111112
});
112113

113114
res.json({ uid });
@@ -228,14 +229,30 @@ class PermissionAPIService extends BaseService {
228229
const in_groups = await svc_group.list_groups_with_member(
229230
{ user_id: req.user.id });
230231

232+
const public_groups = await svc_group.list_public_groups();
233+
231234
res.json({
232235
owned_groups: await Promise.all(owned_groups.map(
233-
g => g.get_client_value())),
236+
g => g.get_client_value({ members: true }))),
234237
in_groups: await Promise.all(in_groups.map(
238+
g => g.get_client_value({ members: true }))),
239+
public_groups: await Promise.all(public_groups.map(
235240
g => g.get_client_value())),
236241
});
237242
}
238243
}).attach(router);
244+
245+
Endpoint({
246+
route: '/public-groups',
247+
methods: ['GET'],
248+
mw: [configurable_auth()],
249+
handler: async (req, res) => {
250+
res.json({
251+
user: this.global_config.default_user_group,
252+
temp: this.global_config.default_temp_group,
253+
});
254+
}
255+
}).attach(router);
239256
}
240257
}
241258

src/backend/src/services/auth/GroupService.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const { DB_WRITE } = require("../database/consts");
3333
*/
3434
class GroupService extends BaseService {
3535
static MODULES = {
36+
kv: globalThis.kv,
3637
uuidv4: require('uuid').v4,
3738
};
3839

@@ -46,6 +47,7 @@ class GroupService extends BaseService {
4647
*/
4748
_init () {
4849
this.db = this.services.get('database').get(DB_WRITE, 'permissions');
50+
this.kvkey = this.modules.uuidv4();
4951

5052
const svc_anomaly = this.services.get('anomaly');
5153
svc_anomaly.register('groups-user-hour', {
@@ -189,6 +191,42 @@ class GroupService extends BaseService {
189191
}
190192

191193

194+
/**
195+
* Lists public groups. May get groups from kv.js cache.
196+
*/
197+
async list_public_groups () {
198+
const public_group_uids = [
199+
this.global_config.default_user_group,
200+
this.global_config.default_temp_group,
201+
];
202+
203+
let groups = this.modules.kv.get(`${this.kvkey}:public-groups`);
204+
if ( groups ) {
205+
return groups;
206+
}
207+
208+
groups = await this.db.read(
209+
'SELECT * FROM `group` WHERE uid IN (' +
210+
public_group_uids.map(() => '?').join(', ') +
211+
')',
212+
public_group_uids,
213+
);
214+
for ( const group of groups ) {
215+
group.extra = this.db.case({
216+
mysql: () => group.extra,
217+
otherwise: () => JSON.parse(group.extra),
218+
})();
219+
group.metadata = this.db.case({
220+
mysql: () => group.metadata,
221+
otherwise: () => JSON.parse(group.metadata),
222+
})();
223+
}
224+
groups = groups.map(g => Group(g));
225+
this.modules.kv.set(`${this.kvkey}:public-groups`, groups, 60);
226+
return groups;
227+
}
228+
229+
192230
/**
193231
* Lists the members of a group by their username.
194232
*
@@ -206,6 +244,7 @@ class GroupService extends BaseService {
206244
);
207245
return users.map(u => u.username);
208246
}
247+
209248

210249

211250
/**

0 commit comments

Comments
 (0)