Schedule posts to Bluesky with Cloudflare workers. skyscheduler.work
cf tool bsky-tool cloudflare bluesky schedule bsky service social-media cloudflare-workers

small cleanup

+18 -16
+2 -2
src/utils/db/data.ts
··· 1 1 import { 2 - and, eq, inArray, 2 + and, eq, inArray, asc, 3 3 lte, ne, notInArray, sql 4 4 } from "drizzle-orm"; 5 5 import { BatchItem } from "drizzle-orm/batch"; ··· 45 45 ) 46 46 )); 47 47 const results = await db.with(postsToMake).select().from(postsToMake) 48 - .where(notInArray(postsToMake.userId, violationUsers)).all(); 48 + .where(notInArray(postsToMake.userId, violationUsers)).orderBy(asc(postsToMake.createdAt)).all(); 49 49 return results.map((item) => createPostObject(item)); 50 50 }; 51 51
+16 -14
src/utils/dbQuery.ts
··· 142 142 } 143 143 } 144 144 145 - // Check to see if this post already exists 146 - let rootPostID = undefined; 147 - let parentPostID = undefined; 145 + // Check to see if this post already exists for thread 146 + let rootPostID:string|undefined = undefined; 147 + let parentPostID:string|undefined = undefined; 148 + let rootPostData: Post|null = null; 148 149 if (uuidValid(rootPost)) { 149 - if (await doesPostExist(c.env, userId, rootPost!)) { 150 - rootPostID = rootPost!; 150 + // returns null if the post doesn't appear on this account 151 + rootPostData = await getPostById(c, rootPost!); 152 + if (rootPostData !== null) { 153 + if (rootPostData.posted) { 154 + return { ok: false, msg: "You cannot make threads off already posted posts"}; 155 + } 156 + rootPostID = rootPostData.rootPost!; 151 157 // If this isn't a direct reply, check directly underneath it 152 158 if (rootPost !== parentPost) { 153 159 if (uuidValid(parentPost)) { ··· 164 170 return { ok: false, msg: "The given root post cannot be found on your account"}; 165 171 } 166 172 } 167 - const isThreadedPost: boolean = (rootPostID !== undefined && parentPostID !== undefined); 173 + const isThreadedPost:boolean = (rootPostID !== undefined && parentPostID !== undefined); 168 174 169 175 // Create repost metadata 170 - let scheduleGUID:string|undefined; 171 - let repostInfo:RepostInfo|undefined; 172 - if (!isThreadedPost) { 173 - scheduleGUID = uuidv4(); 174 - repostInfo = createRepostInfo(scheduleGUID, scheduleDate, false, repostData); 175 - } 176 + const scheduleGUID = (!isThreadedPost) ? uuidv4() : undefined; 177 + const repostInfo = (!isThreadedPost) ? createRepostInfo(scheduleGUID!, scheduleDate, false, repostData) : undefined; 176 178 177 179 // Create the posts 178 180 const postUUID = uuidv4(); ··· 181 183 content, 182 184 uuid: postUUID, 183 185 postNow: makePostNow, 184 - scheduledDate: scheduleDate, 186 + scheduledDate: (!isThreadedPost) ? scheduleDate : new Date(rootPostData?.scheduledDate!), 185 187 /*isThread: isThreadedPost, 186 188 rootPost: rootPostID, 187 189 parentPost: parentPostID,*/ 188 - repostInfo: !isThreadedPost ? [repostInfo!] : [], 190 + repostInfo: (!isThreadedPost) ? [repostInfo!] : [], 189 191 embedContent: embeds, 190 192 contentLabel: label || PostLabel.None, 191 193 userId: userId