annotate Model.lua @ 18:a3328fffef5c

Split of main file into components
author wobin
date Thu, 07 May 2009 02:52:23 +1000
parents
children 431f2fce08f2
rev   line source
wobin@18 1 local Model = Squawk.Model
wobin@18 2 local Settings = Model.UserSettings
wobin@18 3 local Controller = Squawk.Controller
wobin@18 4 local View = Squawk.View
wobin@18 5
wobin@18 6
wobin@18 7 -- Model --
wobin@18 8 --[[
wobin@18 9 --Each Squawk will have the following information:
wobin@18 10 -- * Owner (Name)
wobin@18 11 -- * Time (Epoch)
wobin@18 12 -- * Message (140 characters)
wobin@18 13 -- * ReplyTo (Name)
wobin@18 14 -- * Related (Names)
wobin@18 15 --
wobin@18 16 -- Each User will have the following lists:
wobin@18 17 -- * Follower
wobin@18 18 -- * Following
wobin@18 19 -- * Blocked
wobin@18 20 -- * Pending (Requests to follow that you haven't acted on)
wobin@18 21 -- * Requested (Requests to follow that you have made)
wobin@18 22 -- * Privacy State
wobin@18 23 --
wobin@18 24 -- A user can only request to follow an online person. Requests can be approved
wobin@18 25 -- on or offline, but the initial request must be made online.
wobin@18 26 --
wobin@18 27 -- When a user makes a request to follow a private user, the subsequent paths occur:
wobin@18 28 -- - Followee is added to Settings.Requested
wobin@18 29 -- - Followee receives 'follow request' -> (their) Settings.Pending
wobin@18 30 -- - Followee acts on request -> (their) Settings.Pending cleared
wobin@18 31 -- 1) Follwer is online
wobin@18 32 -- - Follower receives 'request accepted' -> Added to Settings.Following and
wobin@18 33 -- cleared from Settings.Requested
wobin@18 34 -- 2) Follower is offline
wobin@18 35 -- - The next time Follower is online and recieves a Squawk we check if there
wobin@18 36 -- is a Settings.Requested for that name, and if so assume they have approved
wobin@18 37 -- and clear/add records appropriately.
wobin@18 38 --
wobin@18 39 -- For updating, there can be a few methods.
wobin@18 40 --
wobin@18 41 -- Guild open: you're not private, you broadcast to the guild your last X
wobin@18 42 -- squawks on login
wobin@18 43 --
wobin@18 44 -- followers:
wobin@18 45 --]]
wobin@18 46 Model.Squawks = {}
wobin@18 47 local Squawks = Model.Squawks
wobin@18 48 Squawks.Main = {}
wobin@18 49 Squawks.Owners = {}
wobin@18 50
wobin@18 51 local function wrap(str, limit)
wobin@18 52 limit = limit or 72
wobin@18 53 local here = 1
wobin@18 54 return str:gsub("(%s+)()(%S+)()",
wobin@18 55 function(sp, st, word, fi)
wobin@18 56 if fi-here > limit then
wobin@18 57 here = st
wobin@18 58 return "\n"..word
wobin@18 59 end
wobin@18 60 end)
wobin@18 61 end
wobin@18 62
wobin@18 63 function Squawks:new(Message, Owner)
wobin@18 64 local o = {}
wobin@18 65 o.Owner = Owner or UnitName("player")
wobin@18 66 o.Message = wrap(Message)
wobin@18 67 o.Time = time()
wobin@18 68 local reply, to = strsplit("@", ((strsplit(" ", Message))))
wobin@18 69 if reply == "" then
wobin@18 70 o.ReplyTo = to
wobin@18 71 end
wobin@18 72
wobin@18 73 o.Related = {}
wobin@18 74
wobin@18 75 for word in string.gmatch(Message, "@(%a+)") do
wobin@18 76 if word ~= o.ReplyTo or "" then
wobin@18 77 table.insert(o.Related, word)
wobin@18 78 end
wobin@18 79 end
wobin@18 80
wobin@18 81 if #o.Related == 0 then
wobin@18 82 o.Related = nil
wobin@18 83 end
wobin@18 84
wobin@18 85 table.insert(self.Main, o)
wobin@18 86
wobin@18 87 if not self.Owners[Owner] then
wobin@18 88 self.Owners[Owner] = {}
wobin@18 89 end
wobin@18 90 table.insert(self.Owners[Owner], o)
wobin@18 91 return o
wobin@18 92 end
wobin@18 93
wobin@18 94 function Squawks:Reload()
wobin@18 95 for _,squawk in ipairs(Model.Squawks.Main) do
wobin@18 96 if not self.Owners[squawk.Owner] then
wobin@18 97 self.Owners[squawk.Owner] = {}
wobin@18 98 end
wobin@18 99 table.insert(self.Owners[squawk.Owner], squawk)
wobin@18 100 if squawk.Related and #squawk.Related == 0 then
wobin@18 101 squawk.Related = nil
wobin@18 102 end
wobin@18 103 end
wobin@18 104 end
wobin@18 105
wobin@18 106 function Squawks:Sort(Squawks)
wobin@18 107 table.sort(Squawks or self.Main, function(a,b) return a.Time > b.Time end)
wobin@18 108 return Squawks or self.Main
wobin@18 109 end
wobin@18 110
wobin@18 111 function Squawks:GetOwn(Squawks)
wobin@18 112 local mine = {}
wobin@18 113 for _, squawk in ipairs(Squawks or self.Main) do
wobin@18 114 if squawk.Owner == UnitName("player") then
wobin@18 115 table.insert(mine, squawk)
wobin@18 116 end
wobin@18 117 end
wobin@18 118 return self:Sort(mine)
wobin@18 119 end
wobin@18 120
wobin@18 121 function Squawks:GetLast10(Squawks)
wobin@18 122 local mine = {}
wobin@18 123 Squawks = Squawks or self.Main
wobin@18 124 local limit = #Squawks < 10 and #Squawks or 10
wobin@18 125
wobin@18 126 Squawks = self:Sort(Squawks)
wobin@18 127
wobin@18 128 for i=1,limit do
wobin@18 129 table.insert(mine, Squawks[i])
wobin@18 130 end
wobin@18 131 return mine
wobin@18 132 end
wobin@18 133
wobin@18 134 -- initially called with no arguments to get the latest timestamp of
wobin@18 135 -- my squawks, or with a name to find the latest timestamp of all
wobin@18 136 -- squawks from that user
wobin@18 137 function Squawks:GetLatestTimestamp(Name, Squawks)
wobin@18 138 if Name then
wobin@18 139 if self.Owners[Name] then
wobin@18 140 return self:GetLatestTimestamp(nil, self.Owners[Name])
wobin@18 141 else
wobin@18 142 return -1 -- No squawks exist for that name in our records
wobin@18 143 end
wobin@18 144 end
wobin@18 145
wobin@18 146 Squawks = Squawks or self.Main or {}
wobin@18 147 local latest = self:Sort(Squawks)
wobin@18 148 return latest and #latest > 0 and latest[1].Time or -1
wobin@18 149 end
wobin@18 150
wobin@18 151 function Squawks:GetLatestSquawks(Timestamp)
wobin@18 152 local latest = {}
wobin@18 153 for i, squawk in ipairs(self:Sort()) do
wobin@18 154 if squawk.Time > Timestamp and i < 10 then
wobin@18 155 table.insert(latest, squawk)
wobin@18 156 else
wobin@18 157 return latest
wobin@18 158 end
wobin@18 159 end
wobin@18 160 end
wobin@18 161
wobin@18 162 function Settings:IsPrivate()
wobin@18 163 return Settings.Private
wobin@18 164 end
wobin@18 165
wobin@18 166 function Settings:TogglePrivate()
wobin@18 167 Settings.Private = not Settings.Private
wobin@18 168 end
wobin@18 169
wobin@18 170 function Settings:AddFollower(Name)
wobin@18 171 Settings.Follower[Name] = 1
wobin@18 172 self:RemovePending(Name)
wobin@18 173 end
wobin@18 174
wobin@18 175 function Settings:AddFollowing(Name)
wobin@18 176 Settings.Following[Name] = 1
wobin@18 177 self:RemoveRequested(Name)
wobin@18 178 end
wobin@18 179
wobin@18 180 function Settings:AddBlock(Name)
wobin@18 181 Settings.Blocked[Name] = 1
wobin@18 182 self:RemoveFollower(Name)
wobin@18 183 self:RemoveFollowing(Name)
wobin@18 184 end
wobin@18 185
wobin@18 186 function Settings:AddPending(Name)
wobin@18 187 Settings.Pending[Name] = 1
wobin@18 188 end
wobin@18 189
wobin@18 190 function Settings:AddRequested(Name)
wobin@18 191 Settings.Requested[Name] = 1
wobin@18 192 end
wobin@18 193
wobin@18 194 function Settings:RemoveFollower(Name)
wobin@18 195 if Settings.Follower[Name] then
wobin@18 196 Settings.Follower[Name] = nil
wobin@18 197 end
wobin@18 198 end
wobin@18 199
wobin@18 200 function Settings:RemoveFollowing(Name)
wobin@18 201 if Settings.Following[Name] then
wobin@18 202 Settings.Following[Name] = nil
wobin@18 203 end
wobin@18 204 end
wobin@18 205
wobin@18 206 function Settings:RemoveBlock(Name)
wobin@18 207 if Settings.Blocked[Name] then
wobin@18 208 Settings.Blocked[Name] = nil
wobin@18 209 end
wobin@18 210 end
wobin@18 211
wobin@18 212 function Settings:RemovePending(Name)
wobin@18 213 if Settings.Pending[Name] then
wobin@18 214 Settings.Pending[Name] = nil
wobin@18 215 end
wobin@18 216 end
wobin@18 217
wobin@18 218 function Settings:RemoveRequested(Name)
wobin@18 219 if Settings.Requested[Name] then
wobin@18 220 Settings.Requested[Name] = nil
wobin@18 221 end
wobin@18 222 end