|
86 | 86 | var currentHistoryChannel = ""; |
87 | 87 | var historyFallbackMisses = 0; |
88 | 88 | var historyPublishedMessages = false; |
| 89 | + var historyPollStartedAt = 0; |
89 | 90 | var recentMessageContent = {}; |
90 | 91 | var historyChannelCache = {}; |
91 | 92 | var historyChannelPending = {}; |
|
132 | 133 | return "content|\u00b6|" + channel + "|\u00b6|" + name + "|\u00b6|" + text; |
133 | 134 | } |
134 | 135 |
|
| 136 | + function getApiMessageKey(msg, channel){ |
| 137 | + return msg.id |
| 138 | + ? "api-id|\u00b6|" + channel + "|\u00b6|" + msg.id |
| 139 | + : "api-chat|\u00b6|" + channel + "|\u00b6|" + msg.timestamp + "|\u00b6|" + msg.name + "|\u00b6|" + msg.text; |
| 140 | + } |
| 141 | + |
135 | 142 | function rememberRecentMessageContent(key){ |
136 | 143 | var now = Date.now(); |
137 | 144 | var keys; |
|
361 | 368 | if (!msg || !msg.name || !msg.text){ |
362 | 369 | return; |
363 | 370 | } |
364 | | - msgKey = msg.id |
365 | | - ? "api-id|\u00b6|" + channel + "|\u00b6|" + msg.id |
366 | | - : "api-chat|\u00b6|" + channel + "|\u00b6|" + msg.timestamp + "|\u00b6|" + msg.name + "|\u00b6|" + msg.text; |
| 371 | + msgKey = getApiMessageKey(msg, channel); |
367 | 372 | if (!rememberMessageKey(msgKey)){ |
368 | 373 | return; |
369 | 374 | } |
|
386 | 391 | historyPublishedMessages = true; |
387 | 392 | } |
388 | 393 |
|
| 394 | + function seedApiMessage(raw, channel){ |
| 395 | + var msg = normalizeVeloraApiMessage(raw, channel); |
| 396 | + var contentKey; |
| 397 | + if (!msg || !msg.name || !msg.text){ |
| 398 | + return; |
| 399 | + } |
| 400 | + rememberMessageKey(getApiMessageKey(msg, channel)); |
| 401 | + contentKey = getMessageContentKey(channel, msg.name, msg.text); |
| 402 | + if (contentKey){ |
| 403 | + rememberRecentMessageContent(contentKey); |
| 404 | + } |
| 405 | + } |
| 406 | + |
389 | 407 | function pollVeloraChatHistory(){ |
390 | 408 | var channel; |
391 | 409 | if (!isExtensionOn){ |
|
397 | 415 | } |
398 | 416 | if (channel !== currentHistoryChannel){ |
399 | 417 | currentHistoryChannel = channel; |
| 418 | + historyPublishedMessages = false; |
| 419 | + historyPollStartedAt = Date.now(); |
400 | 420 | } |
401 | 421 | resolveVeloraHistoryChannel(channel).then(function(resolvedChannel){ |
402 | 422 | var pollChannel = resolvedChannel || channel; |
403 | 423 | return fetchVeloraJson("https://api.velora.tv/api/chat/channels/" + encodeURIComponent(pollChannel) + "/history?limit=50").then(function(data){ |
404 | 424 | var messages = getHistoryMessagesFromResponse(data).slice(); |
| 425 | + var initialBatch = !historyPublishedMessages; |
405 | 426 | if (messages.length > 1 && getVeloraTimestampValue(messages[0]) > getVeloraTimestampValue(messages[messages.length - 1])){ |
406 | 427 | messages.reverse(); |
407 | 428 | } |
408 | 429 | messages.forEach(function(message){ |
| 430 | + var timestampValue = getVeloraTimestampValue(message); |
| 431 | + if ((timestampValue && timestampValue < historyPollStartedAt) || (initialBatch && !timestampValue)){ |
| 432 | + seedApiMessage(message, channel); |
| 433 | + return; |
| 434 | + } |
409 | 435 | processApiMessage(message, channel); |
410 | 436 | }); |
| 437 | + if (initialBatch){ |
| 438 | + historyPublishedMessages = true; |
| 439 | + } |
411 | 440 | }); |
412 | 441 | }).catch(function(){ |
413 | 442 | }); |
|
418 | 447 | return; |
419 | 448 | } |
420 | 449 | historyPublishedMessages = false; |
| 450 | + historyPollStartedAt = Date.now(); |
421 | 451 | pollVeloraChatHistory(); |
422 | 452 | historyPollTimer = setInterval(pollVeloraChatHistory, 3000); |
423 | 453 | } |
|
427 | 457 | clearInterval(historyPollTimer); |
428 | 458 | historyPollTimer = null; |
429 | 459 | } |
| 460 | + historyPollStartedAt = 0; |
430 | 461 | } |
431 | 462 |
|
432 | 463 | function getChatInput(){ |
|
0 commit comments