diff --git a/Java/Spring/activitypubbot/src/main/java/dev/activitypub/activitypubbot/APProperties.java b/Java/Spring/activitypubbot/src/main/java/dev/activitypub/activitypubbot/APProperties.java index 62f1416..e652d5e 100644 --- a/Java/Spring/activitypubbot/src/main/java/dev/activitypub/activitypubbot/APProperties.java +++ b/Java/Spring/activitypubbot/src/main/java/dev/activitypub/activitypubbot/APProperties.java @@ -21,5 +21,4 @@ public class APProperties { * Server domain - i.e. https://<domain>/@username */ @Getter @Setter private String domain; // = "activitypub.bot"; - } diff --git a/Java/Spring/activitypubbot/src/main/java/dev/activitypub/activitypubbot/Bot.java b/Java/Spring/activitypubbot/src/main/java/dev/activitypub/activitypubbot/Bot.java index aa252de..0267522 100644 --- a/Java/Spring/activitypubbot/src/main/java/dev/activitypub/activitypubbot/Bot.java +++ b/Java/Spring/activitypubbot/src/main/java/dev/activitypub/activitypubbot/Bot.java @@ -58,7 +58,6 @@ public class Bot { @Column(nullable=true,unique=false) @Getter @Setter private String summary; // "

A bot written using Java/Spring

", - /////////////////////////////////////////////////////////////////////////// // The following values will be auto-generated on bot-creation @@ -81,26 +80,8 @@ public class Bot { @Column(nullable=false,unique=false) @Getter private static final boolean indexable = false; - // TODO: should this class have the functions for generating derived values based on config vals? How does it get the config... - - /* these are all just derived from (preferred)Username - the scheme and domain-name should come from config - private String id; // "https://springbot.seth.id.au/users/springbot", - private String url; // "https://springbot.seth.id.au/@springbot", - private String inbox; // "https://springbot.seth.id.au/users/springbot/inbox", - private String publicKeyId; // "https://springbot.seth.id.au/users/springbot#main-key", - */ - public String getId() { - // TODO: is there some sort of 'uribuilder' - probably need our own, lots of uris to build - //return props.getScheme() + "://" + props.getDomain() + "/users/" + this.getUsername(); - return this.getUsername(); - } - - /* this is just a copy of "id" - private String publicKeyOwner; // "https://springap.seth.id.au/users/springbot", - */ - @Override public String toString() { - return "Bot: " + username; + return "Bot: " + this.username; } } diff --git a/Java/Spring/activitypubbot/src/main/java/dev/activitypub/activitypubbot/BotController.java b/Java/Spring/activitypubbot/src/main/java/dev/activitypub/activitypubbot/BotController.java index 3bddeff..e59f7ab 100644 --- a/Java/Spring/activitypubbot/src/main/java/dev/activitypub/activitypubbot/BotController.java +++ b/Java/Spring/activitypubbot/src/main/java/dev/activitypub/activitypubbot/BotController.java @@ -20,11 +20,18 @@ public class BotController { @Autowired private BotRepo botRepo; + private BotService botService; + + @Autowired + public BotController(BotService botService) { + this.botService = botService; + } + @GetMapping("/viewbot") public String listAll(Model model) { log.info("WebHandler::viewbot"); - List botlist = botRepo.findAll(); + List botlist = botService.findAll(); model.addAttribute("bots", botlist); return "viewbot"; diff --git a/Java/Spring/activitypubbot/src/main/java/dev/activitypub/activitypubbot/BotService.java b/Java/Spring/activitypubbot/src/main/java/dev/activitypub/activitypubbot/BotService.java new file mode 100644 index 0000000..ca67746 --- /dev/null +++ b/Java/Spring/activitypubbot/src/main/java/dev/activitypub/activitypubbot/BotService.java @@ -0,0 +1,65 @@ +package dev.activitypub.activitypubbot; + +import org.springframework.beans.BeanUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import java.util.List; + +/** + * The "business logic" and derived data of a Bot. + */ +@Service +public class BotService { + + @Autowired + private BotRepo repo; + + @Autowired + private APProperties props; + + @Transactional + public Bot create(Bot bot) { + // A user must have a key, so we generate and associate one on creation + // https://docs.joinmastodon.org/spec/activitypub/#publicKey + return repo.save(bot); + } + + public List findAll() { + return repo.findAll(); + } + + + // whilst the URI structure of the below are up to the implementor we're using Mastodon as a reference + + // TODO: perhaps these could just be templated elsewhere... or part of a separat JSON generator + + /** + * Generate the users URI + */ + private String getUsersURI() { + return props.getScheme() + "://" + props.getDomain() + "/users/"; + } + + /** + * The"id" of a bot (user/actor) is a URI combining domain and username: https:///users/ + */ + public String getId(Bot bot) { + return getUsersURI() + bot.getUsername(); + } + + /** + * The "inbox" of a user is a URI of the form: https:///users//inbox + */ + public String getInbox(Bot bot) { + return getUsersURI() + bot.getUsername() + "/inbox"; + } + + /** + * The "outbox" of a user is a URI of the form: https:///users//outbox + */ + public String getOutbox(Bot bot) { + return getUsersURI() + bot.getUsername() + "/outbox"; + } +} +