Skip to content

Commit 9982202

Browse files
committed
Fix Velora history and targeted pinned dock actions
1 parent 093db39 commit 9982202

3 files changed

Lines changed: 59 additions & 3 deletions

File tree

dock.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7151,6 +7151,9 @@ <h3>Status</h3>
71517151
if (data.chatbotChunk || data.chatbotResponse) {
71527152
return true;
71537153
}
7154+
if (data.action && data.target && data.target !== "null" && data.target !== thisLabel) {
7155+
return; // does not match, so we assume this isn't for us.
7156+
}
71547157
if ("mid" in data) {
71557158
if (data.mid && (syncDocks || force)) {
71567159
if (autoTimeoutEnabled) {

sources/velora.js

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
var currentHistoryChannel = "";
8787
var historyFallbackMisses = 0;
8888
var historyPublishedMessages = false;
89+
var historyPollStartedAt = 0;
8990
var recentMessageContent = {};
9091
var historyChannelCache = {};
9192
var historyChannelPending = {};
@@ -132,6 +133,12 @@
132133
return "content|\u00b6|" + channel + "|\u00b6|" + name + "|\u00b6|" + text;
133134
}
134135

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+
135142
function rememberRecentMessageContent(key){
136143
var now = Date.now();
137144
var keys;
@@ -361,9 +368,7 @@
361368
if (!msg || !msg.name || !msg.text){
362369
return;
363370
}
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);
367372
if (!rememberMessageKey(msgKey)){
368373
return;
369374
}
@@ -386,6 +391,19 @@
386391
historyPublishedMessages = true;
387392
}
388393

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+
389407
function pollVeloraChatHistory(){
390408
var channel;
391409
if (!isExtensionOn){
@@ -397,17 +415,28 @@
397415
}
398416
if (channel !== currentHistoryChannel){
399417
currentHistoryChannel = channel;
418+
historyPublishedMessages = false;
419+
historyPollStartedAt = Date.now();
400420
}
401421
resolveVeloraHistoryChannel(channel).then(function(resolvedChannel){
402422
var pollChannel = resolvedChannel || channel;
403423
return fetchVeloraJson("https://api.velora.tv/api/chat/channels/" + encodeURIComponent(pollChannel) + "/history?limit=50").then(function(data){
404424
var messages = getHistoryMessagesFromResponse(data).slice();
425+
var initialBatch = !historyPublishedMessages;
405426
if (messages.length > 1 && getVeloraTimestampValue(messages[0]) > getVeloraTimestampValue(messages[messages.length - 1])){
406427
messages.reverse();
407428
}
408429
messages.forEach(function(message){
430+
var timestampValue = getVeloraTimestampValue(message);
431+
if ((timestampValue && timestampValue < historyPollStartedAt) || (initialBatch && !timestampValue)){
432+
seedApiMessage(message, channel);
433+
return;
434+
}
409435
processApiMessage(message, channel);
410436
});
437+
if (initialBatch){
438+
historyPublishedMessages = true;
439+
}
411440
});
412441
}).catch(function(){
413442
});
@@ -418,6 +447,7 @@
418447
return;
419448
}
420449
historyPublishedMessages = false;
450+
historyPollStartedAt = Date.now();
421451
pollVeloraChatHistory();
422452
historyPollTimer = setInterval(pollVeloraChatHistory, 3000);
423453
}
@@ -427,6 +457,7 @@
427457
clearInterval(historyPollTimer);
428458
historyPollTimer = null;
429459
}
460+
historyPollStartedAt = 0;
430461
}
431462

432463
function getChatInput(){

tests/eventflow-pin-message.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,28 @@ async function runTests() {
169169
assert(JSON.stringify(targeted[0].payload) === JSON.stringify({ action: 'nextPinned' }), 'nextPinned command matches dock API');
170170
}
171171

172+
console.log('\n[6] targeted nextPinned keeps the dock label target in the payload');
173+
{
174+
const targeted = [];
175+
const EFS = loadEventFlowSystem({
176+
sendTargetP2P: (payload, target) => {
177+
targeted.push({ payload, target });
178+
},
179+
sendToDestinations: async () => {
180+
throw new Error('targeted nextPinned should not use chat fan-out');
181+
}
182+
});
183+
const sys = new EFS();
184+
const result = await sys.executeAction(
185+
{ id: 'pin4', actionType: 'pinMessage', config: { mode: 'nextPinned', target: 'moderator-dock' } },
186+
{ id: 1, chatmessage: 'ignored' }
187+
);
188+
189+
assert(result.modified === false, 'targeted nextPinned does not modify the message');
190+
assert(targeted[0].target === 'dock', 'targeted nextPinned still uses the dock P2P label');
191+
assert(JSON.stringify(targeted[0].payload) === JSON.stringify({ action: 'nextPinned', target: 'moderator-dock' }), 'targeted nextPinned command carries the dock label');
192+
}
193+
172194
console.log(`\n${'-'.repeat(50)}`);
173195
console.log(`Results: ${passed} passed, ${failed} failed`);
174196
if (failed > 0) {

0 commit comments

Comments
 (0)