changeset 15:9e61a930b822

Modified the Model model for additional filters and subqueries
author wobin
date Sat, 25 Apr 2009 21:42:27 +1000
parents 18c33a32ba31
children 6c28e55a00cf
files Squawk.lua
diffstat 1 files changed, 51 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/Squawk.lua	Fri Apr 24 15:19:43 2009 +1000
+++ b/Squawk.lua	Sat Apr 25 21:42:27 2009 +1000
@@ -82,8 +82,11 @@
 --
 --		followers:
 --]]
-Model.Squawk = {}
-local Squawk = Model.Squawk
+Model.Squawks = {}
+local Squawks = Model.Squawks
+Squawks.Main = {}
+Squawks.Owners = {}
+
 local function wrap(str, limit)
   limit = limit or 72
   local here = 1
@@ -95,7 +98,8 @@
                             end
                           end)
 end
-function Squawk:new(Message, Owner)
+
+function Squawks:new(Message, Owner)
 	local o = {}
 	o.Owner = Owner or UnitName("player")
 	o.Message = wrap(Message)
@@ -112,18 +116,25 @@
 			table.insert(o.Related, word)
 		end
 	end
+	
+	table.insert(self.Main, o)
+	
+	if not self.Owners[Owner] then
+		self.Owners[Owner] = {}
+	end
+	table.insert(self.Owners[Owner], o)
 
 	return o
 end
 
-function Squawk:Sort(Squawks)
-	table.sort(Squawks, function(a,b) return a.Time > b.Time end)
-	return Squawks
+function Squawks:Sort(Squawks)
+	table.sort(Squawks or self.Main, function(a,b) return a.Time > b.Time end)
+	return Squawks or self.Main
 end
 
-function Squawk:GetOwn(Squawks)
+function Squawks:GetOwn(Squawks)
 	local mine = {}
-	for _, squawk in ipairs(Squawks) do
+	for _, squawk in ipairs(Squawks or self.Main) do
 		if squawk.Owner == UnitName("player") then
 			table.insert(mine, squawk)
 		end
@@ -131,8 +142,9 @@
 	return self:Sort(mine)
 end
 
-function Squawk:GetLast10(Squawks)
+function Squawks:GetLast10(Squawks)
 	local mine = {}
+	Squawks = Squawks or self.Main
 	local limit = #Squawks < 10 and #Squawks or 10
 	
 	Squawks = Squawk:Sort(Squawks)
@@ -143,6 +155,34 @@
 	return mine
 end
 
+-- initially called with no arguments to get the latest timestamp of 
+-- my squawks, or with a name to find the latest timestamp of all 
+-- squawks from that user
+function Squawks:GetLatestTimestamp(Name, Squawks)
+	if Name then
+		if self.Owners[Name] then
+			return self:GetLatestTimestamp(nil, self.Owners[Name])
+		else
+			return -1 -- No squawks exist for that name in our records
+		end
+	end
+
+	Squawks = Squawks or self.Main or {}
+	local latest = self:Sort(Squawks)
+	return latest and #latest > 0 and latest[1].Time or -1
+end
+
+function Squawks:GetLatestSquawks(Timestamp)
+	local latest = {}
+	for i, squawk in ipairs(self:Sort()) do
+		if squawk.Time > Timestamp and i < 10 then
+			table.insert(latest, squawk)
+		else
+			return latest
+		end
+	end
+end
+
 function Settings:IsPrivate()
 	return Settings.Private
 end
@@ -354,7 +394,7 @@
 end
 
 function View:ShowMeMySquawks()
-	for _,squawk in ipairs(Model.Squawks) do
+	for _,squawk in ipairs(Model.Squawks.Main) do
 		self:Print(squawk.Message)
 	end
 end
@@ -418,7 +458,7 @@
 	local tooltip = QTip:Acquire("Squawk",4, "LEFT", "LEFT", "LEFT", "RIGHT")
 	self.tooltip = tooltip
 	tooltip:AddHeader('Squawk')
-	for i,squawk in ipairs(Squawk:GetLast10(Model.Squawks)) do
+	for i,squawk in ipairs(Squawk:GetLast10()) do
 		if #squawk.Message > 79 then
 			local head = true
 			local message = {strsplit("\n",squawk.Message)}