Repository navigation
fix(people): correct social analytics and activity counts #290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -238,7 +238,9 @@ export default function PersonPage({ handle, profile, profileUnavailable }: Pers | |
| groupIdAliases.has(item.groupId) && | ||
| item.type === 'expense' && | ||
| item.status === 'posted' && | ||
| item.deletedAt === undefined, | ||
| item.deletedAt === undefined && | ||
| Number(item.occurredAt ?? 0) >= startAt && | ||
| Number(item.occurredAt ?? 0) < endAt, | ||
| ) | ||
| .sort((left, right) => Number(right.occurredAt ?? 0) - Number(left.occurredAt ?? 0)) | ||
| : []; | ||
|
|
@@ -285,10 +287,22 @@ export default function PersonPage({ handle, profile, profileUnavailable }: Pers | |
| sharedGroups.length === 1 | ||
| ? `/split/new?groupId=${encodeURIComponent(idOf(sharedGroups[0]!))}` | ||
| : '/split/new'; | ||
| const balanceError = | ||
| rangeState === 'error' || | ||
| Boolean( | ||
| groupsError || | ||
| membersError || | ||
| transactionsError || | ||
| payersError || | ||
| participantsError || | ||
| settlementsError, | ||
| ); | ||
| const balanceLabel = !balanceAvailable | ||
| ? sharedGroups.length | ||
| ? 'Loading balance…' | ||
| : 'No shared groups' | ||
| ? balanceError | ||
| ? 'Balance unavailable' | ||
| : sharedGroups.length | ||
| ? 'Loading balance…' | ||
| : 'No shared groups' | ||
| : bilateralBalance === 0n | ||
| ? 'You’re even' | ||
| : bilateralBalance > 0n | ||
|
|
@@ -444,7 +458,10 @@ export default function PersonPage({ handle, profile, profileUnavailable }: Pers | |
| </Link> | ||
| </> | ||
| ) : null} | ||
| {person.id ? ( | ||
| {person.id && | ||
| (sharedGroups.length > 0 || | ||
| status === 'friends' || | ||
| Boolean(analytics?.conversationId)) ? ( | ||
|
Comment on lines
+461
to
+464
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If Useful? React with 👍 / 👎. |
||
| <Link | ||
| className="profile-icon-button" | ||
| href={`/network/person/${encodeURIComponent(person.id)}`} | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,9 @@ | ||
| import { v } from 'convex/values'; | ||
| import { query } from '../_generated/server'; | ||
| import type { Doc, Id } from '../_generated/dataModel'; | ||
| import { getAnalyticsDayRange } from '../analytics/domain'; | ||
| import { getOptionalUser } from '../shared/auth'; | ||
| import { loadReadPointers, type ChatScope } from '../shared/chat'; | ||
| import { avatarUrlForUser } from '../avatars/helpers'; | ||
|
|
||
| const safeProfile = (user: Doc<'users'>) => ({ | ||
|
|
@@ -348,12 +350,20 @@ | |
| ...friendHigh.map((row) => String(row.userLowId)), | ||
| ...conversationByPeer.keys(), | ||
| ]); | ||
| const candidates = [...peerIds] | ||
| .sort( | ||
| (a, b) => | ||
| (conversationByPeer.get(b)?.updatedAt ?? 0) - | ||
| (conversationByPeer.get(a)?.updatedAt ?? 0) || | ||
| a.localeCompare(b), | ||
| ) | ||
| .slice(0, PEOPLE_HUB_LIMIT * 2); | ||
|
Comment on lines
+353
to
+360
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For accounts with more than 120 peers, this truncates solely by Useful? React with 👍 / 👎. |
||
| const now = Date.now(); | ||
| const rows = await Promise.all( | ||
| [...peerIds].slice(0, PEOPLE_HUB_LIMIT * 2).map(async (rawId) => { | ||
| candidates.map(async (rawId) => { | ||
| const userId = rawId as Id<'users'>; | ||
| const conversation = conversationByPeer.get(rawId); | ||
| const [user, settings, presence, pointer] = await Promise.all([ | ||
| const [user, settings, presence] = await Promise.all([ | ||
| ctx.db.get(userId), | ||
| ctx.db | ||
| .query('userSettings') | ||
|
|
@@ -363,30 +373,43 @@ | |
| .query('presence') | ||
| .withIndex('by_user', (q) => q.eq('userId', userId)) | ||
| .unique(), | ||
| conversation | ||
| ? ctx.db | ||
| .query('chatReadPointers') | ||
| .withIndex('by_scope_user', (q) => | ||
| q | ||
| .eq('scopeType', 'direct') | ||
| .eq('scopeId', conversation._id) | ||
| .eq('userId', actor._id), | ||
| ) | ||
| .unique() | ||
| : Promise.resolve(null), | ||
| ]); | ||
| if (!user || user.deletedAt !== undefined) return null; | ||
| let unreadCount = 0; | ||
| let unreadCapped = false; | ||
| if (conversation) { | ||
| const lastReadAt = pointer?.lastReadAt ?? 0; | ||
| const recent = await ctx.db | ||
| const pointers = await loadReadPointers( | ||
| ctx, | ||
| { type: 'direct', id: conversation._id }, | ||
| [actor._id], | ||
| ); | ||
| const lastReadAt = pointers.get(actor._id) ?? 0; | ||
| const messageQuery = ctx.db | ||
| .query('directMessages') | ||
| .withIndex('by_conversation_createdAt', (q) => | ||
| q.eq('conversationId', conversation._id).gt('createdAt', lastReadAt), | ||
| ) | ||
| .order('desc') | ||
| .take(PEOPLE_HUB_UNREAD_CAP + 1); | ||
| unreadCount = recent.filter((message) => message.senderId !== actor._id).length; | ||
| .order('desc'); | ||
| let cursor: string | null = null; | ||
| let done = false; | ||
| while (!done && !unreadCapped) { | ||
| const page = await messageQuery.paginate({ numItems: 50, cursor }); | ||
|
Check failure on line 396 in convex/social/queries.ts
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Convex permits only one Useful? React with 👍 / 👎. |
||
| for (const message of page.page) { | ||
| const expiresAt = 'expiresAt' in message ? message.expiresAt : undefined; | ||
| if ( | ||
| message.senderId !== actor._id && | ||
| !(typeof expiresAt === 'number' && expiresAt <= now) | ||
| ) { | ||
| unreadCount += 1; | ||
| if (unreadCount > PEOPLE_HUB_UNREAD_CAP) { | ||
| unreadCapped = true; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| cursor = page.continueCursor; | ||
| done = page.isDone; | ||
| } | ||
| } | ||
| const showActive = settings?.showActive ?? true; | ||
| const showLastSeen = settings?.showLastSeen ?? true; | ||
|
|
@@ -396,7 +419,7 @@ | |
| conversationId: conversation?._id ?? null, | ||
| lastMessageAt: conversation?.updatedAt ?? null, | ||
| unreadCount: Math.min(unreadCount, PEOPLE_HUB_UNREAD_CAP), | ||
| unreadCapped: unreadCount > PEOPLE_HUB_UNREAD_CAP, | ||
| unreadCapped, | ||
| active: showActive ? (presence?.activeUntil ?? 0) > now : null, | ||
| lastSeenAt: showLastSeen ? (presence?.lastSeenAt ?? null) : null, | ||
| }; | ||
|
|
@@ -413,27 +436,127 @@ | |
| .slice(0, PEOPLE_HUB_LIMIT); | ||
| }, | ||
| }); | ||
| export const peopleHubUnreadTotal = query({ | ||
| args: {}, | ||
| handler: async (ctx) => { | ||
| const actor = await getOptionalUser(ctx); | ||
| if (!actor || actor.deletedAt !== undefined) return 0; | ||
| const [convLow, convHigh, memberships] = await Promise.all([ | ||
| ctx.db | ||
| .query('directConversations') | ||
| .withIndex('by_pair', (q) => q.eq('userLowId', actor._id)) | ||
| .collect(), | ||
| ctx.db | ||
| .query('directConversations') | ||
| .withIndex('by_high', (q) => q.eq('userHighId', actor._id)) | ||
| .collect(), | ||
| ctx.db | ||
| .query('groupMembers') | ||
| .withIndex('by_user', (q) => q.eq('userId', actor._id)) | ||
| .collect(), | ||
| ]); | ||
| const scopes: ChatScope[] = [ | ||
| ...[...convLow, ...convHigh].map((conversation) => ({ | ||
| type: 'direct' as const, | ||
| id: conversation._id, | ||
| })), | ||
|
Comment on lines
+459
to
+462
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When the other participant has soft-deleted their account, their conversation and messages remain in the database, so this scope construction still includes their unread messages. Useful? React with 👍 / 👎. |
||
| ...memberships.map((membership) => ({ type: 'group' as const, id: membership.groupId })), | ||
| ]; | ||
| const now = Date.now(); | ||
| const counts = await Promise.all( | ||
| scopes.map(async (scope) => { | ||
| const pointers = await loadReadPointers(ctx, scope, [actor._id]); | ||
| const lastReadAt = pointers.get(actor._id) ?? 0; | ||
| const messages = | ||
| scope.type === 'direct' | ||
| ? await ctx.db | ||
| .query('directMessages') | ||
| .withIndex('by_conversation_createdAt', (q) => | ||
| q.eq('conversationId', scope.id).gt('createdAt', lastReadAt), | ||
| ) | ||
| .collect() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When an account has accumulated enough unread messages, this Useful? React with 👍 / 👎. |
||
| : await ctx.db | ||
| .query('groupMessages') | ||
| .withIndex('by_group_createdAt', (q) => | ||
| q.eq('groupId', scope.id).gt('createdAt', lastReadAt), | ||
| ) | ||
| .collect(); | ||
| let unread = 0; | ||
| for (const message of messages) { | ||
| if ( | ||
| message.senderId !== actor._id && | ||
| !( | ||
| 'expiresAt' in message && | ||
| typeof message.expiresAt === 'number' && | ||
| message.expiresAt <= now | ||
| ) | ||
| ) { | ||
| unread += 1; | ||
| } | ||
| } | ||
| return unread; | ||
| }), | ||
| ); | ||
| return counts.reduce((sum, count) => sum + count, 0); | ||
| }, | ||
| }); | ||
|
|
||
| const ACTIVITY_DAYS = 30; | ||
| const DAY_MS = 24 * 60 * 60 * 1000; | ||
| /** Messages read per conversation for analytics at most; older ones fall outside the window. */ | ||
| const ACTIVITY_MESSAGE_LIMIT = 2000; | ||
|
|
||
| function activityDayStarts(now: number, timeZone: string | undefined, offsetMinutes: number) { | ||
| if (timeZone) { | ||
| const current = getAnalyticsDayRange(now, timeZone); | ||
| const starts = new Array<number>(ACTIVITY_DAYS); | ||
| starts[ACTIVITY_DAYS - 1] = current.startAt; | ||
| for (let index = ACTIVITY_DAYS - 2; index >= 0; index -= 1) { | ||
| starts[index] = getAnalyticsDayRange(starts[index + 1]! - 1, timeZone).startAt; | ||
| } | ||
| return { starts, endAt: current.endAt }; | ||
| } | ||
| const offset = offsetMinutes * 60_000; | ||
| const todayStart = Math.floor((now - offset) / DAY_MS) * DAY_MS + offset; | ||
| return { | ||
| starts: Array.from( | ||
| { length: ACTIVITY_DAYS }, | ||
| (_, index) => todayStart - (ACTIVITY_DAYS - index - 1) * DAY_MS, | ||
| ), | ||
| endAt: todayStart + DAY_MS, | ||
| }; | ||
| } | ||
|
|
||
| function activityDayIndex(starts: readonly number[], timestamp: number) { | ||
| let low = 0; | ||
| let high = starts.length; | ||
| while (low < high) { | ||
| const middle = (low + high) >>> 1; | ||
| if (starts[middle]! <= timestamp) low = middle + 1; | ||
| else high = middle; | ||
| } | ||
| return low - 1; | ||
| } | ||
|
|
||
| /** | ||
| * One person's analytics with you: direct messages per day for the last 30 days (sent and | ||
| * received), message mix, your shared groups and friendship status. Only for people you | ||
| * already share a conversation, friendship or group with. | ||
| */ | ||
| export const personAnalytics = query({ | ||
| args: { userId: v.id('users'), tzOffsetMinutes: v.optional(v.number()) }, | ||
| handler: async (ctx, { userId, tzOffsetMinutes = 0 }) => { | ||
| args: { | ||
| userId: v.id('users'), | ||
| tzOffsetMinutes: v.optional(v.number()), | ||
| timeZone: v.optional(v.string()), | ||
| }, | ||
| handler: async (ctx, { userId, tzOffsetMinutes = 0, timeZone }) => { | ||
| const actor = await getOptionalUser(ctx); | ||
| if (!actor || actor.deletedAt !== undefined || actor._id === userId) return null; | ||
| const person = await ctx.db.get(userId); | ||
| if (!person || person.deletedAt !== undefined) return null; | ||
| const lowId = String(actor._id) < String(userId) ? actor._id : userId; | ||
| const highId = lowId === actor._id ? userId : actor._id; | ||
| const [friendship, conversation, actorGroups, personGroups, presence, settings] = | ||
| const [friendship, conversation, actorGroups, personGroups, presence, settings, actorSettings] = | ||
| await Promise.all([ | ||
| ctx.db | ||
| .query('friendships') | ||
|
|
@@ -459,6 +582,10 @@ | |
| .query('userSettings') | ||
| .withIndex('by_user', (q) => q.eq('userId', userId)) | ||
| .unique(), | ||
| ctx.db | ||
| .query('userSettings') | ||
| .withIndex('by_user', (q) => q.eq('userId', actor._id)) | ||
| .unique(), | ||
| ]); | ||
| const viewerGroupIds = new Set(actorGroups.map((row) => String(row.groupId))); | ||
| const sharedGroupIds = personGroups | ||
|
|
@@ -467,26 +594,36 @@ | |
| if (!friendship && !conversation && sharedGroupIds.length === 0) return null; | ||
|
|
||
| const now = Date.now(); | ||
| const offset = tzOffsetMinutes * 60_000; | ||
| const todayStart = Math.floor((now - offset) / DAY_MS) * DAY_MS + offset; | ||
| const windowStart = todayStart - (ACTIVITY_DAYS - 1) * DAY_MS; | ||
| const days = Array.from({ length: ACTIVITY_DAYS }, (_, index) => ({ | ||
| dayStart: windowStart + index * DAY_MS, | ||
| sent: 0, | ||
| received: 0, | ||
| })); | ||
| let validZone: string | undefined; | ||
| for (const candidate of [timeZone, actorSettings?.timezone, actor.timezone]) { | ||
| if (!candidate) continue; | ||
| try { | ||
| new Intl.DateTimeFormat('en-US', { timeZone: candidate }); | ||
| validZone = candidate; | ||
| break; | ||
| } catch { | ||
| continue; | ||
| } | ||
| } | ||
| const { starts, endAt } = activityDayStarts(now, validZone, tzOffsetMinutes); | ||
|
Comment on lines
+597
to
+608
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
All existing web, mobile, and MCP callers provide Useful? React with 👍 / 👎. |
||
| const windowStart = starts[0]!; | ||
| const days = starts.map((dayStart) => ({ dayStart, sent: 0, received: 0 })); | ||
| const mix = { text: 0, image: 0, audio: 0, ai: 0 }; | ||
| let total = 0; | ||
| let totalMessagesCapped = false; | ||
| if (conversation) { | ||
| const recent = await ctx.db | ||
| .query('directMessages') | ||
| .withIndex('by_conversation_createdAt', (q) => | ||
| q.eq('conversationId', conversation._id).gte('createdAt', windowStart), | ||
| q.eq('conversationId', conversation._id).gte('createdAt', windowStart).lt('createdAt', endAt), | ||
| ) | ||
| .take(ACTIVITY_MESSAGE_LIMIT); | ||
| for (const message of recent) { | ||
| const index = Math.floor((message.createdAt - windowStart) / DAY_MS); | ||
| const day = days[index]; | ||
| .order('desc') | ||
| .take(ACTIVITY_MESSAGE_LIMIT + 1); | ||
| totalMessagesCapped = recent.length > ACTIVITY_MESSAGE_LIMIT; | ||
| const includedMessages = Math.min(recent.length, ACTIVITY_MESSAGE_LIMIT); | ||
| for (let index = 0; index < includedMessages; index += 1) { | ||
| const message = recent[index]!; | ||
| const day = days[activityDayIndex(starts, message.createdAt)]; | ||
| if (!day) continue; | ||
| if (message.senderId === actor._id && message.kind !== 'ai') day.sent += 1; | ||
| else day.received += 1; | ||
|
|
@@ -512,6 +649,7 @@ | |
| lastSeenAt: (settings?.showLastSeen ?? true) ? (presence?.lastSeenAt ?? null) : null, | ||
| days, | ||
| totalMessages: total, | ||
| totalMessagesCapped, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For conversations with more than 2,000 messages in the 30-day window, the query now returns Useful? React with 👍 / 👎. |
||
| mix, | ||
| sharedGroups: groups.filter((group) => group !== null), | ||
| }; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The newly added filter uses
startAtandendAt, which are computed once at module initialization. If the SPA remains loaded and a shared expense is created later, or the user returns to this page without a hard reload, itsoccurredAtcan be at or beyond the staleendAt, so it disappears from the “Last 90 days” count and list even though local records contain it. Compute the bounds per page load/render or refresh them when the range is fetched.Useful? React with 👍 / 👎.