mirror of
https://github.com/cheeaun/phanpy.git
synced 2024-11-28 12:18:51 +03:00
Cache account info fetches for 10mins
This commit is contained in:
parent
806ad2c6a2
commit
5481aa12be
1 changed files with 65 additions and 49 deletions
|
@ -1,6 +1,7 @@
|
||||||
import './account-info.css';
|
import './account-info.css';
|
||||||
|
|
||||||
import { Menu, MenuDivider, MenuItem, SubMenu } from '@szhsin/react-menu';
|
import { Menu, MenuDivider, MenuItem, SubMenu } from '@szhsin/react-menu';
|
||||||
|
import mem from 'mem';
|
||||||
import {
|
import {
|
||||||
useCallback,
|
useCallback,
|
||||||
useEffect,
|
useEffect,
|
||||||
|
@ -55,6 +56,63 @@ const MUTE_DURATIONS_LABELS = {
|
||||||
|
|
||||||
const LIMIT = 80;
|
const LIMIT = 80;
|
||||||
|
|
||||||
|
const ACCOUNT_INFO_MAX_AGE = 1000 * 60 * 10; // 10 mins
|
||||||
|
|
||||||
|
function fetchFamiliarFollowers(currentID, masto) {
|
||||||
|
return masto.v1.accounts.familiarFollowers.fetch({
|
||||||
|
id: [currentID],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
const memFetchFamiliarFollowers = mem(fetchFamiliarFollowers, {
|
||||||
|
maxAge: ACCOUNT_INFO_MAX_AGE,
|
||||||
|
});
|
||||||
|
|
||||||
|
async function fetchPostingStats(accountID, masto) {
|
||||||
|
const fetchStatuses = masto.v1.accounts
|
||||||
|
.$select(accountID)
|
||||||
|
.statuses.list({
|
||||||
|
limit: 20,
|
||||||
|
})
|
||||||
|
.next();
|
||||||
|
|
||||||
|
const { value: statuses } = await fetchStatuses;
|
||||||
|
console.log('fetched statuses', statuses);
|
||||||
|
const stats = {
|
||||||
|
total: statuses.length,
|
||||||
|
originals: 0,
|
||||||
|
replies: 0,
|
||||||
|
boosts: 0,
|
||||||
|
};
|
||||||
|
// Categories statuses by type
|
||||||
|
// - Original posts (not replies to others)
|
||||||
|
// - Threads (self-replies + 1st original post)
|
||||||
|
// - Boosts (reblogs)
|
||||||
|
// - Replies (not-self replies)
|
||||||
|
statuses.forEach((status) => {
|
||||||
|
if (status.reblog) {
|
||||||
|
stats.boosts++;
|
||||||
|
} else if (
|
||||||
|
!!status.inReplyToId &&
|
||||||
|
status.inReplyToAccountId !== status.account.id // Not self-reply
|
||||||
|
) {
|
||||||
|
stats.replies++;
|
||||||
|
} else {
|
||||||
|
stats.originals++;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Count days since last post
|
||||||
|
stats.daysSinceLastPost = Math.ceil(
|
||||||
|
(Date.now() - new Date(statuses[statuses.length - 1].createdAt)) / 86400000,
|
||||||
|
);
|
||||||
|
|
||||||
|
console.log('posting stats', stats);
|
||||||
|
return stats;
|
||||||
|
}
|
||||||
|
const memFetchPostingStats = mem(fetchPostingStats, {
|
||||||
|
maxAge: ACCOUNT_INFO_MAX_AGE,
|
||||||
|
});
|
||||||
|
|
||||||
function AccountInfo({
|
function AccountInfo({
|
||||||
account,
|
account,
|
||||||
fetchAccount = () => {},
|
fetchAccount = () => {},
|
||||||
|
@ -208,18 +266,13 @@ function AccountInfo({
|
||||||
const [postingStats, setPostingStats] = useState();
|
const [postingStats, setPostingStats] = useState();
|
||||||
const [postingStatsUIState, setPostingStatsUIState] = useState('default');
|
const [postingStatsUIState, setPostingStatsUIState] = useState('default');
|
||||||
const hasPostingStats = !!postingStats?.total;
|
const hasPostingStats = !!postingStats?.total;
|
||||||
const currentIDRef = useRef();
|
|
||||||
|
|
||||||
const renderFamiliarFollowers = async () => {
|
const renderFamiliarFollowers = async (currentID) => {
|
||||||
if (!currentIDRef.current) return;
|
|
||||||
const currentID = currentIDRef.current;
|
|
||||||
try {
|
try {
|
||||||
const fetchFamiliarFollowers =
|
const followers = await memFetchFamiliarFollowers(
|
||||||
currentMasto.v1.accounts.familiarFollowers.fetch({
|
currentID,
|
||||||
id: [currentID],
|
currentMasto,
|
||||||
});
|
);
|
||||||
|
|
||||||
const followers = await fetchFamiliarFollowers;
|
|
||||||
console.log('fetched familiar followers', followers);
|
console.log('fetched familiar followers', followers);
|
||||||
setFamiliarFollowers(
|
setFamiliarFollowers(
|
||||||
followers[0].accounts.slice(0, FAMILIAR_FOLLOWERS_LIMIT),
|
followers[0].accounts.slice(0, FAMILIAR_FOLLOWERS_LIMIT),
|
||||||
|
@ -232,43 +285,7 @@ function AccountInfo({
|
||||||
const renderPostingStats = async () => {
|
const renderPostingStats = async () => {
|
||||||
setPostingStatsUIState('loading');
|
setPostingStatsUIState('loading');
|
||||||
try {
|
try {
|
||||||
const fetchStatuses = masto.v1.accounts
|
const stats = await memFetchPostingStats(id, masto);
|
||||||
.$select(id)
|
|
||||||
.statuses.list({
|
|
||||||
limit: 20,
|
|
||||||
})
|
|
||||||
.next();
|
|
||||||
|
|
||||||
const { value: statuses } = await fetchStatuses;
|
|
||||||
console.log('fetched statuses', statuses);
|
|
||||||
const stats = {
|
|
||||||
total: statuses.length,
|
|
||||||
originals: 0,
|
|
||||||
replies: 0,
|
|
||||||
boosts: 0,
|
|
||||||
};
|
|
||||||
// Categories statuses by type
|
|
||||||
// - Original posts (not replies to others)
|
|
||||||
// - Threads (self-replies + 1st original post)
|
|
||||||
// - Boosts (reblogs)
|
|
||||||
// - Replies (not-self replies)
|
|
||||||
statuses.forEach((status) => {
|
|
||||||
if (status.reblog) {
|
|
||||||
stats.boosts++;
|
|
||||||
} else if (status.inReplyToAccountId !== id && !!status.inReplyToId) {
|
|
||||||
stats.replies++;
|
|
||||||
} else {
|
|
||||||
stats.originals++;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Count days since last post
|
|
||||||
stats.daysSinceLastPost = Math.ceil(
|
|
||||||
(Date.now() - new Date(statuses[statuses.length - 1].createdAt)) /
|
|
||||||
86400000,
|
|
||||||
);
|
|
||||||
|
|
||||||
console.log('posting stats', stats);
|
|
||||||
setPostingStats(stats);
|
setPostingStats(stats);
|
||||||
setPostingStatsUIState('default');
|
setPostingStatsUIState('default');
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
@ -279,9 +296,8 @@ function AccountInfo({
|
||||||
|
|
||||||
const onRelationshipChange = useCallback(
|
const onRelationshipChange = useCallback(
|
||||||
({ relationship, currentID }) => {
|
({ relationship, currentID }) => {
|
||||||
currentIDRef.current = currentID;
|
|
||||||
if (!relationship.following) {
|
if (!relationship.following) {
|
||||||
renderFamiliarFollowers();
|
renderFamiliarFollowers(currentID);
|
||||||
if (!standalone) {
|
if (!standalone) {
|
||||||
renderPostingStats();
|
renderPostingStats();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue