···11+import 'dotenv/config';
22+import { Client, CredentialManager, ok } from '@atcute/client';
33+import * as TID from '@atcute/tid';
44+import { setUpEnv, connectionManager } from '.';
55+66+//You want to make sure this is always the same for the applications you are generating upsert tids for
77+//If you use the same timestamp but a different clock id, you will get different tids
88+const CLOCK_ID = 45;
99+// Main execution function - can be called when running this file directly
1010+export const main = async () => {
1111+ const config = setUpEnv();
1212+ if (!config) return;
1313+ const { rpc, manager } = await connectionManager(config);
1414+ const { handle } = config;
1515+1616+ //reversedHandle to make a fun example lexicon
1717+ const reversedHandle = handle.split('.').reverse().map(word => word).join('.');
1818+ //An activity here is like a workout. running, walking, lifting weights, etc.
1919+ const collection = `${reversedHandle}.feed.activity`
2020+2121+ //A list of activities that may be gotten from your phone or wherever, but you get the whole list everytime
2222+ let activities = []
2323+2424+ // Helper function to upload activities
2525+ const uploadActivities = async (activities: any[], handle: string, collection: string, rpc: Client) => {
2626+ for (const activity of activities) {
2727+ //Creates that unique key from the startTime of the activity so we don't have duplicates
2828+ let rKey = TID.create(activity.startTime.getTime() * 1000, CLOCK_ID);
2929+3030+ await ok(rpc.post('com.atproto.repo.putRecord', {
3131+ input: {
3232+ repo: handle,
3333+ collection,
3434+ rkey: rKey,
3535+ record: activity,
3636+ }
3737+ }));
3838+ console.log(`Uploaded activity with rkey: ${rKey}`);
3939+ }
4040+ }
4141+4242+ //I just finished a run, it's saved to my phone, now uploading it to the PDS
4343+ activities.push({
4444+ $type: collection,
4545+ type: 'run',
4646+ startTime: new Date()
4747+ })
4848+4949+ console.log('You just finished a run. Uploading it to the PDS.');
5050+5151+ //Upload my activities
5252+ await uploadActivities(activities, handle, collection, rpc);
5353+5454+ //Going to generate the key here to show you can find the record from it easily if you have a date
5555+ const rkey = TID.create(activities[0].startTime.getTime() * 1000, CLOCK_ID);
5656+5757+ const activityFromPDS = await ok((rpc as any).get('com.atproto.repo.getRecord', {
5858+ params: {
5959+ repo: handle,
6060+ collection,
6161+ rkey,
6262+ }
6363+ })) as any;
6464+6565+ console.log(`The PDS shows you went on a ${activityFromPDS.value.type} at ${activityFromPDS.value.startTime.toLocaleString()}.`);
6666+6767+ //I decide I want to go on a walk later, so I add it to the list
6868+ activities.push({
6969+ $type: collection,
7070+ type: 'walk',
7171+ startTime: new Date()
7272+ })
7373+7474+ console.log('You just finished a walk. Uploading it to the PDS.');
7575+7676+ //upload the new activities so the newest one can sync
7777+ await uploadActivities(activities, handle, collection, rpc);
7878+7979+ const listResult = await ok((rpc as any).get('com.atproto.repo.listRecords', {
8080+ params: {
8181+ repo: handle,
8282+ collection,
8383+ limit: 10,
8484+ }
8585+ })) as any;
8686+8787+ console.log("Since you did an upsert you should only have 2 records even tho you uploaded 3.")
8888+ console.log(`You have ${listResult.records.length} activities saved in the PDS.`);
8989+ for (const recordIndex in listResult.records){
9090+ const record = listResult.records[recordIndex]
9191+ console.log(`The PDS shows you went on a ${record.value.type} at ${record.value.startTime.toLocaleString()}.`);
9292+ }
9393+};
9494+9595+// Run main function if this file is executed directly
9696+//if (require.main === module) {
9797+// main().catch(console.error);
9898+//}