Skip to content

Commit f615700

Browse files
committed
feat: implement AccountWebPushDeviceController and add related routes for managing web push devices
1 parent 697f968 commit f615700

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import { Request, Response } from 'express';
2+
import Joi from 'joi';
3+
import { AccountWebPushDeviceService } from 'podverse-orm';
4+
import { handleGenericErrorResponse } from '@api/controllers/helpers/error';
5+
import { validateBodyObject } from '@api/lib/validation';
6+
import { ensureAuthenticated } from '@api/lib/auth';
7+
8+
const createAccountWebPushDeviceSchema = Joi.object({
9+
endpoint: Joi.string().uri().required(),
10+
p256dh: Joi.string().required(),
11+
auth: Joi.string().required()
12+
});
13+
14+
const updateAccountWebPushDeviceSchema = Joi.object({
15+
endpoint: Joi.string().uri().required(),
16+
p256dh: Joi.string().required(),
17+
auth: Joi.string().required()
18+
});
19+
20+
const deleteAccountWebPushDeviceSchema = Joi.object({
21+
endpoint: Joi.string().uri().required()
22+
});
23+
24+
const updateLocaleForAccountSchema = Joi.object({
25+
locale: Joi.string().required()
26+
});
27+
28+
export class AccountWebPushDeviceController {
29+
private static accountWebPushDeviceService = new AccountWebPushDeviceService();
30+
31+
static async create(req: Request, res: Response): Promise<void> {
32+
ensureAuthenticated(req, res, async () => {
33+
validateBodyObject(createAccountWebPushDeviceSchema, req, res, async () => {
34+
try {
35+
const jwtUser = req.user!;
36+
const { endpoint, p256dh, auth } = req.body as {
37+
endpoint: string;
38+
p256dh: string;
39+
auth: string;
40+
};
41+
const accountWebPushDevice = await AccountWebPushDeviceController
42+
.accountWebPushDeviceService.create(jwtUser.id, {
43+
endpoint,
44+
p256dh,
45+
auth
46+
});
47+
res.json(accountWebPushDevice);
48+
} catch (error) {
49+
handleGenericErrorResponse(res, error);
50+
}
51+
});
52+
});
53+
}
54+
55+
static async update(req: Request, res: Response): Promise<void> {
56+
ensureAuthenticated(req, res, async () => {
57+
validateBodyObject(updateAccountWebPushDeviceSchema, req, res, async () => {
58+
try {
59+
const jwtUser = req.user!;
60+
const { endpoint, p256dh, auth } = req.body as {
61+
endpoint: string;
62+
p256dh: string;
63+
auth: string;
64+
};
65+
const accountWebPushDevice = await AccountWebPushDeviceController
66+
.accountWebPushDeviceService.update(jwtUser.id, {
67+
endpoint,
68+
p256dh,
69+
auth
70+
});
71+
res.json(accountWebPushDevice);
72+
} catch (error) {
73+
handleGenericErrorResponse(res, error);
74+
}
75+
});
76+
});
77+
}
78+
79+
static async delete(req: Request, res: Response): Promise<void> {
80+
ensureAuthenticated(req, res, async () => {
81+
validateBodyObject(deleteAccountWebPushDeviceSchema, req, res, async () => {
82+
try {
83+
const jwtUser = req.user!;
84+
const { endpoint } = req.body as {
85+
endpoint: string;
86+
};
87+
await AccountWebPushDeviceController
88+
.accountWebPushDeviceService.delete(jwtUser.id, {
89+
endpoint
90+
});
91+
res.json({ message: 'WebPush device deleted successfully' });
92+
} catch (error) {
93+
handleGenericErrorResponse(res, error);
94+
}
95+
});
96+
});
97+
}
98+
99+
static async getAllForAccount(req: Request, res: Response): Promise<void> {
100+
ensureAuthenticated(req, res, async () => {
101+
try {
102+
const jwtUser = req.user!;
103+
const devices = await AccountWebPushDeviceController.accountWebPushDeviceService.getAllForAccount(jwtUser.id);
104+
res.json(devices);
105+
} catch (error) {
106+
handleGenericErrorResponse(res, error);
107+
}
108+
});
109+
}
110+
111+
static async updateLocaleForAccount(req: Request, res: Response): Promise<void> {
112+
ensureAuthenticated(req, res, async () => {
113+
validateBodyObject(updateLocaleForAccountSchema, req, res, async () => {
114+
try {
115+
const jwtUser = req.user!;
116+
const { locale } = req.body as { locale: string };
117+
await AccountWebPushDeviceController.accountWebPushDeviceService.updateLocaleForAccount(jwtUser.id, { locale });
118+
res.json({ message: 'Locale updated for account devices' });
119+
} catch (error) {
120+
handleGenericErrorResponse(res, error);
121+
}
122+
});
123+
});
124+
}
125+
}

src/routes/account.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { AccountFollowingPlaylistController } from '@api/controllers/account/acc
99
import { AccountNotificationChannelController } from '@api/controllers/account/accountNotificationChannel';
1010
import { AccountNotificationChannelTypeController } from '@api/controllers/account/accountNotificationChannelType';
1111
import { AccountFCMDeviceController } from '@api/controllers/account/accountFCMDevice';
12+
import { AccountWebPushDeviceController } from '@api/controllers/account/accountWebPushDevice';
1213
import { rateLimitEndpoint } from '@api/lib/rateLimiter';
1314

1415
const router = Router();
@@ -34,6 +35,12 @@ router.delete('/fcm-device/delete', asyncHandler(AccountFCMDeviceController.dele
3435
router.get('/fcm-device/all-for-account', asyncHandler(AccountFCMDeviceController.getAllForAccount));
3536
router.put('/fcm-device/update-locale', asyncHandler(AccountFCMDeviceController.updateLocaleForAccount));
3637

38+
router.post('/webpush-device/create', asyncHandler(AccountWebPushDeviceController.create));
39+
router.put('/webpush-device/update', asyncHandler(AccountWebPushDeviceController.update));
40+
router.delete('/webpush-device/delete', asyncHandler(AccountWebPushDeviceController.delete));
41+
router.get('/webpush-device/all-for-account', asyncHandler(AccountWebPushDeviceController.getAllForAccount));
42+
router.put('/webpush-device/update-locale', asyncHandler(AccountWebPushDeviceController.updateLocaleForAccount));
43+
3744
router.post('/follow/account', asyncHandler(AccountFollowingAccountController.followAccount));
3845
router.post('/unfollow/account', asyncHandler(AccountFollowingAccountController.unfollowAccount));
3946

0 commit comments

Comments
 (0)