__builtin_popcount / Studoši React Count

// ==UserScript==
// @name         Studoši React Count
// @namespace    https://forum.studosi.net/
// @version      1.1
// @description  Show users reacting to posts
// @author       njofra
// @collaborator Amali
// @collaborator __builtin_popcount
// @match        https://forum.studosi.net/*
// @grant        none
// @license      MIT
// ==/UserScript==

const pairings = {
    'laugh': 'Haha',
    'no_mouth': 'WTF',
    'cry': 'Tuga',
}

function get_username(user_id, source) {
    // neki useri se ne mogu dohvatiti jer nisu lokalno cacheani
    //console.log(user_id, source)
    let user = Object.values(flarum.core.app.store.data.users).find(u => u.data.id == user_id)
    if (!user) return "ID: " + user_id
    return user.data.attributes.displayName
}

function get_reactions() {
    const reactions = []
    Object.values(flarum.core.app.store.data.reactions).map(r => r.data.attributes).forEach(r => {
        reactions[r.post_id] = reactions[r.post_id] || {}
        reactions[r.post_id][r.identifier] = reactions[r.post_id][r.identifier] || []
        reactions[r.post_id][r.identifier].push(get_username(r.user_id, "reacts"))
    })
    return reactions
}

function format_reactions(reactions_json, reaction_name) {
    let formatted_reactions = ""
    for (let user of reactions_json[reaction_name]) {
        formatted_reactions += "" + user + "\n"
    }
    return formatted_reactions
}

function format_votes(post_rel) {
    const post_uv = post_rel.upvotes.data
    const post_dv = post_rel.downvotes.data
    let formatted_votes = ''
    if(post_uv.length > 0){
        formatted_votes += 'Upvotes:\n'
        for(let vote of post_uv){
            formatted_votes += '\t' + get_username(vote.id, "upvotes") + '\n'
        }
    }
    if(post_dv.length > 0){
        formatted_votes += '\nDownvotes:\n'
        for(let vote of post_dv){
            formatted_votes += '\t' + get_username(vote.id, "downvotes") + '\n'
        }
    }
    if(formatted_votes.length === 0){
        return 'No votes'
    }
    return formatted_votes
}

function add_reactions() {
    let reactions = get_reactions()
    const posts = document.getElementsByClassName("PostStream-item")
    // console.log(flarum.core.app.forum.store)
    const post_votes = flarum.core.app.forum.store.data.posts

    for (let post of posts) {
        if (post.getAttribute("data-type") !== "comment") {
            continue;
        }

        let id = post.getAttribute("data-id")
        // ignorira postove bez reactova
        if (id in reactions){
            // fetch buttona koji predstavljaju emojije reactova
            const buttons = post.getElementsByClassName("emoji button-emoji")
            // svaki emoji ima samo svoje reactove popisane
            for (let button of buttons) {
                button.title = format_reactions(reactions[post.getAttribute("data-id")], button.alt)
            }
        }
        if(post_votes[parseInt(id)] !== undefined){
            const points = post.getElementsByClassName("Post-points")[0]
            points.title = format_votes(post_votes[parseInt(id)].data.relationships)
        }
    }
}

add_reactions()
window.setInterval(add_reactions, 2000);