Skip to content

Commit

Permalink
Post decomposition
Browse files Browse the repository at this point in the history
  • Loading branch information
yvasyliev committed Oct 19, 2023
1 parent 943ae06 commit ff8f344
Show file tree
Hide file tree
Showing 29 changed files with 242 additions and 194 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.github.yvasyliev.bots.telegram.RedTelBot;
import com.github.yvasyliev.model.dto.ExternalMessageData;
import com.github.yvasyliev.model.dto.Post;
import com.github.yvasyliev.model.dto.post.PhotoGroupPost;
import com.github.yvasyliev.model.dto.post.Post;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.BeanDefinition;
Expand Down Expand Up @@ -89,7 +90,13 @@ protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {

@Bean
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public Map<Integer, Post> intPostMap(@Value("16") int maxSize) {
public Map<Integer, Post> integerPostMap(@Value("16") int maxSize) {
return synchronizedFixedSizeMap(maxSize);
}

@Bean
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public Map<Integer, PhotoGroupPost> integerPhotoGroupPostMap(@Value("16") int maxSize) {
return synchronizedFixedSizeMap(maxSize);
}

Expand Down
117 changes: 0 additions & 117 deletions src/main/java/com/github/yvasyliev/model/dto/Post.java

This file was deleted.

10 changes: 0 additions & 10 deletions src/main/java/com/github/yvasyliev/model/dto/PostType.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.github.yvasyliev.model.dto.post;

public class GifPost extends MediaPost {
@Override
public String getType() {
return Type.GIF;
}
}
13 changes: 13 additions & 0 deletions src/main/java/com/github/yvasyliev/model/dto/post/MediaPost.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.github.yvasyliev.model.dto.post;

public abstract class MediaPost extends SpoilerablePost {
private String mediaUrl;

public String getMediaUrl() {
return mediaUrl;
}

public void setMediaUrl(String mediaUrl) {
this.mediaUrl = mediaUrl;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.github.yvasyliev.model.dto.post;

import java.util.List;

public class PhotoGroupPost extends SpoilerablePost {
private List<List<String>> photoUrlsPages;

public List<List<String>> getPhotoUrlsPages() {
return photoUrlsPages;
}

public void setPhotoUrlsPages(List<List<String>> photoUrlsPages) {
this.photoUrlsPages = photoUrlsPages;
}

@Override
public String getType() {
return Type.PHOTO_GROUP;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.github.yvasyliev.model.dto.post;

public class PhotoPost extends MediaPost {
@Override
public String getType() {
return Type.PHOTO;
}
}
20 changes: 20 additions & 0 deletions src/main/java/com/github/yvasyliev/model/dto/post/PollPost.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.github.yvasyliev.model.dto.post;

import java.util.List;

public class PollPost extends Post {
private List<String> options;

public List<String> getOptions() {
return options;
}

public void setOptions(List<String> options) {
this.options = options;
}

@Override
public String getType() {
return Type.POLL;
}
}
65 changes: 65 additions & 0 deletions src/main/java/com/github/yvasyliev/model/dto/post/Post.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.github.yvasyliev.model.dto.post;

public abstract class Post implements Comparable<Post> {
private int created;
private String author;
private boolean approved;
private String postUrl;
private String text;

public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}

public abstract String getType();

public int getCreated() {
return created;
}

public void setCreated(int created) {
this.created = created;
}

public String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author = author;
}

public boolean isApproved() {
return approved;
}

public void setApproved(boolean approved) {
this.approved = approved;
}

public String getPostUrl() {
return postUrl;
}

public void setPostUrl(String postUrl) {
this.postUrl = postUrl;
}

@Override
public int compareTo(Post post) {
return Integer.compare(created, post.created);
}

public interface Type {
String GIF = "GIF";
String PHOTO_GROUP = "PHOTO_GROUP";
String PHOTO = "PHOTO";
String POLL = "POLL";
String TEXT = "TEXT";
String VIDEO = "VIDEO";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.github.yvasyliev.model.dto.post;

public abstract class SpoilerablePost extends Post {

private boolean hasSpoiler;

public boolean isHasSpoiler() {
return hasSpoiler;
}

public void setHasSpoiler(boolean hasSpoiler) {
this.hasSpoiler = hasSpoiler;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.github.yvasyliev.model.dto.post;

public class TextPost extends Post {
@Override
public String getType() {
return Type.TEXT;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.github.yvasyliev.model.dto.post;

public class VideoPost extends MediaPost {
@Override
public String getType() {
return Type.VIDEO;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.yvasyliev.model.dto.Post;
import com.github.yvasyliev.model.dto.post.Post;
import com.github.yvasyliev.service.state.StateManager;
import org.hibernate.MappingException;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -30,7 +30,6 @@ public Post deserialize(JsonParser jsonParser, DeserializationContext deserializ
var node = jsonParser.getCodec().readTree(jsonParser);
if (node instanceof JsonNode jsonPost) {
var author = jsonPost.get("author").textValue();
var hasSpoiler = "nsfw".equals(jsonPost.get("thumbnail").textValue());
var created = jsonPost.get("created").intValue();
var postUrl = jsonPost.get("url_overridden_by_dest").textValue();
var blockedAuthors = context.getBean(StateManager.class).getBlockedAuthors();
Expand All @@ -40,7 +39,6 @@ public Post deserialize(JsonParser jsonParser, DeserializationContext deserializ
try {
var optionalPost = postMapper.applyWithException(jsonPost).map(post -> {
post.setAuthor(author);
post.setHasSpoiler(hasSpoiler);
post.setCreated(created);
post.setApproved(blockedAuthors.contains(author));
post.setPostUrl(postUrl);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.github.yvasyliev.service.deserializers.mappers;

import com.fasterxml.jackson.databind.JsonNode;
import com.github.yvasyliev.model.dto.Post;
import com.github.yvasyliev.model.dto.PostType;
import com.github.yvasyliev.model.dto.post.GifPost;
import com.github.yvasyliev.model.dto.post.Post;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

Expand All @@ -23,10 +23,10 @@ public Optional<Post> applyWithException(JsonNode jsonPost) {
.get("source")
.get("url")
.textValue();
var post = new Post();
post.setType(PostType.GIF);
var post = new GifPost();
post.setText(jsonPost.get("title").textValue());
post.setMediaUrl(gifUrl);
post.setHasSpoiler("nsfw".equals(jsonPost.get("thumbnail").textValue()));
return Optional.of(post);
}
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.github.yvasyliev.service.deserializers.mappers;

import com.fasterxml.jackson.databind.JsonNode;
import com.github.yvasyliev.model.dto.PostType;
import com.github.yvasyliev.model.dto.Post;
import com.github.yvasyliev.model.dto.post.PhotoGroupPost;
import com.github.yvasyliev.model.dto.post.Post;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
Expand All @@ -24,10 +24,10 @@ public Optional<Post> applyWithException(JsonNode jsonPost) {
if (jsonPost.has("gallery_data")) {
var photoUrlsPages = extractPhotoUrlsPages(jsonPost);
var title = jsonPost.get("title").textValue();
var post = new Post();
post.setType(PostType.PHOTO_GROUP);
var post = new PhotoGroupPost();
post.setText(title);
post.setPhotoUrlsPages(photoUrlsPages);
post.setHasSpoiler("nsfw".equals(jsonPost.get("thumbnail").textValue()));
return Optional.of(post);
}
return Optional.empty();
Expand Down
Loading

0 comments on commit ff8f344

Please sign in to comment.