From 36ebeb217326c6bc23dedfde1e028ae2286afe02 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sat, 24 Dec 2022 03:28:02 +0300 Subject: Cacheable ActivityPub profiles --- src/main/assets/scripts.js | 37 +++++++++------------- src/main/java/com/juick/SignatureManager.java | 2 ++ src/main/java/com/juick/config/SecurityConfig.java | 2 +- .../java/com/juick/www/api/activity/Profile.java | 3 ++ .../com/juick/www/api/activity/model/Context.java | 3 +- src/main/resources/application.properties | 4 +-- 6 files changed, 25 insertions(+), 26 deletions(-) (limited to 'src/main') diff --git a/src/main/assets/scripts.js b/src/main/assets/scripts.js index bf74cfb5..b204766d 100644 --- a/src/main/assets/scripts.js +++ b/src/main/assets/scripts.js @@ -512,29 +512,22 @@ function addTag(tag) { return false; } -var users = {}; - function fetchUserUri(dataUri, callback) { - if (users[dataUri]) { - callback(users[dataUri]); - } else { - let data = new FormData(); - data.append('uri', dataUri); - fetch('/u/', { - method: 'POST', - body: data - }).then(handleErrors) - .then(response => { - return response.json(); - }) - .then(json => { - users[dataUri] = json; - callback(json); - }) - .catch(e => { - callback({ preferredUsername: dataUri }); - }); - } + let data = new FormData(); + data.append('uri', dataUri); + fetch('/api/u/', { + method: 'POST', + body: data + }).then(handleErrors) + .then(response => { + return response.json(); + }) + .then(json => { + callback(json); + }) + .catch(e => { + callback({ uname: dataUri, uri: dataUri }); + }); } function notificationsCheckPermissions(button) { diff --git a/src/main/java/com/juick/SignatureManager.java b/src/main/java/com/juick/SignatureManager.java index 295909cc..8032a6fa 100644 --- a/src/main/java/com/juick/SignatureManager.java +++ b/src/main/java/com/juick/SignatureManager.java @@ -30,6 +30,7 @@ import com.juick.www.api.webfinger.model.Link; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.cache.annotation.Cacheable; import org.springframework.context.ApplicationEventPublisher; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; @@ -160,6 +161,7 @@ public class SignatureManager { return AnonymousUser.INSTANCE; } + @Cacheable("profiles") public Optional getContext(URI contextUri) { try { Context context = apClient.getForEntity(contextUri, Context.class).getBody(); diff --git a/src/main/java/com/juick/config/SecurityConfig.java b/src/main/java/com/juick/config/SecurityConfig.java index 869a6d06..ad189052 100644 --- a/src/main/java/com/juick/config/SecurityConfig.java +++ b/src/main/java/com/juick/config/SecurityConfig.java @@ -133,7 +133,7 @@ public class SecurityConfig { "/api/skypebotendpoint", "/api/_fblogin", "/api/_vklogin", "/api/_tglogin", "/api/_google", "/api/_applelogin", "/api/signup", - "/api/inbox", "/api/events", + "/api/inbox", "/api/events", "/api/u/", "/api/info/**", "/api/nodeinfo/2.0") .permitAll() diff --git a/src/main/java/com/juick/www/api/activity/Profile.java b/src/main/java/com/juick/www/api/activity/Profile.java index 506f5ace..404e0734 100644 --- a/src/main/java/com/juick/www/api/activity/Profile.java +++ b/src/main/java/com/juick/www/api/activity/Profile.java @@ -55,9 +55,11 @@ import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; +import org.springframework.cache.annotation.CacheEvict; import org.springframework.context.ApplicationEventPublisher; import org.springframework.core.convert.ConversionService; import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @@ -268,6 +270,7 @@ public class Profile { throw new HttpNotFoundException(); } + @CacheEvict(cacheNames = "profiles", key = "{ #visitor.uri }") @PostMapping(value = "/api/inbox", consumes = { Context.LD_JSON_MEDIA_TYPE, Context.ACTIVITYSTREAMS_PROFILE_MEDIA_TYPE }) public ResponseEntity processInbox(@Visitor User visitor, InputStream inboxData) throws Exception { diff --git a/src/main/java/com/juick/www/api/activity/model/Context.java b/src/main/java/com/juick/www/api/activity/model/Context.java index 63f991cc..1eb057a6 100644 --- a/src/main/java/com/juick/www/api/activity/model/Context.java +++ b/src/main/java/com/juick/www/api/activity/model/Context.java @@ -24,6 +24,7 @@ import com.juick.www.api.activity.helpers.LinkValueDeserializer; import com.juick.www.api.activity.model.activities.*; import com.juick.www.api.activity.model.objects.*; +import java.io.Serializable; import java.time.Instant; import java.util.ArrayList; import java.util.Arrays; @@ -59,7 +60,7 @@ import java.util.Map; @JsonSubTypes.Type(value = Group.class, name = "Group"), @JsonSubTypes.Type(value = Organization.class, name = "Organization") }) -public class Context { +public class Context implements Serializable { private List context; private String id; diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 91117c31..594c53ac 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -7,6 +7,6 @@ spring.h2.console.enabled=true spring.datasource.generate-unique-name=false spring.flyway.locations=classpath:db/migration,classpath:db/specific/{vendor} spring.sql.init.platform=h2 -spring.cache.type=none -spring.cache.cache-names=help, usernames, users_by_name, discover, discussions, messages, replies, popular_tags +spring.cache.type=simple +spring.cache.cache-names=help, usernames, users_by_name, discover, discussions, messages, replies, popular_tags, profiles spring.profiles.active=@activatedProperties@ -- cgit v1.2.3