tiefen_space_app/utils/relation.js

217 lines
5.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import baseRequest from "./baseRequest";
import Toast from "react-native-toast-message";
import { generateSignature } from "./crypto";
export async function follow(follower, followed) {
const apiUrl = process.env.EXPO_PUBLIC_API_URL;
try {
const base = await baseRequest();
const signature = await generateSignature({
account_relations: [
{ sub_mid: follower, obj_mid: followed, predicate: 0 },
{ sub_mid: followed, obj_mid: follower, predicate: 1 },
],
...base,
});
const response = await fetch(
`${apiUrl}/api/account_relation/create?signature=${signature}`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
account_relations: [
{ sub_mid: follower, obj_mid: followed, predicate: 0 },
{ sub_mid: followed, obj_mid: follower, predicate: 1 },
],
...base,
}),
}
);
const data = await response.json();
if (data.ret === -1) {
Toast.show({
type: "error",
text1: data.msg,
topOffset: 60,
});
return;
}
} catch (error) {
console.error(error);
}
}
export async function unfollow(unfollower, unfollowed) {
const apiUrl = process.env.EXPO_PUBLIC_API_URL;
try {
const base = await baseRequest();
const signature = await generateSignature({
sentences: [
{ sub_mid: unfollower, obj_mid: unfollowed, predicate: 0 },
{ sub_mid: unfollowed, obj_mid: unfollower, predicate: 1 },
],
...base,
});
const response = await fetch(
`${apiUrl}/api/account_relation/delete?signature=${signature}`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
sentences: [
{ sub_mid: unfollower, obj_mid: unfollowed, predicate: 0 },
{ sub_mid: unfollowed, obj_mid: unfollower, predicate: 1 },
],
...base,
}),
}
);
const data = await response.json();
if (data.ret === -1) {
Toast.show({
type: "error",
text1: data.msg,
topOffset: 60,
});
return;
}
} catch (error) {
console.error(error);
}
}
export async function block(blocker, blocked) {
const apiUrl = process.env.EXPO_PUBLIC_API_URL;
try {
const base = await baseRequest();
const signature = await generateSignature({
account_relations: [
{ sub_mid: blocker, obj_mid: blocked, predicate: 2 },
{ sub_mid: blocked, obj_mid: blocker, predicate: 3 },
],
...base,
});
const response = await fetch(
`${apiUrl}/api/account_relation/create?signature=${signature}`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
account_relations: [
{ sub_mid: blocker, obj_mid: blocked, predicate: 2 },
{ sub_mid: blocked, obj_mid: blocker, predicate: 3 },
],
...base,
}),
}
);
const data = await response.json();
if (data.ret === -1) {
Toast.show({
type: "error",
text1: data.msg,
topOffset: 60,
});
return;
}
Toast.show({
type: "success",
text1: "拉黑成功将不再向您推荐Ta",
topOffset: 60,
});
} catch (error) {
console.error(error);
}
}
export async function unblock(unblocker, unblocked) {
const apiUrl = process.env.EXPO_PUBLIC_API_URL;
try {
const base = await baseRequest();
const signature = await generateSignature({
sentences: [
{ sub_mid: unblocker, obj_mid: unblocked, predicate: 2 },
{ sub_mid: unblocked, obj_mid: unblocker, predicate: 3 },
],
...base,
});
const response = await fetch(
`${apiUrl}/api/account_relation/delete?signature=${signature}`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
sentences: [
{ sub_mid: unblocker, obj_mid: unblocked, predicate: 2 },
{ sub_mid: unblocked, obj_mid: unblocker, predicate: 3 },
],
...base,
}),
}
);
const data = await response.json();
if (data.ret === -1) {
Toast.show({
type: "error",
text1: data.msg,
topOffset: 60,
});
return;
}
} catch (error) {
console.error(error);
}
}
export async function checkRelation(subMid, objMid, predicate) {
const apiUrl = process.env.EXPO_PUBLIC_API_URL;
try {
const base = await baseRequest();
const signature = await generateSignature({
...base,
sub_mid: subMid,
obj_mid: objMid,
predicate: predicate,
});
const response = await fetch(
`${apiUrl}/api/account_relation/list_by_sentence?signature=${signature}`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
...base,
sub_mid: subMid,
obj_mid: objMid,
predicate: predicate,
}),
}
);
const relationData = await response.json();
if (relationData.ret === -1) {
Toast.show({
type: "error",
text1: relationData.msg,
topOffset: 60,
});
return;
}
return relationData.data.is_account_relation_existed;
} catch (error) {
console.error(error);
}
}