view Squawk.lua @ 1:188273d0efad

Syntax fix
author wobin
date Thu, 16 Apr 2009 17:29:58 +1000
parents 2c267c596711
children 75a76882c343
line wrap: on
line source
-- A Twitter client of sorts for World of Warcraft
-- Author: Wobin
-- Email: wobster@gmail.com
--
Squawk = LibStub("AceAddon-3.0"):NewAddon("Squawk")

Squawk.Model = {}
Squawk.View = {}
Squawk.Controller = {}

local Model = Squawk.Model
local View = Squawk.View
local Controller = Squawk.Controller

Model.UserSettings = {}
local Settings = Model.UserSettings

local defaults = {
	profile = {
		Squawks = {},
		Follower = {},
		Following = {},
		Blocked = {},
	}
}

function Squawk:OnInitialize()
	Model.db = LibStub("AceDB-3.0"):New("SquawkDB", defaults)
	Settings.Follower = Model.db.profile.Follower
	Settings.Following = Model.db.profile.Following
	Settings.Blocked = Model.db.profile.Blocked
	Settings.Private = Model.db.profile.Private
	
	LibStub("AceComm-3.0"):Embed(Controller)
	Controller:RegisterComm("Squawk", ReceiveMessage)
end

-- Model --
--[[
--Each Squawk will have the following information:
-- * Owner (Name)
-- * Time (Epoch)
-- * Message (140 characters)
-- * ReplyTo (Name)
-- * Related (Names)
--
-- Each User will have the following lists:
-- * Follower
-- * Following
-- * Blocked
-- * Privacy State
--]]
Model.Squawk = {}
local Squawk = Model.Squawk

function Squawk:new(Message, Owner)
	local o = {}
	o.Owner = Owner or UnitName("player")
	o.Message = Message
	o.Time = os.time()
	local reply, to = strsplit("@", ((strsplit(" ", Message))))
	if reply == "" then
		o.ReplyTo = to
	end

	o.Related = {}

	for word in string.gmatch(Message, "@(%a+)") do
		if word ~= o.ReplyTo or "" then
			table.insert(o.Related, word)
		end
	end

	return o
end

function Squawk:Sort(Squawks)
	return table.sort(Squawks, function(a,b) return a.Time < b.Time end)
end

function Settings:IsPrivate()
	return Settings.Private
end

function Settings:AddFollower(Name)
	table.insert(Settings.Follower, Name)
end

function Settings:AddFollowing(Name)
	table.insert(Settings.Following, Name)
end

function Settings:AddBlock(Name)
	table.insert(Settings.Blocked, Name)
end

function Settings:RemoveFollower(Name)
	for i,v in ipairs(Settings.Follower) do
		if v == Name then
			table.remove(Settings.Follower, i)
			return
		end
	end
end

function Settings:RemoveFollowing(Name)
	for i,v in ipairs(Settings.Following) do
		if v == Name then
			table.remove(Settings.Following, i)
			return
		end
	end
end

function Settings:RemoveBlock(Name)
	for i,v in ipairs(Settings.Blocked) do
		if v == Name then
			table.remove(Settings.Blocked, i)
			return
		end
	end
end

--Controller--

function Controller:TheyWantToFollowMe(Name)
	if Settings:IsPrivate() then
		self:PutForwardFollowRequest(Name)
		self:SendMessageToTarget(Name, "#Pending|"..UnitName("player"))
	else
		Settings:AddFollower(Name)
		View:NotifyOfNewFollower(Name)
		self:SendMessageToTarget(Name, "#Follow|"..UnitName("player"))
	end
end

function Controller:IWantToFollowThem(Name)

end

function Controller:IAmNowFollowingThem(Name)
	Settings:AddFollowing(Name)
	View:NotifyOfNewFollowing(Name)
end

function Controller:AddANewSquawk(Name, Message)
	table.insert(Model.db.Squawks, Squawk:new(Message, Name))
	View:UpdateSquawkList()
end

function Controller:ImPending(Name)
	View:NotifyOfPending(Name)
end

function Controller:SendMessageToTarget(Name, Message)
	self:SendCommMessage("Squawk", Message, "WHISPER", Name, "BULK")
end

function Controller:SendMessageToGuild(Message)
	self:SendCommMessage("Squawk", Message, "GUILD")
end

local Parse = { 
		["#Pending"] = Controller.ImPending,
		["#Follow"] = Controller.IAmNowFollowingThem,
		["#Squawk"] = Controller.AddANewSquawk
	}

function Controller:ReceiveMessage(Prefix, Message, Distribution, Sender)
	local command, name, info = strsplit("|",Message)
	Parse[command](self, name, info)
end
-- View --

function View:UpdateSquawkList()
end

function View:NotifyOfPending()
end

function View:NotifyOfNewFollowing(Name)
end

function View:NotifyOfNewFollower(Name)
end