Created
April 3, 2019 16:11
-
-
Save prawnsalad/6a3a4b11ce730c66f1f02726b2eabf1f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'kiwi public'; | |
/** @module */ | |
/** | |
* Adds the +draft/typing IRCv3 spec to irc-framework | |
*/ | |
export default function typingMiddleware() { | |
return function middleware(client, rawEvents, parsedEvents) { | |
addFunctionsToClient(client); | |
rawEvents.use(theMiddleware); | |
}; | |
function theMiddleware(command, message, rawLine, client, next) { | |
if (command !== 'TAGMSG' || !message.tags['+draft/typing']) { | |
next(); | |
return; | |
} | |
client.emit('typing', { | |
target: message.params[0], | |
nick: message.nick, | |
ident: message.ident, | |
hostname: message.hostname, | |
status: message.tags['+draft/typing'], | |
}); | |
} | |
} | |
function addFunctionsToClient(client) { | |
let typing = client.typing = {}; | |
let activeTyping = Object.create(null); | |
typing.start = function start(target) { | |
let lastSentStatus = activeTyping[target.toLowerCase()]; | |
if (lastSentStatus && lastSentStatus > Date.now() - 3000) { | |
return; | |
} | |
activeTyping[target.toLowerCase()] = Date.now(); | |
let message = new client.Message('TAGMSG', target); | |
message.tags['+draft/typing'] = 'active'; | |
client.raw(message); | |
}; | |
typing.pause = function pause(target) { | |
let message = new client.Message('TAGMSG', target); | |
message.tags['+draft/typing'] = 'paused'; | |
client.raw(message); | |
}; | |
typing.stop = function stop(target) { | |
let message = new client.Message('TAGMSG', target); | |
message.tags['+draft/typing'] = 'done'; | |
client.raw(message); | |
delete activeTyping[target.toLowerCase()]; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment