From 849d563d9205117affbe09e97318e4608a6cf9c2 Mon Sep 17 00:00:00 2001 From: nelifs Date: Mon, 9 Dec 2024 12:44:22 +0300 Subject: [PATCH] Migrate to unique names instead of snowflake --- .../controllers/ChannelsController.java | 24 ++++++++--------- .../controllers/MessagesController.java | 16 ++++++------ .../foxogram/controllers/UsersController.java | 8 +++--- .../su/foxogram/services/MessagesService.java | 23 +++++++--------- .../su/foxogram/services/UsersService.java | 10 +++---- .../su/foxogram/dtos/response/ChannelDTO.java | 6 ++--- .../su/foxogram/dtos/response/MemberDTO.java | 7 ++--- .../su/foxogram/dtos/response/MessageDTO.java | 12 ++++----- .../foxogram/dtos/response/MessagesDTO.java | 2 +- .../su/foxogram/dtos/response/UserDTO.java | 2 -- .../interceptors/ChannelInterceptor.java | 10 +++---- .../interceptors/MemberInterceptor.java | 4 +-- .../main/java/su/foxogram/models/Channel.java | 9 ++++--- .../main/java/su/foxogram/models/Code.java | 4 +-- .../main/java/su/foxogram/models/Member.java | 15 ++++++----- .../main/java/su/foxogram/models/Message.java | 7 ++--- .../main/java/su/foxogram/models/User.java | 5 ++-- .../repositories/ChannelRepository.java | 8 ++++-- .../foxogram/repositories/CodeRepository.java | 4 +-- .../repositories/MemberRepository.java | 10 ++++--- .../repositories/MessageRepository.java | 4 +-- .../foxogram/repositories/UserRepository.java | 6 ++--- .../services/AuthenticationService.java | 4 +-- .../su/foxogram/services/ChannelsService.java | 26 ++++++++++--------- .../su/foxogram/services/EmailService.java | 4 +-- .../java/su/foxogram/services/JwtService.java | 8 +++--- 26 files changed, 118 insertions(+), 120 deletions(-) diff --git a/foxogram-api/src/main/java/su/foxogram/controllers/ChannelsController.java b/foxogram-api/src/main/java/su/foxogram/controllers/ChannelsController.java index 837a8c4..7f3323a 100644 --- a/foxogram-api/src/main/java/su/foxogram/controllers/ChannelsController.java +++ b/foxogram-api/src/main/java/su/foxogram/controllers/ChannelsController.java @@ -44,13 +44,13 @@ public ChannelDTO createChannel(@RequestAttribute(value = AttributesConstants.US } @Operation(summary = "Get channel") - @GetMapping("/{id}") + @GetMapping("/{name}") public ChannelDTO getChannel(@RequestAttribute(value = AttributesConstants.CHANNEL) Channel channel) { return new ChannelDTO(channel); } @Operation(summary = "Edit channel") - @PatchMapping("/{id}") + @PatchMapping("/{name}") public ChannelDTO editChannel(@RequestAttribute(value = AttributesConstants.MEMBER) Member member, @RequestAttribute(value = AttributesConstants.CHANNEL) Channel channel, @Valid @RequestBody ChannelEditDTO body) throws MissingPermissionsException { channel = channelsService.editChannel(member, channel, body); @@ -58,7 +58,7 @@ public ChannelDTO editChannel(@RequestAttribute(value = AttributesConstants.MEMB } @Operation(summary = "Delete channel") - @DeleteMapping("/{id}") + @DeleteMapping("/{name}") public OkDTO deleteChannel(@RequestAttribute(value = AttributesConstants.USER) User user, @RequestAttribute(value = AttributesConstants.CHANNEL) Channel channel) throws MissingPermissionsException { channelsService.deleteChannel(channel, user); @@ -66,15 +66,15 @@ public OkDTO deleteChannel(@RequestAttribute(value = AttributesConstants.USER) U } @Operation(summary = "Join channel") - @PutMapping("/{id}/members/@me") - public MemberDTO joinChannel(@RequestAttribute(value = AttributesConstants.USER) User user, @RequestAttribute(value = AttributesConstants.CHANNEL) Channel channel, @PathVariable long id) throws MemberAlreadyInChannelException { + @PutMapping("/{name}/members/@me") + public MemberDTO joinChannel(@RequestAttribute(value = AttributesConstants.USER) User user, @RequestAttribute(value = AttributesConstants.CHANNEL) Channel channel) throws MemberAlreadyInChannelException { Member member = channelsService.joinUser(channel, user); return new MemberDTO(member); } @Operation(summary = "Leave channel") - @DeleteMapping("/{id}/members/@me") + @DeleteMapping("/{name}/members/@me") public OkDTO leaveChannel(@RequestAttribute(value = AttributesConstants.USER) User user, @RequestAttribute(value = AttributesConstants.CHANNEL) Channel channel) throws MemberInChannelNotFoundException { channelsService.leaveUser(channel, user); @@ -82,13 +82,13 @@ public OkDTO leaveChannel(@RequestAttribute(value = AttributesConstants.USER) Us } @Operation(summary = "Get member") - @GetMapping("/{id}/members/{memberId}") - public MemberDTO getMember(@RequestAttribute(value = AttributesConstants.USER) User user, @RequestAttribute(value = AttributesConstants.CHANNEL) Channel channel, @PathVariable String memberId) throws MemberInChannelNotFoundException { - if (Objects.equals(memberId, "@me")) { - memberId = user.getId(); + @GetMapping("/{name}/members/{memberUsername}") + public MemberDTO getMember(@RequestAttribute(value = AttributesConstants.USER) User user, @RequestAttribute(value = AttributesConstants.CHANNEL) Channel channel, @PathVariable String memberUsername) throws MemberInChannelNotFoundException { + if (Objects.equals(memberUsername, "@me")) { + memberUsername = user.getUsername(); } - Member member = channelsService.getMember(channel, memberId); + Member member = channelsService.getMember(channel, memberUsername); if (member == null) throw new MemberInChannelNotFoundException(); @@ -96,7 +96,7 @@ public MemberDTO getMember(@RequestAttribute(value = AttributesConstants.USER) U } @Operation(summary = "Get members") - @GetMapping("/{id}/members") + @GetMapping("/{name}/members") public List getMembers(@RequestAttribute(value = AttributesConstants.CHANNEL) Channel channel) { return channelsService.getMembers(channel); } diff --git a/foxogram-api/src/main/java/su/foxogram/controllers/MessagesController.java b/foxogram-api/src/main/java/su/foxogram/controllers/MessagesController.java index f1303a6..45300da 100644 --- a/foxogram-api/src/main/java/su/foxogram/controllers/MessagesController.java +++ b/foxogram-api/src/main/java/su/foxogram/controllers/MessagesController.java @@ -36,7 +36,7 @@ public MessagesController(MessagesService messagesService) { } @Operation(summary = "Get messages") - @GetMapping("/channel/{channelId}") + @GetMapping("/channel/{name}") public List getMessages(@RequestAttribute(value = AttributesConstants.USER) User user, @RequestAttribute(value = AttributesConstants.CHANNEL) Channel channel, @RequestParam(required = false, defaultValue = "0") long before, @RequestParam(required = false, defaultValue = "0") int limit) { log.info("MESSAGES ({}, {}) from CHANNEL ({}) by USER ({}, {}) requested", before, limit, channel.getId(), user.getId(), user.getEmail()); @@ -44,8 +44,8 @@ public List getMessages(@RequestAttribute(value = AttributesConstant } @Operation(summary = "Get message") - @GetMapping("/channel/{channelId}/{id}") - public MessagesDTO getMessage(@RequestAttribute(value = AttributesConstants.USER) User user, @RequestAttribute(value = AttributesConstants.CHANNEL) Channel channel, @PathVariable String id) throws MessageNotFoundException { + @GetMapping("/channel/{name}/{id}") + public MessagesDTO getMessage(@RequestAttribute(value = AttributesConstants.USER) User user, @RequestAttribute(value = AttributesConstants.CHANNEL) Channel channel, @PathVariable long id) throws MessageNotFoundException { log.info("MESSAGE ({}) from CHANNEL ({}) by USER ({}, {}) requested", id, channel.getId(), user.getId(), user.getEmail()); List message = List.of(messagesService.getMessage(id, channel)); @@ -54,7 +54,7 @@ public MessagesDTO getMessage(@RequestAttribute(value = AttributesConstants.USER } @Operation(summary = "Create message") - @PostMapping("/channel/{channelId}") + @PostMapping("/channel/{name}") public OkDTO createMessage(@RequestAttribute(value = AttributesConstants.USER) User user, @RequestAttribute(value = AttributesConstants.CHANNEL) Channel channel, @Valid @RequestBody MessageCreateDTO body) { log.info("MESSAGE post to CHANNEL ({}) by USER ({}, {}) requested", channel.getId(), user.getId(), user.getEmail()); @@ -64,8 +64,8 @@ public OkDTO createMessage(@RequestAttribute(value = AttributesConstants.USER) U } @Operation(summary = "Delete message") - @DeleteMapping("/channel/{channelId}/{id}") - public OkDTO deleteMessage(@RequestAttribute(value = AttributesConstants.USER) User user, @RequestAttribute(value = AttributesConstants.MEMBER) Member member, @RequestAttribute(value = AttributesConstants.CHANNEL) Channel channel, @PathVariable String id) throws MessageNotFoundException, MissingPermissionsException { + @DeleteMapping("/channel/{name}/{id}") + public OkDTO deleteMessage(@RequestAttribute(value = AttributesConstants.USER) User user, @RequestAttribute(value = AttributesConstants.MEMBER) Member member, @RequestAttribute(value = AttributesConstants.CHANNEL) Channel channel, @PathVariable long id) throws MessageNotFoundException, MissingPermissionsException { log.info("MESSAGE ({}) delete from CHANNEL ({}) by USER ({}, {}) requested", id, channel.getId(), user.getId(), user.getEmail()); messagesService.deleteMessage(id, member, channel); @@ -74,8 +74,8 @@ public OkDTO deleteMessage(@RequestAttribute(value = AttributesConstants.USER) U } @Operation(summary = "Edit message") - @PatchMapping("/channel/{channelId}/{id}") - public MessagesDTO editMessage(@RequestAttribute(value = AttributesConstants.USER) User user, @RequestAttribute(value = AttributesConstants.MEMBER) Member member, @RequestAttribute(value = AttributesConstants.CHANNEL) Channel channel, @Valid @RequestBody MessageCreateDTO body, @PathVariable String id) throws MessageNotFoundException, MissingPermissionsException { + @PatchMapping("/channel/{name}/{id}") + public MessagesDTO editMessage(@RequestAttribute(value = AttributesConstants.USER) User user, @RequestAttribute(value = AttributesConstants.MEMBER) Member member, @RequestAttribute(value = AttributesConstants.CHANNEL) Channel channel, @Valid @RequestBody MessageCreateDTO body, @PathVariable long id) throws MessageNotFoundException, MissingPermissionsException { log.info("MESSAGE ({}) patch in CHANNEL ({}) by USER ({}, {}) requested", id, channel.getId(), user.getId(), user.getEmail()); List message = List.of(messagesService.editMessage(id, channel, member, body)); diff --git a/foxogram-api/src/main/java/su/foxogram/controllers/UsersController.java b/foxogram-api/src/main/java/su/foxogram/controllers/UsersController.java index 910a299..89ca87d 100644 --- a/foxogram-api/src/main/java/su/foxogram/controllers/UsersController.java +++ b/foxogram-api/src/main/java/su/foxogram/controllers/UsersController.java @@ -30,13 +30,13 @@ public UsersController(UsersService usersService) { } @Operation(summary = "Get user") - @GetMapping("/{userKey}") - public UserDTO getUser(@RequestAttribute(value = AttributesConstants.USER) User authenticatedUser, @PathVariable String userKey) throws UserNotFoundException { - if (Objects.equals(userKey, "@me")) { + @GetMapping("/{username}") + public UserDTO getUser(@RequestAttribute(value = AttributesConstants.USER) User authenticatedUser, @PathVariable String username) throws UserNotFoundException { + if (Objects.equals(username, "@me")) { return new UserDTO(authenticatedUser, true); } - return new UserDTO(usersService.getUser(userKey), false); + return new UserDTO(usersService.getUser(username), false); } @Operation(summary = "Edit user") diff --git a/foxogram-api/src/main/java/su/foxogram/services/MessagesService.java b/foxogram-api/src/main/java/su/foxogram/services/MessagesService.java index 7443382..537e0de 100644 --- a/foxogram-api/src/main/java/su/foxogram/services/MessagesService.java +++ b/foxogram-api/src/main/java/su/foxogram/services/MessagesService.java @@ -13,7 +13,6 @@ import su.foxogram.models.Message; import su.foxogram.models.User; import su.foxogram.repositories.MessageRepository; -import su.foxogram.structures.Snowflake; import java.util.List; import java.util.Objects; @@ -37,16 +36,15 @@ public List getMessages(long before, int limit, Channel channel) { return messagesArray.stream() .map(message -> new MessageDTO( - message.getId(), message.getContent(), - message.getAuthor().getUserId(), - message.getChannel().getId(), + message.getAuthor().getUser().getUsername(), + message.getChannel().getName(), message.getAttachments() )) .collect(Collectors.toList()); } - public Message getMessage(String id, Channel channel) throws MessageNotFoundException { + public Message getMessage(long id, Channel channel) throws MessageNotFoundException { Message message = messageRepository.findByChannelAndId(channel, id); if (message == null) throw new MessageNotFoundException(); @@ -57,35 +55,34 @@ public Message getMessage(String id, Channel channel) throws MessageNotFoundExce } public void addMessage(Channel channel, User user, MessageCreateDTO body) { - String id = Snowflake.create(); - String authorId = user.getId(); + long authorId = user.getId(); long timestamp = System.currentTimeMillis(); List attachments = body.getAttachments(); String content = body.getContent(); - Message message = new Message(id, channel, content, authorId, timestamp, attachments); + Message message = new Message(0, channel, content, authorId, timestamp, attachments); messageRepository.save(message); - log.info("MESSAGE ({}) to CHANNEL ({}) saved to database successfully", id, channel.getId()); + log.info("MESSAGE ({}) to CHANNEL ({}) saved to database successfully", message.getId(), channel.getId()); } - public void deleteMessage(String id, Member member, Channel channel) throws MessageNotFoundException, MissingPermissionsException { + public void deleteMessage(long id, Member member, Channel channel) throws MessageNotFoundException, MissingPermissionsException { Message message = messageRepository.findByChannelAndId(channel, id); if (message == null) throw new MessageNotFoundException(); - if (!Objects.equals(message.getAuthor().getUserId(), member.getUserId()) || member.hasAnyPermission(MemberConstants.Permissions.ADMIN, MemberConstants.Permissions.MANAGE_MESSAGES)) + if (!Objects.equals(message.getAuthor().getUsername(), member.getUsername()) || member.hasAnyPermission(MemberConstants.Permissions.ADMIN, MemberConstants.Permissions.MANAGE_MESSAGES)) throw new MissingPermissionsException(); messageRepository.delete(message); log.info("MESSAGE ({}) in CHANNEL ({}) deleted successfully", id, channel.getId()); } - public Message editMessage(String id, Channel channel, Member member, MessageCreateDTO body) throws MessageNotFoundException, MissingPermissionsException { + public Message editMessage(long id, Channel channel, Member member, MessageCreateDTO body) throws MessageNotFoundException, MissingPermissionsException { Message message = messageRepository.findByChannelAndId(channel, id); String content = body.getContent(); if (message == null) throw new MessageNotFoundException(); - if (!Objects.equals(message.getAuthor().getUserId(), member.getUserId())) + if (!Objects.equals(message.getAuthor().getUsername(), member.getUsername())) throw new MissingPermissionsException(); message.setContent(content); diff --git a/foxogram-api/src/main/java/su/foxogram/services/UsersService.java b/foxogram-api/src/main/java/su/foxogram/services/UsersService.java index b01c2a6..6643fdc 100644 --- a/foxogram-api/src/main/java/su/foxogram/services/UsersService.java +++ b/foxogram-api/src/main/java/su/foxogram/services/UsersService.java @@ -16,8 +16,6 @@ import su.foxogram.util.CodeGenerator; import su.foxogram.util.Encryptor; -import java.util.Optional; - @Slf4j @Service public class UsersService { @@ -34,12 +32,12 @@ public UsersService(UserRepository userRepository, EmailService emailService, Co this.codeRepository = codeRepository; } - public User getUser(String key) throws UserNotFoundException { - Optional optionalUser = userRepository.findByIdOrUsername(key, key); + public User getUser(String username) throws UserNotFoundException { + User user = userRepository.findByUsername(username); - if (optionalUser.isEmpty()) throw new UserNotFoundException(); + if (user == null) throw new UserNotFoundException(); - return optionalUser.get(); + return user; } public User editUser(User user, UserEditDTO body) throws UserCredentialsDuplicateException { diff --git a/foxogram-common/src/main/java/su/foxogram/dtos/response/ChannelDTO.java b/foxogram-common/src/main/java/su/foxogram/dtos/response/ChannelDTO.java index df47381..5b897ad 100644 --- a/foxogram-common/src/main/java/su/foxogram/dtos/response/ChannelDTO.java +++ b/foxogram-common/src/main/java/su/foxogram/dtos/response/ChannelDTO.java @@ -7,18 +7,16 @@ @Getter @Setter public class ChannelDTO { - private String id; private String name; private int type; - private String ownerId; + private String owner; public ChannelDTO(Channel channel) { - this.id = channel.getId(); this.name = channel.getName(); this.type = channel.getType(); - this.ownerId = channel.getOwnerId(); + this.owner = channel.getOwner(); } } diff --git a/foxogram-common/src/main/java/su/foxogram/dtos/response/MemberDTO.java b/foxogram-common/src/main/java/su/foxogram/dtos/response/MemberDTO.java index 4be4b99..b546bf2 100644 --- a/foxogram-common/src/main/java/su/foxogram/dtos/response/MemberDTO.java +++ b/foxogram-common/src/main/java/su/foxogram/dtos/response/MemberDTO.java @@ -7,15 +7,12 @@ @Getter @Setter public class MemberDTO { - private String userId; - - private String channelId; + private String channelName; private long permissions; public MemberDTO(Member member) { - this.userId = member.getUserId(); - this.channelId = member.getChannel().getId(); + this.channelName = member.getChannel().getName(); this.permissions = member.getPermissions(); } } diff --git a/foxogram-common/src/main/java/su/foxogram/dtos/response/MessageDTO.java b/foxogram-common/src/main/java/su/foxogram/dtos/response/MessageDTO.java index 39f49f9..678a8a3 100644 --- a/foxogram-common/src/main/java/su/foxogram/dtos/response/MessageDTO.java +++ b/foxogram-common/src/main/java/su/foxogram/dtos/response/MessageDTO.java @@ -8,21 +8,19 @@ @Getter @Setter public class MessageDTO { - private String id; private String content; - private String authorId; + private String authorUsername; - private String channelId; + private String channelName; private List attachments; - public MessageDTO(String id, String content, String authorId, String channelId, List attachments) { - this.id = id; + public MessageDTO(String content, String authorUsername, String channelName, List attachments) { this.content = content; - this.authorId = authorId; - this.channelId = channelId; + this.authorUsername = authorUsername; + this.channelName = channelName; this.attachments = attachments; } } diff --git a/foxogram-common/src/main/java/su/foxogram/dtos/response/MessagesDTO.java b/foxogram-common/src/main/java/su/foxogram/dtos/response/MessagesDTO.java index a2177dc..22cd558 100644 --- a/foxogram-common/src/main/java/su/foxogram/dtos/response/MessagesDTO.java +++ b/foxogram-common/src/main/java/su/foxogram/dtos/response/MessagesDTO.java @@ -13,7 +13,7 @@ public class MessagesDTO { public MessagesDTO(List messages) { for (Message message : messages) { - this.messages.add(new MessageDTO(message.getId(), message.getContent(), message.getAuthor().getUserId(), message.getChannel().getId(), message.getAttachments())); + this.messages.add(new MessageDTO(message.getContent(), message.getAuthor().getUser().getUsername(), message.getChannel().getName(), message.getAttachments())); } } } diff --git a/foxogram-common/src/main/java/su/foxogram/dtos/response/UserDTO.java b/foxogram-common/src/main/java/su/foxogram/dtos/response/UserDTO.java index 20aa1b9..bf96a12 100644 --- a/foxogram-common/src/main/java/su/foxogram/dtos/response/UserDTO.java +++ b/foxogram-common/src/main/java/su/foxogram/dtos/response/UserDTO.java @@ -8,7 +8,6 @@ @Getter @Setter public class UserDTO { - private String id; private String avatar; @@ -24,7 +23,6 @@ public class UserDTO { private long type; public UserDTO(User user, boolean includeEmail) { - this.id = user.getId(); this.avatar = user.getAvatar(); this.displayName = user.getDisplayName(); this.username = user.getUsername(); diff --git a/foxogram-common/src/main/java/su/foxogram/interceptors/ChannelInterceptor.java b/foxogram-common/src/main/java/su/foxogram/interceptors/ChannelInterceptor.java index e7d6037..8a8b8bb 100644 --- a/foxogram-common/src/main/java/su/foxogram/interceptors/ChannelInterceptor.java +++ b/foxogram-common/src/main/java/su/foxogram/interceptors/ChannelInterceptor.java @@ -29,9 +29,9 @@ public ChannelInterceptor(ChannelsService channelsService) { public boolean preHandle(@NotNull HttpServletRequest request, @NotNull HttpServletResponse response, @NotNull Object handler) throws ChannelNotFoundException { Map uriVariables = (Map) getUriVariables(request); - String channelId = getChannelId(uriVariables).orElseThrow(ChannelNotFoundException::new); + String channelName = getChannelName(uriVariables).orElseThrow(ChannelNotFoundException::new); - request.setAttribute(AttributesConstants.CHANNEL, channelsService.getChannel(channelId)); + request.setAttribute(AttributesConstants.CHANNEL, channelsService.getChannel(channelName)); return true; } @@ -43,11 +43,11 @@ public boolean preHandle(@NotNull HttpServletRequest request, @NotNull HttpServl .orElseGet(Collections::emptyMap); } - private Optional getChannelId(Map uriVariables) { - String channelIdString = uriVariables.get("id"); + private Optional getChannelName(Map uriVariables) { + String channelName = uriVariables.get("name"); try { - return Optional.ofNullable(channelIdString); + return Optional.ofNullable(channelName); } catch (NumberFormatException e) { return Optional.empty(); } diff --git a/foxogram-common/src/main/java/su/foxogram/interceptors/MemberInterceptor.java b/foxogram-common/src/main/java/su/foxogram/interceptors/MemberInterceptor.java index abff7b7..1224685 100644 --- a/foxogram-common/src/main/java/su/foxogram/interceptors/MemberInterceptor.java +++ b/foxogram-common/src/main/java/su/foxogram/interceptors/MemberInterceptor.java @@ -29,9 +29,7 @@ public boolean preHandle(HttpServletRequest request, @NotNull HttpServletRespons User user = (User) request.getAttribute(AttributesConstants.USER); Channel channel = (Channel) request.getAttribute(AttributesConstants.CHANNEL); - Member member = channelsService.getMember(channel, user.getId()); - - if (member == null) throw new ChannelNotFoundException(); + Member member = channelsService.getMember(channel, user.getUsername()); request.setAttribute("member", member); return true; diff --git a/foxogram-common/src/main/java/su/foxogram/models/Channel.java b/foxogram-common/src/main/java/su/foxogram/models/Channel.java index b5f7ead..883e459 100644 --- a/foxogram-common/src/main/java/su/foxogram/models/Channel.java +++ b/foxogram-common/src/main/java/su/foxogram/models/Channel.java @@ -15,7 +15,8 @@ }) public class Channel { @Id - public String id; + @GeneratedValue(strategy = GenerationType.IDENTITY) + public long id; @Column() public String name; @@ -24,7 +25,7 @@ public class Channel { public int type; @Column() - public String ownerId; + public String owner; @OneToMany(mappedBy = "channel", cascade = CascadeType.ALL, orphanRemoval = true) private List members; @@ -35,10 +36,10 @@ public class Channel { public Channel() { } - public Channel(String id, String name, int type, String ownerId) { + public Channel(long id, String name, int type, String owner) { this.id = id; this.name = name; this.type = type; - this.ownerId = ownerId; + this.owner = owner; } } diff --git a/foxogram-common/src/main/java/su/foxogram/models/Code.java b/foxogram-common/src/main/java/su/foxogram/models/Code.java index f4825b0..76d2550 100644 --- a/foxogram-common/src/main/java/su/foxogram/models/Code.java +++ b/foxogram-common/src/main/java/su/foxogram/models/Code.java @@ -13,7 +13,7 @@ }) public class Code { @Id() - public String userId; + public long userId; @Column() public String type; @@ -30,7 +30,7 @@ public class Code { public Code() { } - public Code(String userId, String type, String value, long issuedAt, long expiresAt) { + public Code(long userId, String type, String value, long issuedAt, long expiresAt) { this.userId = userId; this.type = type; this.value = value; diff --git a/foxogram-common/src/main/java/su/foxogram/models/Member.java b/foxogram-common/src/main/java/su/foxogram/models/Member.java index de26696..f84a211 100644 --- a/foxogram-common/src/main/java/su/foxogram/models/Member.java +++ b/foxogram-common/src/main/java/su/foxogram/models/Member.java @@ -12,31 +12,34 @@ @Index(name = "idx_member_user_channel_id", columnList = "user_id, channel") }) public class Member { + @Id + public long userId; + + public String username; + @Column() public long permissions; @MapsId - @ManyToOne + @ManyToOne() @JoinColumn(name = "user_id", nullable = false) private User user; - @ManyToOne + @ManyToOne() @JoinColumn(name = "channel", nullable = false) private Channel channel; - @Id - private String userId; - public Member() { } - public Member(String userId) { + public Member(long userId) { this.userId = userId; } public Member(User user, Channel channel, long permissions) { this.userId = user.getId(); + this.username = user.getUsername(); this.user = user; this.channel = channel; this.permissions = permissions; diff --git a/foxogram-common/src/main/java/su/foxogram/models/Message.java b/foxogram-common/src/main/java/su/foxogram/models/Message.java index 6b922c7..4c4742e 100644 --- a/foxogram-common/src/main/java/su/foxogram/models/Message.java +++ b/foxogram-common/src/main/java/su/foxogram/models/Message.java @@ -12,11 +12,12 @@ @Table(name = "messages", indexes = { @Index(name = "idx_message_id", columnList = "id", unique = true), @Index(name = "idx_message_channel", columnList = "channel", unique = true), - @Index(name = "idx_message__id_channel_id", columnList = "id, channel", unique = true) + @Index(name = "idx_message_id_channel_id", columnList = "id, channel", unique = true) }) public class Message { @Id - public String id; + @GeneratedValue(strategy = GenerationType.IDENTITY) + public long id; @Column() public String content; @@ -40,7 +41,7 @@ public class Message { public Message() { } - public Message(String id, Channel channel, String content, String authorId, long timestamp, List attachments) { + public Message(long id, Channel channel, String content, long authorId, long timestamp, List attachments) { this.id = id; this.channel = channel; this.author = new Member(authorId); diff --git a/foxogram-common/src/main/java/su/foxogram/models/User.java b/foxogram-common/src/main/java/su/foxogram/models/User.java index 00154c7..a4bc51d 100644 --- a/foxogram-common/src/main/java/su/foxogram/models/User.java +++ b/foxogram-common/src/main/java/su/foxogram/models/User.java @@ -15,7 +15,8 @@ }) public class User { @Id - public String id; + @GeneratedValue(strategy = GenerationType.IDENTITY) + public long id; @Column() public String displayName; @@ -47,7 +48,7 @@ public class User { public User() { } - public User(String id, String avatar, String displayName, String username, String email, String password, long flags, int type, long deletion, String key) { + public User(long id, String avatar, String displayName, String username, String email, String password, long flags, int type, long deletion, String key) { this.id = id; this.avatar = avatar; this.displayName = displayName; diff --git a/foxogram-common/src/main/java/su/foxogram/repositories/ChannelRepository.java b/foxogram-common/src/main/java/su/foxogram/repositories/ChannelRepository.java index a4a6341..c730a3c 100644 --- a/foxogram-common/src/main/java/su/foxogram/repositories/ChannelRepository.java +++ b/foxogram-common/src/main/java/su/foxogram/repositories/ChannelRepository.java @@ -8,9 +8,13 @@ import java.util.Optional; @Repository -public interface ChannelRepository extends CrudRepository { +public interface ChannelRepository extends CrudRepository { @NotNull - Optional findById(@NotNull String id); + Optional findById(long id); + + Channel findByName(String name); + + Channel findByIdOrName(long id, String name); @Override void delete(@NotNull Channel channel); diff --git a/foxogram-common/src/main/java/su/foxogram/repositories/CodeRepository.java b/foxogram-common/src/main/java/su/foxogram/repositories/CodeRepository.java index cd5a7f6..1925878 100644 --- a/foxogram-common/src/main/java/su/foxogram/repositories/CodeRepository.java +++ b/foxogram-common/src/main/java/su/foxogram/repositories/CodeRepository.java @@ -6,8 +6,8 @@ import su.foxogram.models.Code; @Repository -public interface CodeRepository extends CrudRepository { - Code findByUserId(String userId); +public interface CodeRepository extends CrudRepository { + Code findByUserId(long userId); Code findByValue(String value); diff --git a/foxogram-common/src/main/java/su/foxogram/repositories/MemberRepository.java b/foxogram-common/src/main/java/su/foxogram/repositories/MemberRepository.java index 45d9c20..d473057 100644 --- a/foxogram-common/src/main/java/su/foxogram/repositories/MemberRepository.java +++ b/foxogram-common/src/main/java/su/foxogram/repositories/MemberRepository.java @@ -7,13 +7,17 @@ import org.springframework.stereotype.Repository; import su.foxogram.models.Channel; import su.foxogram.models.Member; +import su.foxogram.models.User; import java.util.List; @Repository -public interface MemberRepository extends CrudRepository { - @Query(value = "SELECT m FROM Member m WHERE m.channel = :ch AND m.user.id = :id") - Member findByChannelAndId(@Param("ch") Channel channel, @Param("id") String id); +public interface MemberRepository extends CrudRepository { + @Query(value = "SELECT m FROM Member m WHERE m.channel = :ch AND m.user = :usr") + Member findByChannelAndUser(@Param("ch") Channel channel, @Param("usr") User user); + + @Query(value = "SELECT m FROM Member m WHERE m.channel = :ch AND m.username = :username") + Member findByChannelAndUsername(@Param("ch") Channel channel, @Param("username") String username); List findAllByChannel(Channel channel); diff --git a/foxogram-common/src/main/java/su/foxogram/repositories/MessageRepository.java b/foxogram-common/src/main/java/su/foxogram/repositories/MessageRepository.java index e4ce24f..d30a84b 100644 --- a/foxogram-common/src/main/java/su/foxogram/repositories/MessageRepository.java +++ b/foxogram-common/src/main/java/su/foxogram/repositories/MessageRepository.java @@ -11,11 +11,11 @@ import java.util.List; @Repository -public interface MessageRepository extends CrudRepository { +public interface MessageRepository extends CrudRepository { List findAllByChannel(Channel channel); @Query("SELECT m FROM Message m WHERE m.channel = :ch AND m.id = :id") - Message findByChannelAndId(@Param("ch") Channel channel, @Param("id") String id); + Message findByChannelAndId(@Param("ch") Channel channel, @Param("id") long id); @Override void delete(@NotNull Message message); diff --git a/foxogram-common/src/main/java/su/foxogram/repositories/UserRepository.java b/foxogram-common/src/main/java/su/foxogram/repositories/UserRepository.java index c612d88..4f24bdd 100644 --- a/foxogram-common/src/main/java/su/foxogram/repositories/UserRepository.java +++ b/foxogram-common/src/main/java/su/foxogram/repositories/UserRepository.java @@ -8,11 +8,11 @@ import java.util.Optional; @Repository -public interface UserRepository extends CrudRepository { +public interface UserRepository extends CrudRepository { @NotNull - Optional findById(@NotNull String id); + Optional findById(long id); - Optional findByIdOrUsername(String id, String username); + Optional findByIdOrUsername(long id, String username); Optional findByEmail(String email); diff --git a/foxogram-common/src/main/java/su/foxogram/services/AuthenticationService.java b/foxogram-common/src/main/java/su/foxogram/services/AuthenticationService.java index 62c195f..0d2b20d 100644 --- a/foxogram-common/src/main/java/su/foxogram/services/AuthenticationService.java +++ b/foxogram-common/src/main/java/su/foxogram/services/AuthenticationService.java @@ -15,7 +15,6 @@ import su.foxogram.models.User; import su.foxogram.repositories.CodeRepository; import su.foxogram.repositories.UserRepository; -import su.foxogram.structures.Snowflake; import su.foxogram.util.CodeGenerator; import su.foxogram.util.Encryptor; @@ -56,13 +55,12 @@ public String userSignUp(String username, String email, String password) throws } private User createUser(String username, String email, String password) { - String id = Snowflake.create(); long deletion = 0; String avatar = new Avatar("").getId(); long flags = UserConstants.Flags.AWAITING_CONFIRMATION.getBit(); int type = UserConstants.Type.USER.getType(); - return new User(id, avatar, null, username, email, Encryptor.hashPassword(password), flags, type, deletion, null); + return new User(0, avatar, null, username, email, Encryptor.hashPassword(password), flags, type, deletion, null); } private void sendConfirmationEmail(User user) { diff --git a/foxogram-common/src/main/java/su/foxogram/services/ChannelsService.java b/foxogram-common/src/main/java/su/foxogram/services/ChannelsService.java index e1f20ee..a09af76 100644 --- a/foxogram-common/src/main/java/su/foxogram/services/ChannelsService.java +++ b/foxogram-common/src/main/java/su/foxogram/services/ChannelsService.java @@ -15,7 +15,6 @@ import su.foxogram.models.User; import su.foxogram.repositories.ChannelRepository; import su.foxogram.repositories.MemberRepository; -import su.foxogram.structures.Snowflake; import java.util.List; @@ -33,10 +32,9 @@ public ChannelsService(ChannelRepository channelRepository, MemberRepository mem } public Channel createChannel(User user, int type, String name) { - String id = Snowflake.create(); - String ownerId = user.getId(); + String owner = user.getUsername(); - Channel channel = new Channel(id, name, type, ownerId); + Channel channel = new Channel(0, name, type, owner); channelRepository.save(channel); Member member = new Member(user, channel, MemberConstants.Permissions.ADMIN.getBit()); @@ -45,8 +43,12 @@ public Channel createChannel(User user, int type, String name) { return channel; } - public Channel getChannel(String id) throws ChannelNotFoundException { - return channelRepository.findById(id).orElseThrow(ChannelNotFoundException::new); + public Channel getChannel(String name) throws ChannelNotFoundException { + Channel channel = channelRepository.findByName(name); + + if (channel == null) throw new ChannelNotFoundException(); + + return channel; } public Channel editChannel(Member member, Channel channel, ChannelEditDTO body) throws MissingPermissionsException { @@ -61,7 +63,7 @@ public Channel editChannel(Member member, Channel channel, ChannelEditDTO body) } public void deleteChannel(Channel channel, User user) throws MissingPermissionsException { - Member member = memberRepository.findByChannelAndId(channel, user.getId()); + Member member = memberRepository.findByChannelAndUser(channel, user); if (member.hasPermission(MemberConstants.Permissions.ADMIN)) { channelRepository.delete(channel); @@ -71,7 +73,7 @@ public void deleteChannel(Channel channel, User user) throws MissingPermissionsE } public Member joinUser(Channel channel, User user) throws MemberAlreadyInChannelException { - Member member = memberRepository.findByChannelAndId(channel, user.getId()); + Member member = memberRepository.findByChannelAndUsername(channel, user.getId()); if (member != null) throw new MemberAlreadyInChannelException(); @@ -80,11 +82,11 @@ public Member joinUser(Channel channel, User user) throws MemberAlreadyInChannel } public void leaveUser(Channel channel, User user) throws MemberInChannelNotFoundException { - Member member = memberRepository.findByChannelAndId(channel, user.getId()); + Member member = memberRepository.findByChannelAndUser(channel, user); if (member == null) throw new MemberInChannelNotFoundException(); - member = memberRepository.findByChannelAndId(channel, user.getId()); + member = memberRepository.findByChannelAndUser(channel, user); memberRepository.delete(member); } @@ -94,7 +96,7 @@ public List getMembers(Channel channel) { .toList(); } - public Member getMember(Channel channel, String id) { - return memberRepository.findByChannelAndId(channel, id); + public Member getMember(Channel channel, String memberUsername) { + return memberRepository.findByChannelAndUsername(channel, memberUsername); } } diff --git a/foxogram-common/src/main/java/su/foxogram/services/EmailService.java b/foxogram-common/src/main/java/su/foxogram/services/EmailService.java index c76919b..5bc439c 100644 --- a/foxogram-common/src/main/java/su/foxogram/services/EmailService.java +++ b/foxogram-common/src/main/java/su/foxogram/services/EmailService.java @@ -41,7 +41,7 @@ public EmailService(JavaMailSender javaMailSender, ResourceLoader resourceLoader } @Async - public void sendEmail(String to, String id, String type, String username, String digitCode, long issuedAt, long expiresAt, String token) { + public void sendEmail(String to, long id, String type, String username, String digitCode, long issuedAt, long expiresAt, String token) { MimeMessage mimeMessage = javaMailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, StandardCharsets.UTF_8.name()); @@ -91,7 +91,7 @@ private String readHTML() throws IOException { } } - private void saveCode(String id, String type, String digitCode, long issuedAt, long expiresAt) { + private void saveCode(long id, String type, String digitCode, long issuedAt, long expiresAt) { Code code = new Code(id, type, digitCode, issuedAt, expiresAt); codeRepository.save(code); } diff --git a/foxogram-common/src/main/java/su/foxogram/services/JwtService.java b/foxogram-common/src/main/java/su/foxogram/services/JwtService.java index 8640247..2249c0b 100644 --- a/foxogram-common/src/main/java/su/foxogram/services/JwtService.java +++ b/foxogram-common/src/main/java/su/foxogram/services/JwtService.java @@ -30,7 +30,7 @@ public JwtService(UserRepository userRepository, JwtConfig jwtConfig) { } public User getUser(String header, boolean ignoreEmailVerification) throws UserUnauthorizedException, UserEmailNotVerifiedException { - String userId; + long userId; try { Jws claimsJws = Jwts.parserBuilder() @@ -38,7 +38,7 @@ public User getUser(String header, boolean ignoreEmailVerification) throws UserU .build() .parseClaimsJws(header.substring(7)); - userId = claimsJws.getBody().getId(); + userId = Long.parseLong(claimsJws.getBody().getId()); } catch (Exception e) { throw new UserUnauthorizedException(); } @@ -51,12 +51,12 @@ public User getUser(String header, boolean ignoreEmailVerification) throws UserU return userRepository.findById(userId).orElseThrow(UserUnauthorizedException::new); } - public String generate(String id) { + public String generate(long id) { long now = System.currentTimeMillis(); Date expirationDate = new Date(now + TokenConstants.LIFETIME); return Jwts.builder() - .setId(id) + .setId(String.valueOf(id)) .setExpiration(expirationDate) .signWith(getSigningKey()) .compact();