comparison Model.lua @ 18:a3328fffef5c

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