|
| 1 | +/* eslint-disable camelcase */ |
| 2 | + |
| 3 | +import crypto from "crypto"; |
| 4 | + |
| 5 | +import { UserKV } from "./kv"; |
| 6 | +import { range, randomNumber, randomNewNumber } from "./utils"; |
| 7 | +import { getRequestParams } from "./helper"; |
| 8 | + |
| 9 | +import { stringify } from "query-string"; |
| 10 | + |
| 11 | +const baseUri = TWITTER_BASE_URL; |
| 12 | + |
| 13 | +export const getAllRetweets = async (tweetId: string) => { |
| 14 | + if (!tweetId) throw new Error("tweet id is needed"); |
| 15 | + |
| 16 | + let result = []; |
| 17 | + let isNextResults = true; |
| 18 | + let pagination = ""; |
| 19 | + |
| 20 | + while (isNextResults) { |
| 21 | + const paginateResults = await getRetweets(tweetId, pagination); |
| 22 | + result = result.concat(paginateResults?.data); |
| 23 | + isNextResults = !!paginateResults?.meta?.next_token; |
| 24 | + pagination = paginateResults?.meta?.next_token; |
| 25 | + } |
| 26 | + |
| 27 | + return result; |
| 28 | +}; |
| 29 | + |
| 30 | +export const getRetweets = async (tweetId: string, pagination_token = "") => { |
| 31 | + if (!tweetId) throw new Error("tweet id is needed"); |
| 32 | + |
| 33 | + const endpointURL = `${baseUri}/2/tweets/${tweetId}/retweeted_by`; |
| 34 | + |
| 35 | + const params = { |
| 36 | + "user.fields": "id,profile_image_url,username", |
| 37 | + max_results: 100, |
| 38 | + }; |
| 39 | + |
| 40 | + if (pagination_token) params.pagination_token = pagination_token; |
| 41 | + |
| 42 | + const endpointWithParamsURL = `${endpointURL}?${stringify(params)}`; |
| 43 | + |
| 44 | + const res = await fetch(endpointWithParamsURL, getRequestParams()); |
| 45 | + |
| 46 | + if (res.ok) return await res.json(); |
| 47 | + |
| 48 | + throw new Error("Unsuccessful request"); |
| 49 | +}; |
| 50 | + |
| 51 | +export const getTweet = async (tweetId: string) => { |
| 52 | + if (!tweetId) throw new Error("tweet id is needed"); |
| 53 | + |
| 54 | + const endpointURL = `${baseUri}/2/tweets/${tweetId}`; |
| 55 | + |
| 56 | + const res = await fetch(endpointURL, getRequestParams()); |
| 57 | + if (res.ok) return await res.json(); |
| 58 | + |
| 59 | + throw new Error("Unsuccessful request"); |
| 60 | +}; |
| 61 | + |
| 62 | +export const signUp = async ( |
| 63 | + userId: string, |
| 64 | + userAddress: string, |
| 65 | + accessToken: string |
| 66 | +) => { |
| 67 | + if (!userId) throw new Error("user id is needed"); |
| 68 | + if (!userAddress) throw new Error("user address is needed"); |
| 69 | + if (!accessToken) throw new Error("user access token is needed"); |
| 70 | + |
| 71 | + const user = await UserKV.getUser(userId); |
| 72 | + if (!user) throw new Error("no user found"); |
| 73 | + if (checkUserSignUp(userId)) throw new Error("user already sign up"); |
| 74 | + if (user.accessToken !== accessToken) throw new Error("accessToken mismatch"); |
| 75 | + |
| 76 | + const userData = { |
| 77 | + ...user, |
| 78 | + isRegistered: true, |
| 79 | + userAddress, |
| 80 | + }; |
| 81 | + |
| 82 | + UserKV.saveUser(userId, userData); |
| 83 | + return { |
| 84 | + userId, |
| 85 | + ...userData, |
| 86 | + }; |
| 87 | +}; |
| 88 | + |
| 89 | +export const checkUserSignUp = async (userId: string) => { |
| 90 | + const user = await UserKV.getUser(userId); |
| 91 | + return user?.isRegistered || false; |
| 92 | +}; |
| 93 | + |
| 94 | +export const drawWinners = async ( |
| 95 | + giveawayId: string, |
| 96 | + tweetId: string, |
| 97 | + retweetMaxCount: number, |
| 98 | + prizes: number |
| 99 | +) => { |
| 100 | + if (!giveawayId) throw new Error("giveaway id is needed"); |
| 101 | + if (!tweetId) throw new Error("tweet id is needed"); |
| 102 | + if (typeof retweetMaxCount === "undefined") |
| 103 | + throw new Error("retweetMaxCount id is needed"); |
| 104 | + if (!prizes) throw new Error("prizes id is needed"); |
| 105 | + |
| 106 | + const retweets = await getAllRetweets(tweetId); |
| 107 | + |
| 108 | + // TODO order retweets by date if needed ? |
| 109 | + |
| 110 | + const winners = [], |
| 111 | + randoms = [], |
| 112 | + positions = []; |
| 113 | + const retweetCountLimit = |
| 114 | + retweetMaxCount && retweets.length > retweetMaxCount |
| 115 | + ? retweetMaxCount |
| 116 | + : retweets.length; |
| 117 | + |
| 118 | + for (let i = 0; i < +prizes; i++) { |
| 119 | + const random = randomNewNumber(0, retweetCountLimit, randoms); |
| 120 | + winners.push(retweets[random].id); |
| 121 | + positions.push(i); |
| 122 | + randoms.push(random); |
| 123 | + } |
| 124 | + |
| 125 | + return { |
| 126 | + winners, |
| 127 | + positions, |
| 128 | + }; |
| 129 | +}; |
0 commit comments