Skip to content

Commit d55f38c

Browse files
committed
feat: add /group/list endpoint
1 parent 3633349 commit d55f38c

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const { Context } = require("../util/context");
2+
3+
module.exports = function SimpleEntity ({ name, methods, fetchers }) {
4+
const create = function (values) {
5+
const entity = { values };
6+
Object.assign(entity, methods);
7+
for ( const fetcher_name in fetchers ) {
8+
entity['fetch_' + fetcher_name] = async function () {
9+
if ( this.values.hasOwnProperty(fetcher_name) ) {
10+
return this.values[fetcher_name];
11+
}
12+
const value = await fetchers[fetcher_name].call(this);
13+
this.values[fetcher_name] = value;
14+
return value;
15+
}
16+
}
17+
entity.context = values.context ?? Context.get();
18+
entity.services = entity.context.get('services');
19+
return entity;
20+
};
21+
22+
create.name = name;
23+
return create;
24+
};
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const SimpleEntity = require("../definitions/SimpleEntity");
2+
3+
module.exports = SimpleEntity({
4+
name: 'group',
5+
fetchers: {
6+
async members () {
7+
const svc_group = this.services.get('group');
8+
const members = await svc_group.list_members({ uid: this.values.uid });
9+
return members;
10+
}
11+
},
12+
methods: {
13+
async get_client_value () {
14+
await this.fetch_members();
15+
const group = {
16+
uid: this.values.uid,
17+
metadata: this.values.metadata,
18+
members: this.values.members,
19+
};
20+
return group;
21+
}
22+
}
23+
});

packages/backend/src/services/PermissionAPIService.js

+24
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,30 @@ class PermissionAPIService extends BaseService {
165165
res.json({});
166166
}
167167
}).attach(router);
168+
169+
Endpoint({
170+
route: '/list',
171+
methods: ['GET'],
172+
mw: [configurable_auth()],
173+
handler: async (req, res) => {
174+
const svc_group = this.services.get('group');
175+
176+
// TODO: validate string and uuid for request
177+
178+
const owned_groups = await svc_group.list_groups_with_owner(
179+
{ owner_user_id: req.user.id });
180+
181+
const in_groups = await svc_group.list_groups_with_member(
182+
{ user_id: req.user.id });
183+
184+
res.json({
185+
owned_groups: await Promise.all(owned_groups.map(
186+
g => g.get_client_value())),
187+
in_groups: await Promise.all(in_groups.map(
188+
g => g.get_client_value())),
189+
});
190+
}
191+
}).attach(router);
168192
}
169193
}
170194

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

+49
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const Group = require("../../entities/Group");
12
const BaseService = require("../BaseService");
23
const { DB_WRITE } = require("../database/consts");
34

@@ -44,6 +45,54 @@ class GroupService extends BaseService {
4445

4546
return uid;
4647
}
48+
49+
async list_groups_with_owner ({ owner_user_id }) {
50+
const groups = await this.db.read(
51+
'SELECT * FROM `group` WHERE owner_user_id=?',
52+
[owner_user_id],
53+
);
54+
for ( const group of groups ) {
55+
group.extra = this.db.case({
56+
mysql: () => group.extra,
57+
otherwise: () => JSON.parse(group.extra),
58+
})();
59+
group.metadata = this.db.case({
60+
mysql: () => group.metadata,
61+
otherwise: () => JSON.parse(group.metadata),
62+
})();
63+
}
64+
return groups.map(g => Group(g));
65+
}
66+
67+
async list_groups_with_member ({ user_id }) {
68+
const groups = await this.db.read(
69+
'SELECT * FROM `group` WHERE id IN (' +
70+
'SELECT group_id FROM `jct_user_group` WHERE user_id=?)',
71+
[user_id],
72+
);
73+
for ( const group of groups ) {
74+
group.extra = this.db.case({
75+
mysql: () => group.extra,
76+
otherwise: () => JSON.parse(group.extra),
77+
})();
78+
group.metadata = this.db.case({
79+
mysql: () => group.metadata,
80+
otherwise: () => JSON.parse(group.metadata),
81+
})();
82+
}
83+
return groups.map(g => Group(g));
84+
}
85+
86+
async list_members ({ uid }) {
87+
const users = await this.db.read(
88+
'SELECT u.username FROM user u ' +
89+
'JOIN (SELECT user_id FROM `jct_user_group` WHERE group_id = ' +
90+
'(SELECT id FROM `group` WHERE uid=?)) ug ' +
91+
'ON u.id = ug.user_id',
92+
[uid],
93+
);
94+
return users.map(u => u.username);
95+
}
4796

4897
async add_users ({ uid, users }) {
4998
const question_marks =

0 commit comments

Comments
 (0)