#256 Sort Favorites option

This commit is contained in:
Physton
2023-10-31 16:21:45 +08:00
parent 59bb040e91
commit 63f0c23640
9 changed files with 137 additions and 15 deletions

View File

@@ -221,6 +221,24 @@ def on_app_started(_: gr.Blocks, app: FastAPI):
hi.push_favorite(data['type'], data['tags'], data['prompt'], data.get('name', ''))
return {"success": True}
@app.post("/physton_prompt/move_up_favorite")
async def _move_up_favorite(request: Request):
data = await request.json()
if 'type' not in data:
return {"success": False, "message": get_lang('is_required', {'0': 'type'})}
if 'id' not in data:
return {"success": False, "message": get_lang('is_required', {'0': 'id'})}
return {"success": hi.move_up_favorite(data['type'], data['id'])}
@app.post("/physton_prompt/move_down_favorite")
async def _move_down_favorite(request: Request):
data = await request.json()
if 'type' not in data:
return {"success": False, "message": get_lang('is_required', {'0': 'type'})}
if 'id' not in data:
return {"success": False, "message": get_lang('is_required', {'0': 'id'})}
return {"success": hi.move_down_favorite(data['type'], data['id'])}
@app.get("/physton_prompt/get_latest_history")
async def _get_latest_history(type: str):
return {"history": hi.get_latest_history(type)}

View File

@@ -79,6 +79,26 @@ class History:
self.__save_favorites(type)
return item
def move_up_favorite(self, type, id):
for index, favorite in enumerate(self.favorites[type]):
if favorite['id'] == id:
if index > 0:
self.favorites[type].insert(index - 1, self.favorites[type].pop(index))
self.__save_favorites(type)
return True
return False
return False
def move_down_favorite(self, type, id):
for index, favorite in enumerate(self.favorites[type]):
if favorite['id'] == id:
if index < len(self.favorites[type]) - 1:
self.favorites[type].insert(index + 1, self.favorites[type].pop(index))
self.__save_favorites(type)
return True
return False
return False
def get_latest_history(self, type):
if len(self.histories[type]) > 0:
return self.histories[type][-1]