summaryrefslogtreecommitdiffstats
path: root/app/src/main/java/app/fedilab/android/mastodon/viewmodel/mastodon/SearchVM.java
blob: b5f92bb1c862206e9b93e9e132b9d3b77cf52fcb (plain)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package app.fedilab.android.mastodon.viewmodel.mastodon;
/* Copyright 2021 Thomas Schneider
 *
 * This file is a part of Fedilab
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 *
 * Fedilab 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 General
 * Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with Fedilab; if not,
 * see <http://www.gnu.org/licenses>. */

import android.app.Application;
import android.os.Handler;
import android.os.Looper;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.net.IDN;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import app.fedilab.android.mastodon.client.endpoints.MastodonSearchService;
import app.fedilab.android.mastodon.client.entities.api.Results;
import app.fedilab.android.mastodon.client.entities.api.Status;
import app.fedilab.android.mastodon.client.entities.app.StatusCache;
import app.fedilab.android.mastodon.exception.DBException;
import app.fedilab.android.mastodon.helper.Helper;
import okhttp3.OkHttpClient;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class SearchVM extends AndroidViewModel {

    final OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .readTimeout(60, TimeUnit.SECONDS)
            .connectTimeout(60, TimeUnit.SECONDS)
            .callTimeout(60, TimeUnit.SECONDS)
            .proxy(Helper.getProxy(getApplication().getApplicationContext()))
            .build();
    private MutableLiveData<Results> resultsMutableLiveData;

    public SearchVM(@NonNull Application application) {
        super(application);
    }

    private MastodonSearchService init(String instance) {
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").create();
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://" + (instance != null ? IDN.toASCII(instance, IDN.ALLOW_UNASSIGNED) : null) + "/api/v2/")
                .addConverterFactory(GsonConverterFactory.create(Helper.getDateBuilder()))
                .client(okHttpClient)
                .build();
        return retrofit.create(MastodonSearchService.class);
    }


    /**
     * Search for content in accounts, statuses and hashtags with API v2
     *
     * @param instance           Instance domain of the active account
     * @param token              Access token of the active account
     * @param q                  String - search words
     * @param account_id         String - If provided, statuses returned will be authored only by this account
     * @param type               String - Enum(accounts, hashtags, statuses)
     * @param exclude_unreviewed boolean - Filter out unreviewed tags? Defaults to false. Use true when trying to find trending tags.
     * @param resolve            boolean - Attempt WebFinger lookup. Defaults to false.
     * @param following          boolean - Only include accounts that the user is following. Defaults to false.
     * @param offset             int - Offset in search results. Used for pagination. Defaults to 0.
     * @param max_id             String - Return results older than this id
     * @param min_id             String - Return results immediately newer than this id
     * @param limit              int - Maximum number of results to load, per type. Defaults to 20. Max 40.
     * @return {@link LiveData} containing an {@link Results}
     */
    public LiveData<Results> search(@NonNull String instance,
                                    String token,
                                    @NonNull String q,
                                    String account_id,
                                    String type,
                                    Boolean exclude_unreviewed,
                                    Boolean resolve,
                                    Boolean following,
                                    Integer offset,
                                    String max_id,
                                    String min_id,
                                    Integer limit) {
        MastodonSearchService mastodonSearchService = init(instance);
        resultsMutableLiveData = new MutableLiveData<>();
        new Thread(() -> {
            int finalLimit = 40;
            if (limit != null && limit < 40) {
                finalLimit = limit;
            }
            Call<Results> resultsCall = mastodonSearchService.search(
                    token, q, account_id, type, exclude_unreviewed,
                    resolve, following, offset, max_id, min_id, finalLimit);
            Results results = null;

            if (resultsCall != null) {
                try {
                    Response<Results> resultsResponse = resultsCall.execute();
                    if (resultsResponse.isSuccessful()) {
                        results = resultsResponse.body();
                        if (results != null) {
                            if (results.statuses == null) {
                                results.statuses = new ArrayList<>();
                            }
                            if (results.accounts == null) {
                                results.accounts = new ArrayList<>();
                            }
                            if (results.hashtags == null) {
                                results.hashtags = new ArrayList<>();
                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            Handler mainHandler = new Handler(Looper.getMainLooper());
            Results finalResults = results;
            Runnable myRunnable = () -> resultsMutableLiveData.setValue(finalResults);
            mainHandler.post(myRunnable);
        }).start();
        return resultsMutableLiveData;
    }

    public LiveData<Results> searchCache(@NonNull String instance, String userId, @NonNull String q) {
        resultsMutableLiveData = new MutableLiveData<>();
        new Thread(() -> {
            Results results = new Results();
            try {
                results.statuses = new ArrayList<>();
                List<Status> statuses = new StatusCache(getApplication()).searchStatus(instance, userId, q);
                if (statuses != null) {
                    results.statuses.addAll(statuses);
                }
            } catch (DBException e) {
                e.printStackTrace();
            }
            Handler mainHandler = new Handler(Looper.getMainLooper());
            Runnable myRunnable = () -> resultsMutableLiveData.setValue(results);
            mainHandler.post(myRunnable);
        }).start();
        return resultsMutableLiveData;
    }

}