Skip to content
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

native: sync start fixes #3808

Merged
merged 5 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion apps/tlon-mobile/src/components/AuthenticatedApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import crashlytics from '@react-native-firebase/crashlytics';
import { setCrashReporter, sync } from '@tloncorp/shared';
import { QueryClientProvider, queryClient } from '@tloncorp/shared/dist/api';
import * as logic from '@tloncorp/shared/dist/logic';
import * as store from '@tloncorp/shared/dist/store';
import { ZStack } from '@tloncorp/ui';
import { useEffect } from 'react';

Expand All @@ -26,6 +27,7 @@ function AuthenticatedApp({
const currentUserId = useCurrentUserId();
useNotificationListener(notificationListenerProps);
useDeepLinkListener();
const session = store.useCurrentSession();

useEffect(() => {
configureClient({
Expand All @@ -46,7 +48,12 @@ function AuthenticatedApp({
}, [currentUserId, ship, shipUrl]);

useAppForegrounded(() => {
sync.syncUnreads({ priority: sync.SyncPriority.High });
// only run these updates if we've initialized the session
// (i.e. first startSync has completed)
if (session) {
sync.syncUnreads({ priority: sync.SyncPriority.High });
sync.syncPinnedItems({ priority: sync.SyncPriority.High });
}
});

return (
Expand Down
11 changes: 0 additions & 11 deletions apps/tlon-mobile/src/screens/ChatListScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,6 @@ export default function ChatListScreen(
};
}, [chats]);

const isInitialFocus = useRef(true);

useFocusEffect(
useCallback(() => {
if (!isInitialFocus) {
store.syncPinnedItems({ priority: store.SyncPriority.High });
}
isInitialFocus.current = false;
}, [])
);

const goToDm = useCallback(
async (participants: string[]) => {
const dmChannel = await store.upsertDmChannel({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ CREATE TABLE `groups` (
--> statement-breakpoint
CREATE TABLE `pins` (
`type` text NOT NULL,
`index` integer NOT NULL,
`pin_index` integer NOT NULL,
`item_id` text PRIMARY KEY NOT NULL
);
--> statement-breakpoint
Expand Down
6 changes: 3 additions & 3 deletions packages/shared/src/db/migrations/meta/0000_snapshot.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"version": "5",
"dialect": "sqlite",
"id": "97f10bf1-b7ef-46e2-9186-38eb82d307b2",
"id": "74c8411b-620b-418d-a547-c57f710604a9",
"prevId": "00000000-0000-0000-0000-000000000000",
"tables": {
"activity_events": {
Expand Down Expand Up @@ -1315,8 +1315,8 @@
"notNull": true,
"autoincrement": false
},
"index": {
"name": "index",
"pin_index": {
"name": "pin_index",
"type": "integer",
"primaryKey": false,
"notNull": true,
Expand Down
4 changes: 2 additions & 2 deletions packages/shared/src/db/migrations/meta/_journal.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
{
"idx": 0,
"version": "5",
"when": 1721769919625,
"tag": "0000_swift_yellow_claw",
"when": 1722362512526,
"tag": "0000_outstanding_namora",
"breakpoints": true
}
]
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/src/db/migrations/migrations.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This file is required for Expo/React Native SQLite migrations - https://orm.drizzle.team/quick-sqlite/expo

import journal from './meta/_journal.json';
import m0000 from './0000_swift_yellow_claw.sql';
import m0000 from './0000_outstanding_namora.sql';

export default {
journal,
Expand Down
25 changes: 19 additions & 6 deletions packages/shared/src/db/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2953,12 +2953,25 @@ export const getBucketedActivity = createReadQuery(
export const insertPinnedItems = createWriteQuery(
'insertPinnedItems',
async (pinnedItems: Pin[], ctx: QueryCtx) => {
return withTransactionCtx(ctx, async (txCtx) => {
await txCtx.db.delete($pins);
// users may not have pinned items
if (!pinnedItems.length) return;
await txCtx.db.insert($pins).values(pinnedItems);
});
return withTransactionCtx(
{ ...ctx, meta: { ...ctx.meta, label: 'pins' } },
async (txCtx) => {
// users may not have pinned items
if (!pinnedItems.length) return;
const pinnedItemIds = pinnedItems.map((item) => item.itemId);

await txCtx.db
.insert($pins)
.values(pinnedItems)
.onConflictDoUpdate({
target: $pins.itemId,
set: conflictUpdateSetAll($pins),
});
await txCtx.db
.delete($pins)
.where(notInArray($pins.itemId, pinnedItemIds));
}
);
},
['pins', 'groups']
);
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/src/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ export const pins = sqliteTable(
'pins',
{
type: text('type').$type<PinType>().notNull(),
index: integer('index').notNull(),
index: integer('pin_index').notNull(),
itemId: text('item_id').notNull(),
},
(table) => {
Expand Down
39 changes: 23 additions & 16 deletions packages/shared/src/store/sync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,28 @@ import rawAfterNewestPostData from '../test/channelPostsAfterNewest.json';
import rawContactsData from '../test/contacts.json';
import rawGroupsData from '../test/groups.json';
import rawGroupsInitData from '../test/groupsInit.json';
import rawHeadsData from '../test/heads.json';
import {
getClient,
setScryOutput,
setScryOutputs,
setupDatabaseTestSuite,
} from '../test/helpers';
import { GroupsInit, PagedPosts, PostDataResponse } from '../urbit';
import rawGroupsInit2 from '../test/init.json';
import {
CombinedHeads,
GroupsInit,
PagedPosts,
PostDataResponse,
} from '../urbit';
import { Contact as UrbitContact } from '../urbit/contact';
import { Group as UrbitGroup } from '../urbit/groups';
import {
syncContacts,
syncDms,
syncGroups,
syncInitData,
syncLatestPosts,
syncPinnedItems,
syncPosts,
syncThreadPosts,
Expand All @@ -34,6 +42,8 @@ const channelPostWithRepliesData =
const contactsData = rawContactsData as unknown as Record<string, UrbitContact>;
const groupsData = rawGroupsData as unknown as Record<string, UrbitGroup>;
const groupsInitData = rawGroupsInitData as unknown as GroupsInit;
const groupsInitData2 = rawGroupsInit2 as unknown as GroupsInit;
const headsData = rawHeadsData as unknown as CombinedHeads;

setupDatabaseTestSuite();

Expand Down Expand Up @@ -308,7 +318,6 @@ test('deletes removed posts', async () => {
expect(posts.length).toEqual(0);
});

// TODO: fix once test init data added
test('syncs init data', async () => {
setScryOutput(rawGroupsInitData);
await syncInitData();
Expand All @@ -329,20 +338,18 @@ test('syncs init data', async () => {
groupsInitData.chat.dms.length +
Object.keys(groupsInitData.chat.clubs).length
);
// // TODO: fix once activity integrated
// // const staleChannels = await db.getStaleChannels();
// // expect(staleChannels.slice(0, 10).map((c) => [c.id])).toEqual([
// // ['chat/~bolbex-fogdys/watercooler-4926'],
// // ['chat/~dabben-larbet/hosting-6173'],
// // ['chat/~bolbex-fogdys/tlon-general'],
// // ['chat/~bolbex-fogdys/marcom'],
// // ['heap/~bolbex-fogdys/design-1761'],
// // ['chat/~bitpyx-dildus/interface'],
// // ['chat/~bolbex-fogdys/ops'],
// // ['heap/~dabben-larbet/fanmail-3976'],
// // ['diary/~bolbex-fogdys/bulletins'],
// // ['chat/~nocsyx-lassul/bongtable'],
// // ]);
});

test('syncs last posts', async () => {
latter-bolden marked this conversation as resolved.
Show resolved Hide resolved
setScryOutputs([groupsInitData2, headsData]);
await syncInitData();
await syncLatestPosts();
const chats = await db.getChats();
const NUM_EMPTY_TEST_GROUPS = 6;
const chatsWithLatestPosts = chats.filter((c) => c.lastPost);
expect(chatsWithLatestPosts.length).toEqual(
chats.length - NUM_EMPTY_TEST_GROUPS
);
});

test('syncs thread posts', async () => {
Expand Down
Loading