Skip to content
This repository was archived by the owner on Apr 21, 2025. It is now read-only.

Commit 9eb1d53

Browse files
Merge pull request #863 from NoodleOfDeath/dev
~api
2 parents 81735f8 + c2ba91c commit 9eb1d53

File tree

14 files changed

+167
-108
lines changed

14 files changed

+167
-108
lines changed

src/server/src/api/v1/controllers/account/AccountController.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ export class AccountController extends BaseControllerWithPersistentStorageAccess
294294
if (!token.user) {
295295
throw new AuthError('UNKNOWN_ALIAS');
296296
}
297-
await token.user.syncProfile(); // get profile
297+
await token.user.syncProfile(req); // get profile
298298
return {
299299
profile: token.user.toJSON().profile ?? {},
300300
token: token.wrapped,
@@ -314,7 +314,7 @@ export class AccountController extends BaseControllerWithPersistentStorageAccess
314314
if (!user) {
315315
throw new AuthError('UNKNOWN_ALIAS');
316316
}
317-
await user.syncProfile(); // get profile
317+
await user.syncProfile(req); // get profile
318318
if (token) {
319319
// account is anonymous, return JWT
320320
return {
@@ -376,7 +376,7 @@ export class AccountController extends BaseControllerWithPersistentStorageAccess
376376
}
377377
}
378378
// user is authenticated, generate JWT else {
379-
await user.syncProfile(); // get profile
379+
await user.syncProfile(req); // get profile
380380
const userData = user.toJSON();
381381
const token = await JWT.as(body.requestedRole ?? 'standard', userData.id);
382382
await user.createCredential('jwt', token);
@@ -414,7 +414,7 @@ export class AccountController extends BaseControllerWithPersistentStorageAccess
414414
@Body() body: RegisterAliasRequest
415415
): Promise<RegistrationResponse> {
416416
const user = await User.from(body, req.body);
417-
await user.syncProfile();
417+
await user.syncProfile(req);
418418
if (user.toJSON().profile?.linkedThirdPartyAccounts?.includes(body.otherAlias.thirdParty.name)) {
419419
throw new AuthError('DUPLICATE_USER');
420420
}

src/server/src/api/v1/controllers/category/CategoryController.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,7 @@ import {
1313
Tags,
1414
} from 'tsoa';
1515

16-
import {
17-
BaseController,
18-
BulkResponse,
19-
InteractionRequest,
20-
} from '../';
16+
import { BaseController, BulkResponse } from '../';
2117
import { SupportedLocale } from '../../../../core/locales';
2218
import {
2319
AuthError,
@@ -27,6 +23,7 @@ import {
2723
import {
2824
Category,
2925
CategoryInteraction,
26+
InteractionCreationAttributes,
3027
InteractionType,
3128
PublicCategoryAttributes,
3229
} from '../../schema';
@@ -58,14 +55,15 @@ export class CategoryController extends BaseController {
5855
@Request() req: ExpressRequest,
5956
@Path() targetId: number,
6057
@Path() type: InteractionType,
61-
@Body() body: InteractionRequest
58+
@Body() body: InteractionCreationAttributes,
6259
): Promise<PublicCategoryAttributes> {
6360
const user = req.jwt?.user;
64-
const {
65-
content, metadata, remoteAddr,
66-
} = body;
6761
const interaction = await CategoryInteraction.create({
68-
content, metadata, remoteAddr, targetId, type, userId: user?.id,
62+
...body,
63+
remoteAddr: req.ip,
64+
targetId,
65+
type,
66+
userId: user?.id,
6967
});
7068
if (!interaction) {
7169
throw new InternalError('Failed to create interaction');

src/server/src/api/v1/controllers/event/EventController.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
Tags,
1313
} from 'tsoa';
1414

15-
import { BulkResponse, InteractionRequest } from '../';
15+
import { BulkResponse } from '../';
1616
import {
1717
AuthError,
1818
Request as ExpressRequest,
@@ -21,6 +21,7 @@ import {
2121
import {
2222
Event,
2323
EventInteraction,
24+
InteractionCreationAttributes,
2425
InteractionType,
2526
PublicEventAttributes,
2627
} from '../../schema';
@@ -50,14 +51,15 @@ export class EventController {
5051
@Request() req: ExpressRequest,
5152
@Path() targetId: number,
5253
@Path() type: InteractionType,
53-
@Body() body: InteractionRequest
54+
@Body() body: InteractionCreationAttributes
5455
): Promise<PublicEventAttributes> {
5556
const user = req.jwt?.user;
56-
const {
57-
content, metadata, remoteAddr,
58-
} = body;
5957
const interaction = await EventInteraction.create({
60-
content, metadata, remoteAddr, targetId, type, userId: user?.id,
58+
...body,
59+
remoteAddr: req.ip,
60+
targetId,
61+
type,
62+
userId: user?.id,
6163
});
6264
if (!interaction) {
6365
throw new InternalError('Failed to create interaction');

src/server/src/api/v1/controllers/profile/ProfileController.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export class ProfileController extends BaseControllerWithPersistentStorageAccess
3333
@Request() req: ExpressRequest
3434
): Promise<ProfileResponse> {
3535
const user = req.jwt.user;
36-
await user.syncProfile();
36+
await user.syncProfile(req);
3737
const userData = user.toJSON();
3838
return { profile: userData.profile };
3939
}

src/server/src/api/v1/controllers/publisher/PublisherController.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,15 @@ import {
1212
Tags,
1313
} from 'tsoa';
1414

15-
import {
16-
BaseController,
17-
BulkResponse,
18-
InteractionRequest,
19-
} from '..';
15+
import { BaseController, BulkResponse } from '..';
2016
import { SupportedLocale } from '../../../../core/locales';
2117
import {
2218
AuthError,
2319
Request as ExpressRequest,
2420
InternalError,
2521
} from '../../middleware';
2622
import {
23+
InteractionCreationAttributes,
2724
InteractionType,
2825
PublicPublisherAttributes,
2926
Publisher,
@@ -57,14 +54,15 @@ export class PublisherController extends BaseController {
5754
@Request() req: ExpressRequest,
5855
@Path() targetId: number,
5956
@Path() type: InteractionType,
60-
@Body() body: InteractionRequest
57+
@Body() body: InteractionCreationAttributes,
6158
): Promise<PublicPublisherAttributes> {
6259
const user = req.jwt?.user;
63-
const {
64-
content, metadata, remoteAddr,
65-
} = body;
6660
const interaction = await PublisherInteraction.create({
67-
content, metadata, remoteAddr, targetId, type, userId: user?.id,
61+
...body,
62+
remoteAddr: req.ip,
63+
targetId,
64+
type,
65+
userId: user?.id,
6866
});
6967
if (!interaction) {
7068
throw new InternalError('Failed to create interaction');

src/server/src/api/v1/controllers/summary/SummaryController.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import {
1919
BulkMetadataResponse,
2020
BulkResponse,
2121
DestroyResponse,
22-
InteractionRequest,
2322
} from '../';
2423
import { BaseControllerWithPersistentStorageAccess } from '../';
2524
import { SupportedLocale } from '../../../../core/locales';
@@ -30,6 +29,7 @@ import {
3029
InternalError,
3130
} from '../../middleware';
3231
import {
32+
InteractionCreationAttributes,
3333
InteractionType,
3434
PublicRecapAttributes,
3535
PublicSummaryAttributes,
@@ -132,14 +132,11 @@ export class SummaryController extends BaseControllerWithPersistentStorageAccess
132132
@Request() req: ExpressRequest,
133133
@Path() targetId: number,
134134
@Path() type: InteractionType,
135-
@Body() body: InteractionRequest
135+
@Body() body: InteractionCreationAttributes
136136
): Promise<PublicSummaryAttributes> {
137137
const user = req.jwt?.user;
138-
console.log(user);
139-
const { content, metadata } = body;
140138
const interaction = await SummaryInteraction.create({
141-
content,
142-
metadata,
139+
...body,
143140
remoteAddr: req.ip,
144141
targetId,
145142
type,
@@ -152,7 +149,7 @@ export class SummaryController extends BaseControllerWithPersistentStorageAccess
152149
await new MailService().sendMail({
153150
154151
subject: 'Feedback',
155-
text: [content, JSON.stringify(metadata)].join('\n\n'),
152+
text: [body.content, JSON.stringify(body.metadata)].join('\n\n'),
156153
157154
});
158155
}

src/server/src/api/v1/controllers/types.ts

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -50,30 +50,3 @@ export type LocalizeRequest = JobRequest & {
5050
export type TtsRequest = JobRequest & {
5151
voice?: string;
5252
};
53-
54-
// interactions
55-
56-
export type InteractionRequest = {
57-
userId?: number;
58-
remoteAddr?: string;
59-
content?: string;
60-
metadata?: Record<string, unknown>;
61-
};
62-
63-
export type InteractionUserVote = 'down' | 'up';
64-
65-
// uh this type exists? forcing rebuild
66-
export type InteractionResponse = {
67-
bookmark: number;
68-
userBookmarked?: boolean;
69-
favorite: number;
70-
userFavorited?: boolean;
71-
comment: number;
72-
downvote: number;
73-
listen: number;
74-
read: number;
75-
share: number;
76-
upvote: number;
77-
uservote?: InteractionUserVote;
78-
view: number;
79-
};

src/server/src/api/v1/middleware/AuthMiddleware.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { AuthError, internalErrorHandler } from './internal-errors';
22
import { RequestHandler } from './types';
3-
import { JWT } from '../controllers/types';
43

54
type AuthMiddlewareOptions = {
65
scope?: string[];

src/server/src/api/v1/middleware/PreprocessHeadersMiddleware.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ export const preprocessHeadersMiddleware: RequestHandler = async (req, res, next
66
try {
77
delete req.body.userId;
88
delete req.query.userId;
9-
req.ip = req.get('x-forwarded-from') || req.ip;
109
const auth = req.get('authorization') || '';
1110
const [type, token] = auth.split(' ');
1211
if (type === 'Bearer') {
1312
const jwt = await JWT.from(token);
1413
req.jwt = jwt;
1514
req.body.userId = jwt.userId;
1615
}
16+
const version = req.get('x-version');
17+
req.version = version;
1718
next();
1819
} catch (e) {
1920
internalErrorHandler(res, e);

src/server/src/api/v1/middleware/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { JWT } from '../controllers/types';
88

99
export type Request = ExpressRequest & {
1010
jwt?: JWT;
11+
version?: string;
1112
};
1213

1314
// eslint-disable-next-line @typescript-eslint/no-explicit-any

0 commit comments

Comments
 (0)