CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/client/messages.ts
Views: 821
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
/*
7
This are two simple functions that you can use anywhere in code that make it easy to send
8
a message to any other user and also download all messages you have sent or received.
9
See @cocalc/util/db-schema/messages for definitions of parameters.
10
11
The interactive message UI interface uses a different way of getting messages
12
via synctables/changefeeds, so that it dynamically updates whenever anything
13
changes.
14
15
Messages also trigger (throttled) emails to people with a verified email account.
16
17
Ideas/Application for this:
18
19
- make it easy for an instructor to send a message to everybody in their course.
20
21
- make it easy for a student in a class to contact "course support", which will involve
22
some special not-yet-implemented metadata on a message to track support progress.
23
24
- when some event happens, e.g., a computation completes, a message could be sent.
25
26
*/
27
28
import api from "./api";
29
import type { ApiMessagesGet, Message } from "@cocalc/util/db-schema/messages";
30
31
export class Messages {
32
// Send a message to the given accounts. Returns the id number of the message, which can be used
33
// via reply_id to send followup messages in the same thread.
34
send = async (opts): Promise<number> => {
35
return await send(opts);
36
};
37
38
get = async (opts): Promise<Message[]> => {
39
return await get(opts);
40
};
41
}
42
43
export async function send({
44
to_ids,
45
subject,
46
body,
47
reply_id,
48
}: {
49
// if to_ids is not given, then message is sent *to the user* themselves. This can be useful
50
// for various sort of alerts that can get backed by batched emails (e.g., my computation is done).
51
to_ids?: string[];
52
subject: string;
53
body: string;
54
reply_id?: number;
55
}): Promise<number> {
56
const { id } = await api("/messages/send", {
57
to_ids,
58
subject,
59
body,
60
reply_id,
61
});
62
return id;
63
}
64
65
export async function get(opts: ApiMessagesGet): Promise<Message[]> {
66
const { messages } = await api("/messages/get", opts);
67
return messages;
68
}
69
70