Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
chenpangpang
open-webui
Commits
e20bb234
Commit
e20bb234
authored
May 26, 2024
by
Timothy J. Baek
Browse files
feat: access archived chats as admin
parent
62d37f1f
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
89 additions
and
51 deletions
+89
-51
backend/apps/webui/models/chats.py
backend/apps/webui/models/chats.py
+38
-10
backend/apps/webui/routers/chats.py
backend/apps/webui/routers/chats.py
+49
-37
src/lib/components/chat/Messages/ResponseMessage.svelte
src/lib/components/chat/Messages/ResponseMessage.svelte
+1
-1
src/lib/utils/index.ts
src/lib/utils/index.ts
+1
-3
No files found.
backend/apps/webui/models/chats.py
View file @
e20bb234
...
@@ -191,6 +191,20 @@ class ChatTable:
...
@@ -191,6 +191,20 @@ class ChatTable:
except
:
except
:
return
None
return
None
def
archive_all_chats_by_user_id
(
self
,
user_id
:
str
)
->
bool
:
try
:
chats
=
self
.
get_chats_by_user_id
(
user_id
)
for
chat
in
chats
:
query
=
Chat
.
update
(
archived
=
True
,
).
where
(
Chat
.
id
==
chat
.
id
)
query
.
execute
()
return
True
except
:
return
False
def
get_archived_chat_list_by_user_id
(
def
get_archived_chat_list_by_user_id
(
self
,
user_id
:
str
,
skip
:
int
=
0
,
limit
:
int
=
50
self
,
user_id
:
str
,
skip
:
int
=
0
,
limit
:
int
=
50
)
->
List
[
ChatModel
]:
)
->
List
[
ChatModel
]:
...
@@ -205,17 +219,31 @@ class ChatTable:
...
@@ -205,17 +219,31 @@ class ChatTable:
]
]
def
get_chat_list_by_user_id
(
def
get_chat_list_by_user_id
(
self
,
user_id
:
str
,
skip
:
int
=
0
,
limit
:
int
=
50
self
,
user_id
:
str
,
include_archived
:
bool
=
False
,
skip
:
int
=
0
,
limit
:
int
=
50
,
)
->
List
[
ChatModel
]:
)
->
List
[
ChatModel
]:
return
[
if
include_archived
:
ChatModel
(
**
model_to_dict
(
chat
))
return
[
for
chat
in
Chat
.
select
()
ChatModel
(
**
model_to_dict
(
chat
))
.
where
(
Chat
.
archived
==
False
)
for
chat
in
Chat
.
select
()
.
where
(
Chat
.
user_id
==
user_id
)
.
where
(
Chat
.
user_id
==
user_id
)
.
order_by
(
Chat
.
updated_at
.
desc
())
.
order_by
(
Chat
.
updated_at
.
desc
())
# .limit(limit)
# .limit(limit)
# .offset(skip)
# .offset(skip)
]
]
else
:
return
[
ChatModel
(
**
model_to_dict
(
chat
))
for
chat
in
Chat
.
select
()
.
where
(
Chat
.
archived
==
False
)
.
where
(
Chat
.
user_id
==
user_id
)
.
order_by
(
Chat
.
updated_at
.
desc
())
# .limit(limit)
# .offset(skip)
]
def
get_chat_list_by_chat_ids
(
def
get_chat_list_by_chat_ids
(
self
,
chat_ids
:
List
[
str
],
skip
:
int
=
0
,
limit
:
int
=
50
self
,
chat_ids
:
List
[
str
],
skip
:
int
=
0
,
limit
:
int
=
50
...
...
backend/apps/webui/routers/chats.py
View file @
e20bb234
...
@@ -78,43 +78,25 @@ async def delete_all_user_chats(request: Request, user=Depends(get_current_user)
...
@@ -78,43 +78,25 @@ async def delete_all_user_chats(request: Request, user=Depends(get_current_user)
async
def
get_user_chat_list_by_user_id
(
async
def
get_user_chat_list_by_user_id
(
user_id
:
str
,
user
=
Depends
(
get_admin_user
),
skip
:
int
=
0
,
limit
:
int
=
50
user_id
:
str
,
user
=
Depends
(
get_admin_user
),
skip
:
int
=
0
,
limit
:
int
=
50
):
):
return
Chats
.
get_chat_list_by_user_id
(
user_id
,
skip
,
limit
)
return
Chats
.
get_chat_list_by_user_id
(
user_id
,
include_archived
=
True
,
skip
=
skip
,
limit
=
limit
)
############################
# GetArchivedChats
############################
@
router
.
get
(
"/archived"
,
response_model
=
List
[
ChatTitleIdResponse
])
async
def
get_archived_session_user_chat_list
(
user
=
Depends
(
get_current_user
),
skip
:
int
=
0
,
limit
:
int
=
50
):
return
Chats
.
get_archived_chat_list_by_user_id
(
user
.
id
,
skip
,
limit
)
############################
############################
#
GetSharedChatById
#
CreateNewChat
############################
############################
@
router
.
get
(
"/share/{share_id}"
,
response_model
=
Optional
[
ChatResponse
])
@
router
.
post
(
"/new"
,
response_model
=
Optional
[
ChatResponse
])
async
def
get_shared_chat_by_id
(
share_id
:
str
,
user
=
Depends
(
get_current_user
)):
async
def
create_new_chat
(
form_data
:
ChatForm
,
user
=
Depends
(
get_current_user
)):
if
user
.
role
==
"pending"
:
try
:
raise
HTTPException
(
chat
=
Chats
.
insert_new_chat
(
user
.
id
,
form_data
)
status_code
=
status
.
HTTP_401_UNAUTHORIZED
,
detail
=
ERROR_MESSAGES
.
NOT_FOUND
)
if
user
.
role
==
"user"
:
chat
=
Chats
.
get_chat_by_share_id
(
share_id
)
elif
user
.
role
==
"admin"
:
chat
=
Chats
.
get_chat_by_id
(
share_id
)
if
chat
:
return
ChatResponse
(
**
{
**
chat
.
model_dump
(),
"chat"
:
json
.
loads
(
chat
.
chat
)})
return
ChatResponse
(
**
{
**
chat
.
model_dump
(),
"chat"
:
json
.
loads
(
chat
.
chat
)})
else
:
except
Exception
as
e
:
log
.
exception
(
e
)
raise
HTTPException
(
raise
HTTPException
(
status_code
=
status
.
HTTP_40
1_UNAUTHORIZED
,
detail
=
ERROR_MESSAGES
.
NOT_FOUND
status_code
=
status
.
HTTP_40
0_BAD_REQUEST
,
detail
=
ERROR_MESSAGES
.
DEFAULT
()
)
)
...
@@ -150,19 +132,49 @@ async def get_all_user_chats_in_db(user=Depends(get_admin_user)):
...
@@ -150,19 +132,49 @@ async def get_all_user_chats_in_db(user=Depends(get_admin_user)):
############################
############################
#
CreateNew
Chat
#
GetArchived
Chat
s
############################
############################
@
router
.
post
(
"/new"
,
response_model
=
Optional
[
ChatResponse
])
@
router
.
get
(
"/archived"
,
response_model
=
List
[
ChatTitleIdResponse
])
async
def
create_new_chat
(
form_data
:
ChatForm
,
user
=
Depends
(
get_current_user
)):
async
def
get_archived_session_user_chat_list
(
try
:
user
=
Depends
(
get_current_user
),
skip
:
int
=
0
,
limit
:
int
=
50
chat
=
Chats
.
insert_new_chat
(
user
.
id
,
form_data
)
):
return
Chats
.
get_archived_chat_list_by_user_id
(
user
.
id
,
skip
,
limit
)
############################
# ArchiveAllChats
############################
@
router
.
get
(
"/archive/all"
,
response_model
=
List
[
ChatTitleIdResponse
])
async
def
archive_all_chats
(
user
=
Depends
(
get_current_user
)):
return
Chats
.
archive_all_chats_by_user_id
(
user
.
id
)
############################
# GetSharedChatById
############################
@
router
.
get
(
"/share/{share_id}"
,
response_model
=
Optional
[
ChatResponse
])
async
def
get_shared_chat_by_id
(
share_id
:
str
,
user
=
Depends
(
get_current_user
)):
if
user
.
role
==
"pending"
:
raise
HTTPException
(
status_code
=
status
.
HTTP_401_UNAUTHORIZED
,
detail
=
ERROR_MESSAGES
.
NOT_FOUND
)
if
user
.
role
==
"user"
:
chat
=
Chats
.
get_chat_by_share_id
(
share_id
)
elif
user
.
role
==
"admin"
:
chat
=
Chats
.
get_chat_by_id
(
share_id
)
if
chat
:
return
ChatResponse
(
**
{
**
chat
.
model_dump
(),
"chat"
:
json
.
loads
(
chat
.
chat
)})
return
ChatResponse
(
**
{
**
chat
.
model_dump
(),
"chat"
:
json
.
loads
(
chat
.
chat
)})
except
Exception
as
e
:
else
:
log
.
exception
(
e
)
raise
HTTPException
(
raise
HTTPException
(
status_code
=
status
.
HTTP_40
0_BAD_REQUEST
,
detail
=
ERROR_MESSAGES
.
DEFAULT
()
status_code
=
status
.
HTTP_40
1_UNAUTHORIZED
,
detail
=
ERROR_MESSAGES
.
NOT_FOUND
)
)
...
...
src/lib/components/chat/Messages/ResponseMessage.svelte
View file @
e20bb234
...
@@ -82,7 +82,7 @@
...
@@ -82,7 +82,7 @@
// Open all links in a new tab/window (from https://github.com/markedjs/marked/issues/655#issuecomment-383226346)
// Open all links in a new tab/window (from https://github.com/markedjs/marked/issues/655#issuecomment-383226346)
const origLinkRenderer = renderer.link;
const origLinkRenderer = renderer.link;
renderer.link =
(href, title, text) => {
renderer.link =
(href, title, text) => {
const html = origLinkRenderer.call(renderer, href, title, text);
const html = origLinkRenderer.call(renderer, href, title, text);
return html.replace(/^<a /, '<a target="_blank" rel="nofollow" ');
return html.replace(/^<a /, '<a target="_blank" rel="nofollow" ');
};
};
...
...
src/lib/utils/index.ts
View file @
e20bb234
...
@@ -17,9 +17,7 @@ export const sanitizeResponseContent = (content: string) => {
...
@@ -17,9 +17,7 @@ export const sanitizeResponseContent = (content: string) => {
};
};
export
const
revertSanitizedResponseContent
=
(
content
:
string
)
=>
{
export
const
revertSanitizedResponseContent
=
(
content
:
string
)
=>
{
return
content
return
content
.
replaceAll
(
'
<
'
,
'
<
'
).
replaceAll
(
'
>
'
,
'
>
'
);
.
replaceAll
(
'
<
'
,
'
<
'
)
.
replaceAll
(
'
>
'
,
'
>
'
);
};
};
export
const
capitalizeFirstLetter
=
(
string
)
=>
{
export
const
capitalizeFirstLetter
=
(
string
)
=>
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment