-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #140 from RitehWebTeam/websocket-image-upload
Web - Chat image upload
- Loading branch information
Showing
47 changed files
with
611 additions
and
308 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
root = true | ||
|
||
[*] | ||
charset = utf-8 | ||
end_of_line = lf | ||
indent_style = space | ||
insert_final_newline = true | ||
trim_trailing_whitespace = true | ||
indent_size = 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 57 additions & 6 deletions
63
backend/src/main/java/com/rimatch/rimatchbackend/controller/MessageController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,94 @@ | ||
package com.rimatch.rimatchbackend.controller; | ||
|
||
import com.rimatch.rimatchbackend.model.Match; | ||
import com.rimatch.rimatchbackend.model.Message; | ||
import com.rimatch.rimatchbackend.model.User; | ||
import com.rimatch.rimatchbackend.repository.MatchRepository; | ||
import com.rimatch.rimatchbackend.repository.MessageRepository; | ||
import com.rimatch.rimatchbackend.service.S3Service; | ||
import com.rimatch.rimatchbackend.service.UserService; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.PageRequest; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.mongodb.core.MongoTemplate; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.util.Optional; | ||
|
||
@RestController | ||
@RequestMapping("api/messages") | ||
public class MessageController { | ||
|
||
@Autowired MessageRepository messageRepository; | ||
@Autowired private MessageRepository messageRepository; | ||
|
||
@Autowired | ||
private MongoTemplate mongoTemplate; | ||
@Autowired private MatchRepository matchRepository; | ||
|
||
@Autowired private UserService userService; | ||
|
||
@Autowired private S3Service s3Service; | ||
|
||
@PostMapping("/upload-image") | ||
public ResponseEntity<?> uploadImage(@RequestBody MultipartFile photo,String chatId,HttpServletRequest request) throws Exception { | ||
String authToken = request.getHeader("Authorization"); | ||
User user = userService.getUserByToken(authToken); | ||
Optional<Match> match = matchRepository.findById(chatId); | ||
if(match.isPresent()){ | ||
if(!userHasAccessToChat(match.get(),user.getId())){ | ||
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); | ||
} | ||
}else{ | ||
return new ResponseEntity<>(HttpStatus.BAD_REQUEST); | ||
} | ||
return ResponseEntity.ok(s3Service.uploadImage(photo,chatId,"chats")); | ||
} | ||
|
||
/*@PostMapping("/upload-voice") | ||
public ResponseEntity<?> uploadVoice(@RequestBody MultipartFile audio,String chatId,HttpServletRequest request) throws Exception { | ||
String authToken = request.getHeader("Authorization"); | ||
Optional<Message> message = messageRepository.findById(chatId); | ||
if(message.isPresent()){ | ||
if(!userHasAccessToChat(message.get(),authToken)){ | ||
ResponseEntity.status(400); | ||
} | ||
} | ||
return ResponseEntity.ok(s3Service.uploadAudioFile(audio,chatId,"chats")); | ||
}*/ | ||
|
||
@GetMapping("/{chatId}") | ||
public Page<Message> getMessagesFromId(@PathVariable String chatId, | ||
@RequestParam(required = false) Integer page, | ||
@RequestParam(required = false) Integer pageSize, | ||
@RequestParam(required = false) String messageId) { | ||
@RequestParam(required = false) String messageId, | ||
HttpServletRequest request) { | ||
String authToken = request.getHeader("Authorization"); | ||
if(page == null) page = 0; | ||
if(pageSize == null) pageSize = 5; | ||
Pageable p = PageRequest.of(page,pageSize); | ||
if (messageId != null) { | ||
|
||
Optional<Message> m = messageRepository.findById(messageId); | ||
if(m.isPresent()){ | ||
|
||
//if(!userHasAccessToChat(m.get(),authToken)) return Page.empty(); //should be handled better but due to time left like this | ||
return messageRepository.findByChatIdAndTimestampBeforeOrderByTimestampDesc(chatId,m.get().getTimestamp(), p); | ||
}else{ | ||
return null; | ||
} | ||
} | ||
return messageRepository.findByChatIdOrderByTimestampDesc(chatId,p); | ||
} | ||
|
||
private boolean userHasAccessToChat(Match match,String userId){ | ||
if(match.getFirstUserId().equals(userId) || match.getSecondUserId().equals(userId)){ | ||
if(match.isAccepted() && match.isFinished()){ | ||
return true; | ||
} | ||
return false; | ||
} | ||
return false; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,4 +24,4 @@ public class Match { | |
private boolean accepted = false; | ||
|
||
private boolean finished = false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
backend/src/main/java/com/rimatch/rimatchbackend/model/MessageType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.rimatch.rimatchbackend.model; | ||
|
||
public enum MessageType { | ||
TEXT, | ||
IMAGE, | ||
VOICE, | ||
REPLY | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module type="WEB_MODULE" version="4"> | ||
<component name="NewModuleRootManager" inherit-compiler-output="true"> | ||
<exclude-output /> | ||
<content url="file://$MODULE_DIR$" /> | ||
<orderEntry type="inheritedJdk" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
</component> | ||
</module> |
Oops, something went wrong.