Moving derived data accessors out of the @Entity... have created a Bot @Service... and implemented the calls there but I'm unconvinced by this as not sure how we can then use that in the Controller/Model side of the code now. What I want to do is access a Bot's "id" which is derived from its username so needs code to build it. I'm starting to wonder if I should just store some of the derived data in the database but that feels clunky (could be more performant though, hmm..)

This commit is contained in:
Yvan 2025-01-27 00:48:38 +00:00
parent 1518f7597d
commit 614b3a48dd
4 changed files with 74 additions and 22 deletions

View file

@ -21,5 +21,4 @@ public class APProperties {
* Server domain - i.e. https://<domain>/@username
*/
@Getter @Setter private String domain; // = "activitypub.bot";
}

View file

@ -58,7 +58,6 @@ public class Bot {
@Column(nullable=true,unique=false)
@Getter @Setter private String summary; // "<p>A bot written using Java/Spring</p>",
///////////////////////////////////////////////////////////////////////////
// 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;
}
}

View file

@ -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<Bot> botlist = botRepo.findAll();
List<Bot> botlist = botService.findAll();
model.addAttribute("bots", botlist);
return "viewbot";

View file

@ -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<Bot> 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://<domain>/users/<username>
*/
public String getId(Bot bot) {
return getUsersURI() + bot.getUsername();
}
/**
* The "inbox" of a user is a URI of the form: https://<domain>/users/<username>/inbox
*/
public String getInbox(Bot bot) {
return getUsersURI() + bot.getUsername() + "/inbox";
}
/**
* The "outbox" of a user is a URI of the form: https://<domain>/users/<username>/outbox
*/
public String getOutbox(Bot bot) {
return getUsersURI() + bot.getUsername() + "/outbox";
}
}