signalbird 1.4.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/README.md +576 -0
- package/dist/angular.d.mts +53 -0
- package/dist/angular.d.ts +53 -0
- package/dist/angular.js +578 -0
- package/dist/angular.js.map +1 -0
- package/dist/angular.mjs +574 -0
- package/dist/angular.mjs.map +1 -0
- package/dist/app.d.mts +343 -0
- package/dist/app.d.ts +343 -0
- package/dist/app.js +518 -0
- package/dist/app.js.map +1 -0
- package/dist/app.mjs +514 -0
- package/dist/app.mjs.map +1 -0
- package/dist/browser.d.mts +56 -0
- package/dist/browser.d.ts +56 -0
- package/dist/browser.js +113 -0
- package/dist/browser.js.map +1 -0
- package/dist/browser.mjs +109 -0
- package/dist/browser.mjs.map +1 -0
- package/dist/index.d.mts +1170 -0
- package/dist/index.d.ts +1170 -0
- package/dist/index.js +853 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +841 -0
- package/dist/index.mjs.map +1 -0
- package/dist/react-native.d.mts +46 -0
- package/dist/react-native.d.ts +46 -0
- package/dist/react-native.js +576 -0
- package/dist/react-native.js.map +1 -0
- package/dist/react-native.mjs +569 -0
- package/dist/react-native.mjs.map +1 -0
- package/dist/react.d.mts +43 -0
- package/dist/react.d.ts +43 -0
- package/dist/react.js +587 -0
- package/dist/react.js.map +1 -0
- package/dist/react.mjs +580 -0
- package/dist/react.mjs.map +1 -0
- package/dist/signalbird.js +141 -0
- package/dist/vue.d.mts +46 -0
- package/dist/vue.d.ts +46 -0
- package/dist/vue.js +575 -0
- package/dist/vue.js.map +1 -0
- package/dist/vue.mjs +568 -0
- package/dist/vue.mjs.map +1 -0
- package/package.json +125 -0
package/dist/angular.js
ADDED
|
@@ -0,0 +1,578 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var rxjs = require('rxjs');
|
|
4
|
+
|
|
5
|
+
// src/angular/index.ts
|
|
6
|
+
|
|
7
|
+
// src/app/client.ts
|
|
8
|
+
var DEFAULT_BASE_URL = "https://signalbird.io/api";
|
|
9
|
+
var STORAGE_KEY = "sb_visitor";
|
|
10
|
+
function defaultStorage() {
|
|
11
|
+
try {
|
|
12
|
+
if (typeof localStorage !== "undefined") {
|
|
13
|
+
return {
|
|
14
|
+
getItem: (k) => localStorage.getItem(k),
|
|
15
|
+
setItem: (k, v) => localStorage.setItem(k, v),
|
|
16
|
+
removeItem: (k) => localStorage.removeItem(k)
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
} catch {
|
|
20
|
+
}
|
|
21
|
+
const memory = /* @__PURE__ */ new Map();
|
|
22
|
+
return {
|
|
23
|
+
getItem: (k) => memory.get(k) ?? null,
|
|
24
|
+
setItem: (k, v) => void memory.set(k, v),
|
|
25
|
+
removeItem: (k) => void memory.delete(k)
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
function clientId() {
|
|
29
|
+
try {
|
|
30
|
+
if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
|
|
31
|
+
return crypto.randomUUID();
|
|
32
|
+
}
|
|
33
|
+
} catch {
|
|
34
|
+
}
|
|
35
|
+
return `c_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 10)}`;
|
|
36
|
+
}
|
|
37
|
+
var SignalbirdApp = class {
|
|
38
|
+
constructor(config) {
|
|
39
|
+
this.config = config;
|
|
40
|
+
this.visitor = null;
|
|
41
|
+
this.loaded = false;
|
|
42
|
+
if (!config?.appKey) {
|
|
43
|
+
throw new Error("Signalbird: appKey zorunlu (sbw_pub_\u2026).");
|
|
44
|
+
}
|
|
45
|
+
if (!config.appKey.startsWith("sbw_pub_")) {
|
|
46
|
+
throw new Error(
|
|
47
|
+
"Signalbird: uygulama istemcisi a\xE7\u0131k uygulama anahtar\u0131 ister (sbw_pub_\u2026). Tak\u0131m anahtar\u0131n\u0131 (sb_\u2026) istemci koduna KOYMAYIN."
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
this.baseUrl = (config.baseUrl ?? DEFAULT_BASE_URL).replace(/\/$/, "");
|
|
51
|
+
this.storage = config.storage ?? defaultStorage();
|
|
52
|
+
this.timeout = config.timeout ?? 1e4;
|
|
53
|
+
this.doFetch = config.fetchImpl ?? ((...args) => fetch(...args));
|
|
54
|
+
}
|
|
55
|
+
// ── Kimlik ────────────────────────────────────────────────────────────
|
|
56
|
+
/** Uygulama ayarları: sohbet açık mı, renk, çalışma saati, ön-form. */
|
|
57
|
+
bootstrap() {
|
|
58
|
+
return this.request("POST", "/v1/sdk/bootstrap", { locale: this.config.locale });
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Ziyaretçi oturumu açar ya da mevcut olanı günceller.
|
|
62
|
+
*
|
|
63
|
+
* Sır saklanır; ikinci çağrı aynı ziyaretçiyi tazeler. Sunucu `VISITOR_INVALID`
|
|
64
|
+
* derse yerel kimlik silinir ve bir sonraki çağrı yeni oturum açar.
|
|
65
|
+
*/
|
|
66
|
+
async startSession(input = {}) {
|
|
67
|
+
const result = await this.request("POST", "/v1/sdk/chat/session", input);
|
|
68
|
+
const visitor = result.data?.visitor;
|
|
69
|
+
if (result.ok && visitor?.id && visitor.secret) {
|
|
70
|
+
await this.storeVisitor({
|
|
71
|
+
id: visitor.id,
|
|
72
|
+
secret: visitor.secret,
|
|
73
|
+
appKey: this.config.appKey,
|
|
74
|
+
name: visitor.name ?? null,
|
|
75
|
+
email: visitor.email ?? null
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
return result;
|
|
79
|
+
}
|
|
80
|
+
/** Oturum açmış kullanıcıyı ziyaretçiye bağlar (kişi kaydı upsert edilir). */
|
|
81
|
+
identify(input) {
|
|
82
|
+
return this.request("POST", "/v1/sdk/identify", input);
|
|
83
|
+
}
|
|
84
|
+
/** Saklanan ziyaretçi kimliği — yoksa `null`. */
|
|
85
|
+
async currentVisitor() {
|
|
86
|
+
const stored = await this.loadVisitor();
|
|
87
|
+
return stored ? { id: stored.id, name: stored.name, email: stored.email } : null;
|
|
88
|
+
}
|
|
89
|
+
/** Yerel kimliği siler: çıkış yapıldığında çağrılır. Sunucudaki kayıt kalır. */
|
|
90
|
+
async signOut() {
|
|
91
|
+
this.visitor = null;
|
|
92
|
+
this.loaded = true;
|
|
93
|
+
await this.storage.removeItem(STORAGE_KEY);
|
|
94
|
+
}
|
|
95
|
+
// ── Sohbet ────────────────────────────────────────────────────────────
|
|
96
|
+
listConversations() {
|
|
97
|
+
return this.request("GET", "/v1/sdk/chat/conversations");
|
|
98
|
+
}
|
|
99
|
+
getConversation(id, query) {
|
|
100
|
+
return this.request("GET", `/v1/sdk/chat/conversations/${enc(id)}`, void 0, query);
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* İlk mesajla konuşma açar. Kota burada harcanır — konuşma başına sayılır,
|
|
104
|
+
* mesaj başına değil.
|
|
105
|
+
*/
|
|
106
|
+
startConversation(input) {
|
|
107
|
+
return this.request("POST", "/v1/sdk/chat/conversations", {
|
|
108
|
+
client_id: clientId(),
|
|
109
|
+
...input
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
sendMessage(conversationId, input) {
|
|
113
|
+
return this.request("POST", `/v1/sdk/chat/conversations/${enc(conversationId)}/messages`, {
|
|
114
|
+
client_id: clientId(),
|
|
115
|
+
...input
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
/** Yalnız kendi mesajı ve gönderimden sonraki 15 dakika içinde. */
|
|
119
|
+
editMessage(conversationId, messageId, body) {
|
|
120
|
+
return this.request(
|
|
121
|
+
"PATCH",
|
|
122
|
+
`/v1/sdk/chat/conversations/${enc(conversationId)}/messages/${enc(messageId)}`,
|
|
123
|
+
{ body }
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
deleteMessage(conversationId, messageId) {
|
|
127
|
+
return this.request(
|
|
128
|
+
"DELETE",
|
|
129
|
+
`/v1/sdk/chat/conversations/${enc(conversationId)}/messages/${enc(messageId)}`
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
/** Aynı emoji ikinci kez gönderilirse tepki kaldırılır. */
|
|
133
|
+
reactToMessage(conversationId, messageId, emoji) {
|
|
134
|
+
return this.request(
|
|
135
|
+
"POST",
|
|
136
|
+
`/v1/sdk/chat/conversations/${enc(conversationId)}/messages/${enc(messageId)}/reactions`,
|
|
137
|
+
{ emoji }
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
setTyping(conversationId, isTyping) {
|
|
141
|
+
return this.request("POST", `/v1/sdk/chat/conversations/${enc(conversationId)}/typing`, {
|
|
142
|
+
is_typing: isTyping
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
markRead(conversationId, lastMessageId) {
|
|
146
|
+
return this.request("POST", `/v1/sdk/chat/conversations/${enc(conversationId)}/read`, {
|
|
147
|
+
last_message_id: lastMessageId
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Ek dosya yükler; dönen tanımlayıcı `sendMessage`'a `attachments` içinde
|
|
152
|
+
* verilir. İki adım olmasının sebebi: dosya yüklenirken mesaj metni hâlâ
|
|
153
|
+
* yazılıyor olabilir ve yarım kalan yükleme mesaj kaydı yaratmamalı.
|
|
154
|
+
*/
|
|
155
|
+
uploadAttachment(conversationId, file, fileName) {
|
|
156
|
+
const form = new FormData();
|
|
157
|
+
form.append("file", file, fileName);
|
|
158
|
+
return this.request(
|
|
159
|
+
"POST",
|
|
160
|
+
`/v1/sdk/chat/conversations/${enc(conversationId)}/attachments`,
|
|
161
|
+
form
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
closeConversation(conversationId) {
|
|
165
|
+
return this.request("POST", `/v1/sdk/chat/conversations/${enc(conversationId)}/close`);
|
|
166
|
+
}
|
|
167
|
+
rateConversation(conversationId, rating, comment) {
|
|
168
|
+
return this.request("POST", `/v1/sdk/chat/conversations/${enc(conversationId)}/rate`, {
|
|
169
|
+
rating,
|
|
170
|
+
comment
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
// ── Push ──────────────────────────────────────────────────────────────
|
|
174
|
+
/**
|
|
175
|
+
* Cihaz token'ını kaydeder. Token'ı almak (FCM/APNs/Web Push izni) ev
|
|
176
|
+
* sahibinin işidir; SDK yalnız iletir — izin diyaloğunu kimin, ne zaman
|
|
177
|
+
* göstereceği ürün kararıdır, kütüphane kararı değil.
|
|
178
|
+
*/
|
|
179
|
+
registerDevice(input) {
|
|
180
|
+
return this.request("POST", "/v1/sdk/devices", input);
|
|
181
|
+
}
|
|
182
|
+
/** Çıkışta çağrılır: kayıt silinmez, kapatılır (geçmiş korunur). */
|
|
183
|
+
unregisterDevice(token) {
|
|
184
|
+
return this.request("DELETE", `/v1/sdk/devices/${enc(token)}`);
|
|
185
|
+
}
|
|
186
|
+
// ── HTTP ──────────────────────────────────────────────────────────────
|
|
187
|
+
async request(method, path, body, query) {
|
|
188
|
+
const stored = await this.loadVisitor();
|
|
189
|
+
const isForm = typeof FormData !== "undefined" && body instanceof FormData;
|
|
190
|
+
const headers = {
|
|
191
|
+
Accept: "application/json",
|
|
192
|
+
"X-Signalbird-App-Key": this.config.appKey
|
|
193
|
+
};
|
|
194
|
+
if (stored?.secret) headers["X-Signalbird-Visitor"] = stored.secret;
|
|
195
|
+
if (body !== void 0 && !isForm) headers["Content-Type"] = "application/json";
|
|
196
|
+
if (this.config.locale) headers["X-Locale"] = this.config.locale;
|
|
197
|
+
const controller = new AbortController();
|
|
198
|
+
const timer = setTimeout(() => controller.abort(), this.timeout);
|
|
199
|
+
try {
|
|
200
|
+
const response = await this.doFetch(this.baseUrl + path + buildQuery(query), {
|
|
201
|
+
method,
|
|
202
|
+
headers,
|
|
203
|
+
body: body === void 0 ? void 0 : isForm ? body : JSON.stringify(body),
|
|
204
|
+
signal: controller.signal
|
|
205
|
+
});
|
|
206
|
+
const text = await response.text();
|
|
207
|
+
let data = null;
|
|
208
|
+
try {
|
|
209
|
+
data = text ? JSON.parse(text) : null;
|
|
210
|
+
} catch {
|
|
211
|
+
data = text;
|
|
212
|
+
}
|
|
213
|
+
if (response.ok) {
|
|
214
|
+
return { ok: true, status: response.status, data };
|
|
215
|
+
}
|
|
216
|
+
const code = data && typeof data === "object" && typeof data.code === "string" && data.code || (response.status === 422 ? "VALIDATION_ERROR" : `HTTP_${response.status}`);
|
|
217
|
+
if (code === "VISITOR_INVALID" || response.status === 401) {
|
|
218
|
+
await this.signOut();
|
|
219
|
+
}
|
|
220
|
+
if (this.config.debug) {
|
|
221
|
+
console.warn(`[signalbird] ${code} (HTTP ${response.status})`);
|
|
222
|
+
}
|
|
223
|
+
return {
|
|
224
|
+
ok: false,
|
|
225
|
+
status: response.status,
|
|
226
|
+
code,
|
|
227
|
+
message: data && typeof data === "object" && typeof data.message === "string" && data.message || `HTTP ${response.status}`,
|
|
228
|
+
data
|
|
229
|
+
};
|
|
230
|
+
} catch (error) {
|
|
231
|
+
const timedOut = error instanceof Error && error.name === "AbortError";
|
|
232
|
+
if (this.config.debug) {
|
|
233
|
+
console.warn("[signalbird] ula\u015F\u0131lamad\u0131:", error);
|
|
234
|
+
}
|
|
235
|
+
return {
|
|
236
|
+
ok: false,
|
|
237
|
+
status: 0,
|
|
238
|
+
code: timedOut ? "TIMEOUT" : "NETWORK_ERROR",
|
|
239
|
+
message: error instanceof Error ? error.message : "network error"
|
|
240
|
+
};
|
|
241
|
+
} finally {
|
|
242
|
+
clearTimeout(timer);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
async loadVisitor() {
|
|
246
|
+
if (this.loaded) return this.visitor;
|
|
247
|
+
this.loaded = true;
|
|
248
|
+
try {
|
|
249
|
+
const raw = await this.storage.getItem(STORAGE_KEY);
|
|
250
|
+
if (!raw) return null;
|
|
251
|
+
const parsed = JSON.parse(raw);
|
|
252
|
+
if (!parsed?.secret || parsed.appKey !== this.config.appKey) return null;
|
|
253
|
+
this.visitor = parsed;
|
|
254
|
+
} catch {
|
|
255
|
+
this.visitor = null;
|
|
256
|
+
}
|
|
257
|
+
return this.visitor;
|
|
258
|
+
}
|
|
259
|
+
async storeVisitor(visitor) {
|
|
260
|
+
this.visitor = visitor;
|
|
261
|
+
this.loaded = true;
|
|
262
|
+
try {
|
|
263
|
+
await this.storage.setItem(STORAGE_KEY, JSON.stringify(visitor));
|
|
264
|
+
} catch {
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
};
|
|
268
|
+
function enc(value) {
|
|
269
|
+
return encodeURIComponent(value);
|
|
270
|
+
}
|
|
271
|
+
function buildQuery(query) {
|
|
272
|
+
if (!query) return "";
|
|
273
|
+
const params = new URLSearchParams();
|
|
274
|
+
for (const [key, value] of Object.entries(query)) {
|
|
275
|
+
if (value === void 0 || value === null) continue;
|
|
276
|
+
params.append(key, String(value));
|
|
277
|
+
}
|
|
278
|
+
const encoded = params.toString();
|
|
279
|
+
return encoded ? `?${encoded}` : "";
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// src/app/session.ts
|
|
283
|
+
var IDLE_LADDER = [2e4, 2e4, 2e4, 6e4, 6e4, 18e4];
|
|
284
|
+
var ACTIVE_INTERVAL = 3e3;
|
|
285
|
+
var ChatSession = class {
|
|
286
|
+
constructor(app, options = {}) {
|
|
287
|
+
this.app = app;
|
|
288
|
+
this.options = options;
|
|
289
|
+
this.state = {
|
|
290
|
+
enabled: false,
|
|
291
|
+
loading: true,
|
|
292
|
+
conversation: null,
|
|
293
|
+
messages: [],
|
|
294
|
+
unread: 0,
|
|
295
|
+
agentTyping: false,
|
|
296
|
+
withinHours: true
|
|
297
|
+
};
|
|
298
|
+
this.listeners = /* @__PURE__ */ new Set();
|
|
299
|
+
this.timer = null;
|
|
300
|
+
this.step = 0;
|
|
301
|
+
this.stopped = false;
|
|
302
|
+
this.polling = false;
|
|
303
|
+
this.active = options.active ?? false;
|
|
304
|
+
}
|
|
305
|
+
// ── Abonelik ──────────────────────────────────────────────────────────
|
|
306
|
+
subscribe(listener) {
|
|
307
|
+
this.listeners.add(listener);
|
|
308
|
+
listener(this.state);
|
|
309
|
+
return () => this.listeners.delete(listener);
|
|
310
|
+
}
|
|
311
|
+
snapshot() {
|
|
312
|
+
return this.state;
|
|
313
|
+
}
|
|
314
|
+
// ── Yaşam döngüsü ─────────────────────────────────────────────────────
|
|
315
|
+
/** Bootstrap + varsa mevcut konuşmayı yükler, sonra yoklamayı başlatır. */
|
|
316
|
+
async start() {
|
|
317
|
+
this.stopped = false;
|
|
318
|
+
const boot = await this.app.bootstrap();
|
|
319
|
+
const app = boot.data?.app;
|
|
320
|
+
if (!boot.ok || !app?.chat_enabled) {
|
|
321
|
+
this.patch({ enabled: false, loading: false, errorCode: boot.code });
|
|
322
|
+
return;
|
|
323
|
+
}
|
|
324
|
+
this.patch({ enabled: true, withinHours: app.within_hours ?? true });
|
|
325
|
+
if (await this.app.currentVisitor()) {
|
|
326
|
+
await this.refresh();
|
|
327
|
+
}
|
|
328
|
+
this.patch({ loading: false });
|
|
329
|
+
this.schedule();
|
|
330
|
+
}
|
|
331
|
+
/** Panel açıldı/kapandı — yoklama hızı buna göre değişir. */
|
|
332
|
+
setActive(active) {
|
|
333
|
+
if (this.active === active) return;
|
|
334
|
+
this.active = active;
|
|
335
|
+
this.step = 0;
|
|
336
|
+
if (active) void this.refresh();
|
|
337
|
+
this.schedule();
|
|
338
|
+
}
|
|
339
|
+
stop() {
|
|
340
|
+
this.stopped = true;
|
|
341
|
+
if (this.timer) clearTimeout(this.timer);
|
|
342
|
+
this.timer = null;
|
|
343
|
+
}
|
|
344
|
+
// ── Eylemler ──────────────────────────────────────────────────────────
|
|
345
|
+
/** Ön-form gönderildiğinde ya da uygulama kullanıcıyı tanıdığında. */
|
|
346
|
+
async openSession(input) {
|
|
347
|
+
const result = await this.app.startSession(input);
|
|
348
|
+
if (result.ok) await this.refresh();
|
|
349
|
+
return result;
|
|
350
|
+
}
|
|
351
|
+
/**
|
|
352
|
+
* Mesaj gönderir. Konuşma yoksa açar.
|
|
353
|
+
*
|
|
354
|
+
* İyimser: mesaj listeye ANINDA düşer, `client_id` ile eşlenir. Sunucu
|
|
355
|
+
* cevabı gelince yerel kopya onunla değiştirilir; başarısızsa `failed`
|
|
356
|
+
* işaretlenir ve arayüz "yeniden dene" gösterebilir.
|
|
357
|
+
*/
|
|
358
|
+
async send(body, attachments) {
|
|
359
|
+
const trimmed = body.trim();
|
|
360
|
+
if (!trimmed && !attachments?.length) {
|
|
361
|
+
return { ok: false, status: 0, code: "EMPTY_BODY", message: "Mesaj bo\u015F." };
|
|
362
|
+
}
|
|
363
|
+
const cid = clientId();
|
|
364
|
+
const optimistic = {
|
|
365
|
+
id: cid,
|
|
366
|
+
client_id: cid,
|
|
367
|
+
sender_type: "visitor",
|
|
368
|
+
body: trimmed,
|
|
369
|
+
created_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
370
|
+
};
|
|
371
|
+
this.patch({ messages: [...this.state.messages, optimistic] });
|
|
372
|
+
if (!await this.app.currentVisitor()) {
|
|
373
|
+
const session = await this.app.startSession(this.options.visitor ?? {});
|
|
374
|
+
if (!session.ok) return this.markFailed(cid, session);
|
|
375
|
+
}
|
|
376
|
+
const conversation = this.state.conversation;
|
|
377
|
+
const result = conversation ? await this.app.sendMessage(conversation.id, { body: trimmed, client_id: cid, attachments }) : await this.app.startConversation({ body: trimmed, client_id: cid, attachments });
|
|
378
|
+
if (!result.ok) return this.markFailed(cid, result);
|
|
379
|
+
await this.refresh();
|
|
380
|
+
this.step = 0;
|
|
381
|
+
this.schedule();
|
|
382
|
+
return result;
|
|
383
|
+
}
|
|
384
|
+
/** İlk tuşta `true`, 2.5 s hareketsizlikte `false` — çağıran zamanlar. */
|
|
385
|
+
typing(isTyping) {
|
|
386
|
+
const conversation = this.state.conversation;
|
|
387
|
+
if (!conversation) return;
|
|
388
|
+
void this.app.setTyping(conversation.id, isTyping);
|
|
389
|
+
}
|
|
390
|
+
/** Görülen son mesaja kadar okundu işaretler. */
|
|
391
|
+
async markRead() {
|
|
392
|
+
const conversation = this.state.conversation;
|
|
393
|
+
const last = this.state.messages[this.state.messages.length - 1];
|
|
394
|
+
if (!conversation || !last) return;
|
|
395
|
+
await this.app.markRead(conversation.id, last.id);
|
|
396
|
+
this.patch({ unread: 0 });
|
|
397
|
+
}
|
|
398
|
+
async close(rating, comment) {
|
|
399
|
+
const conversation = this.state.conversation;
|
|
400
|
+
if (!conversation) return;
|
|
401
|
+
if (typeof rating === "number") {
|
|
402
|
+
await this.app.rateConversation(conversation.id, rating, comment);
|
|
403
|
+
}
|
|
404
|
+
await this.app.closeConversation(conversation.id);
|
|
405
|
+
await this.refresh();
|
|
406
|
+
}
|
|
407
|
+
/** Sunucudaki durumu çeker; imleç varsa yalnız yenileri ister. */
|
|
408
|
+
async refresh() {
|
|
409
|
+
const current = this.state.conversation;
|
|
410
|
+
if (!current) {
|
|
411
|
+
const list = await this.app.listConversations();
|
|
412
|
+
const first = list.data?.data?.[0];
|
|
413
|
+
if (!first) {
|
|
414
|
+
this.patch({ errorCode: list.ok ? void 0 : list.code });
|
|
415
|
+
return;
|
|
416
|
+
}
|
|
417
|
+
const detail2 = await this.app.getConversation(first.id);
|
|
418
|
+
this.applyConversation(detail2.data?.conversation ?? first, true);
|
|
419
|
+
return;
|
|
420
|
+
}
|
|
421
|
+
const after = this.lastServerMessageId();
|
|
422
|
+
const detail = await this.app.getConversation(current.id, after ? { after } : void 0);
|
|
423
|
+
if (!detail.ok || !detail.data?.conversation) {
|
|
424
|
+
this.patch({ errorCode: detail.code });
|
|
425
|
+
return;
|
|
426
|
+
}
|
|
427
|
+
this.applyConversation(detail.data.conversation, !after);
|
|
428
|
+
}
|
|
429
|
+
// ── İç işler ──────────────────────────────────────────────────────────
|
|
430
|
+
applyConversation(conversation, replace) {
|
|
431
|
+
const incoming = conversation.messages ?? [];
|
|
432
|
+
const merged = replace ? incoming : mergeMessages(this.state.messages, incoming);
|
|
433
|
+
if (incoming.length) this.step = 0;
|
|
434
|
+
this.patch({
|
|
435
|
+
conversation,
|
|
436
|
+
messages: merged,
|
|
437
|
+
unread: conversation.unread_count ?? 0,
|
|
438
|
+
agentTyping: !!conversation.agent_typing,
|
|
439
|
+
withinHours: conversation.within_hours ?? this.state.withinHours,
|
|
440
|
+
errorCode: void 0
|
|
441
|
+
});
|
|
442
|
+
}
|
|
443
|
+
/** İyimser kayıtlar sunucu kimliği taşımaz; imleç yalnız gerçek kimliktir. */
|
|
444
|
+
lastServerMessageId() {
|
|
445
|
+
for (let i = this.state.messages.length - 1; i >= 0; i--) {
|
|
446
|
+
const message = this.state.messages[i];
|
|
447
|
+
if (message.id && !message.id.startsWith("c_") && message.id !== message.client_id) {
|
|
448
|
+
return message.id;
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
return void 0;
|
|
452
|
+
}
|
|
453
|
+
markFailed(cid, result) {
|
|
454
|
+
this.patch({
|
|
455
|
+
messages: this.state.messages.map(
|
|
456
|
+
(m) => m.client_id === cid ? { ...m, failed: true } : m
|
|
457
|
+
),
|
|
458
|
+
errorCode: result.code
|
|
459
|
+
});
|
|
460
|
+
return result;
|
|
461
|
+
}
|
|
462
|
+
schedule() {
|
|
463
|
+
if (this.timer) clearTimeout(this.timer);
|
|
464
|
+
if (this.stopped) return;
|
|
465
|
+
const delay = this.active ? ACTIVE_INTERVAL : IDLE_LADDER[Math.min(this.step, IDLE_LADDER.length - 1)];
|
|
466
|
+
this.timer = setTimeout(() => void this.tick(), delay);
|
|
467
|
+
}
|
|
468
|
+
async tick() {
|
|
469
|
+
if (this.stopped || this.polling) return;
|
|
470
|
+
if (!this.visible()) {
|
|
471
|
+
this.schedule();
|
|
472
|
+
return;
|
|
473
|
+
}
|
|
474
|
+
this.polling = true;
|
|
475
|
+
try {
|
|
476
|
+
await this.refresh();
|
|
477
|
+
} finally {
|
|
478
|
+
this.polling = false;
|
|
479
|
+
}
|
|
480
|
+
if (!this.active) this.step++;
|
|
481
|
+
this.schedule();
|
|
482
|
+
}
|
|
483
|
+
visible() {
|
|
484
|
+
if (this.options.isVisible) return this.options.isVisible();
|
|
485
|
+
try {
|
|
486
|
+
if (typeof document !== "undefined" && document.visibilityState) {
|
|
487
|
+
return document.visibilityState === "visible";
|
|
488
|
+
}
|
|
489
|
+
} catch {
|
|
490
|
+
}
|
|
491
|
+
return true;
|
|
492
|
+
}
|
|
493
|
+
patch(partial) {
|
|
494
|
+
this.state = { ...this.state, ...partial };
|
|
495
|
+
for (const listener of this.listeners) {
|
|
496
|
+
try {
|
|
497
|
+
listener(this.state);
|
|
498
|
+
} catch (error) {
|
|
499
|
+
console.warn("[signalbird] dinleyici hatas\u0131:", error);
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
};
|
|
504
|
+
function mergeMessages(existing, incoming) {
|
|
505
|
+
if (!incoming.length) return existing;
|
|
506
|
+
const byKey = /* @__PURE__ */ new Map();
|
|
507
|
+
for (const message of existing) {
|
|
508
|
+
byKey.set(message.client_id ?? message.id, message);
|
|
509
|
+
}
|
|
510
|
+
for (const message of incoming) {
|
|
511
|
+
const key = message.client_id ?? message.id;
|
|
512
|
+
byKey.delete(key);
|
|
513
|
+
byKey.set(key, message);
|
|
514
|
+
}
|
|
515
|
+
return [...byKey.values()].sort((a, b) => (a.created_at ?? "").localeCompare(b.created_at ?? ""));
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
// src/angular/index.ts
|
|
519
|
+
var SIGNALBIRD_CONFIG = "SIGNALBIRD_CONFIG";
|
|
520
|
+
var SignalbirdService = class {
|
|
521
|
+
constructor(config) {
|
|
522
|
+
this.started = false;
|
|
523
|
+
this.client = new SignalbirdApp(config);
|
|
524
|
+
this.session = new ChatSession(this.client, { active: false });
|
|
525
|
+
this.subject = new rxjs.BehaviorSubject(this.session.snapshot());
|
|
526
|
+
this.session.subscribe((state) => this.subject.next(state));
|
|
527
|
+
}
|
|
528
|
+
/** Sohbet durumu akışı. İlk abone olduğunda oturum başlar. */
|
|
529
|
+
chat$() {
|
|
530
|
+
if (!this.started) {
|
|
531
|
+
this.started = true;
|
|
532
|
+
void this.session.start();
|
|
533
|
+
}
|
|
534
|
+
return this.subject.asObservable();
|
|
535
|
+
}
|
|
536
|
+
/** Panel açık mı — yoklama hızını belirler. */
|
|
537
|
+
setOpen(open) {
|
|
538
|
+
this.session.setActive(open);
|
|
539
|
+
}
|
|
540
|
+
send(body, attachments) {
|
|
541
|
+
return this.session.send(body, attachments);
|
|
542
|
+
}
|
|
543
|
+
typing(isTyping) {
|
|
544
|
+
this.session.typing(isTyping);
|
|
545
|
+
}
|
|
546
|
+
markRead() {
|
|
547
|
+
return this.session.markRead();
|
|
548
|
+
}
|
|
549
|
+
closeConversation(rating, comment) {
|
|
550
|
+
return this.session.close(rating, comment);
|
|
551
|
+
}
|
|
552
|
+
openSession(input) {
|
|
553
|
+
return this.session.openSession(input);
|
|
554
|
+
}
|
|
555
|
+
identify(input) {
|
|
556
|
+
return this.client.identify(input);
|
|
557
|
+
}
|
|
558
|
+
registerDevice(input) {
|
|
559
|
+
return this.client.registerDevice(input);
|
|
560
|
+
}
|
|
561
|
+
/** Uygulama kapanırken ya da testte: yoklamayı durdurur. */
|
|
562
|
+
destroy() {
|
|
563
|
+
this.session.stop();
|
|
564
|
+
this.subject.complete();
|
|
565
|
+
}
|
|
566
|
+
};
|
|
567
|
+
function provideSignalbird(config) {
|
|
568
|
+
return {
|
|
569
|
+
provide: SignalbirdService,
|
|
570
|
+
useFactory: () => new SignalbirdService(config)
|
|
571
|
+
};
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
exports.SIGNALBIRD_CONFIG = SIGNALBIRD_CONFIG;
|
|
575
|
+
exports.SignalbirdService = SignalbirdService;
|
|
576
|
+
exports.provideSignalbird = provideSignalbird;
|
|
577
|
+
//# sourceMappingURL=angular.js.map
|
|
578
|
+
//# sourceMappingURL=angular.js.map
|