-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnctalkconversation.js
136 lines (110 loc) · 5.63 KB
/
nctalkconversation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
class TalkConversation {
constructor(talkclient, nchttp, capabilities, roominfo) {
this.nchttp = nchttp;
this.capabilities = capabilities;
this.roominfo = roominfo;
this.talkclient = talkclient;
this.lastmsgid = roominfo.lastReadMessage;
this.listenactive = false; // We start with active listing off
this.waitmsgongoing = false;
// Build url based on capabilities
let conversation_feature = capabilities.CheckFeature("chat-v");
if (conversation_feature == "chat-v2") {
this.chatVersion = 2;
}
else {
this.chatVersion = 1;
}
// So far only url API Version 1 exists...
// this.url = `/ocs/v2.php/apps/spreed/api/v1/chat/${this.roominfo.token}?lookIntoFuture=1&includeLastKnown=0&format=json&lastKnownMessageId=${this.lastmsgid}&setReadMarker=1`;
}
_geturl(func, param1) {
switch (func) {
case "SendMessage":
return `/ocs/v2.php/apps/spreed/api/v1/chat/${this.roominfo.token}?format=json`;
case "WaitNewMessages":
// https://nextcloud-talk.readthedocs.io/en/latest/chat/ - chat version chat-v2 doesn't impact url it added support for rich object strings
return `/ocs/v2.php/apps/spreed/api/v1/chat/${this.roominfo.token}?lookIntoFuture=1&setReadMarker=1&format=json&lastKnownMessageId=${this.lastmsgid}`;
case "ShareFile":
return `/ocs/v2.php/apps/files_sharing/api/v1/shares?shareType=10&shareWith=${this.roominfo.token}&path=${param1}`;
}
}
SendMessage(comment, Callback) {
let message = JSON.stringify({ message: comment, replyTo: 0 });
this.nchttp.RequestfromHost("POST", this._geturl("SendMessage"), message, (retcode, res) => {
switch (retcode) {
case "OK":
Callback();
break;
case "ERROR":
break;
}
});
}
ShareFile(filename, Callback) {
this.nchttp.RequestfromHost("POST", this._geturl("ShareFile", filename), null, (retcode, res) => {
switch (retcode) {
case "OK":
Callback();
break;
case "ERROR":
break;
}
});
}
SetListenMode(active) {
this.listenactive = active;
}
WaitNewMessages(Callback) {
if ((this.listenactive == true) && (this.waitmsgongoing == false)) {
this.waitmsgongoing = true; // Only one WaitNewMessages per conversation
this.talkclient.DebugLog("WaitNewMessages IN " + this.roominfo.token);
this.nchttp.RequestfromHost("GET", this._geturl("WaitNewMessages"), null, (retcode, res) => {
if (this.waitmsgongoing == false) {
this.talkclient.ErrorLog("Callback called with waitmsgongoing = false!", this.roominfo.token, retcode, res);
Callback("STOP", res);
}
else {
this.talkclient.DebugLog("WaitNewMessages OUT " + this.roominfo.token + " " + this.waitmsgongoing);
// WaitNewMessages is done - do this before any callbacks are called in case the trigger a new WaitNewMessage
this.waitmsgongoing = false;
// https://nextcloud-talk.readthedocs.io/en/latest/chat/#receive-chat-messages-of-a-conversation
switch (retcode) {
case "OK":
let messages = undefined;
this.talkclient.DebugLog(res);
if (res.statusCode == 200) {
// 200 OK
try {
messages = JSON.parse(res.body).ocs.data;
this.lastmsgid = res.headers["x-chat-last-given"];
Callback("OK", messages);
}
catch {
Callback("ERROR", `ERROR reply string is not a JSON ${res.body}`);
}
} else if (res.statusCode == 304) {
// 304 Not Modified When there were no older/newer messages
Callback("NOMSG", "WaitNewMessages - Talk timeout empty reply after no new message was received");
} else if (res.statusCode == 404) {
// 404 Not Found When the conversation could not be found for the participant
Callback("ERROR", "Error http statusCode - 404 Not Found When the conversation could not be found for the participant");
}
else if (res.statusCode == 412) {
// 412 Precondition Failed When the lobby is active and the user is not a moderator
Callback("ERROR", "Error http statusCode - 412 Precondition Failed When the lobby is active and the user is not a moderator");
}
else {
Callback("ERROR", `Error unknown http statusCode - ${res.statusCode}`);
}
break;
case "ERROR":
Callback("ERROR", res);
break;
}
}
});
}
}
}
module.exports = TalkConversation;