annotate Model.lua @ 22:8df5d8ef2e27 tip

switch back to LibQTip-1.0 as LQTC is depreciated Fixed up the line resizing as LQT does this automatically now
author wobin
date Wed, 22 Jul 2009 23:16:42 +1000
parents 431f2fce08f2
children
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@22 15 -- * Trends (Term)
wobin@18 16 --
wobin@18 17 -- Each User will have the following lists:
wobin@18 18 -- * Follower
wobin@18 19 -- * Following
wobin@18 20 -- * Blocked
wobin@18 21 -- * Pending (Requests to follow that you haven't acted on)
wobin@18 22 -- * Requested (Requests to follow that you have made)
wobin@18 23 -- * Privacy State
wobin@18 24 --
wobin@18 25 -- A user can only request to follow an online person. Requests can be approved
wobin@18 26 -- on or offline, but the initial request must be made online.
wobin@18 27 --
wobin@18 28 -- When a user makes a request to follow a private user, the subsequent paths occur:
wobin@18 29 -- - Followee is added to Settings.Requested
wobin@18 30 -- - Followee receives 'follow request' -> (their) Settings.Pending
wobin@18 31 -- - Followee acts on request -> (their) Settings.Pending cleared
wobin@22 32 -- 1) Follower is online
wobin@18 33 -- - Follower receives 'request accepted' -> Added to Settings.Following and
wobin@18 34 -- cleared from Settings.Requested
wobin@18 35 -- 2) Follower is offline
wobin@18 36 -- - The next time Follower is online and recieves a Squawk we check if there
wobin@18 37 -- is a Settings.Requested for that name, and if so assume they have approved
wobin@18 38 -- and clear/add records appropriately.
wobin@18 39 --
wobin@22 40 --For updating, there can be a few methods.
wobin@18 41 --
wobin@18 42 -- Guild open: you're not private, you broadcast to the guild your last X
wobin@18 43 -- squawks on login
wobin@18 44 --
wobin@22 45 -- Followers: If you login and are following people, you send a request to them
wobin@22 46 -- directly upon login (after a brief pause to load everything) to get the last
wobin@22 47 -- 5 tweets from them. You also broadcast your last 5 tweets to them.
wobin@22 48 --
wobin@22 49 -- Problems inherent in the system:
wobin@22 50 -- - Number of transmissions if Follow group is large.
wobin@22 51 -- - Guild transmissions in large guilds, how do we get the information of
wobin@22 52 -- people who have already logged on previously without being spammed? We
wobin@22 53 -- send a request for a guild listing of people using the addon and then
wobin@22 54 -- go through that list requesting the last 5 of each user one by one.
wobin@22 55 -- - Transmission must be able to be delayed if sender is in combat.
wobin@18 56 --]]
wobin@18 57 Model.Squawks = {}
wobin@18 58 local Squawks = Model.Squawks
wobin@18 59 Squawks.Main = {}
wobin@18 60 Squawks.Owners = {}
wobin@18 61
wobin@19 62
wobin@19 63 local classcolours = {
wobin@19 64 ["DEATHKNIGHT"] = "C41F3B",
wobin@19 65 ["DRUID"] = "FF7D0A",
wobin@19 66 ["HUNTER"] = "ABD473",
wobin@19 67 ["MAGE"] = "69CCF0",
wobin@19 68 ["PALADIN"] = "F58CBA",
wobin@19 69 ["PRIEST"] = "FFFFFF",
wobin@19 70 ["ROGUE"] = "FFF569",
wobin@19 71 ["SHAMAN"] = "2459FF",
wobin@19 72 ["WARLOCK"] = "9482C9",
wobin@19 73 ["WARRIOR"] = "C79C6E",
wobin@19 74 }
wobin@19 75
wobin@19 76 function Squawks:new(Message, Owner, Class, ReplyStamp)
wobin@19 77 View:Print("New: "..ReplyStamp)
wobin@18 78 local o = {}
wobin@18 79 o.Owner = Owner or UnitName("player")
wobin@22 80 o.Message = Message
wobin@18 81 o.Time = time()
wobin@18 82 local reply, to = strsplit("@", ((strsplit(" ", Message))))
wobin@18 83 if reply == "" then
wobin@19 84 o.ReplyTo = to:match("%P+") -- Get everything except punctuation
wobin@19 85 o.ReplyStamp = ReplyStamp
wobin@18 86 end
wobin@18 87
wobin@18 88 o.Related = {}
wobin@18 89
wobin@19 90 for word in string.gmatch(Message, "@(%S+)") do
wobin@18 91 if word ~= o.ReplyTo or "" then
wobin@18 92 table.insert(o.Related, word)
wobin@18 93 end
wobin@18 94 end
wobin@18 95
wobin@18 96 if #o.Related == 0 then
wobin@18 97 o.Related = nil
wobin@18 98 end
wobin@22 99
wobin@22 100 o.Trends = {}
wobin@22 101
wobin@22 102 for word in string.gmatch(Message, "#(%S+)") do
wobin@22 103 table.insert(o.Trends, word)
wobin@22 104 end
wobin@22 105
wobin@22 106 if #o.Trends == 0 then
wobin@22 107 o.Trends = nil
wobin@22 108 end
wobin@18 109
wobin@18 110 table.insert(self.Main, o)
wobin@18 111
wobin@19 112 if not self.Squawkers[Owner] then
wobin@19 113 self.Squawkers[Owner] = {name = Owner, class=Class}
wobin@19 114 end
wobin@19 115
wobin@18 116 if not self.Owners[Owner] then
wobin@18 117 self.Owners[Owner] = {}
wobin@18 118 end
wobin@19 119
wobin@18 120 table.insert(self.Owners[Owner], o)
wobin@18 121 return o
wobin@18 122 end
wobin@18 123
wobin@19 124 function Squawks:OwnerString(squawk)
wobin@19 125 return "|cff"..classcolours[self.Squawkers[squawk.Owner].class or "WARRIOR"]..squawk.Owner.."|r"
wobin@19 126 end
wobin@19 127
wobin@18 128 function Squawks:Reload()
wobin@18 129 for _,squawk in ipairs(Model.Squawks.Main) do
wobin@18 130 if not self.Owners[squawk.Owner] then
wobin@18 131 self.Owners[squawk.Owner] = {}
wobin@18 132 end
wobin@18 133 table.insert(self.Owners[squawk.Owner], squawk)
wobin@18 134 if squawk.Related and #squawk.Related == 0 then
wobin@18 135 squawk.Related = nil
wobin@18 136 end
wobin@18 137 end
wobin@18 138 end
wobin@18 139
wobin@18 140 function Squawks:Sort(Squawks)
wobin@18 141 table.sort(Squawks or self.Main, function(a,b) return a.Time > b.Time end)
wobin@18 142 return Squawks or self.Main
wobin@18 143 end
wobin@18 144
wobin@18 145 function Squawks:GetOwn(Squawks)
wobin@18 146 local mine = {}
wobin@18 147 for _, squawk in ipairs(Squawks or self.Main) do
wobin@18 148 if squawk.Owner == UnitName("player") then
wobin@18 149 table.insert(mine, squawk)
wobin@18 150 end
wobin@18 151 end
wobin@18 152 return self:Sort(mine)
wobin@18 153 end
wobin@18 154
wobin@18 155 function Squawks:GetLast10(Squawks)
wobin@18 156 local mine = {}
wobin@18 157 Squawks = Squawks or self.Main
wobin@18 158 local limit = #Squawks < 10 and #Squawks or 10
wobin@18 159
wobin@18 160 Squawks = self:Sort(Squawks)
wobin@18 161
wobin@18 162 for i=1,limit do
wobin@18 163 table.insert(mine, Squawks[i])
wobin@18 164 end
wobin@18 165 return mine
wobin@18 166 end
wobin@18 167
wobin@18 168 -- initially called with no arguments to get the latest timestamp of
wobin@18 169 -- my squawks, or with a name to find the latest timestamp of all
wobin@18 170 -- squawks from that user
wobin@18 171 function Squawks:GetLatestTimestamp(Name, Squawks)
wobin@18 172 if Name then
wobin@18 173 if self.Owners[Name] then
wobin@18 174 return self:GetLatestTimestamp(nil, self.Owners[Name])
wobin@18 175 else
wobin@18 176 return -1 -- No squawks exist for that name in our records
wobin@18 177 end
wobin@18 178 end
wobin@18 179
wobin@18 180 Squawks = Squawks or self.Main or {}
wobin@18 181 local latest = self:Sort(Squawks)
wobin@18 182 return latest and #latest > 0 and latest[1].Time or -1
wobin@18 183 end
wobin@18 184
wobin@18 185 function Squawks:GetLatestSquawks(Timestamp)
wobin@18 186 local latest = {}
wobin@18 187 for i, squawk in ipairs(self:Sort()) do
wobin@18 188 if squawk.Time > Timestamp and i < 10 then
wobin@18 189 table.insert(latest, squawk)
wobin@18 190 else
wobin@18 191 return latest
wobin@18 192 end
wobin@18 193 end
wobin@18 194 end
wobin@18 195
wobin@18 196 function Settings:IsPrivate()
wobin@18 197 return Settings.Private
wobin@18 198 end
wobin@18 199
wobin@18 200 function Settings:TogglePrivate()
wobin@18 201 Settings.Private = not Settings.Private
wobin@18 202 end
wobin@18 203
wobin@18 204 function Settings:AddFollower(Name)
wobin@18 205 Settings.Follower[Name] = 1
wobin@18 206 self:RemovePending(Name)
wobin@18 207 end
wobin@18 208
wobin@18 209 function Settings:AddFollowing(Name)
wobin@18 210 Settings.Following[Name] = 1
wobin@18 211 self:RemoveRequested(Name)
wobin@18 212 end
wobin@18 213
wobin@18 214 function Settings:AddBlock(Name)
wobin@18 215 Settings.Blocked[Name] = 1
wobin@18 216 self:RemoveFollower(Name)
wobin@18 217 self:RemoveFollowing(Name)
wobin@18 218 end
wobin@18 219
wobin@18 220 function Settings:AddPending(Name)
wobin@18 221 Settings.Pending[Name] = 1
wobin@18 222 end
wobin@18 223
wobin@18 224 function Settings:AddRequested(Name)
wobin@18 225 Settings.Requested[Name] = 1
wobin@18 226 end
wobin@18 227
wobin@18 228 function Settings:RemoveFollower(Name)
wobin@18 229 if Settings.Follower[Name] then
wobin@18 230 Settings.Follower[Name] = nil
wobin@18 231 end
wobin@18 232 end
wobin@18 233
wobin@18 234 function Settings:RemoveFollowing(Name)
wobin@18 235 if Settings.Following[Name] then
wobin@18 236 Settings.Following[Name] = nil
wobin@18 237 end
wobin@18 238 end
wobin@18 239
wobin@18 240 function Settings:RemoveBlock(Name)
wobin@18 241 if Settings.Blocked[Name] then
wobin@18 242 Settings.Blocked[Name] = nil
wobin@18 243 end
wobin@18 244 end
wobin@18 245
wobin@18 246 function Settings:RemovePending(Name)
wobin@18 247 if Settings.Pending[Name] then
wobin@18 248 Settings.Pending[Name] = nil
wobin@18 249 end
wobin@18 250 end
wobin@18 251
wobin@18 252 function Settings:RemoveRequested(Name)
wobin@18 253 if Settings.Requested[Name] then
wobin@18 254 Settings.Requested[Name] = nil
wobin@18 255 end
wobin@18 256 end