|
1 | | -import * as bp from '.botpress' |
| 1 | +import * as lin from '../utils/linear-utils' |
2 | 2 |
|
3 | 3 | const RECENT_THRESHOLD: number = 1000 * 60 * 10 // 10 minutes |
4 | | -type IssueLintEntry = bp.states.recentlyLinted.RecentlyLinted['payload']['issues'][number] |
5 | 4 |
|
6 | 5 | export class RecentlyLintedManager { |
7 | | - public constructor( |
8 | | - private _client: bp.Client, |
9 | | - private _botId: string |
10 | | - ) {} |
| 6 | + public constructor(private _linear: lin.LinearApi) {} |
11 | 7 |
|
12 | | - public async getRecentlyLinted(): Promise<bp.states.recentlyLinted.RecentlyLinted['payload']['issues']> { |
13 | | - const { |
14 | | - state: { |
15 | | - payload: { issues }, |
16 | | - }, |
17 | | - } = await this._client.getOrSetState({ |
18 | | - id: this._botId, |
19 | | - type: 'bot', |
20 | | - name: 'recentlyLinted', |
21 | | - payload: { issues: [] }, |
22 | | - }) |
23 | | - return issues.filter(this._isRecentlyLinted) |
24 | | - } |
25 | | - |
26 | | - public async setRecentlyLinted(issues: bp.states.recentlyLinted.RecentlyLinted['payload']['issues']): Promise<void> { |
27 | | - await this._client.setState({ |
28 | | - id: this._botId, |
29 | | - type: 'bot', |
30 | | - name: 'recentlyLinted', |
31 | | - payload: { |
32 | | - issues: issues.filter(this._isRecentlyLinted), |
33 | | - }, |
34 | | - }) |
35 | | - } |
36 | | - |
37 | | - private _isRecentlyLinted = (issue: IssueLintEntry): boolean => { |
38 | | - const lintedAt = new Date(issue.lintedAt).getTime() |
| 8 | + public async isRecentlyLinted(issue: lin.Issue): Promise<boolean> { |
| 9 | + const me = await this._linear.getMe() |
| 10 | + const timestamps = issue.comments.nodes |
| 11 | + .filter((comment) => comment.user.id === me.id) |
| 12 | + .map((comment) => new Date(comment.createdAt).getTime()) |
39 | 13 | const now = new Date().getTime() |
40 | | - return now - lintedAt < RECENT_THRESHOLD |
| 14 | + for (const timestamp of timestamps) { |
| 15 | + if (now - timestamp < RECENT_THRESHOLD) { |
| 16 | + return true |
| 17 | + } |
| 18 | + } |
| 19 | + return false |
41 | 20 | } |
42 | 21 | } |
0 commit comments