A tool for publishing static site markdown to the ATmosphere
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add old testing flow to new file, get it out of the way.

+98
+98
src/test-flow.ts
··· 1 + import 'dotenv/config'; 2 + import { Client, CredentialManager, ok } from '@atcute/client'; 3 + import * as TID from '@atcute/tid'; 4 + import { setUpEnv, connectionManager } from '.'; 5 + 6 + //You want to make sure this is always the same for the applications you are generating upsert tids for 7 + //If you use the same timestamp but a different clock id, you will get different tids 8 + const CLOCK_ID = 45; 9 + // Main execution function - can be called when running this file directly 10 + export const main = async () => { 11 + const config = setUpEnv(); 12 + if (!config) return; 13 + const { rpc, manager } = await connectionManager(config); 14 + const { handle } = config; 15 + 16 + //reversedHandle to make a fun example lexicon 17 + const reversedHandle = handle.split('.').reverse().map(word => word).join('.'); 18 + //An activity here is like a workout. running, walking, lifting weights, etc. 19 + const collection = `${reversedHandle}.feed.activity` 20 + 21 + //A list of activities that may be gotten from your phone or wherever, but you get the whole list everytime 22 + let activities = [] 23 + 24 + // Helper function to upload activities 25 + const uploadActivities = async (activities: any[], handle: string, collection: string, rpc: Client) => { 26 + for (const activity of activities) { 27 + //Creates that unique key from the startTime of the activity so we don't have duplicates 28 + let rKey = TID.create(activity.startTime.getTime() * 1000, CLOCK_ID); 29 + 30 + await ok(rpc.post('com.atproto.repo.putRecord', { 31 + input: { 32 + repo: handle, 33 + collection, 34 + rkey: rKey, 35 + record: activity, 36 + } 37 + })); 38 + console.log(`Uploaded activity with rkey: ${rKey}`); 39 + } 40 + } 41 + 42 + //I just finished a run, it's saved to my phone, now uploading it to the PDS 43 + activities.push({ 44 + $type: collection, 45 + type: 'run', 46 + startTime: new Date() 47 + }) 48 + 49 + console.log('You just finished a run. Uploading it to the PDS.'); 50 + 51 + //Upload my activities 52 + await uploadActivities(activities, handle, collection, rpc); 53 + 54 + //Going to generate the key here to show you can find the record from it easily if you have a date 55 + const rkey = TID.create(activities[0].startTime.getTime() * 1000, CLOCK_ID); 56 + 57 + const activityFromPDS = await ok((rpc as any).get('com.atproto.repo.getRecord', { 58 + params: { 59 + repo: handle, 60 + collection, 61 + rkey, 62 + } 63 + })) as any; 64 + 65 + console.log(`The PDS shows you went on a ${activityFromPDS.value.type} at ${activityFromPDS.value.startTime.toLocaleString()}.`); 66 + 67 + //I decide I want to go on a walk later, so I add it to the list 68 + activities.push({ 69 + $type: collection, 70 + type: 'walk', 71 + startTime: new Date() 72 + }) 73 + 74 + console.log('You just finished a walk. Uploading it to the PDS.'); 75 + 76 + //upload the new activities so the newest one can sync 77 + await uploadActivities(activities, handle, collection, rpc); 78 + 79 + const listResult = await ok((rpc as any).get('com.atproto.repo.listRecords', { 80 + params: { 81 + repo: handle, 82 + collection, 83 + limit: 10, 84 + } 85 + })) as any; 86 + 87 + console.log("Since you did an upsert you should only have 2 records even tho you uploaded 3.") 88 + console.log(`You have ${listResult.records.length} activities saved in the PDS.`); 89 + for (const recordIndex in listResult.records){ 90 + const record = listResult.records[recordIndex] 91 + console.log(`The PDS shows you went on a ${record.value.type} at ${record.value.startTime.toLocaleString()}.`); 92 + } 93 + }; 94 + 95 + // Run main function if this file is executed directly 96 + //if (require.main === module) { 97 + // main().catch(console.error); 98 + //}