aboutsummaryrefslogtreecommitdiff
path: root/juick-server/src/main/java/com/juick/server/api/webfinger/Resource.java
blob: 2df895fda6c358b596a2abe7fd86056284ac214f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.juick.server.api.webfinger;

import com.juick.User;
import com.juick.server.api.webfinger.model.Account;
import com.juick.server.api.webfinger.model.Link;
import com.juick.server.util.HttpNotFoundException;
import com.juick.service.UserService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.UriComponentsBuilder;
import rocks.xmpp.addr.Jid;

import javax.inject.Inject;
import java.util.Collections;

@RestController
public class Resource {
    @Inject
    private UserService userService;
    @Value("${web_domain:localhost}")
    private String domain;
    @Value("${ap_base_uri:http://localhost:8080/}")
    private String baseUri;

    @GetMapping("/.well-known/webfinger")
    public Account getWebResource(@RequestParam String resource) {
        if (resource.startsWith("acct:")) {
            Jid account = Jid.of(resource.substring(5));
            if (account.getDomain().equals(domain)) {
                User user = userService.getUserByName(account.getLocal());
                if (!user.isAnonymous()) {
                    UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(baseUri);
                    builder.path(String.format("/u/%s", user.getName()));
                    Link blog = new Link();
                    blog.setRel("self");
                    blog.setType("application/activity+json");
                    blog.setHref(builder.toUriString());
                    Account result = new Account();
                    result.setSubject(resource);
                    result.setLinks(Collections.singletonList(blog));
                    return result;
                }
            }
        }
        throw new HttpNotFoundException();
    }
}