somod-chat-service 0.6.3 → 0.7.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/build/lib/sessionUtil.d.ts +1 -0
- package/build/lib/sessionUtil.js +20 -8
- package/build/lib/types.d.ts +3 -2
- package/build/lib/types.js +1 -7
- package/build/serverless/functions/message.js +2 -1
- package/build/serverless/functions/thread.http.json +1 -1
- package/build/serverless/functions/thread.js +17 -39
- package/build/serverless/functions/wsOnMessage.js +2 -1
- package/package.json +1 -1
package/build/lib/sessionUtil.js
CHANGED
|
@@ -10,31 +10,43 @@ export var Error = {
|
|
|
10
10
|
invalid: "invalid field: sessionToken"
|
|
11
11
|
};
|
|
12
12
|
export var handleSessionToken = function (userId, threadId, type, action, sessionToken) { return __awaiter(void 0, void 0, void 0, function () {
|
|
13
|
-
var result, isSessionRequiredForThisTypeAction,
|
|
14
|
-
var _a, _b;
|
|
15
|
-
return __generator(this, function (
|
|
16
|
-
switch (
|
|
13
|
+
var result, isSessionRequiredForThisTypeAction, oneDay, thread_1, sessionRequired, session, now;
|
|
14
|
+
var _a, _b, _c;
|
|
15
|
+
return __generator(this, function (_d) {
|
|
16
|
+
switch (_d.label) {
|
|
17
17
|
case 0:
|
|
18
18
|
result = { sessionId: "" };
|
|
19
19
|
if (!sessionJwtSecret) return [3 /*break*/, 6];
|
|
20
20
|
if (!!sessionToken) return [3 /*break*/, 5];
|
|
21
21
|
isSessionRequiredForThisTypeAction = (_a = sessionRequirement[type]) === null || _a === void 0 ? void 0 : _a[action];
|
|
22
22
|
if (!(isSessionRequiredForThisTypeAction !== undefined)) return [3 /*break*/, 4];
|
|
23
|
+
oneDay = Date.now() + 1000 * 60 * 60 * 24;
|
|
23
24
|
if (!(isSessionRequiredForThisTypeAction === "always")) return [3 /*break*/, 1];
|
|
24
25
|
result.error = Error.required;
|
|
26
|
+
result.sessionRequiredTill = oneDay;
|
|
25
27
|
return [3 /*break*/, 4];
|
|
26
28
|
case 1:
|
|
27
29
|
if (!(isSessionRequiredForThisTypeAction === "thread")) return [3 /*break*/, 4];
|
|
28
30
|
if (!(sessionForce == "true")) return [3 /*break*/, 2];
|
|
29
31
|
result.error = Error.required;
|
|
32
|
+
result.sessionRequiredTill = oneDay;
|
|
30
33
|
return [3 /*break*/, 4];
|
|
31
34
|
case 2: return [4 /*yield*/, threadCache.get(threadId, -1)];
|
|
32
35
|
case 3:
|
|
33
|
-
|
|
34
|
-
|
|
36
|
+
thread_1 = _d.sent();
|
|
37
|
+
sessionRequired = Object.fromEntries((_c = (_b = thread_1 === null || thread_1 === void 0 ? void 0 : thread_1.sessionRequired) === null || _b === void 0 ? void 0 : _b.map(function (userId, index) {
|
|
38
|
+
var _a;
|
|
39
|
+
return [
|
|
40
|
+
userId,
|
|
41
|
+
(_a = thread_1 === null || thread_1 === void 0 ? void 0 : thread_1.sessionRequiredTill) === null || _a === void 0 ? void 0 : _a[index]
|
|
42
|
+
];
|
|
43
|
+
})) !== null && _c !== void 0 ? _c : []);
|
|
44
|
+
if (sessionRequired[userId] !== undefined &&
|
|
45
|
+
sessionRequired[userId] < Date.now()) {
|
|
35
46
|
result.error = Error.required;
|
|
47
|
+
result.sessionRequiredTill = sessionRequired[userId];
|
|
36
48
|
}
|
|
37
|
-
|
|
49
|
+
_d.label = 4;
|
|
38
50
|
case 4: return [3 /*break*/, 6];
|
|
39
51
|
case 5:
|
|
40
52
|
try {
|
|
@@ -60,7 +72,7 @@ export var handleSessionToken = function (userId, threadId, type, action, sessio
|
|
|
60
72
|
err);
|
|
61
73
|
result.error = Error.invalid;
|
|
62
74
|
}
|
|
63
|
-
|
|
75
|
+
_d.label = 6;
|
|
64
76
|
case 6: return [2 /*return*/, result];
|
|
65
77
|
}
|
|
66
78
|
});
|
package/build/lib/types.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
export type ThreadInput = {
|
|
2
2
|
participants: string[];
|
|
3
|
-
};
|
|
3
|
+
} & ThreadSessionRequired;
|
|
4
4
|
export type ThreadSessionRequired = {
|
|
5
5
|
sessionRequired?: string[];
|
|
6
|
+
sessionRequiredTill?: number[];
|
|
6
7
|
};
|
|
7
8
|
export type Thread = {
|
|
8
9
|
id: string;
|
|
@@ -10,7 +11,7 @@ export type Thread = {
|
|
|
10
11
|
export type MessageInput = {
|
|
11
12
|
threadId: string;
|
|
12
13
|
type: "text" | "image" | "control" | "call" | "pooja";
|
|
13
|
-
action: "new" | "edit" | "delete" | "sessionStart" | "sessionExtend" | "sessionEnd" | "
|
|
14
|
+
action: "new" | "edit" | "delete" | "sessionStart" | "sessionExtend" | "sessionEnd" | "initiated" | "declined";
|
|
14
15
|
message: string;
|
|
15
16
|
sessionToken?: string;
|
|
16
17
|
};
|
package/build/lib/types.js
CHANGED
|
@@ -1,13 +1,7 @@
|
|
|
1
1
|
export var typeToAllowedActionsMap = {
|
|
2
2
|
text: ["new", "edit"],
|
|
3
3
|
image: ["new", "edit"],
|
|
4
|
-
control: [
|
|
5
|
-
"delete",
|
|
6
|
-
"sessionStart",
|
|
7
|
-
"sessionExtend",
|
|
8
|
-
"sessionEnd",
|
|
9
|
-
"sessionRequirementChange"
|
|
10
|
-
],
|
|
4
|
+
control: ["delete", "sessionStart", "sessionExtend", "sessionEnd"],
|
|
11
5
|
call: ["initiated", "declined"],
|
|
12
6
|
pooja: ["new", "edit"]
|
|
13
7
|
};
|
|
@@ -30,7 +30,8 @@ var postMessageHandler = function (request, event) { return __awaiter(void 0, vo
|
|
|
30
30
|
headers: { "Content-Type": "application/json; charset=utf-8" },
|
|
31
31
|
body: JSON.stringify({
|
|
32
32
|
message: sessionIdResult.error,
|
|
33
|
-
threadId: request.body.threadId
|
|
33
|
+
threadId: request.body.threadId,
|
|
34
|
+
sessionRequiredTill: sessionIdResult.sessionRequiredTill
|
|
34
35
|
})
|
|
35
36
|
}];
|
|
36
37
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"/thread":{"POST":{"body":{"parser":"json","schema":{"type":"object","additionalProperties":false,"required":["participants"],"properties":{"participants":{"type":"array","items":{"type":"string","pattern":"^[a-
|
|
1
|
+
{"/thread":{"POST":{"body":{"parser":"json","schema":{"type":"object","additionalProperties":false,"required":["participants"],"properties":{"participants":{"type":"array","items":{"type":"string","pattern":"^[a-f0-9]{32}$"},"minItems":2,"maxItems":2},"sessionRequired":{"type":"array","items":{"type":"string","pattern":"^[a-f0-9]{32}$"},"minItems":0,"maxItems":2},"sessionRequiredTill":{"type":"array","items":{"type":"number"},"minItems":0,"maxItems":2}}}}}},"/thread/{id}":{"GET":{"parameters":[{"name":"id","in":"path","schema":{"type":"string","pattern":"^[a-f0-9]{32}$"},"required":true}]}},"/thread/{id}/session":{"POST":{"parameters":[{"name":"id","in":"path","schema":{"type":"string","pattern":"^[a-f0-9]{32}$"},"required":true}],"body":{"parser":"json","schema":{"type":"object","required":["sessionRequired","sessionRequiredTill"],"properties":{"sessionRequired":{"type":"array","items":{"type":"string","pattern":"^[a-f0-9]{32}$"},"minItems":0,"maxItems":2},"sessionRequiredTill":{"type":"array","items":{"type":"number"},"minItems":0,"maxItems":2}}}}}}}
|
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
import { __assign, __awaiter, __generator } from "tslib";
|
|
2
|
-
import { RouteBuilder } from "somod-http-extension";
|
|
3
2
|
import { DynamoDBClient, GetItemCommand, PutItemCommand, UpdateItemCommand } from "@aws-sdk/client-dynamodb";
|
|
4
3
|
import { convertToAttr, marshall, unmarshall } from "@aws-sdk/util-dynamodb";
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import { getUserIdFromEvent } from "../../lib/getUserIdFromEvent";
|
|
4
|
+
import { RouteBuilder } from "somod-http-extension";
|
|
5
|
+
import { v1 as v1uuid } from "uuid";
|
|
8
6
|
var builder = new RouteBuilder();
|
|
9
7
|
var dynamodb = new DynamoDBClient();
|
|
10
8
|
var createThread = function (request) { return __awaiter(void 0, void 0, void 0, function () {
|
|
@@ -54,52 +52,32 @@ var getThread = function (request) { return __awaiter(void 0, void 0, void 0, fu
|
|
|
54
52
|
}
|
|
55
53
|
});
|
|
56
54
|
}); };
|
|
57
|
-
var updateSessionRequired = function (request
|
|
58
|
-
var
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
switch (_b.label) {
|
|
55
|
+
var updateSessionRequired = function (request) { return __awaiter(void 0, void 0, void 0, function () {
|
|
56
|
+
var updateItemCommand;
|
|
57
|
+
return __generator(this, function (_a) {
|
|
58
|
+
switch (_a.label) {
|
|
62
59
|
case 0:
|
|
63
|
-
userId = getUserIdFromEvent(event);
|
|
64
60
|
updateItemCommand = new UpdateItemCommand({
|
|
65
61
|
TableName: process.env.THREAD_TABLE_NAME + "",
|
|
66
62
|
Key: marshall({ id: request.parameters.path.id }),
|
|
67
|
-
UpdateExpression: "SET #sessionRequired = :sessionRequired",
|
|
63
|
+
UpdateExpression: "SET #sessionRequired = :sessionRequired, #sessionRequiredTill = :sessionRequiredTill",
|
|
68
64
|
ExpressionAttributeNames: {
|
|
69
|
-
"#sessionRequired": "sessionRequired"
|
|
65
|
+
"#sessionRequired": "sessionRequired",
|
|
66
|
+
"#sessionRequiredTill": "sessionRequiredTill"
|
|
70
67
|
},
|
|
71
68
|
ExpressionAttributeValues: {
|
|
72
|
-
":sessionRequired": convertToAttr(request.body.sessionRequired)
|
|
69
|
+
":sessionRequired": convertToAttr(request.body.sessionRequired),
|
|
70
|
+
":sessionRequiredTill": convertToAttr(request.body.sessionRequiredTill)
|
|
73
71
|
}
|
|
74
72
|
});
|
|
75
73
|
return [4 /*yield*/, dynamodb.send(updateItemCommand)];
|
|
76
74
|
case 1:
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
type: "control",
|
|
84
|
-
action: "sessionRequirementChange",
|
|
85
|
-
from: userId,
|
|
86
|
-
message: JSON.stringify(request.body.sessionRequired),
|
|
87
|
-
sentAt: Date.now(),
|
|
88
|
-
threadId: request.parameters.path.id
|
|
89
|
-
})];
|
|
90
|
-
case 3:
|
|
91
|
-
_b.sent();
|
|
92
|
-
return [3 /*break*/, 5];
|
|
93
|
-
case 4:
|
|
94
|
-
e_1 = _b.sent();
|
|
95
|
-
// eslint-disable-next-line no-console
|
|
96
|
-
console.error(e_1);
|
|
97
|
-
return [3 /*break*/, 5];
|
|
98
|
-
case 5: return [2 /*return*/, {
|
|
99
|
-
statusCode: 200,
|
|
100
|
-
headers: { "Content-Type": "application/json; charset=utf-8" },
|
|
101
|
-
body: JSON.stringify({ message: "Thread updated successfully" })
|
|
102
|
-
}];
|
|
75
|
+
_a.sent();
|
|
76
|
+
return [2 /*return*/, {
|
|
77
|
+
statusCode: 200,
|
|
78
|
+
headers: { "Content-Type": "application/json; charset=utf-8" },
|
|
79
|
+
body: JSON.stringify({ message: "Thread updated successfully" })
|
|
80
|
+
}];
|
|
103
81
|
}
|
|
104
82
|
});
|
|
105
83
|
}); };
|
|
@@ -26,7 +26,8 @@ builder.add("$default", function (message, event) { return __awaiter(void 0, voi
|
|
|
26
26
|
headers: { "Content-Type": "application/json; charset=utf-8" },
|
|
27
27
|
body: JSON.stringify({
|
|
28
28
|
message: sessionIdResult.error,
|
|
29
|
-
threadId: message.body.threadId
|
|
29
|
+
threadId: message.body.threadId,
|
|
30
|
+
sessionRequiredTill: sessionIdResult.sessionRequiredTill
|
|
30
31
|
})
|
|
31
32
|
}];
|
|
32
33
|
}
|
package/package.json
CHANGED