Files
cinny/src/app/utils/lastActive.ts
T

25 lines
1.0 KiB
TypeScript
Raw Normal View History

/**
* [Gitea #150] "Active now" / "Last active 12 min ago" from presence.
* Returns null when there is nothing honest to say: no presence data, an
* offline user with no timestamp (e.g. someone hiding their online status).
*/
export const describeLastActive = (
presence: { presence: string; active: boolean; lastActiveTs?: number } | undefined,
now = Date.now(),
): string | null => {
if (!presence) return null;
if (presence.presence === 'online' && presence.active) return 'Active now';
const ts = presence.lastActiveTs;
if (!ts || ts <= 0) return null;
const ago = Math.max(0, now - ts);
const min = Math.round(ago / 60_000);
if (min < 1) return 'Active just now';
if (min < 60) return `Last active ${min} min ago`;
const hours = Math.round(min / 60);
if (hours < 24) return `Last active ${hours} ${hours === 1 ? 'hour' : 'hours'} ago`;
const days = Math.round(hours / 24);
if (days === 1) return 'Last active yesterday';
if (days < 30) return `Last active ${days} days ago`;
return 'Last active over a month ago';
};