aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick/service/ActivityPubService.java
blob: e98b1c75806859298a4cbb8fd47087bb3e557b73 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
 * Copyright (C) 2008-2019, Juick
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.juick.service;

import com.juick.model.User;
import com.juick.model.AnonymousUser;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;

import javax.annotation.Nonnull;
import javax.inject.Inject;
import java.util.List;

@Repository
public class ActivityPubService extends BaseJdbcService implements SocialService {
    @Value("${ap_base_uri:http://localhost:8080/}")
    private String baseUri;
    @Inject
    private UserService userService;

    @Transactional(readOnly = true)
    @Override
    public @Nonnull User getUserByAccountUri(String acct) {
        UriComponents baseUriComponents = UriComponentsBuilder.fromUriString(baseUri).build();
        UriComponents acctComponents = UriComponentsBuilder.fromUriString(acct).build();
        if (acctComponents.getHost().equals(baseUriComponents.getHost())) {
            // /u/ugnich -> ugnich
            String userName = acctComponents.getPath().substring(3);
            return userService.getUserByName(userName);
        }
        return AnonymousUser.INSTANCE;
    }

    @Transactional(readOnly = true)
    @Override
    public @Nonnull List<String> getFollowers(User user) {
        return getJdbcTemplate().queryForList("SELECT acct FROM followers WHERE user_id=?", String.class, user.getUid());
    }

    @Transactional
    @Override
    public void addFollower(User user, String acct) {
        getJdbcTemplate().update("INSERT INTO followers(user_id, acct) " +
                "VALUES(?, ?)", user.getUid(), acct);
    }

    @Transactional
    @Override
    public void removeFollower(User user, String acct) {
        getJdbcTemplate().update("DELETE FROM followers WHERE user_id=? AND acct=?", user.getUid(), acct);
    }

    @Transactional
    @Override
    public void removeAccount(String acct) {
        getJdbcTemplate().update("DELETE FROM followers WHERE acct=?", acct);
    }
}