@betterinternship/core 2.1.1 → 2.2.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/dist/index.js +5 -5
- package/dist/lib/broker/broker.d.ts +4 -4
- package/dist/lib/broker/broker.js +28 -35
- package/dist/lib/broker/broker.js.map +1 -1
- package/dist/lib/chat/chat.d.ts +51 -51
- package/dist/lib/chat/chat.js +183 -183
- package/dist/lib/chat/index.d.ts +2 -2
- package/dist/lib/chat/index.js +2 -2
- package/dist/lib/chat/pb.d.ts +3 -3
- package/dist/lib/chat/pb.js +15 -15
- package/dist/lib/email/email.js +66 -66
- package/dist/lib/email/index.d.ts +1 -1
- package/dist/lib/email/index.js +1 -1
- package/dist/lib/forms/fields.client.d.ts +29 -29
- package/dist/lib/forms/index.d.ts +3 -3
- package/dist/lib/forms/index.js +3 -3
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export * from './lib/chat/index.js';
|
|
2
|
-
export * from './lib/env.js';
|
|
3
|
-
export * from './lib/broker/index.js';
|
|
4
|
-
export * from './lib/email/index.js';
|
|
5
|
-
export * from './lib/forms/index.js';
|
|
1
|
+
export * from './lib/chat/index.js';
|
|
2
|
+
export * from './lib/env.js';
|
|
3
|
+
export * from './lib/broker/index.js';
|
|
4
|
+
export * from './lib/email/index.js';
|
|
5
|
+
export * from './lib/forms/index.js';
|
|
6
6
|
//# sourceMappingURL=index.js.map
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import * as amqp from 'amqplib
|
|
1
|
+
import * as amqp from 'amqplib';
|
|
2
2
|
export declare const QueueName: (...args: string[]) => string;
|
|
3
3
|
export declare const BrokerAPI: {
|
|
4
4
|
assertQueue: (..._: any[]) => Promise<void>;
|
|
5
5
|
sendToQueue: (..._: any[]) => Promise<void>;
|
|
6
6
|
readFromQueue: (..._: any[]) => Promise<void>;
|
|
7
7
|
} | {
|
|
8
|
-
assertQueue: (queueId: string, options?: amqp.Options.AssertQueue) =>
|
|
9
|
-
sendToQueue: (queueId: string, content: any) =>
|
|
10
|
-
readFromQueue: (queueId: string) => Promise<
|
|
8
|
+
assertQueue: (queueId: string, options?: amqp.Options.AssertQueue) => Promise<amqp.Replies.AssertQueue>;
|
|
9
|
+
sendToQueue: (queueId: string, content: any) => boolean;
|
|
10
|
+
readFromQueue: <T>(queueId: string) => Promise<null>;
|
|
11
11
|
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import * as amqp from 'amqplib
|
|
1
|
+
import * as amqp from 'amqplib';
|
|
2
2
|
import { ENV } from '../env.js';
|
|
3
3
|
const RABBITMQ_URL = ENV.RABBITMQ_URL;
|
|
4
4
|
if (!RABBITMQ_URL && ENV.RABBITMQ_ENABLED)
|
|
@@ -17,36 +17,31 @@ export const BrokerAPI = await (async () => {
|
|
|
17
17
|
readFromQueue: async (..._) => { },
|
|
18
18
|
};
|
|
19
19
|
}
|
|
20
|
-
const connection = await
|
|
21
|
-
amqp.connect(RABBITMQ_URL, (err, connection) => {
|
|
22
|
-
if (err)
|
|
23
|
-
throw err;
|
|
24
|
-
resolve(connection);
|
|
25
|
-
});
|
|
26
|
-
});
|
|
27
|
-
const createChannel = async () => {
|
|
28
|
-
return new Promise((resolve) => {
|
|
29
|
-
connection.createChannel((err, channel) => {
|
|
30
|
-
if (err)
|
|
31
|
-
throw err;
|
|
32
|
-
resolve(channel);
|
|
33
|
-
});
|
|
34
|
-
});
|
|
35
|
-
};
|
|
20
|
+
const connection = await amqp.connect(RABBITMQ_URL);
|
|
36
21
|
const channels = {
|
|
37
|
-
asserter: await createChannel(),
|
|
38
|
-
reader: await createChannel(),
|
|
39
|
-
writer: await createChannel(),
|
|
22
|
+
asserter: await connection.createChannel(),
|
|
23
|
+
reader: await connection.createChannel(),
|
|
24
|
+
writer: await connection.createChannel(),
|
|
40
25
|
};
|
|
41
|
-
const assertQueue = (queueId, options) => {
|
|
42
|
-
channels.asserter.assertQueue(queueId, {
|
|
26
|
+
const assertQueue = async (queueId, options) => {
|
|
27
|
+
const result = await channels.asserter.assertQueue(queueId, {
|
|
28
|
+
durable: true,
|
|
29
|
+
...options,
|
|
30
|
+
});
|
|
43
31
|
console.log(`[BROKER] Queue "${queueId}" has been asserted.`);
|
|
32
|
+
return result;
|
|
44
33
|
};
|
|
45
34
|
const sendToQueue = (queueId, content) => {
|
|
46
|
-
channels.writer.sendToQueue(queueId, Buffer.from(JSON.stringify(content)));
|
|
47
|
-
|
|
35
|
+
const result = channels.writer.sendToQueue(queueId, Buffer.from(JSON.stringify(content)));
|
|
36
|
+
if (result) {
|
|
37
|
+
console.log(`[BROKER] Message sent to queue "${queueId}": ${JSON.stringify(content)}.`);
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
console.log(`[BROKER] Message could not sent to queue "${queueId}": ${JSON.stringify(content)}.`);
|
|
41
|
+
}
|
|
42
|
+
return result;
|
|
48
43
|
};
|
|
49
|
-
const readFromQueue = (queueId) => {
|
|
44
|
+
const readFromQueue = async (queueId) => {
|
|
50
45
|
const resolveFromQueue = (message) => {
|
|
51
46
|
channels.writer.ack(message);
|
|
52
47
|
console.log(`[BROKER] Message "${message.properties.messageId}" resolved from queue "${queueId}".`);
|
|
@@ -55,19 +50,17 @@ export const BrokerAPI = await (async () => {
|
|
|
55
50
|
channels.writer.nack(message);
|
|
56
51
|
console.log(`[BROKER] Message "${message.properties.messageId}" rejected from queue "${queueId}".`);
|
|
57
52
|
};
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
53
|
+
let result = null;
|
|
54
|
+
await channels.reader.consume(queueId, (message) => {
|
|
55
|
+
const content = message?.content?.toString?.();
|
|
56
|
+
result = {
|
|
57
|
+
content: content ? JSON.parse(content) : null,
|
|
61
58
|
resolve: () => message && resolveFromQueue(message),
|
|
62
59
|
reject: () => message && rejectFromQueue(message),
|
|
63
60
|
};
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
else {
|
|
68
|
-
resolve(null);
|
|
69
|
-
}
|
|
70
|
-
}));
|
|
61
|
+
return message ? result : null;
|
|
62
|
+
});
|
|
63
|
+
return result;
|
|
71
64
|
};
|
|
72
65
|
return {
|
|
73
66
|
assertQueue,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"broker.js","sourceRoot":"","sources":["../../../lib/broker/broker.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,IAAI,MAAM,
|
|
1
|
+
{"version":3,"file":"broker.js","sourceRoot":"","sources":["../../../lib/broker/broker.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,IAAI,MAAM,SAAS,CAAC;AAChC,OAAO,EAAE,GAAG,EAAE,MAAM,WAAW,CAAC;AAEhC,MAAM,YAAY,GAAG,GAAG,CAAC,YAAY,CAAC;AACtC,IAAI,CAAC,YAAY,IAAI,GAAG,CAAC,gBAAgB;IACvC,MAAM,IAAI,KAAK,CAAC,6CAA6C,CAAC,CAAC;AAKjE,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,GAAG,IAAc,EAAU,EAAE;IACrD,IAAI,GAAG,CAAC,WAAW,KAAK,YAAY;QAAE,OAAO,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;;QACvD,OAAO,CAAC,KAAK,EAAE,GAAG,IAAI,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AACzC,CAAC,CAAC;AAKF,MAAM,CAAC,MAAM,SAAS,GAAG,MAAM,CAAC,KAAK,IAAI,EAAE;IACzC,IAAI,CAAC,YAAY,EAAE,CAAC;QAClB,OAAO;YACL,WAAW,EAAE,KAAK,EAAE,GAAG,CAAC,EAAE,EAAE,GAAE,CAAC;YAC/B,WAAW,EAAE,KAAK,EAAE,GAAG,CAAC,EAAE,EAAE,GAAE,CAAC;YAC/B,aAAa,EAAE,KAAK,EAAE,GAAG,CAAC,EAAE,EAAE,GAAE,CAAC;SAClC,CAAC;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_id: string;
|
|
5
|
-
timestamp: number;
|
|
6
|
-
digested?: boolean;
|
|
7
|
-
}
|
|
8
|
-
export interface Consumer extends RecordModel {
|
|
9
|
-
email: string;
|
|
10
|
-
last_reads: {
|
|
11
|
-
[conversationId: string]: Message;
|
|
12
|
-
};
|
|
13
|
-
last_unreads: {
|
|
14
|
-
[conversationId: string]: Message;
|
|
15
|
-
};
|
|
16
|
-
conversations: string[];
|
|
17
|
-
}
|
|
18
|
-
export interface Conversation extends RecordModel {
|
|
19
|
-
id: string;
|
|
20
|
-
subscribers: string[];
|
|
21
|
-
contents: Message[];
|
|
22
|
-
last_message: Message;
|
|
23
|
-
}
|
|
24
|
-
export declare class ConsumerService {
|
|
25
|
-
findOne(id: string): Promise<Consumer>;
|
|
26
|
-
findAll(): Promise<Consumer[]>;
|
|
27
|
-
register(id: string, email: string): Promise<Consumer>;
|
|
28
|
-
setLastRead(id: string, conversationId: string, message: Message): Promise<RecordModel>;
|
|
29
|
-
authenticateAs(id: string): Promise<{
|
|
30
|
-
token: string;
|
|
31
|
-
user: import("pocketbase").AuthRecord;
|
|
32
|
-
}>;
|
|
33
|
-
}
|
|
34
|
-
export declare class MessageService {
|
|
35
|
-
private readonly consumerService;
|
|
36
|
-
private readonly conversationService;
|
|
37
|
-
private updateConversationUnread;
|
|
38
|
-
send(senderId: string, conversationId: string, messageContent: string): Promise<Message>;
|
|
39
|
-
read(consumerId: string, conversationId: string): Promise<Message>;
|
|
40
|
-
digest(consumerId: string, conversationId: string): Promise<Message>;
|
|
41
|
-
}
|
|
42
|
-
export declare class ConversationService {
|
|
43
|
-
consumerService: ConsumerService;
|
|
44
|
-
findOne(id: string): Promise<Conversation>;
|
|
45
|
-
appendMessage(conversationId: string, message: Message): Promise<Conversation>;
|
|
46
|
-
subscribe(subscriberId: string, conversationId: string): Promise<Conversation>;
|
|
47
|
-
getSubscriptions(subscriberId: any): Promise<Conversation[]>;
|
|
48
|
-
findExisting(userId: string, employerId: string): Promise<Conversation>;
|
|
49
|
-
checkIfSubscribed(subscriberId: string, conversationId: string): Promise<boolean>;
|
|
50
|
-
create(userId: string, employerId: string): Promise<Conversation>;
|
|
51
|
-
}
|
|
1
|
+
import { RecordModel } from 'pocketbase';
|
|
2
|
+
export interface Message {
|
|
3
|
+
message: string;
|
|
4
|
+
sender_id: string;
|
|
5
|
+
timestamp: number;
|
|
6
|
+
digested?: boolean;
|
|
7
|
+
}
|
|
8
|
+
export interface Consumer extends RecordModel {
|
|
9
|
+
email: string;
|
|
10
|
+
last_reads: {
|
|
11
|
+
[conversationId: string]: Message;
|
|
12
|
+
};
|
|
13
|
+
last_unreads: {
|
|
14
|
+
[conversationId: string]: Message;
|
|
15
|
+
};
|
|
16
|
+
conversations: string[];
|
|
17
|
+
}
|
|
18
|
+
export interface Conversation extends RecordModel {
|
|
19
|
+
id: string;
|
|
20
|
+
subscribers: string[];
|
|
21
|
+
contents: Message[];
|
|
22
|
+
last_message: Message;
|
|
23
|
+
}
|
|
24
|
+
export declare class ConsumerService {
|
|
25
|
+
findOne(id: string): Promise<Consumer>;
|
|
26
|
+
findAll(): Promise<Consumer[]>;
|
|
27
|
+
register(id: string, email: string): Promise<Consumer>;
|
|
28
|
+
setLastRead(id: string, conversationId: string, message: Message): Promise<RecordModel>;
|
|
29
|
+
authenticateAs(id: string): Promise<{
|
|
30
|
+
token: string;
|
|
31
|
+
user: import("pocketbase").AuthRecord;
|
|
32
|
+
}>;
|
|
33
|
+
}
|
|
34
|
+
export declare class MessageService {
|
|
35
|
+
private readonly consumerService;
|
|
36
|
+
private readonly conversationService;
|
|
37
|
+
private updateConversationUnread;
|
|
38
|
+
send(senderId: string, conversationId: string, messageContent: string): Promise<Message>;
|
|
39
|
+
read(consumerId: string, conversationId: string): Promise<Message>;
|
|
40
|
+
digest(consumerId: string, conversationId: string): Promise<Message>;
|
|
41
|
+
}
|
|
42
|
+
export declare class ConversationService {
|
|
43
|
+
consumerService: ConsumerService;
|
|
44
|
+
findOne(id: string): Promise<Conversation>;
|
|
45
|
+
appendMessage(conversationId: string, message: Message): Promise<Conversation>;
|
|
46
|
+
subscribe(subscriberId: string, conversationId: string): Promise<Conversation>;
|
|
47
|
+
getSubscriptions(subscriberId: any): Promise<Conversation[]>;
|
|
48
|
+
findExisting(userId: string, employerId: string): Promise<Conversation>;
|
|
49
|
+
checkIfSubscribed(subscriberId: string, conversationId: string): Promise<boolean>;
|
|
50
|
+
create(userId: string, employerId: string): Promise<Conversation>;
|
|
51
|
+
}
|
package/dist/lib/chat/chat.js
CHANGED
|
@@ -1,184 +1,184 @@
|
|
|
1
|
-
import { v4 as uuid } from 'uuid';
|
|
2
|
-
import { pb } from './pb.js';
|
|
3
|
-
import * as generator from 'generate-password';
|
|
4
|
-
export class ConsumerService {
|
|
5
|
-
async findOne(id) {
|
|
6
|
-
return await pb.collection('users').getOne(id);
|
|
7
|
-
}
|
|
8
|
-
async findAll() {
|
|
9
|
-
return await pb.collection('users').getFullList();
|
|
10
|
-
}
|
|
11
|
-
async register(id, email) {
|
|
12
|
-
try {
|
|
13
|
-
return await pb.collection('users').getOne(id);
|
|
14
|
-
}
|
|
15
|
-
catch (error) {
|
|
16
|
-
const e = error;
|
|
17
|
-
if (e.status !== 404)
|
|
18
|
-
throw error;
|
|
19
|
-
}
|
|
20
|
-
const password = generator.generate({
|
|
21
|
-
length: 10,
|
|
22
|
-
numbers: true,
|
|
23
|
-
symbols: true,
|
|
24
|
-
uppercase: true,
|
|
25
|
-
lowercase: true,
|
|
26
|
-
strict: true,
|
|
27
|
-
});
|
|
28
|
-
return await pb.collection('users').create({
|
|
29
|
-
id,
|
|
30
|
-
email,
|
|
31
|
-
password,
|
|
32
|
-
passwordConfirm: password,
|
|
33
|
-
last_reads: {},
|
|
34
|
-
last_unreads: {},
|
|
35
|
-
conversations: [],
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
async setLastRead(id, conversationId, message) {
|
|
39
|
-
const subscriber = await pb.collection('users').getOne(id);
|
|
40
|
-
const last_reads = subscriber.last_reads;
|
|
41
|
-
const last_unreads = subscriber.last_unreads;
|
|
42
|
-
return await pb.collection('users').update(id, {
|
|
43
|
-
last_reads: {
|
|
44
|
-
...last_reads,
|
|
45
|
-
[conversationId]: last_reads?.[conversationId]?.timestamp < message.timestamp
|
|
46
|
-
? message
|
|
47
|
-
: last_reads?.[conversationId],
|
|
48
|
-
},
|
|
49
|
-
last_unreads: {
|
|
50
|
-
...last_unreads,
|
|
51
|
-
[conversationId]: last_unreads?.[conversationId]?.timestamp < message.timestamp
|
|
52
|
-
? message
|
|
53
|
-
: last_unreads?.[conversationId],
|
|
54
|
-
},
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
async authenticateAs(id) {
|
|
58
|
-
const asUser = await pb.collection('users').impersonate(id, 60 * 60 * 24);
|
|
59
|
-
const token = asUser.authStore.token;
|
|
60
|
-
const record = asUser.authStore.record;
|
|
61
|
-
return { token, user: record };
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
export class MessageService {
|
|
65
|
-
consumerService = new ConsumerService();
|
|
66
|
-
conversationService = new ConversationService();
|
|
67
|
-
async updateConversationUnread(subscriberId, conversationId, message) {
|
|
68
|
-
let subscriber;
|
|
69
|
-
try {
|
|
70
|
-
subscriber = await this.consumerService.findOne(subscriberId);
|
|
71
|
-
}
|
|
72
|
-
catch (error) {
|
|
73
|
-
const e = error;
|
|
74
|
-
if (e.status !== 404)
|
|
75
|
-
throw error;
|
|
76
|
-
subscriber = await this.consumerService.register(subscriberId, '');
|
|
77
|
-
}
|
|
78
|
-
await pb.collection('users').update(subscriberId, {
|
|
79
|
-
last_unreads: {
|
|
80
|
-
...subscriber.last_unreads,
|
|
81
|
-
[conversationId]: subscriber.last_unreads?.[conversationId]?.timestamp <
|
|
82
|
-
message.timestamp
|
|
83
|
-
? message
|
|
84
|
-
: subscriber.last_unreads?.[conversationId],
|
|
85
|
-
},
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
async send(senderId, conversationId, messageContent) {
|
|
89
|
-
const message = {
|
|
90
|
-
sender_id: senderId,
|
|
91
|
-
message: messageContent,
|
|
92
|
-
timestamp: new Date().getTime(),
|
|
93
|
-
};
|
|
94
|
-
const conversation = await this.conversationService.appendMessage(conversationId, message);
|
|
95
|
-
await this.consumerService.setLastRead(senderId, conversationId, message);
|
|
96
|
-
await Promise.all(conversation.subscribers.map((subscriberId) => this.updateConversationUnread(subscriberId, conversationId, message)));
|
|
97
|
-
return message;
|
|
98
|
-
}
|
|
99
|
-
async read(consumerId, conversationId) {
|
|
100
|
-
const consumer = await pb.collection('users').getOne(consumerId);
|
|
101
|
-
const conversation = await pb
|
|
102
|
-
.collection('conversations')
|
|
103
|
-
.getOne(conversationId);
|
|
104
|
-
await pb.collection('users').update(consumerId, {
|
|
105
|
-
last_reads: {
|
|
106
|
-
...consumer.last_reads,
|
|
107
|
-
[conversationId]: conversation.last_message,
|
|
108
|
-
},
|
|
109
|
-
last_unreads: {
|
|
110
|
-
...consumer.last_unreads,
|
|
111
|
-
[conversationId]: conversation.last_message,
|
|
112
|
-
},
|
|
113
|
-
});
|
|
114
|
-
return conversation.last_message;
|
|
115
|
-
}
|
|
116
|
-
async digest(consumerId, conversationId) {
|
|
117
|
-
const consumer = await pb.collection('users').getOne(consumerId);
|
|
118
|
-
const conversation = await pb
|
|
119
|
-
.collection('conversations')
|
|
120
|
-
.getOne(conversationId);
|
|
121
|
-
await pb.collection('users').update(consumerId, {
|
|
122
|
-
last_unreads: {
|
|
123
|
-
...consumer.last_unreads,
|
|
124
|
-
[conversationId]: {
|
|
125
|
-
...conversation.last_message,
|
|
126
|
-
digested: true,
|
|
127
|
-
},
|
|
128
|
-
},
|
|
129
|
-
});
|
|
130
|
-
return { ...conversation.last_message, digested: true };
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
export class ConversationService {
|
|
134
|
-
consumerService = new ConsumerService();
|
|
135
|
-
async findOne(id) {
|
|
136
|
-
return await pb.collection('conversations').getOne(id);
|
|
137
|
-
}
|
|
138
|
-
async appendMessage(conversationId, message) {
|
|
139
|
-
const conversation = await this.findOne(conversationId);
|
|
140
|
-
return await pb.collection('conversations').update(conversationId, {
|
|
141
|
-
contents: [...conversation.contents, message],
|
|
142
|
-
last_message: message,
|
|
143
|
-
});
|
|
144
|
-
}
|
|
145
|
-
async subscribe(subscriberId, conversationId) {
|
|
146
|
-
const conversation = await this.findOne(conversationId);
|
|
147
|
-
const subscriber = await this.consumerService.findOne(subscriberId);
|
|
148
|
-
await pb.collection('users').update(subscriberId, {
|
|
149
|
-
conversations: [...subscriber.conversations, conversationId],
|
|
150
|
-
});
|
|
151
|
-
return await pb.collection('conversations').update(conversationId, {
|
|
152
|
-
subscribers: [...conversation.subscribers, subscriberId],
|
|
153
|
-
});
|
|
154
|
-
}
|
|
155
|
-
async getSubscriptions(subscriberId) {
|
|
156
|
-
return await pb.collection('conversations').getFullList({
|
|
157
|
-
filter: `subscribers ~ '${subscriberId}'`,
|
|
158
|
-
});
|
|
159
|
-
}
|
|
160
|
-
async findExisting(userId, employerId) {
|
|
161
|
-
return await pb
|
|
162
|
-
.collection('conversations')
|
|
163
|
-
.getFirstListItem(`subscribers ~ '${userId}' && subscribers ~ '${employerId}'`);
|
|
164
|
-
}
|
|
165
|
-
async checkIfSubscribed(subscriberId, conversationId) {
|
|
166
|
-
const conversation = await this.findOne(conversationId);
|
|
167
|
-
if (!conversation)
|
|
168
|
-
return false;
|
|
169
|
-
if (!conversation.subscribers.includes(subscriberId))
|
|
170
|
-
return false;
|
|
171
|
-
return true;
|
|
172
|
-
}
|
|
173
|
-
async create(userId, employerId) {
|
|
174
|
-
const conversationId = uuid();
|
|
175
|
-
const newConversation = await pb.collection('conversations').create({
|
|
176
|
-
id: conversationId,
|
|
177
|
-
subscribers: [userId, employerId],
|
|
178
|
-
contents: [],
|
|
179
|
-
last_message: {},
|
|
180
|
-
});
|
|
181
|
-
return newConversation;
|
|
182
|
-
}
|
|
183
|
-
}
|
|
1
|
+
import { v4 as uuid } from 'uuid';
|
|
2
|
+
import { pb } from './pb.js';
|
|
3
|
+
import * as generator from 'generate-password';
|
|
4
|
+
export class ConsumerService {
|
|
5
|
+
async findOne(id) {
|
|
6
|
+
return await pb.collection('users').getOne(id);
|
|
7
|
+
}
|
|
8
|
+
async findAll() {
|
|
9
|
+
return await pb.collection('users').getFullList();
|
|
10
|
+
}
|
|
11
|
+
async register(id, email) {
|
|
12
|
+
try {
|
|
13
|
+
return await pb.collection('users').getOne(id);
|
|
14
|
+
}
|
|
15
|
+
catch (error) {
|
|
16
|
+
const e = error;
|
|
17
|
+
if (e.status !== 404)
|
|
18
|
+
throw error;
|
|
19
|
+
}
|
|
20
|
+
const password = generator.generate({
|
|
21
|
+
length: 10,
|
|
22
|
+
numbers: true,
|
|
23
|
+
symbols: true,
|
|
24
|
+
uppercase: true,
|
|
25
|
+
lowercase: true,
|
|
26
|
+
strict: true,
|
|
27
|
+
});
|
|
28
|
+
return await pb.collection('users').create({
|
|
29
|
+
id,
|
|
30
|
+
email,
|
|
31
|
+
password,
|
|
32
|
+
passwordConfirm: password,
|
|
33
|
+
last_reads: {},
|
|
34
|
+
last_unreads: {},
|
|
35
|
+
conversations: [],
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
async setLastRead(id, conversationId, message) {
|
|
39
|
+
const subscriber = await pb.collection('users').getOne(id);
|
|
40
|
+
const last_reads = subscriber.last_reads;
|
|
41
|
+
const last_unreads = subscriber.last_unreads;
|
|
42
|
+
return await pb.collection('users').update(id, {
|
|
43
|
+
last_reads: {
|
|
44
|
+
...last_reads,
|
|
45
|
+
[conversationId]: last_reads?.[conversationId]?.timestamp < message.timestamp
|
|
46
|
+
? message
|
|
47
|
+
: last_reads?.[conversationId],
|
|
48
|
+
},
|
|
49
|
+
last_unreads: {
|
|
50
|
+
...last_unreads,
|
|
51
|
+
[conversationId]: last_unreads?.[conversationId]?.timestamp < message.timestamp
|
|
52
|
+
? message
|
|
53
|
+
: last_unreads?.[conversationId],
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
async authenticateAs(id) {
|
|
58
|
+
const asUser = await pb.collection('users').impersonate(id, 60 * 60 * 24);
|
|
59
|
+
const token = asUser.authStore.token;
|
|
60
|
+
const record = asUser.authStore.record;
|
|
61
|
+
return { token, user: record };
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
export class MessageService {
|
|
65
|
+
consumerService = new ConsumerService();
|
|
66
|
+
conversationService = new ConversationService();
|
|
67
|
+
async updateConversationUnread(subscriberId, conversationId, message) {
|
|
68
|
+
let subscriber;
|
|
69
|
+
try {
|
|
70
|
+
subscriber = await this.consumerService.findOne(subscriberId);
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
const e = error;
|
|
74
|
+
if (e.status !== 404)
|
|
75
|
+
throw error;
|
|
76
|
+
subscriber = await this.consumerService.register(subscriberId, '');
|
|
77
|
+
}
|
|
78
|
+
await pb.collection('users').update(subscriberId, {
|
|
79
|
+
last_unreads: {
|
|
80
|
+
...subscriber.last_unreads,
|
|
81
|
+
[conversationId]: subscriber.last_unreads?.[conversationId]?.timestamp <
|
|
82
|
+
message.timestamp
|
|
83
|
+
? message
|
|
84
|
+
: subscriber.last_unreads?.[conversationId],
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
async send(senderId, conversationId, messageContent) {
|
|
89
|
+
const message = {
|
|
90
|
+
sender_id: senderId,
|
|
91
|
+
message: messageContent,
|
|
92
|
+
timestamp: new Date().getTime(),
|
|
93
|
+
};
|
|
94
|
+
const conversation = await this.conversationService.appendMessage(conversationId, message);
|
|
95
|
+
await this.consumerService.setLastRead(senderId, conversationId, message);
|
|
96
|
+
await Promise.all(conversation.subscribers.map((subscriberId) => this.updateConversationUnread(subscriberId, conversationId, message)));
|
|
97
|
+
return message;
|
|
98
|
+
}
|
|
99
|
+
async read(consumerId, conversationId) {
|
|
100
|
+
const consumer = await pb.collection('users').getOne(consumerId);
|
|
101
|
+
const conversation = await pb
|
|
102
|
+
.collection('conversations')
|
|
103
|
+
.getOne(conversationId);
|
|
104
|
+
await pb.collection('users').update(consumerId, {
|
|
105
|
+
last_reads: {
|
|
106
|
+
...consumer.last_reads,
|
|
107
|
+
[conversationId]: conversation.last_message,
|
|
108
|
+
},
|
|
109
|
+
last_unreads: {
|
|
110
|
+
...consumer.last_unreads,
|
|
111
|
+
[conversationId]: conversation.last_message,
|
|
112
|
+
},
|
|
113
|
+
});
|
|
114
|
+
return conversation.last_message;
|
|
115
|
+
}
|
|
116
|
+
async digest(consumerId, conversationId) {
|
|
117
|
+
const consumer = await pb.collection('users').getOne(consumerId);
|
|
118
|
+
const conversation = await pb
|
|
119
|
+
.collection('conversations')
|
|
120
|
+
.getOne(conversationId);
|
|
121
|
+
await pb.collection('users').update(consumerId, {
|
|
122
|
+
last_unreads: {
|
|
123
|
+
...consumer.last_unreads,
|
|
124
|
+
[conversationId]: {
|
|
125
|
+
...conversation.last_message,
|
|
126
|
+
digested: true,
|
|
127
|
+
},
|
|
128
|
+
},
|
|
129
|
+
});
|
|
130
|
+
return { ...conversation.last_message, digested: true };
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
export class ConversationService {
|
|
134
|
+
consumerService = new ConsumerService();
|
|
135
|
+
async findOne(id) {
|
|
136
|
+
return await pb.collection('conversations').getOne(id);
|
|
137
|
+
}
|
|
138
|
+
async appendMessage(conversationId, message) {
|
|
139
|
+
const conversation = await this.findOne(conversationId);
|
|
140
|
+
return await pb.collection('conversations').update(conversationId, {
|
|
141
|
+
contents: [...conversation.contents, message],
|
|
142
|
+
last_message: message,
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
async subscribe(subscriberId, conversationId) {
|
|
146
|
+
const conversation = await this.findOne(conversationId);
|
|
147
|
+
const subscriber = await this.consumerService.findOne(subscriberId);
|
|
148
|
+
await pb.collection('users').update(subscriberId, {
|
|
149
|
+
conversations: [...subscriber.conversations, conversationId],
|
|
150
|
+
});
|
|
151
|
+
return await pb.collection('conversations').update(conversationId, {
|
|
152
|
+
subscribers: [...conversation.subscribers, subscriberId],
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
async getSubscriptions(subscriberId) {
|
|
156
|
+
return await pb.collection('conversations').getFullList({
|
|
157
|
+
filter: `subscribers ~ '${subscriberId}'`,
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
async findExisting(userId, employerId) {
|
|
161
|
+
return await pb
|
|
162
|
+
.collection('conversations')
|
|
163
|
+
.getFirstListItem(`subscribers ~ '${userId}' && subscribers ~ '${employerId}'`);
|
|
164
|
+
}
|
|
165
|
+
async checkIfSubscribed(subscriberId, conversationId) {
|
|
166
|
+
const conversation = await this.findOne(conversationId);
|
|
167
|
+
if (!conversation)
|
|
168
|
+
return false;
|
|
169
|
+
if (!conversation.subscribers.includes(subscriberId))
|
|
170
|
+
return false;
|
|
171
|
+
return true;
|
|
172
|
+
}
|
|
173
|
+
async create(userId, employerId) {
|
|
174
|
+
const conversationId = uuid();
|
|
175
|
+
const newConversation = await pb.collection('conversations').create({
|
|
176
|
+
id: conversationId,
|
|
177
|
+
subscribers: [userId, employerId],
|
|
178
|
+
contents: [],
|
|
179
|
+
last_message: {},
|
|
180
|
+
});
|
|
181
|
+
return newConversation;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
184
|
//# sourceMappingURL=chat.js.map
|
package/dist/lib/chat/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from './chat.js';
|
|
2
|
-
export * from './pb.js';
|
|
1
|
+
export * from './chat.js';
|
|
2
|
+
export * from './pb.js';
|
package/dist/lib/chat/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export * from './chat.js';
|
|
2
|
-
export * from './pb.js';
|
|
1
|
+
export * from './chat.js';
|
|
2
|
+
export * from './pb.js';
|
|
3
3
|
//# sourceMappingURL=index.js.map
|
package/dist/lib/chat/pb.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import PocketBase from 'pocketbase';
|
|
2
|
-
export declare const pb: PocketBase;
|
|
3
|
-
export declare const authenticatePocketbase: () => Promise<void>;
|
|
1
|
+
import PocketBase from 'pocketbase';
|
|
2
|
+
export declare const pb: PocketBase;
|
|
3
|
+
export declare const authenticatePocketbase: () => Promise<void>;
|