vote system

not working :(
This commit is contained in:
gdlbo
2024-02-16 21:38:09 +03:00
parent 9efccb37ad
commit 9aa8a3d45c
3 changed files with 59 additions and 19 deletions
@@ -1,19 +1,23 @@
package com.vk.newsfeed.holders;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import com.vk.core.dialogs.alert.VkAlertDialog;
import com.vk.core.ui.themes.VKThemeHelper;
import com.vk.dto.newsfeed.entries.NewsEntry;
import com.vk.dto.newsfeed.entries.Post;
import com.vk.dto.newsfeed.entries.PromoPost;
import com.vk.extensions.ViewExtKt;
import com.vtosters.lite.R;
import ru.vtosters.lite.utils.AndroidUtils;
import ru.vtosters.lite.utils.newsfeed.NewsFeedFiltersUtils;
import ru.vtosters.sponsorpost.internal.VotesPreferences;
import ru.vtosters.sponsorpost.internal.VotesService;
import ru.vtosters.sponsorpost.utils.PostsPreferences;
public final class AdMarkHolder extends BaseNewsEntryHolder<NewsEntry> {
@@ -42,8 +46,10 @@ public final class AdMarkHolder extends BaseNewsEntryHolder<NewsEntry> {
if (PostsPreferences.isPostAd(ownerId, postId)) {
disclamer = "SponsorPost: Реклама";
this.F.setOnClickListener(listener -> voteDialog(ownerId, postId, this.F.getContext()));
} else if (VotesPreferences.isPostAd(ownerId, postId)) {
disclamer = "SponsorPost: Возможно реклама";
this.F.setOnClickListener(listener -> voteDialog(ownerId, postId, this.F.getContext()));
} else if (NewsFeedFiltersUtils.sponsorFilters(text)) {
disclamer = "SponsorPost: Заблокировано фильтрами";
} else {
@@ -64,4 +70,29 @@ public final class AdMarkHolder extends BaseNewsEntryHolder<NewsEntry> {
this.F.setMinHeight(height);
this.F.setText(disclamer);
}
private static void voteDialog(int ownerId, int postId, Context ctx) {
new VkAlertDialog.Builder(ctx)
.setTitle("SponsorPost")
.setMessage("Этот пост является рекламным?")
.setPositiveButton("Да", ((dialog, which) -> {
int resp = VotesService.ratePost(ownerId, postId, true).optInt("code");
if (resp == 201 || resp == 100) {
AndroidUtils.sendToast("Спасибо за голос!");
} else {
AndroidUtils.sendToast("Ошибка при голосовании");
}
}))
.setNeutralButton("Нет", ((dialog, which) -> {
int resp = VotesService.ratePost(ownerId, postId, false).optInt("code");
if (resp == 201 || resp == 100) {
AndroidUtils.sendToast("Спасибо за голос!");
} else {
AndroidUtils.sendToast("Ошибка при голосовании");
}
}))
.show();
}
}
@@ -29,6 +29,16 @@ public class VotesPreferences {
preferences.edit().putBoolean("sponsortesting", status).apply();
}
public static void setUserToken(String token) {
preferences.edit()
.putString("token", token)
.apply();
}
public static String getUserToken() {
return preferences.getString("token", "");
}
public static void savePosts(List<String> posts) {
preferences.edit()
.putStringSet("posts", new HashSet<>(posts))
@@ -1,5 +1,6 @@
package ru.vtosters.sponsorpost.internal;
import android.util.Log;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
@@ -20,6 +21,7 @@ import java.util.stream.IntStream;
public class VotesService {
private static final OkHttpClient client = new OkHttpClient();
private static final String apiPath = ApiUtils.getURL() + "/votes";
private static final String userPath = ApiUtils.getURL() + "/users";
public static Map<String, List<String>> getPosts(Long date) {
String getFromDate = date > 0 ? ("&date=" + date) : "";
@@ -115,22 +117,19 @@ public class VotesService {
}
}
public static JSONObject ratePost(Long ownerId, Long postId, boolean reportToServer) {
String requestUrl = apiPath + (reportToServer ? "/dislike" : "/like") + "?ownerId=" + ownerId + "&postId=" + postId;
public static JSONObject ratePost(int ownerId, int postId, boolean isAd) {
String token = VotesPreferences.getUserToken() == null || VotesPreferences.getUserToken().isEmpty() ? getVoteToken() : VotesPreferences.getUserToken();
String requestUrl = apiPath + (isAd ? "/dislike" : "/like") + "?ownerId=" + ownerId + "&postId=" + postId + "&token=" + token;
Request request = new Request.a()
.b(requestUrl)
.a("Content-Type", "application/json")
.a();
try (Response response = client.a(request).execute()) {
if (response.h()) {
String encoding = response.a("Content-Encoding");
if (encoding != null && encoding.equals("gzip")) {
return new JSONObject(GzipDecompressor.decompress(response.a().b()));
} else {
return new JSONObject(response.a().g());
}
String encoding = response.a("Content-Encoding");
if (encoding != null && encoding.equals("gzip")) {
return new JSONObject(GzipDecompressor.decompress(response.a().b()));
} else {
throw new RuntimeException("Failed to get vote token: " + response.l());
return new JSONObject(response.a().g());
}
} catch (IOException | JSONException e) {
throw new RuntimeException(e);
@@ -138,7 +137,7 @@ public class VotesService {
}
public static String getVoteToken() {
String requestUrl = apiPath + "/getUserToken" + "?private_key=" + Native.pkey();
String requestUrl = userPath + "/getUserToken" + "?private_key=" + Native.pkey();
Request request = new Request.a()
.b(requestUrl)
.a("Content-Type", "application/json")
@@ -146,16 +145,16 @@ public class VotesService {
.a("Signature", Native.sig())
.a();
try (Response response = client.a(request).execute()) {
if (response.h()) {
String encoding = response.a("Content-Encoding");
if (encoding != null && encoding.equals("gzip")) {
return new JSONObject(GzipDecompressor.decompress(response.a().b())).getString("token");
} else {
return new JSONObject(response.a().g()).getString("token");
}
String encoding = response.a("Content-Encoding");
String userToken;
if (encoding != null && encoding.equals("gzip")) {
userToken = new JSONObject(GzipDecompressor.decompress(response.a().b())).getString("token");
VotesPreferences.setUserToken(userToken);
} else {
throw new RuntimeException("Failed to get vote token: " + response.l());
userToken = new JSONObject(response.a().g()).getString("token");
VotesPreferences.setUserToken(userToken);
}
return userToken;
} catch (IOException | JSONException e) {
throw new RuntimeException(e);
}