Module:Discussions

From Anthroposophy

Documentation for this module may be created at Module:Discussions/doc

local p = {}

function formatTimestamp(timestamp)
    -- Input timestamp in milliseconds
    local timestamp_ms = tonumber(timestamp) -- Default value for testing

    -- Convert milliseconds to seconds
    local timestamp_seconds = math.floor(timestamp_ms / 1000)

    -- Format the timestamp as Y-m-d
    local formatted_date = os.date("!%Y-%m-%d", timestamp_seconds)

    -- Return the formatted date
    return formatted_date
end

local function capitalizeFirstLetter(str)
    return mw.ustring.upper(mw.ustring.sub(str, 1, 1)) .. mw.ustring.sub(str, 2)
end

function p.main(frame)

	local getdata = mw.ext.externalData.getDbData {
		db = 'fmc',
		from = 'flow_workflow',
		where = 'workflow_type = "discussion"',
		format = JSON,
		data = {
			page_title   = 'workflow_title_text',
			namespace    = 'workflow_namespace'
		}
	}

	local topics = mw.html.create()
	local header = string.format('<tr><th>Discussion Topic</th><th>Last Updated</th></tr>')
	
	local results = {}
	
    for _, board in ipairs(getdata) do
    	
    	local ns = ''
    	
		if board['namespace'] == '1' then
    		ns = 'Talk:'
    	elseif board['namespace'] == '3' then
    		ns = 'User_talk:'
    	end
    	
        local title = ns .. board['page_title'] 
        
        local query = mw.ext.externalData.getWebData {
            url = 'https://anthroposophy.eu/w/api.php?action=flow&format=json&submodule=view-topiclist&page=' .. 
                   mw.uri.encode(title, "PATH") .. 
                  '&vtloffset-dir=fwd&vtltoconly=1&vtlformat=wikitext',
            format = 'JSON with jsonpath',
            data = {
            	revisions = '$..topiclist.revisions',
            	roots = '$..topiclist.roots',
            	posts = '$..topiclist.posts'
            }
	    }

		local roots = query.roots or {}	    
	    local posts = query.posts or {}
	    local revisions = query.revisions or {}
	    
		for _, root in ipairs(roots) do
   			-- Check if the root exists in the posts table
       		if posts[root] then
           		-- Iterate through each post ID associated with the root
       			for _, post in ipairs(posts[root]) do
               		-- Check if the post ID exists in the revisions table
               		if revisions[post] then
                   		-- Append the root|revision pair to the results table
                   		local title = revisions[post].content.plaintext
                   		local sort_title = capitalizeFirstLetter(title:gsub("[\"']", ""))
                   		local updated = formatTimestamp(revisions[post].last_updated)
                   		table.insert(results, '<tr><td data-sort="' .. sort_title .. '">[[Topic:' .. root .. "|" .. title .. ']]</td><td>' .. updated .. '</tr>')
               		end
       			end
       		end
   		end
    end

	table.sort(results)
	
	local topicsTable = topics:tag('table')
	    :addClass('smwtable-clean sortable w-100')
	    :wikitext(header .. table.concat(results, ''))
	
	return topics
end

return p