aboutsummaryrefslogtreecommitdiff
path: root/juick-server/src/main/java/com/juick/server/VKWallManager.java
blob: 5c0fee81491f5a3690eb9765eb077e4149abfcdf (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
package com.juick.server;

import com.juick.User;
import com.juick.service.CrosspostService;
import com.juick.service.UserService;
import com.vk.api.sdk.client.TransportClient;
import com.vk.api.sdk.client.VkApiClient;
import com.vk.api.sdk.client.actors.UserActor;
import com.vk.api.sdk.exceptions.ApiException;
import com.vk.api.sdk.exceptions.ClientException;
import com.vk.api.sdk.httpclient.HttpTransportClient;
import com.vk.api.sdk.objects.apps.responses.GetResponse;
import com.vk.api.sdk.objects.wall.responses.PostResponse;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

@Component
public class VKWallManager {
    @Value("${vk_appid}")
    private String VK_APPID;
    @Value("${vk_secret}")
    private String VK_SECRET;
    @Inject
    private UserService userService;
    @Inject
    private CrosspostService crosspostService;
    @Value("${vk_user:juick}")
    private String serviceUsername;
    @Value("${vk_group:-1}")
    private String vkGroup;

    private User serviceUser;
    private  VkApiClient vk;
    private UserActor actor;


    @PostConstruct
    public void init() throws ClientException, ApiException {
        serviceUser = userService.getUserByName(serviceUsername);
        TransportClient transportClient = HttpTransportClient.getInstance();
        vk = new VkApiClient(transportClient);
        Pair<String, String> tokens = crosspostService.getVkTokens(serviceUser.getUid()).orElse(Pair.of("0", "0"));
        actor = new UserActor(Integer.valueOf(tokens.getLeft()), tokens.getRight());
    }
    public void post(String status, String link) throws ClientException, ApiException {
        vk.wall().post(actor)
                .ownerId(Integer.valueOf(vkGroup))
                .fromGroup(true)
                .message(status)
                .attachments(link)
                .execute();
    }
}