<commit_before>// In renderer process (web page).
const {
    ipcRenderer,
    remote,
    clipboard
} = nodeRequire('electron')
const {
    Menu,
    MenuItem
} = remote
const log = nodeRequire('winston')
log.level = 'debug'
var shell = nodeRequire('electron').shell

var tr = nodeRequire('./app/tweet_renderer')

function getTimelineId(user, tl) {
    return 'timeline_' + user.data.screen_name + '_' + tl
}

function updateUnreadCount(user, tl) {
    let timelineDiv = $('#' + getTimelineId(user, tl))
    let username = user.data.screen_name
    let count = timelineDiv.children().length
    let counterStr = ' (' + count + ')'
    $('#counter_' + username + '_' + tl).text(count == 0 ? '' : counterStr)
}

ipcRenderer.on('tweet-arrived', (event, user, tl, tweets) => {
    let timelineDiv = $('#' + getTimelineId(user, tl))
    for (let i = tweets.length - 1; i >= 0; i--) {
        try {
            let tweet = tweets[i]
            let tweetDiv = tr.createTweetDiv($, tweet)
            timelineDiv.append(tweetDiv)
            tweetDiv.contextmenu(function(event) {
                event.preventDefault()
                const menu = new Menu()
                menu.append(new MenuItem({
                    label: 'Mark this and previous and read',
                    click() {
                        ipcRenderer.send('mark-as-read', user, tl, tweet.id_str)
                        timelineDiv.children().each(function(i, elem) {
                            elem.remove()
                            if (elem.id == 'tweet_' + tweet.id_str) {
                                return false
                            }
                        })
                        $('body').scrollTop(0)
                        updateUnreadCount(user, tl)
                    }
                }))
                menu.append(new MenuItem({
                    label: 'Copy JSON',
                    click() {
                        clipboard.writeText(JSON.stringify(tweet, null, 4))
                    }
                }))
                menu.popup(remote.getCurrentWindow())
            })
            $('#reply-action-' + tweet.id_str).click(function(event) {
                event.preventDefault()
                $('#modal').show()
                $('#tweet-dialog-post').prop('disabled', false)
                $('#tweet-dialog-text').prop('disabled', false)
                $('#tweet-dialog-text').val(tr.getMentions(user.data, tweet).map((username) => '@' + username).join(' ') + ' ')
                $('#tweet-dialog-text').focus()
                $('#tweet-dialog-author').val(user.data.screen_name)
                $('#tweet-dialog-reply-to').val(tweet.id_str)
            })
        } catch (err) {
            console.error(err.stack)
        }
    }
    updateUnreadCount(user, tl)
})

ipcRenderer.on('user-added', (event, user) => {
    log.debug('ipcRenderer user-added called with', user)
    let username = user.data.screen_name

    let divs = {}
    for (let tl of['home', 'mentions', 'dms']) {
        let div = $('<div></div>', {
            id: getTimelineId(user, tl),
            class: 'timeline'
        })
        $('#timeline').append(div)
        divs[tl] = div
    }
    $('#timeline').children().hide()
    divs.home.show()

    let links = {}
    let linkNames = {
        home: 'Home',
        mentions: 'Mentions',
        dms: 'Direct Messages'
    }
    for (let tl of ['home', 'mentions', 'dms']) {
        let link = $('<a></a>', {
            href: '#' + username + '/' + tl
        }).append(
            linkNames[tl],
            $('<span></span>', {
                id: 'counter_' + username + '_' + tl
            })
        )
        link.click(function(event) {
            event.preventDefault()
            $('#timeline').children().hide()
            divs[tl].show()
        })
        links[tl] = link
    }
    $('#user_list').append(
        $('<li></li>').text(username),
        $('<ul></ul>').append(
            $('<li></li>').append(links.home),
            $('<li></li>').append(links.mentions),
            $('<li></li>').append(links.dms)
        )
    )
})

ipcRenderer.on('tweet-posted', (event, user, tweet) => {
    $('#modal').hide()
})

