Jump to content

Module:Mass notification

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 12:47, 1 July 2014 (create mass-notification module). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

-- This module sends out notifications to multiple users.

local mList = require('Module:List')

local MAX_USERS = math.huge -- Not sure if there is a limit on notifications, so building it in now.
local GROUP_PAGE_PATH = 'Module:Mass notification/groups/'
local MAX_USER_ERROR = 'attempted to send notifications to more than $1 users'
local NO_USER_ERROR = 'could not find any usernames in $1'
local INTRO_BLURB = 'Notifying $1 <small>([[Template:Mass notification|more info]] &middot; <span class="plainlinks">[$2 opt out]</span>)</small>:\n'
local LINK_LIST_STYLE = 'font-size: smaller'

local p = {}

local function message(msg, ...)
	return mw.message.newRawMessage(msg):params{...}:plain()
end

local function makeWikitextError(msg)
	return string.format(
		'<strong class="error">Error: %s.</strong>',
		msg
	)
end

local function makeUserLinks(t)
end

function p._main(groupName)
	-- Validate input.
	if type(groupName) ~= 'string' then
		error(string.format(
			'bad argument #1 to _main (string expected, got %s)',
			type(groupName)
		), 2)
	end

	local groupSubmodule = GROUP_PAGE_PATH .. groupName

	-- Load the group submodule and check for errors.
	local success, usernames = pcall(mw.loadData, groupSubmodule)
	if not success then
		return makeWikitextError(usernames) -- usernames is the error message
	elseif type(usernames) ~= 'table' or not usernames[1] then -- # doesn't work with mw.loadData
		return makeWikitextError(message(NO_USER_ERROR, groupName))
	elseif usernames[MAX_USERS + 1] then -- # doesn't work with mw.loadData
		return makeWikitextError(message(MAX_USER_ERROR, groupName))
	end

	-- Make the intro blurb.
	local introBlurb
	do
		local optOutUrl = tostring(mw.uri.fullUrl(
			groupSubmodule,
			{action = 'edit'}
		))
		introBlurb = message(INTRO_BLURB, groupName, optOutUrl)
	end

	-- Make the user links.
	local userLinks
	do
		local userNamespace = mw.site.namespaces[2].name
		local listArgs = {}
		for i, username in ipairs(usernames) do
			username = tostring(username)
			listArgs[i] = string.format(
				'[[%s:%s|%s]]',
				userNamespace,
				username,
				username
			)
		end
		listArgs.style = LINK_LIST_STYLE
		userLinks = mList.makeList('horizontal', listArgs)
	end

	return introBlurb .. userLinks
end

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame, {wrappers = 'Template:Mass notification'})
	local groupName = args[1]
	return p._main(groupName)
end

return p