-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.ts
More file actions
108 lines (100 loc) · 3.02 KB
/
index.ts
File metadata and controls
108 lines (100 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import * as functions from 'firebase-functions'
import * as admin from 'firebase-admin'
import { execTweet } from './utils/tweet'
import { Environments, TweetRequest, UserData } from './types'
import { crawl } from './utils/crawler'
;(admin as any).initializeApp({
// apiKey: process.env.FIREBASE_APIKEY,
// authDomain: process.env.FIREBASE_AUTHDOMAIN,
// databaseURL: process.env.FIREBASE_DATABASEURL,
// projectId: process.env.FIREBASE_PROJECTID,
// storageBucket: process.env.FIREBASE_STORAGEBUCKET
})
const environments = functions.config() as Environments
const firestore = admin.firestore()
async function clearTweetRequest() {
const tweetRequestsSnapshot = await firestore.collection('tweetRequest').get()
const tweetRequest: TweetRequest[] = []
tweetRequestsSnapshot.forEach(doc => {
tweetRequest.push({
id: doc.id,
...(doc.data() as TweetRequest)
})
})
await Promise.all(
tweetRequest.map(async tr => {
if (tr.keep!) {
return
}
await firestore
.collection('tweetRequest')
.doc(tr.id!)
.delete()
})
)
return
}
async function addTweetRequests() {
const usersSnapshot = await firestore.collection('users').get()
const users: UserData[] = []
usersSnapshot.forEach(doc => {
users.push(doc.data() as UserData)
})
await Promise.all(
users.map(async u => {
const tr: TweetRequest = {
uid: u.uid
}
await firestore.collection('tweetRequest').add(tr)
})
)
return
}
export const dailyClearTweetRequest = functions.pubsub
.schedule('1 12 * * *')
.timeZone('Asia/Tokyo')
.onRun(async () => {
await clearTweetRequest()
})
export const dailyTweet = functions.pubsub
.schedule('1 0 * * *')
.timeZone('Asia/Tokyo')
.onRun(async () => {
await addTweetRequests()
})
export const dailyTweetAgain = functions.pubsub
.schedule('16 0 * * *')
.timeZone('Asia/Tokyo')
.onRun(async () => {
await addTweetRequests()
})
export const dailyTweetAgainAgain = functions.pubsub
.schedule('31 0 * * *')
.timeZone('Asia/Tokyo')
.onRun(async () => {
await addTweetRequests()
})
export const tweet = functions.firestore
.document('tweetRequest/{tweetRequestId}')
.onCreate(async snapshot => {
const tweetRequest = snapshot.data() as TweetRequest
const userData = await firestore
.collection('users')
.doc(`${tweetRequest.uid}`)
.get()
.then(snapshot => snapshot.data() as UserData)
if (!(userData.GitHubID && userData.enabled)) {
return
}
const contribution = await crawl(userData.GitHubID)
const status = `${userData.TwitterID} さんの ${contribution.date} の contribution 数: ${contribution.count}\n#contributter_report`
await execTweet(
{
consumer_key: environments.twitter.ck || process.env.TWITTER_CK!,
consumer_secret: environments.twitter.cs || process.env.TWITTER_CS!,
access_token_key: userData.accessToken,
access_token_secret: userData.accessSecret
},
status
)
})