Skip to content

Commit

Permalink
Merge pull request #102 from siwonKH/main
Browse files Browse the repository at this point in the history
Update
  • Loading branch information
siwonkh authored Jul 11, 2023
2 parents 2b8aaf2 + a91a9d2 commit dd0feab
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,14 @@ public List<ChatMessage> getChats(HttpSession session, HttpServletResponse respo
}

@DeleteMapping("/delete")
public ResponseEntity<String> deleteLastTwoChatItems() {
public ResponseEntity<String> deleteLastTwoChatItems(HttpSession session) {
if (session.getAttribute("user") == null) {
log.info("Chat delete: No session");
return ResponseEntity.notFound().build();
}

String userId = session.getAttribute("user").toString();

List<Chat> chatItems = chatRepository.findAll();
int size = chatItems.size();
if (size >= 2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public enum Sort {
@Getter
public static class JobData {
@JsonPropertyDescription("The keyword to search for job posting information, e.g. 백엔드")
@JsonProperty(required = true)
public String keyword;

@JsonPropertyDescription("pd:sort by date of publication, rc:sort by hits, ac:sort by number of applicants")
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/com/khpt/projectkim/service/ChatService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.theokanning.openai.completion.chat.ChatMessageRole;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
Expand All @@ -20,6 +21,8 @@
@Slf4j
public class ChatService {

private final UserService userService;

private final UserRepository userRepository;

@Transactional
Expand All @@ -42,6 +45,23 @@ public ExtractListFromUserDto getListFromUser(String userId) {
return extractListFromUserDto;
}

public void deleteLastTwoChat(String userId) {
User user = userService.getUserByStringId(userId);

List<Chat> chatItems = user.getChats();
if (chatItems.size() >= 2) {
if (chatItems.get(chatItems.size() - 1).getRole() == ChatMessageRole.ASSISTANT) {
chatItems.remove(chatItems.size() - 1);
chatItems.remove(chatItems.size() - 1);
} else {
chatItems.remove(chatItems.size() - 1);
}

}
user.setChats(chatItems);
userRepository.save(user);
}

@Transactional
public List<ChatMessage> getUserChats(String userId) {
User user = userRepository.findById(Long.parseLong(userId))
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/static/css/output.css

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions src/main/resources/static/js/chatInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ chatInput.addEventListener('input', function() {
})

chatBtn.addEventListener('click', function (event) {
inputHandler()
event.preventDefault()
if (!inputAvailable) {
return
}
sendMessage(chatInput.value)
chatInput.value = ''
inputHandler()
})

chatInput.addEventListener('keydown', function(event) {
Expand Down Expand Up @@ -115,7 +115,8 @@ function sendMessage(inputValue) {

const eventSource = new EventSource("/chat/events")
eventSource.addEventListener('message', function(event) {
assistantChatItem.lastChild.textContent = assistantChatItem.lastChild.textContent + event.data.toString().replaceAll("%20", " ").replaceAll("%0A", "\n")
assistantChatItem.lastChild.firstChild.textContent = assistantChatItem.lastChild.firstChild.textContent + event.data.toString().replaceAll("%20", " ").replaceAll("%0A", "\n")


const chats = document.getElementById('chats')
chats.scrollTo(0, chats.scrollHeight)
Expand All @@ -126,14 +127,14 @@ function sendMessage(inputValue) {
eventSource.addEventListener('process', function(event) {
console.log(event.data)
const chats = document.getElementById('chats')
const processElem = chats.querySelector(".process span")
const processElem = chats.querySelector("div .process span")
if (event.data === "fine") {
processElem.textContent = ""
return
}
if (processElem == null) {
const process = createCustomElement(event.data)
chats.lastChild.lastChild.appendChild(process)
chats.lastChild.lastChild.firstChild.appendChild(process)
} else {
processElem.textContent = event.data
}
Expand Down
28 changes: 18 additions & 10 deletions src/main/resources/static/js/chatStatusChange.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,26 @@ function addIconsToChat() {

copyBtns.forEach(btn => {
btn.addEventListener("click", () => {
const text = btn.parentNode.parentNode.children[0]
console.log(text)
const range = document.createRange();
range.selectNode(text);
window.getSelection().removeAllRanges();
window.getSelection().addRange(range);
// const text = btn.parentNode.parentNode.children[0]
// console.log(text)
// const range = document.createRange();
// range.selectNode(text);
// window.getSelection().removeAllRanges();
// window.getSelection().addRange(range);
//
// // 클립보드에 복사
// document.execCommand("copy");
//
// // 선택해제
// window.getSelection().removeAllRanges();

// 클립보드에 복사
document.execCommand("copy");
const textarea = document.createElement("textarea");
textarea.value = btn.parentNode.parentNode.firstChild.textContent;

// 선택해제
window.getSelection().removeAllRanges();
document.body.appendChild(textarea);
textarea.select();
document.execCommand("copy");
document.body.removeChild(textarea);
})
})

Expand Down

0 comments on commit dd0feab

Please sign in to comment.