Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/editors/markdown-input/mentions.ts
5913 views
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import { redux } from "@cocalc/frontend/app-framework";
7
import { webapp_client } from "@cocalc/frontend/webapp-client";
8
import { isValidUUID, original_path } from "@cocalc/util/misc";
9
10
interface Mention {
11
account_id: string;
12
description: string;
13
fragment_id?: string;
14
}
15
16
const seenFragmentIds = new Set<string>();
17
18
export async function submit_mentions(
19
project_id: string,
20
path: string,
21
mentions: Mention[],
22
): Promise<void> {
23
const source = redux.getStore("account")?.get("account_id");
24
if (source == null) {
25
return;
26
}
27
for (const { account_id, description, fragment_id } of mentions) {
28
if (!isValidUUID(account_id)) {
29
// Ignore all language model mentions, they are processed by the chat actions in the frontend
30
continue;
31
}
32
if (fragment_id) {
33
if (seenFragmentIds.has(fragment_id)) {
34
continue;
35
}
36
seenFragmentIds.add(fragment_id);
37
}
38
try {
39
await webapp_client.query_client.query({
40
query: {
41
mentions: {
42
project_id,
43
path: original_path(path),
44
fragment_id,
45
target: account_id,
46
priority: 2,
47
description,
48
source,
49
},
50
},
51
});
52
} catch (err) {
53
// TODO: this is just naively assuming that no errors happen.
54
// What if there is a network blip?
55
// Then we would just loose the mention, which is no good. Do better.
56
console.warn("Failed to submit mention ", err);
57
}
58
}
59
}
60
61