@molecule/api-resource-message 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +115 -0
- package/dist/authorizers/index.d.ts +12 -0
- package/dist/authorizers/index.d.ts.map +1 -0
- package/dist/authorizers/index.js +11 -0
- package/dist/browser-guard.d.ts +2 -0
- package/dist/browser-guard.d.ts.map +1 -0
- package/dist/browser-guard.js +18 -0
- package/dist/handlers/createThread.d.ts +15 -0
- package/dist/handlers/createThread.d.ts.map +1 -0
- package/dist/handlers/createThread.js +54 -0
- package/dist/handlers/deleteMessage.d.ts +14 -0
- package/dist/handlers/deleteMessage.d.ts.map +1 -0
- package/dist/handlers/deleteMessage.js +56 -0
- package/dist/handlers/editMessage.d.ts +14 -0
- package/dist/handlers/editMessage.d.ts.map +1 -0
- package/dist/handlers/editMessage.js +63 -0
- package/dist/handlers/index.d.ts +9 -0
- package/dist/handlers/index.d.ts.map +1 -0
- package/dist/handlers/index.js +8 -0
- package/dist/handlers/listThreads.d.ts +14 -0
- package/dist/handlers/listThreads.d.ts.map +1 -0
- package/dist/handlers/listThreads.js +39 -0
- package/dist/handlers/markRead.d.ts +16 -0
- package/dist/handlers/markRead.d.ts.map +1 -0
- package/dist/handlers/markRead.js +68 -0
- package/dist/handlers/messages.d.ts +23 -0
- package/dist/handlers/messages.d.ts.map +1 -0
- package/dist/handlers/messages.js +135 -0
- package/dist/handlers/readThread.d.ts +15 -0
- package/dist/handlers/readThread.d.ts.map +1 -0
- package/dist/handlers/readThread.js +66 -0
- package/dist/handlers/unreadCount.d.ts +14 -0
- package/dist/handlers/unreadCount.d.ts.map +1 -0
- package/dist/handlers/unreadCount.js +37 -0
- package/dist/i18n.d.ts +10 -0
- package/dist/i18n.d.ts.map +1 -0
- package/dist/i18n.js +17 -0
- package/dist/index.d.ts +82 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +81 -0
- package/dist/requestHandlerMap.d.ts +28 -0
- package/dist/requestHandlerMap.d.ts.map +1 -0
- package/dist/requestHandlerMap.js +27 -0
- package/dist/routes.d.ts +58 -0
- package/dist/routes.d.ts.map +1 -0
- package/dist/routes.js +67 -0
- package/dist/service.d.ts +117 -0
- package/dist/service.d.ts.map +1 -0
- package/dist/service.js +301 -0
- package/dist/types.d.ts +102 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +32 -0
- package/dist/validation.d.ts +36 -0
- package/dist/validation.d.ts.map +1 -0
- package/dist/validation.js +38 -0
- package/package.json +74 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work.
|
|
38
|
+
|
|
39
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
40
|
+
form, that is based on (or derived from) the Work and for which the
|
|
41
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
42
|
+
represent, as a whole, an original work of authorship.
|
|
43
|
+
|
|
44
|
+
"Contribution" shall mean any work of authorship, including the
|
|
45
|
+
original version of the Work and any modifications or additions
|
|
46
|
+
to that Work, that is intentionally submitted to the Licensor for
|
|
47
|
+
inclusion in the Work by the copyright owner or by an individual or
|
|
48
|
+
Legal Entity authorized to submit on behalf of the copyright owner.
|
|
49
|
+
|
|
50
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
51
|
+
on behalf of whom a Contribution has been received by the Licensor and
|
|
52
|
+
subsequently incorporated within the Work.
|
|
53
|
+
|
|
54
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
55
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
56
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
57
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
58
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
59
|
+
Work and such Derivative Works in Source or Object form.
|
|
60
|
+
|
|
61
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
62
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
63
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
64
|
+
patent license to make, have made, use, offer to sell, sell, import,
|
|
65
|
+
and otherwise transfer the Work.
|
|
66
|
+
|
|
67
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
68
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
69
|
+
modifications, and in Source or Object form, provided that You
|
|
70
|
+
meet the following conditions:
|
|
71
|
+
|
|
72
|
+
(a) You must give any other recipients of the Work or
|
|
73
|
+
Derivative Works a copy of this License; and
|
|
74
|
+
|
|
75
|
+
(b) You must cause any modified files to carry prominent notices
|
|
76
|
+
stating that You changed the files; and
|
|
77
|
+
|
|
78
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
79
|
+
that You distribute, all copyright, patent, trademark, and
|
|
80
|
+
attribution notices from the Source form of the Work,
|
|
81
|
+
excluding those notices that do not pertain to any part of
|
|
82
|
+
the Derivative Works; and
|
|
83
|
+
|
|
84
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
85
|
+
distribution, then any Derivative Works that You distribute must
|
|
86
|
+
include a readable copy of the attribution notices contained
|
|
87
|
+
within such NOTICE file.
|
|
88
|
+
|
|
89
|
+
5. Submission of Contributions.
|
|
90
|
+
|
|
91
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
92
|
+
names, trademarks, service marks, or product names of the Licensor.
|
|
93
|
+
|
|
94
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
95
|
+
agreed to in writing, Licensor provides the Work on an "AS IS" BASIS,
|
|
96
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND.
|
|
97
|
+
|
|
98
|
+
8. Limitation of Liability. In no event and under no legal theory shall
|
|
99
|
+
any Contributor be liable to You for damages.
|
|
100
|
+
|
|
101
|
+
9. Accepting Warranty or Additional Liability.
|
|
102
|
+
|
|
103
|
+
Copyright 2026 Molecule Dev, Inc.
|
|
104
|
+
|
|
105
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
106
|
+
you may not use this file except in compliance with the License.
|
|
107
|
+
You may obtain a copy of the License at
|
|
108
|
+
|
|
109
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
110
|
+
|
|
111
|
+
Unless required by applicable law or agreed to in writing, software
|
|
112
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
113
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
114
|
+
See the License for the specific language governing permissions and
|
|
115
|
+
limitations under the License.
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export {};
|
|
2
|
+
/**
|
|
3
|
+
* Message resource authorizers.
|
|
4
|
+
*
|
|
5
|
+
* Thread participation is enforced inside each handler (the authenticated
|
|
6
|
+
* user must be `participantAId` or `participantBId`). Message authorship
|
|
7
|
+
* is enforced in the service layer (`editMessage`, `deleteMessage`).
|
|
8
|
+
* No additional authorizers are required.
|
|
9
|
+
*
|
|
10
|
+
* @module
|
|
11
|
+
*/
|
|
12
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/authorizers/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;GASG"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export {};
|
|
2
|
+
/**
|
|
3
|
+
* Message resource authorizers.
|
|
4
|
+
*
|
|
5
|
+
* Thread participation is enforced inside each handler (the authenticated
|
|
6
|
+
* user must be `participantAId` or `participantBId`). Message authorship
|
|
7
|
+
* is enforced in the service layer (`editMessage`, `deleteMessage`).
|
|
8
|
+
* No additional authorizers are required.
|
|
9
|
+
*
|
|
10
|
+
* @module
|
|
11
|
+
*/
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"browser-guard.d.ts","sourceRoot":"","sources":["../src/browser-guard.ts"],"names":[],"mappings":"AAuBA,OAAO,EAAE,CAAA"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser guard — `@molecule/api-resource-message` is SERVER-ONLY.
|
|
3
|
+
*
|
|
4
|
+
* Generated by scripts/gen-browser-guards.mjs (workspace root) — edit THAT, not this.
|
|
5
|
+
* Evaluating a server package in a browser bundle is always an import-graph mistake
|
|
6
|
+
* (node APIs, secrets); without this guard it surfaces as a cryptic downstream crash
|
|
7
|
+
* ("Buffer is not defined") far from the culprit. Throwing here names the package and
|
|
8
|
+
* the fix at the exact moment the client bundle evaluates it. jsdom tests and SSR are
|
|
9
|
+
* unaffected: the throw requires browser globals AND the absence of a node runtime.
|
|
10
|
+
*/
|
|
11
|
+
const g = globalThis;
|
|
12
|
+
if (g.window !== undefined && g.document !== undefined && !g.process?.versions?.node) {
|
|
13
|
+
throw new Error('@molecule/api-resource-message is SERVER-ONLY: it was bundled into browser/client code. Import it only ' +
|
|
14
|
+
'from server code (a server route/function or your API), or dynamic-import it inside ' +
|
|
15
|
+
'the server handler — never from components or shared client modules, and never ' +
|
|
16
|
+
'polyfill Buffer/process to silence this.');
|
|
17
|
+
}
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Create / look up a 1:1 message thread.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import type { MoleculeRequest, MoleculeResponse } from '@molecule/api-resource';
|
|
7
|
+
/**
|
|
8
|
+
* Resolves (or lazily creates) the canonical thread between the
|
|
9
|
+
* authenticated user and a counter-participant.
|
|
10
|
+
*
|
|
11
|
+
* @param req - The request with `participantId` in body.
|
|
12
|
+
* @param res - The response object.
|
|
13
|
+
*/
|
|
14
|
+
export declare function createThread(req: MoleculeRequest, res: MoleculeResponse): Promise<void>;
|
|
15
|
+
//# sourceMappingURL=createThread.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createThread.d.ts","sourceRoot":"","sources":["../../src/handlers/createThread.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAA;AAK/E;;;;;;GAMG;AACH,wBAAsB,YAAY,CAAC,GAAG,EAAE,eAAe,EAAE,GAAG,EAAE,gBAAgB,GAAG,OAAO,CAAC,IAAI,CAAC,CAuC7F"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Create / look up a 1:1 message thread.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { t } from '@molecule/api-i18n';
|
|
7
|
+
import { logger } from '@molecule/api-logger';
|
|
8
|
+
import { getOrCreateThread } from '../service.js';
|
|
9
|
+
import { getOrCreateThreadSchema } from '../validation.js';
|
|
10
|
+
/**
|
|
11
|
+
* Resolves (or lazily creates) the canonical thread between the
|
|
12
|
+
* authenticated user and a counter-participant.
|
|
13
|
+
*
|
|
14
|
+
* @param req - The request with `participantId` in body.
|
|
15
|
+
* @param res - The response object.
|
|
16
|
+
*/
|
|
17
|
+
export async function createThread(req, res) {
|
|
18
|
+
const userId = res.locals.session?.userId;
|
|
19
|
+
if (!userId) {
|
|
20
|
+
res.status(401).json({
|
|
21
|
+
error: t('resource.error.unauthorized', undefined, { defaultValue: 'Unauthorized' }),
|
|
22
|
+
errorKey: 'resource.error.unauthorized',
|
|
23
|
+
});
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
const parsed = getOrCreateThreadSchema.safeParse(req.body);
|
|
27
|
+
if (!parsed.success) {
|
|
28
|
+
const errors = parsed.error.issues.map((e) => `${e.path.join('.')}: ${e.message}`).join(', ');
|
|
29
|
+
res.status(400).json({ error: errors, errorKey: 'message.error.validationFailed' });
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
if (parsed.data.participantId === userId) {
|
|
33
|
+
res.status(400).json({
|
|
34
|
+
error: t('message.error.selfThread', undefined, {
|
|
35
|
+
defaultValue: 'Cannot start a thread with yourself',
|
|
36
|
+
}),
|
|
37
|
+
errorKey: 'message.error.selfThread',
|
|
38
|
+
});
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
try {
|
|
42
|
+
const thread = await getOrCreateThread(userId, parsed.data.participantId);
|
|
43
|
+
res.status(201).json(thread);
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
logger.error('Failed to create message thread', { userId, error });
|
|
47
|
+
res.status(500).json({
|
|
48
|
+
error: t('message.error.threadCreateFailed', undefined, {
|
|
49
|
+
defaultValue: 'Failed to create thread',
|
|
50
|
+
}),
|
|
51
|
+
errorKey: 'message.error.threadCreateFailed',
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Soft-delete a message.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import type { MoleculeRequest, MoleculeResponse } from '@molecule/api-resource';
|
|
7
|
+
/**
|
|
8
|
+
* Soft-deletes a message. Only the original sender may delete.
|
|
9
|
+
*
|
|
10
|
+
* @param req - The request with `messageId` URL param.
|
|
11
|
+
* @param res - The response object.
|
|
12
|
+
*/
|
|
13
|
+
export declare function deleteMessageHandler(req: MoleculeRequest, res: MoleculeResponse): Promise<void>;
|
|
14
|
+
//# sourceMappingURL=deleteMessage.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"deleteMessage.d.ts","sourceRoot":"","sources":["../../src/handlers/deleteMessage.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAA;AAI/E;;;;;GAKG;AACH,wBAAsB,oBAAoB,CACxC,GAAG,EAAE,eAAe,EACpB,GAAG,EAAE,gBAAgB,GACpB,OAAO,CAAC,IAAI,CAAC,CA0Cf"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Soft-delete a message.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { t } from '@molecule/api-i18n';
|
|
7
|
+
import { logger } from '@molecule/api-logger';
|
|
8
|
+
import { deleteMessage } from '../service.js';
|
|
9
|
+
/**
|
|
10
|
+
* Soft-deletes a message. Only the original sender may delete.
|
|
11
|
+
*
|
|
12
|
+
* @param req - The request with `messageId` URL param.
|
|
13
|
+
* @param res - The response object.
|
|
14
|
+
*/
|
|
15
|
+
export async function deleteMessageHandler(req, res) {
|
|
16
|
+
const userId = res.locals.session?.userId;
|
|
17
|
+
if (!userId) {
|
|
18
|
+
res.status(401).json({
|
|
19
|
+
error: t('resource.error.unauthorized', undefined, { defaultValue: 'Unauthorized' }),
|
|
20
|
+
errorKey: 'resource.error.unauthorized',
|
|
21
|
+
});
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
const { messageId } = req.params;
|
|
25
|
+
if (!messageId) {
|
|
26
|
+
res.status(400).json({
|
|
27
|
+
error: t('message.error.missingMessageId', undefined, {
|
|
28
|
+
defaultValue: 'Message ID is required',
|
|
29
|
+
}),
|
|
30
|
+
errorKey: 'message.error.missingMessageId',
|
|
31
|
+
});
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
try {
|
|
35
|
+
const deleted = await deleteMessage(messageId, userId);
|
|
36
|
+
if (!deleted) {
|
|
37
|
+
res.status(404).json({
|
|
38
|
+
error: t('message.error.messageNotFound', undefined, {
|
|
39
|
+
defaultValue: 'Message not found or not deletable',
|
|
40
|
+
}),
|
|
41
|
+
errorKey: 'message.error.messageNotFound',
|
|
42
|
+
});
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
res.status(204).end();
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
logger.error('Failed to delete message', { messageId, userId, error });
|
|
49
|
+
res.status(500).json({
|
|
50
|
+
error: t('message.error.deleteFailed', undefined, {
|
|
51
|
+
defaultValue: 'Failed to delete message',
|
|
52
|
+
}),
|
|
53
|
+
errorKey: 'message.error.deleteFailed',
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Edit an existing message.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import type { MoleculeRequest, MoleculeResponse } from '@molecule/api-resource';
|
|
7
|
+
/**
|
|
8
|
+
* Edits a message body. Only the original sender may edit.
|
|
9
|
+
*
|
|
10
|
+
* @param req - The request with `messageId` URL param and `body` in body.
|
|
11
|
+
* @param res - The response object.
|
|
12
|
+
*/
|
|
13
|
+
export declare function editMessageHandler(req: MoleculeRequest, res: MoleculeResponse): Promise<void>;
|
|
14
|
+
//# sourceMappingURL=editMessage.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"editMessage.d.ts","sourceRoot":"","sources":["../../src/handlers/editMessage.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAA;AAK/E;;;;;GAKG;AACH,wBAAsB,kBAAkB,CACtC,GAAG,EAAE,eAAe,EACpB,GAAG,EAAE,gBAAgB,GACpB,OAAO,CAAC,IAAI,CAAC,CAiDf"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Edit an existing message.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { t } from '@molecule/api-i18n';
|
|
7
|
+
import { logger } from '@molecule/api-logger';
|
|
8
|
+
import { editMessage } from '../service.js';
|
|
9
|
+
import { editMessageSchema } from '../validation.js';
|
|
10
|
+
/**
|
|
11
|
+
* Edits a message body. Only the original sender may edit.
|
|
12
|
+
*
|
|
13
|
+
* @param req - The request with `messageId` URL param and `body` in body.
|
|
14
|
+
* @param res - The response object.
|
|
15
|
+
*/
|
|
16
|
+
export async function editMessageHandler(req, res) {
|
|
17
|
+
const userId = res.locals.session?.userId;
|
|
18
|
+
if (!userId) {
|
|
19
|
+
res.status(401).json({
|
|
20
|
+
error: t('resource.error.unauthorized', undefined, { defaultValue: 'Unauthorized' }),
|
|
21
|
+
errorKey: 'resource.error.unauthorized',
|
|
22
|
+
});
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
const { messageId } = req.params;
|
|
26
|
+
if (!messageId) {
|
|
27
|
+
res.status(400).json({
|
|
28
|
+
error: t('message.error.missingMessageId', undefined, {
|
|
29
|
+
defaultValue: 'Message ID is required',
|
|
30
|
+
}),
|
|
31
|
+
errorKey: 'message.error.missingMessageId',
|
|
32
|
+
});
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
const parsed = editMessageSchema.safeParse(req.body);
|
|
36
|
+
if (!parsed.success) {
|
|
37
|
+
const errors = parsed.error.issues.map((e) => `${e.path.join('.')}: ${e.message}`).join(', ');
|
|
38
|
+
res.status(400).json({ error: errors, errorKey: 'message.error.validationFailed' });
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
try {
|
|
42
|
+
const updated = await editMessage(messageId, userId, parsed.data.body);
|
|
43
|
+
if (!updated) {
|
|
44
|
+
res.status(404).json({
|
|
45
|
+
error: t('message.error.messageNotFound', undefined, {
|
|
46
|
+
defaultValue: 'Message not found or not editable',
|
|
47
|
+
}),
|
|
48
|
+
errorKey: 'message.error.messageNotFound',
|
|
49
|
+
});
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
res.json(updated);
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
logger.error('Failed to edit message', { messageId, userId, error });
|
|
56
|
+
res.status(500).json({
|
|
57
|
+
error: t('message.error.editFailed', undefined, {
|
|
58
|
+
defaultValue: 'Failed to edit message',
|
|
59
|
+
}),
|
|
60
|
+
errorKey: 'message.error.editFailed',
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from './createThread.js';
|
|
2
|
+
export * from './deleteMessage.js';
|
|
3
|
+
export * from './editMessage.js';
|
|
4
|
+
export * from './listThreads.js';
|
|
5
|
+
export * from './markRead.js';
|
|
6
|
+
export * from './messages.js';
|
|
7
|
+
export * from './readThread.js';
|
|
8
|
+
export * from './unreadCount.js';
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/handlers/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAA;AACjC,cAAc,oBAAoB,CAAA;AAClC,cAAc,kBAAkB,CAAA;AAChC,cAAc,kBAAkB,CAAA;AAChC,cAAc,eAAe,CAAA;AAC7B,cAAc,eAAe,CAAA;AAC7B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,kBAAkB,CAAA"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from './createThread.js';
|
|
2
|
+
export * from './deleteMessage.js';
|
|
3
|
+
export * from './editMessage.js';
|
|
4
|
+
export * from './listThreads.js';
|
|
5
|
+
export * from './markRead.js';
|
|
6
|
+
export * from './messages.js';
|
|
7
|
+
export * from './readThread.js';
|
|
8
|
+
export * from './unreadCount.js';
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* List threads for the authenticated user.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import type { MoleculeRequest, MoleculeResponse } from '@molecule/api-resource';
|
|
7
|
+
/**
|
|
8
|
+
* Returns paginated threads for the authenticated user, newest first.
|
|
9
|
+
*
|
|
10
|
+
* @param req - The request with optional `limit` / `offset` query params.
|
|
11
|
+
* @param res - The response object.
|
|
12
|
+
*/
|
|
13
|
+
export declare function listThreads(req: MoleculeRequest, res: MoleculeResponse): Promise<void>;
|
|
14
|
+
//# sourceMappingURL=listThreads.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"listThreads.d.ts","sourceRoot":"","sources":["../../src/handlers/listThreads.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAA;AAI/E;;;;;GAKG;AACH,wBAAsB,WAAW,CAAC,GAAG,EAAE,eAAe,EAAE,GAAG,EAAE,gBAAgB,GAAG,OAAO,CAAC,IAAI,CAAC,CAyB5F"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* List threads for the authenticated user.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { t } from '@molecule/api-i18n';
|
|
7
|
+
import { logger } from '@molecule/api-logger';
|
|
8
|
+
import { listThreadsForParticipant } from '../service.js';
|
|
9
|
+
/**
|
|
10
|
+
* Returns paginated threads for the authenticated user, newest first.
|
|
11
|
+
*
|
|
12
|
+
* @param req - The request with optional `limit` / `offset` query params.
|
|
13
|
+
* @param res - The response object.
|
|
14
|
+
*/
|
|
15
|
+
export async function listThreads(req, res) {
|
|
16
|
+
const userId = res.locals.session?.userId;
|
|
17
|
+
if (!userId) {
|
|
18
|
+
res.status(401).json({
|
|
19
|
+
error: t('resource.error.unauthorized', undefined, { defaultValue: 'Unauthorized' }),
|
|
20
|
+
errorKey: 'resource.error.unauthorized',
|
|
21
|
+
});
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
const limit = parseInt(req.query.limit, 10) || 50;
|
|
25
|
+
const offset = parseInt(req.query.offset, 10) || 0;
|
|
26
|
+
try {
|
|
27
|
+
const threads = await listThreadsForParticipant(userId, { limit, offset });
|
|
28
|
+
res.json({ data: threads, limit, offset });
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
logger.error('Failed to list threads', { userId, error });
|
|
32
|
+
res.status(500).json({
|
|
33
|
+
error: t('message.error.listThreadsFailed', undefined, {
|
|
34
|
+
defaultValue: 'Failed to list threads',
|
|
35
|
+
}),
|
|
36
|
+
errorKey: 'message.error.listThreadsFailed',
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mark a thread as read for the authenticated user.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import type { MoleculeRequest, MoleculeResponse } from '@molecule/api-resource';
|
|
7
|
+
/**
|
|
8
|
+
* Marks a thread as read and zeroes the authenticated user's unread
|
|
9
|
+
* counter for that thread. Broadcasts a `message:read` event over the
|
|
10
|
+
* bonded realtime provider.
|
|
11
|
+
*
|
|
12
|
+
* @param req - The request with `threadId` URL param.
|
|
13
|
+
* @param res - The response object.
|
|
14
|
+
*/
|
|
15
|
+
export declare function markReadHandler(req: MoleculeRequest, res: MoleculeResponse): Promise<void>;
|
|
16
|
+
//# sourceMappingURL=markRead.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"markRead.d.ts","sourceRoot":"","sources":["../../src/handlers/markRead.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAA;AAI/E;;;;;;;GAOG;AACH,wBAAsB,eAAe,CAAC,GAAG,EAAE,eAAe,EAAE,GAAG,EAAE,gBAAgB,GAAG,OAAO,CAAC,IAAI,CAAC,CAoDhG"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mark a thread as read for the authenticated user.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { t } from '@molecule/api-i18n';
|
|
7
|
+
import { logger } from '@molecule/api-logger';
|
|
8
|
+
import { markRead } from '../service.js';
|
|
9
|
+
/**
|
|
10
|
+
* Marks a thread as read and zeroes the authenticated user's unread
|
|
11
|
+
* counter for that thread. Broadcasts a `message:read` event over the
|
|
12
|
+
* bonded realtime provider.
|
|
13
|
+
*
|
|
14
|
+
* @param req - The request with `threadId` URL param.
|
|
15
|
+
* @param res - The response object.
|
|
16
|
+
*/
|
|
17
|
+
export async function markReadHandler(req, res) {
|
|
18
|
+
const userId = res.locals.session?.userId;
|
|
19
|
+
if (!userId) {
|
|
20
|
+
res.status(401).json({
|
|
21
|
+
error: t('resource.error.unauthorized', undefined, { defaultValue: 'Unauthorized' }),
|
|
22
|
+
errorKey: 'resource.error.unauthorized',
|
|
23
|
+
});
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
const { threadId } = req.params;
|
|
27
|
+
if (!threadId) {
|
|
28
|
+
res.status(400).json({
|
|
29
|
+
error: t('message.error.missingThreadId', undefined, {
|
|
30
|
+
defaultValue: 'Thread ID is required',
|
|
31
|
+
}),
|
|
32
|
+
errorKey: 'message.error.missingThreadId',
|
|
33
|
+
});
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
try {
|
|
37
|
+
await markRead(threadId, userId);
|
|
38
|
+
res.status(204).end();
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
|
|
42
|
+
if (errorMessage.includes('not found')) {
|
|
43
|
+
res.status(404).json({
|
|
44
|
+
error: t('message.error.threadNotFound', undefined, {
|
|
45
|
+
defaultValue: 'Thread not found',
|
|
46
|
+
}),
|
|
47
|
+
errorKey: 'message.error.threadNotFound',
|
|
48
|
+
});
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
if (errorMessage.includes('not a participant')) {
|
|
52
|
+
res.status(403).json({
|
|
53
|
+
error: t('message.error.notParticipant', undefined, {
|
|
54
|
+
defaultValue: 'You are not a participant in this thread',
|
|
55
|
+
}),
|
|
56
|
+
errorKey: 'message.error.notParticipant',
|
|
57
|
+
});
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
logger.error('Failed to mark thread as read', { threadId, userId, error });
|
|
61
|
+
res.status(500).json({
|
|
62
|
+
error: t('message.error.markReadFailed', undefined, {
|
|
63
|
+
defaultValue: 'Failed to mark thread as read',
|
|
64
|
+
}),
|
|
65
|
+
errorKey: 'message.error.markReadFailed',
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Send + list messages within a thread.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import type { MoleculeRequest, MoleculeResponse } from '@molecule/api-resource';
|
|
7
|
+
/**
|
|
8
|
+
* Lists messages in a thread, newest first.
|
|
9
|
+
*
|
|
10
|
+
* @param req - The request with `threadId` URL param and optional
|
|
11
|
+
* `before` / `limit` query params.
|
|
12
|
+
* @param res - The response object.
|
|
13
|
+
*/
|
|
14
|
+
export declare function listMessagesHandler(req: MoleculeRequest, res: MoleculeResponse): Promise<void>;
|
|
15
|
+
/**
|
|
16
|
+
* Sends a new message in a thread. Authenticated user must be a
|
|
17
|
+
* participant. Broadcasts a `message:sent` event when realtime is bonded.
|
|
18
|
+
*
|
|
19
|
+
* @param req - The request with `threadId` URL param and message body.
|
|
20
|
+
* @param res - The response object.
|
|
21
|
+
*/
|
|
22
|
+
export declare function sendMessageHandler(req: MoleculeRequest, res: MoleculeResponse): Promise<void>;
|
|
23
|
+
//# sourceMappingURL=messages.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"messages.d.ts","sourceRoot":"","sources":["../../src/handlers/messages.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAA;AAK/E;;;;;;GAMG;AACH,wBAAsB,mBAAmB,CACvC,GAAG,EAAE,eAAe,EACpB,GAAG,EAAE,gBAAgB,GACpB,OAAO,CAAC,IAAI,CAAC,CAwDf;AAED;;;;;;GAMG;AACH,wBAAsB,kBAAkB,CACtC,GAAG,EAAE,eAAe,EACpB,GAAG,EAAE,gBAAgB,GACpB,OAAO,CAAC,IAAI,CAAC,CA2Df"}
|