ipcRenderer.on('post-tweet-error', (event, err) => {<commit_msg>Added post function.<commit_after>// In renderer process (web page).
const {
    ipcRenderer,
    remote,
    clipboard
} = nodeRequire('electron')
const {
    Menu,
    MenuItem
} = remote
const log = nodeRequire('winston')
log.level = 'debug'
var shell = nodeRequire('electron').shell

var tr = nodeRequire('./app/tweet_renderer')

function getTimelineId(user, tl) {
    return 'timeline_' + user.data.screen_name + '_' + tl
}

function updateUnreadCount(user, tl) {
    let timelineDiv = $('#' + getTimelineId(user, tl))
    let username = user.data.screen_name
    let count = timelineDiv.children().length
    let counterStr = ' (' + count + ')'
    $('#counter_' + username + '_' + tl).text(count == 0 ? '' : counterStr)
}

function showTweetDialog(initialText, author, replyTo) {
    $('#modal').show()
    $('#tweet-dialog-post').prop('disabled', false)
    $('#tweet-dialog-text').prop('disabled', false)
    $('#tweet-dialog-text').val(initialText)
    $('#tweet-dialog-text').focus()
    $('#tweet-dialog-author').val(author)
    $('#tweet-dialog-reply-to').val(replyTo)
}

ipcRenderer.on('tweet-arrived', (event, user, tl, tweets) => {
    let timelineDiv = $('#' + getTimelineId(user, tl))
    for (let i = tweets.length - 1; i >= 0; i--) {
        try {
            let tweet = tweets[i]
            let tweetDiv = tr.createTweetDiv($, tweet)
            timelineDiv.append(tweetDiv)
            tweetDiv.contextmenu(function(event) {
                event.preventDefault()
                const menu = new Menu()
                menu.append(new MenuItem({
                    label: 'Mark this and previous and read',
                    click() {
                        ipcRenderer.send('mark-as-read', user, tl, tweet.id_str)
                        timelineDiv.children().each(function(i, elem) {
                            elem.remove()
                            if (elem.id == 'tweet_' + tweet.id_str) {
                                return false
                            }
                        })
                        $('body').scrollTop(0)
                        updateUnreadCount(user, tl)
                    }
                }))
                menu.append(new MenuItem({
                    label: 'Copy JSON',
                    click() {
                        clipboard.writeText(JSON.stringify(tweet, null, 4))
                    }
                }))
                menu.popup(remote.getCurrentWindow())
            })
            $('#reply-action-' + tweet.id_str).click(function(event) {
                event.preventDefault()
                var mentions = tr.getMentions(user.data, tweet).map((username) => '@' + username).join(' ') + ' '
                showTweetDialog(mentions, user.data.screen_name, tweet.id_str)
            })
        } catch (err) {
            console.error(err.stack)
        }
    }
    updateUnreadCount(user, tl)
})

ipcRenderer.on('user-added', (event, user) => {
    log.debug('ipcRenderer user-added called with', user)
    let username = user.data.screen_name

    let divs = {}
    for (let tl of['home', 'mentions', 'dms']) {
        let div = $('<div></div>', {
            id: getTimelineId(user, tl),
            class: 'timeline'
        })
        $('#timeline').append(div)
        divs[tl] = div
    }
    $('#timeline').children().hide()
    divs.home.show()

    let links = {}
    let linkNames = {
        home: 'Home',
        mentions: 'Mentions',
        dms: 'Direct Messages'
    }
    for (let tl of ['home', 'mentions', 'dms']) {
        let link = $('<a></a>', {
            href: '#' + username + '/' + tl
        }).append(
            linkNames[tl],
            $('<span></span>', {
                id: 'counter_' + username + '_' + tl
            })
        )
        link.click(function(event) {
            event.preventDefault()
            $('#timeline').children().hide()
            divs[tl].show()
        })
        links[tl] = link
    }
    let postLink = $('<a></a>', {
        href: '#' + username + '/post'
    }).text('Post')
    postLink.click(function(event) {
        event.preventDefault()
        showTweetDialog('', username, '')
    })
    $('#user_list').append(
        $('<li></li>').text(username),
        $('<ul></ul>').append(
            $('<li></li>').append(postLink),
            $('<li></li>').append(links.home),
            $('<li></li>').append(links.mentions),
            $('<li></li>').append(links.dms)
        )
    )
})

ipcRenderer.on('tweet-posted', (event, user, tweet) => {
    $('#modal').hide()
})

ipcRenderer.on('post-tweet-error', (event, err) => {
{'fraction_non_alphanumeric': 0.17218095881161377, 'fraction_numerical': 0.0011253657438667567, 'mean_word_length': 1.708625419079549, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '687603', 'n_tokens_mistral': 2725, 'n_tokens_neox': 2660, 'n_words': 472}