mirror of
https://github.com/cheeaun/phanpy.git
synced 2025-02-02 14:16:39 +01:00
Fix follow/unfollow not working for remote accounts
This commit is contained in:
parent
cda169a131
commit
8891e0f01c
1 changed files with 198 additions and 172 deletions
|
@ -1,6 +1,6 @@
|
||||||
import './account.css';
|
import './account.css';
|
||||||
|
|
||||||
import { useEffect, useState } from 'preact/hooks';
|
import { useEffect, useRef, useState } from 'preact/hooks';
|
||||||
|
|
||||||
import { api } from '../utils/api';
|
import { api } from '../utils/api';
|
||||||
import emojifyText from '../utils/emojify-text';
|
import emojifyText from '../utils/emojify-text';
|
||||||
|
@ -17,12 +17,6 @@ import Link from './link';
|
||||||
|
|
||||||
function Account({ account, instance: propInstance, onClose }) {
|
function Account({ account, instance: propInstance, onClose }) {
|
||||||
const { masto, instance, authenticated } = api({ instance: propInstance });
|
const { masto, instance, authenticated } = api({ instance: propInstance });
|
||||||
const {
|
|
||||||
masto: currentMasto,
|
|
||||||
instance: currentInstance,
|
|
||||||
authenticated: currentAuthenticated,
|
|
||||||
} = api();
|
|
||||||
const sameInstance = instance === currentInstance;
|
|
||||||
const [uiState, setUIState] = useState('default');
|
const [uiState, setUIState] = useState('default');
|
||||||
const isString = typeof account === 'string';
|
const isString = typeof account === 'string';
|
||||||
const [info, setInfo] = useState(isString ? null : account);
|
const [info, setInfo] = useState(isString ? null : account);
|
||||||
|
@ -88,89 +82,6 @@ function Account({ account, instance: propInstance, onClose }) {
|
||||||
username,
|
username,
|
||||||
} = info || {};
|
} = info || {};
|
||||||
|
|
||||||
const [relationshipUIState, setRelationshipUIState] = useState('default');
|
|
||||||
const [relationship, setRelationship] = useState(null);
|
|
||||||
const [familiarFollowers, setFamiliarFollowers] = useState([]);
|
|
||||||
useEffect(() => {
|
|
||||||
if (info) {
|
|
||||||
const currentAccount = store.session.get('currentAccount');
|
|
||||||
let accountID;
|
|
||||||
(async () => {
|
|
||||||
if (sameInstance && authenticated) {
|
|
||||||
accountID = id;
|
|
||||||
} else if (!sameInstance && currentAuthenticated) {
|
|
||||||
// Grab this account from my logged-in instance
|
|
||||||
const acctHasInstance = info.acct.includes('@');
|
|
||||||
try {
|
|
||||||
const results = await currentMasto.v2.search({
|
|
||||||
q: acctHasInstance ? info.acct : `${info.username}@${instance}`,
|
|
||||||
type: 'accounts',
|
|
||||||
limit: 1,
|
|
||||||
resolve: true,
|
|
||||||
});
|
|
||||||
console.log('🥏 Fetched account from logged-in instance', results);
|
|
||||||
accountID = results.accounts[0].id;
|
|
||||||
} catch (e) {
|
|
||||||
console.error(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!accountID) return;
|
|
||||||
|
|
||||||
if (currentAccount === accountID) {
|
|
||||||
// It's myself!
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
setRelationshipUIState('loading');
|
|
||||||
setFamiliarFollowers([]);
|
|
||||||
|
|
||||||
const fetchRelationships = currentMasto.v1.accounts.fetchRelationships([
|
|
||||||
accountID,
|
|
||||||
]);
|
|
||||||
const fetchFamiliarFollowers =
|
|
||||||
currentMasto.v1.accounts.fetchFamiliarFollowers(accountID);
|
|
||||||
|
|
||||||
try {
|
|
||||||
const relationships = await fetchRelationships;
|
|
||||||
console.log('fetched relationship', relationships);
|
|
||||||
if (relationships.length) {
|
|
||||||
const relationship = relationships[0];
|
|
||||||
setRelationship(relationship);
|
|
||||||
|
|
||||||
if (!relationship.following) {
|
|
||||||
try {
|
|
||||||
const followers = await fetchFamiliarFollowers;
|
|
||||||
console.log('fetched familiar followers', followers);
|
|
||||||
setFamiliarFollowers(followers[0].accounts.slice(0, 10));
|
|
||||||
} catch (e) {
|
|
||||||
console.error(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
setRelationshipUIState('default');
|
|
||||||
} catch (e) {
|
|
||||||
console.error(e);
|
|
||||||
setRelationshipUIState('error');
|
|
||||||
}
|
|
||||||
})();
|
|
||||||
}
|
|
||||||
}, [info, authenticated]);
|
|
||||||
|
|
||||||
const {
|
|
||||||
following,
|
|
||||||
showingReblogs,
|
|
||||||
notifying,
|
|
||||||
followedBy,
|
|
||||||
blocking,
|
|
||||||
blockedBy,
|
|
||||||
muting,
|
|
||||||
mutingNotifications,
|
|
||||||
requested,
|
|
||||||
domainBlocking,
|
|
||||||
endorsed,
|
|
||||||
} = relationship || {};
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
id="account-container"
|
id="account-container"
|
||||||
|
@ -300,6 +211,119 @@ function Account({ account, instance: propInstance, onClose }) {
|
||||||
</span>
|
</span>
|
||||||
)}
|
)}
|
||||||
</p>
|
</p>
|
||||||
|
<RelatedActions
|
||||||
|
info={info}
|
||||||
|
instance={instance}
|
||||||
|
authenticated={authenticated}
|
||||||
|
/>
|
||||||
|
</main>
|
||||||
|
</>
|
||||||
|
)
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function RelatedActions({ info, instance, authenticated }) {
|
||||||
|
if (!info) return null;
|
||||||
|
const {
|
||||||
|
masto: currentMasto,
|
||||||
|
instance: currentInstance,
|
||||||
|
authenticated: currentAuthenticated,
|
||||||
|
} = api();
|
||||||
|
const sameInstance = instance === currentInstance;
|
||||||
|
|
||||||
|
const [relationshipUIState, setRelationshipUIState] = useState('default');
|
||||||
|
const [relationship, setRelationship] = useState(null);
|
||||||
|
const [familiarFollowers, setFamiliarFollowers] = useState([]);
|
||||||
|
|
||||||
|
const { id, locked } = info;
|
||||||
|
const accountID = useRef(id);
|
||||||
|
|
||||||
|
const {
|
||||||
|
following,
|
||||||
|
showingReblogs,
|
||||||
|
notifying,
|
||||||
|
followedBy,
|
||||||
|
blocking,
|
||||||
|
blockedBy,
|
||||||
|
muting,
|
||||||
|
mutingNotifications,
|
||||||
|
requested,
|
||||||
|
domainBlocking,
|
||||||
|
endorsed,
|
||||||
|
} = relationship || {};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (info) {
|
||||||
|
const currentAccount = store.session.get('currentAccount');
|
||||||
|
let currentID;
|
||||||
|
(async () => {
|
||||||
|
if (sameInstance && authenticated) {
|
||||||
|
currentID = id;
|
||||||
|
} else if (!sameInstance && currentAuthenticated) {
|
||||||
|
// Grab this account from my logged-in instance
|
||||||
|
const acctHasInstance = info.acct.includes('@');
|
||||||
|
try {
|
||||||
|
const results = await currentMasto.v2.search({
|
||||||
|
q: acctHasInstance ? info.acct : `${info.username}@${instance}`,
|
||||||
|
type: 'accounts',
|
||||||
|
limit: 1,
|
||||||
|
resolve: true,
|
||||||
|
});
|
||||||
|
console.log('🥏 Fetched account from logged-in instance', results);
|
||||||
|
currentID = results.accounts[0].id;
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!currentID) return;
|
||||||
|
|
||||||
|
if (currentAccount === currentID) {
|
||||||
|
// It's myself!
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
accountID.current = currentID;
|
||||||
|
|
||||||
|
setRelationshipUIState('loading');
|
||||||
|
setFamiliarFollowers([]);
|
||||||
|
|
||||||
|
const fetchRelationships = currentMasto.v1.accounts.fetchRelationships([
|
||||||
|
currentID,
|
||||||
|
]);
|
||||||
|
const fetchFamiliarFollowers =
|
||||||
|
currentMasto.v1.accounts.fetchFamiliarFollowers(currentID);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const relationships = await fetchRelationships;
|
||||||
|
console.log('fetched relationship', relationships);
|
||||||
|
if (relationships.length) {
|
||||||
|
const relationship = relationships[0];
|
||||||
|
setRelationship(relationship);
|
||||||
|
|
||||||
|
if (!relationship.following) {
|
||||||
|
try {
|
||||||
|
const followers = await fetchFamiliarFollowers;
|
||||||
|
console.log('fetched familiar followers', followers);
|
||||||
|
setFamiliarFollowers(followers[0].accounts.slice(0, 10));
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
setRelationshipUIState('default');
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
setRelationshipUIState('error');
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
}
|
||||||
|
}, [info, authenticated]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
{familiarFollowers?.length > 0 && (
|
{familiarFollowers?.length > 0 && (
|
||||||
<p class="common-followers">
|
<p class="common-followers">
|
||||||
Common followers{' '}
|
Common followers{' '}
|
||||||
|
@ -336,23 +360,29 @@ function Account({ account, instance: propInstance, onClose }) {
|
||||||
disabled={relationshipUIState === 'loading'}
|
disabled={relationshipUIState === 'loading'}
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
setRelationshipUIState('loading');
|
setRelationshipUIState('loading');
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
try {
|
try {
|
||||||
let newRelationship;
|
let newRelationship;
|
||||||
|
|
||||||
if (following || requested) {
|
if (following || requested) {
|
||||||
const yes = confirm(
|
const yes = confirm(
|
||||||
requested
|
requested
|
||||||
? 'Withdraw follow request?'
|
? 'Withdraw follow request?'
|
||||||
: `Unfollow @${info.acct || info.username}?`,
|
: `Unfollow @${info.acct || info.username}?`,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (yes) {
|
if (yes) {
|
||||||
newRelationship =
|
newRelationship = await currentMasto.v1.accounts.unfollow(
|
||||||
await currentMasto.v1.accounts.unfollow(id);
|
accountID.current,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
newRelationship =
|
newRelationship = await currentMasto.v1.accounts.follow(
|
||||||
await currentMasto.v1.accounts.follow(id);
|
accountID.current,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (newRelationship) setRelationship(newRelationship);
|
if (newRelationship) setRelationship(newRelationship);
|
||||||
setRelationshipUIState('default');
|
setRelationshipUIState('default');
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
@ -382,11 +412,7 @@ function Account({ account, instance: propInstance, onClose }) {
|
||||||
</button>
|
</button>
|
||||||
)}
|
)}
|
||||||
</p>
|
</p>
|
||||||
</main>
|
|
||||||
</>
|
</>
|
||||||
)
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue