var gun = Gun([location.origin + '/gun', 'https://gun-us.herokuapp.com/gun']); var chat = gun.get('converse/' + location.hash.slice(1)); var chats = {}; var activeChat; var getKey = new Promise(resolve => { var pair = localStorage.getItem('chatKeyPair'); if (pair) { resolve(JSON.parse(pair)); } else { Gun.SEA.pair(pair => { localStorage.setItem('chatKeyPair', JSON.stringify(pair)); resolve(pair); }); } }); var key, myIdenticon; getKey.then(k => { key = k; irisLib.Chat.initUser(gun, key); myIdenticon = $(new irisLib.Attribute({type:'keyID', value: key.pub}).identicon({width:40, showType: false})); $(".user-info").append(myIdenticon); myIdenticon.click(showSettings); irisLib.Chat.getChats(gun, key, addChat); irisLib.Chat.setOnline(gun, true); var chatWith = getUrlParameter('chatWith'); if (chatWith) { addChat(chatWith); showChat(chatWith); window.history.pushState({}, "Iris Chat", "/"+window.location.href.substring(window.location.href.lastIndexOf('/') + 1).split("?")[0]); // remove param } gun.user().get('profile').get('name').on(name => { if (name && typeof name === 'string') { var el = $('#settings-name'); if (!el.is(':focus')) { $('#settings-name').val(name); } } }); }); $('#paste-chat-link').on('keyup paste', event => { var val = $(event.target).val(); if (val.length < 30 || val.indexOf('chatWith') === -1) { return; } var s = val.split('?'); if (s.length !== 2) { return; } $(event.target).val(''); var pub = getUrlParameter('chatWith', s[1]); addChat(pub); showChat(pub); }); $('.chat-item.new').click(showNewChat); $('#settings-name').on('keyup paste', event => { var name = $(event.target).val().trim(); gun.user().get('profile').get('name').put(name); }); function hideStuff() { $('.chat-item').toggleClass('active', false); $('.main-view').hide(); $('#not-seen-by-them').hide(); $(".message-form").hide(); $("#topbar").empty(); } function showSettings() { hideStuff(); $('#topbar').text('Settings'); $('#settings').show(); } function showNewChat() { hideStuff(); $('.chat-item.new').toggleClass('active', true); $('#new-chat').show(); $("#topbar").text('Start new chat'); } showNewChat(); $('.copy-chat-link').click(event => { copyToClipboard('https://chat.iris.to/?chatWith=' + key.pub); var t = $(event.target); var originalText = t.text(); var originalWidth = t.width(); t.width(originalWidth); t.text('Copied'); setTimeout(() => { t.text(originalText); t.css('width', ''); }, 2000); }); $('#copy-private-key').click(event => { copyToClipboard(JSON.stringify(key)); var t = $(event.target); var originalText = t.text(); var originalWidth = t.width(); t.width(originalWidth); t.text('Copied'); setTimeout(() => { t.text(originalText); t.css('width', ''); }, 2000); }); $('#download-private-key').click(downloadKey); function showChat(pub) { if (!pub || !Object.prototype.hasOwnProperty.call(chats, pub)) { return; } activeChat = pub; hideStuff(); $('.chat-item[data-pub="' + pub +'"]').toggleClass('active', true); $("#message-list").empty(); $("#message-list").show(); $(".message-form").show(); $(".message-form form").off('submit'); $(".message-form form").on('submit', event => { event.preventDefault(); var text = $('#new-msg').val(); if (!text.length) { return; } chats[pub].send(text); $('#new-msg').val(''); }); var nameEl = $(''); if (chats[pub].name) { nameEl.text(chats[pub].name); nameEl.show(); } $("#topbar").append(chats[pub].identicon.clone()); $("#topbar").append(nameEl); $("#topbar").append($('')); var msgs = Object.values(chats[pub].messages); msgs.forEach(addMessage); sortMessagesByTime(); lastSeenTimeChanged(pub); $('#message-list').scrollTop($('#message-list')[0].scrollHeight - $('#message-list')[0].clientHeight); chats[pub].setMyMsgsLastSeenTime(); function setOnlineStatus() { var online = chats[pub].online; if (activeChat === pub) { if (online.isOnline) { $('#topbar .last-seen').text('online'); } else if (online.lastActive) { $('#topbar .last-seen').text('last seen ' + formatDate(new Date(online.lastActive * 1000))); } } } if (!chats[pub].online) { chats[pub].online = {}; irisLib.Chat.getOnline(gun, pub, (online) => { chats[pub].online = online; setOnlineStatus(); }); } setOnlineStatus(); } function sortChatsByLatest() { var sorted = $(".chat-item").sort((a, b) => $(b).data('latestTime') - $(a).data('latestTime')); $(".chat-list").append(sorted); } function sortMessagesByTime() { var sorted = $(".msg").sort((a, b) => $(a).data('time') - $(b).data('time')); $("#message-list").append(sorted); } function addMessage(msg) { var msgEl = $( '