annotate Model.lua @ 19:431f2fce08f2

Added in link stripping Coloured class names Reply to specific squawk Related squawks fixed new arrow to indicate reply limit to 140 characters
author wobin
date Tue, 12 May 2009 00:57:59 +1000
parents a3328fffef5c
children 8df5d8ef2e27
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@19 51
wobin@19 52 local classcolours = {
wobin@19 53 ["DEATHKNIGHT"] = "C41F3B",
wobin@19 54 ["DRUID"] = "FF7D0A",
wobin@19 55 ["HUNTER"] = "ABD473",
wobin@19 56 ["MAGE"] = "69CCF0",
wobin@19 57 ["PALADIN"] = "F58CBA",
wobin@19 58 ["PRIEST"] = "FFFFFF",
wobin@19 59 ["ROGUE"] = "FFF569",
wobin@19 60 ["SHAMAN"] = "2459FF",
wobin@19 61 ["WARLOCK"] = "9482C9",
wobin@19 62 ["WARRIOR"] = "C79C6E",
wobin@19 63 }
wobin@19 64
wobin@18 65 local function wrap(str, limit)
wobin@18 66 limit = limit or 72
wobin@19 67 local here = 1
wobin@19 68 return str:gsub("|c%x-|H.-|h(.-)|h|r", "%1"):gsub("(%s+)()(%S+)()",
wobin@18 69 function(sp, st, word, fi)
wobin@18 70 if fi-here > limit then
wobin@18 71 here = st
wobin@18 72 return "\n"..word
wobin@18 73 end
wobin@18 74 end)
wobin@18 75 end
wobin@18 76
wobin@19 77 function Squawks:new(Message, Owner, Class, ReplyStamp)
wobin@19 78 View:Print("New: "..ReplyStamp)
wobin@18 79 local o = {}
wobin@18 80 o.Owner = Owner or UnitName("player")
wobin@18 81 o.Message = wrap(Message)
wobin@18 82 o.Time = time()
wobin@18 83 local reply, to = strsplit("@", ((strsplit(" ", Message))))
wobin@18 84 if reply == "" then
wobin@19 85 o.ReplyTo = to:match("%P+") -- Get everything except punctuation
wobin@19 86 o.ReplyStamp = ReplyStamp
wobin@18 87 end
wobin@18 88
wobin@18 89 o.Related = {}
wobin@18 90
wobin@19 91 for word in string.gmatch(Message, "@(%S+)") do
wobin@18 92 if word ~= o.ReplyTo or "" then
wobin@18 93 table.insert(o.Related, word)
wobin@18 94 end
wobin@18 95 end
wobin@18 96
wobin@18 97 if #o.Related == 0 then
wobin@18 98 o.Related = nil
wobin@18 99 end
wobin@18 100
wobin@18 101 table.insert(self.Main, o)
wobin@18 102
wobin@19 103 if not self.Squawkers[Owner] then
wobin@19 104 self.Squawkers[Owner] = {name = Owner, class=Class}
wobin@19 105 end
wobin@19 106
wobin@18 107 if not self.Owners[Owner] then
wobin@18 108 self.Owners[Owner] = {}
wobin@18 109 end
wobin@19 110
wobin@18 111 table.insert(self.Owners[Owner], o)
wobin@18 112 return o
wobin@18 113 end
wobin@18 114
wobin@19 115 function Squawks:OwnerString(squawk)
wobin@19 116 return "|cff"..classcolours[self.Squawkers[squawk.Owner].class or "WARRIOR"]..squawk.Owner.."|r"
wobin@19 117 end
wobin@19 118
wobin@18 119 function Squawks:Reload()
wobin@18 120 for _,squawk in ipairs(Model.Squawks.Main) do
wobin@18 121 if not self.Owners[squawk.Owner] then
wobin@18 122 self.Owners[squawk.Owner] = {}
wobin@18 123 end
wobin@18 124 table.insert(self.Owners[squawk.Owner], squawk)
wobin@18 125 if squawk.Related and #squawk.Related == 0 then
wobin@18 126 squawk.Related = nil
wobin@18 127 end
wobin@18 128 end
wobin@18 129 end
wobin@18 130
wobin@18 131 function Squawks:Sort(Squawks)
wobin@18 132 table.sort(Squawks or self.Main, function(a,b) return a.Time > b.Time end)
wobin@18 133 return Squawks or self.Main
wobin@18 134 end
wobin@18 135
wobin@18 136 function Squawks:GetOwn(Squawks)
wobin@18 137 local mine = {}
wobin@18 138 for _, squawk in ipairs(Squawks or self.Main) do
wobin@18 139 if squawk.Owner == UnitName("player") then
wobin@18 140 table.insert(mine, squawk)
wobin@18 141 end
wobin@18 142 end
wobin@18 143 return self:Sort(mine)
wobin@18 144 end
wobin@18 145
wobin@18 146 function Squawks:GetLast10(Squawks)
wobin@18 147 local mine = {}
wobin@18 148 Squawks = Squawks or self.Main
wobin@18 149 local limit = #Squawks < 10 and #Squawks or 10
wobin@18 150
wobin@18 151 Squawks = self:Sort(Squawks)
wobin@18 152
wobin@18 153 for i=1,limit do
wobin@18 154 table.insert(mine, Squawks[i])
wobin@18 155 end
wobin@18 156 return mine
wobin@18 157 end
wobin@18 158
wobin@18 159 -- initially called with no arguments to get the latest timestamp of
wobin@18 160 -- my squawks, or with a name to find the latest timestamp of all
wobin@18 161 -- squawks from that user
wobin@18 162 function Squawks:GetLatestTimestamp(Name, Squawks)
wobin@18 163 if Name then
wobin@18 164 if self.Owners[Name] then
wobin@18 165 return self:GetLatestTimestamp(nil, self.Owners[Name])
wobin@18 166 else
wobin@18 167 return -1 -- No squawks exist for that name in our records
wobin@18 168 end
wobin@18 169 end
wobin@18 170
wobin@18 171 Squawks = Squawks or self.Main or {}
wobin@18 172 local latest = self:Sort(Squawks)
wobin@18 173 return latest and #latest > 0 and latest[1].Time or -1
wobin@18 174 end
wobin@18 175
wobin@18 176 function Squawks:GetLatestSquawks(Timestamp)
wobin@18 177 local latest = {}
wobin@18 178 for i, squawk in ipairs(self:Sort()) do
wobin@18 179 if squawk.Time > Timestamp and i < 10 then
wobin@18 180 table.insert(latest, squawk)
wobin@18 181 else
wobin@18 182 return latest
wobin@18 183 end
wobin@18 184 end
wobin@18 185 end
wobin@18 186
wobin@18 187 function Settings:IsPrivate()
wobin@18 188 return Settings.Private
wobin@18 189 end
wobin@18 190
wobin@18 191 function Settings:TogglePrivate()
wobin@18 192 Settings.Private = not Settings.Private
wobin@18 193 end
wobin@18 194
wobin@18 195 function Settings:AddFollower(Name)
wobin@18 196 Settings.Follower[Name] = 1
wobin@18 197 self:RemovePending(Name)
wobin@18 198 end
wobin@18 199
wobin@18 200 function Settings:AddFollowing(Name)
wobin@18 201 Settings.Following[Name] = 1
wobin@18 202 self:RemoveRequested(Name)
wobin@18 203 end
wobin@18 204
wobin@18 205 function Settings:AddBlock(Name)
wobin@18 206 Settings.Blocked[Name] = 1
wobin@18 207 self:RemoveFollower(Name)
wobin@18 208 self:RemoveFollowing(Name)
wobin@18 209 end
wobin@18 210
wobin@18 211 function Settings:AddPending(Name)
wobin@18 212 Settings.Pending[Name] = 1
wobin@18 213 end
wobin@18 214
wobin@18 215 function Settings:AddRequested(Name)
wobin@18 216 Settings.Requested[Name] = 1
wobin@18 217 end
wobin@18 218
wobin@18 219 function Settings:RemoveFollower(Name)
wobin@18 220 if Settings.Follower[Name] then
wobin@18 221 Settings.Follower[Name] = nil
wobin@18 222 end
wobin@18 223 end
wobin@18 224
wobin@18 225 function Settings:RemoveFollowing(Name)
wobin@18 226 if Settings.Following[Name] then
wobin@18 227 Settings.Following[Name] = nil
wobin@18 228 end
wobin@18 229 end
wobin@18 230
wobin@18 231 function Settings:RemoveBlock(Name)
wobin@18 232 if Settings.Blocked[Name] then
wobin@18 233 Settings.Blocked[Name] = nil
wobin@18 234 end
wobin@18 235 end
wobin@18 236
wobin@18 237 function Settings:RemovePending(Name)
wobin@18 238 if Settings.Pending[Name] then
wobin@18 239 Settings.Pending[Name] = nil
wobin@18 240 end
wobin@18 241 end
wobin@18 242
wobin@18 243 function Settings:RemoveRequested(Name)
wobin@18 244 if Settings.Requested[Name] then
wobin@18 245 Settings.Requested[Name] = nil
wobin@18 246 end
wobin@18 247 end