-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFavBoard.py
260 lines (229 loc) · 8.34 KB
/
FavBoard.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import json
import Config
import User
from Util import Util
from BoardManager import BoardManager
from UserManager import UserManager
from BCache import BCache
from errors import *
TYPE_BOARD = 1
TYPE_DIR = 2
DEFAULT_LIST_FAVBOARD_COUNT = 20
class FavBoard(object):
def __init__(self, index = -1, name = '', father = -1):
self._index = index
self._type = type
self._name = name
self._father = father
if index == -1:
self._type = TYPE_DIR
else:
self._type = TYPE_BOARD
def Exists(self):
if self._type == TYPE_DIR:
return True
board = BoardManager.GetBoardByIndex(self._index + 1)
if board is None:
return False
return True
def IsBoard(self):
return self._type == TYPE_BOARD
def IsDir(self):
return self._type == TYPE_DIR
@staticmethod
def GET(svc, session, params, action):
if session == None:
raise Unauthorized("login first")
if not session.CheckScope('bbs'): raise NoPerm("out of scope")
if action == "list":
FavBoardMgr.ListFavBoards(svc, session, params)
elif action == "dirnames":
FavBoardMgr.Dirnames(svc, session, params)
else:
raise WrongArgs("unknown action")
@staticmethod
def POST(svc, session, params, action):
if session == None:
raise NoPerm("login first")
if not session.CheckScope('bbs'): raise NoPerm("out of scope")
raise WrongArgs("unknown action")
def GetInfo(self, index, user):
rfb = {}
rfb['index'] = index
rfb['father'] = self._father
if self.IsBoard():
rfb['type'] = 'board'
board = BoardManager.GetBoardByIndex(self._index + 1)
if board != None:
rfb['binfo'] = board.GetInfoWithUser(user)
else:
rfb['type'] = 'dir'
rfb['name'] = self._name
return rfb
def GetInfoJSON(self, index, user):
return json.dumps(self.GetInfo(index, user))
class FavBoardMgr(object):
_favboards_set = {}
@staticmethod
def LoadFavBoards(userid):
if userid not in FavBoardMgr._favboards_set:
fboards = FavBoardMgr.LoadNewFavBoards(userid)
if fboards == None:
return None
FavBoardMgr._favboards_set[userid] = fboards
return FavBoardMgr._favboards_set[userid]
@staticmethod
def LoadNewFavBoards(userid):
fboards = FavBoards(userid)
fboards.LoadFavBoards()
return fboards
@staticmethod
def ListFavBoards(svc, session, params):
start = Util.GetInt(params, 'start')
end = Util.GetInt(params, 'end')
count = Util.GetInt(params, 'count')
father = Util.GetInt(params, 'father', -1)
fboards = FavBoardMgr.LoadFavBoards(session.GetUser().name)
if fboards == None:
raise ServerError("failed to load fav boards")
fboards.LoadFavBoards()
start, end = Util.CheckRange(start, end, count,
DEFAULT_LIST_FAVBOARD_COUNT, fboards._count)
if (start <= end) and (start >= 1) and (end <= fboards._count):
first = True
result = '[\n'
for index in range(0, fboards._count):
fboard = fboards._favboards[index]
if fboard.Exists():
if fboard._father == father:
if not first:
result += ',\n'
first = False
result += fboard.GetInfoJSON(index, session.GetUser())
result += '\n]'
svc.writedata(result)
return
else:
raise WrongArgs('invalid arguments')
@staticmethod
def Dirnames(svc, session, params):
idx = svc.get_int(params, 'index')
fboards = FavBoardMgr.LoadFavBoards(session.GetUser().name)
if fboards is None:
raise ServerError("failed to load fav boards")
fboards.LoadFavBoards()
if idx < 0 or idx >= fboards._count:
raise OutOfRange('index out of range')
fboard = fboards._favboards[idx]
if not fboard.Exists():
raise WrongArgs("fav board does not exist")
result = [fboard.GetInfo(idx, session.GetUser())]
while fboard._father != -1:
idx = fboard._father
fboard = fboards._favboards[idx]
if not fboard.Exists():
raise ServerError("dir on the path does not exist")
result.append(fboard.GetInfo(idx, session.GetUser()))
svc.writedata(json.dumps(result))
class FavBoards:
def __init__(self, userid):
self._userid = userid
self._favboards = {}
self._count = 0
self._current = 0
def DelFavBoard(self, index):
if index >= self._count:
return self._count
if index < 0:
return self._count
fboard = self._favboards[index]
if fboard.IsDir():
j = 0
while j < self._count:
if self._favboards[j]._father == index:
self.DelFavBoard(j)
if j < index:
index = index - 1
j = j - 1
j = j + 1
self._count = self._count - 1
j = index
while j < self._count:
self._favboards[j] = self._favboards[j+1]
j = j + 1
j = 0
while j < self._count:
if self._favboards[j]._father >= index:
self._favboards[j]._father = self._favboards[j]._father-1
j = j + 1
if self._current >= index:
self._current = self._current - 1
if self._count == 0:
self._count = 1
self._favboards[0] = FavBoard(0)
return 0
def LoadFavBoards(self):
path = User.User.OwnFile(self._userid, "favboard")
self._current = -1
fd = open(path, "rb")
if fd != None:
magic = Util.ReadInt(fd)
if magic != 0x8080:
self._count = magic
index = 0
while index < self._count:
bindex = Util.ReadInt(fd)
self._favboards[index] = FavBoard(bindex)
index = index + 1
else:
self._count = Util.ReadInt(fd)
index = 0
while index < self._count:
flag = Util.ReadInt(fd)
title = ''
if flag == -1:
length = Util.ReadChar(fd)
title = Util.gbkDec(Util.CString(fd.read(length)))
father = Util.ReadInt(fd)
self._favboards[index] = FavBoard(flag, title, father)
index = index + 1
fd.close()
if self._count <= 0:
fd = open(Config.BBS_ROOT + "etc/initial_favboard", "r")
if fd == None:
self._count = 1
self._favboards[0] = FavBoard(0)
else:
self._count = 1
self._favboards[0] = FavBoard(0)
while True:
board = Util.ReadString(fd)
if board == '':
break
bobj = BoardManager.GetBoard(board)
if bobj != None:
self._favboards[self._count] = FavBoard(bobj.index - 1)
fd.close()
else:
count = self._count
index = 0
while index < self._count:
fboard = self._favboards[index]
if fboard.IsDir():
index = index + 1
continue
bindex = fboard._index
board = BoardManager.GetBoardByIndex(bindex + 1)
user = UserManager.LoadUser(self._userid)
if ((bindex >= 0) and (bindex <= BCache.GetBoardCount())
and (user != None)
and (board != None)
and (board.CheckSeePerm(user))):
index = index + 1
continue
self.DelFavBoard(index)
index = index + 1
if count != self._count:
self.SaveFavBoards()
def SaveFavBoards(self):
pass