tangled
alpha
login
or
join now
edavis.dev
/
bsky-feeds
1
fork
atom
this repo has no description
1
fork
atom
overview
issues
pulls
pipelines
feat: move safeTimestamp to feeds.SafeTimestamp
Eric Davis
1 year ago
fd1a30cb
a973ab2e
+40
-31
3 changed files
expand all
collapse all
unified
split
pkg
feeds
doc.go
timestamps.go
mostliked
handler.go
+3
pkg/feeds/doc.go
···
1
1
+
package feeds
2
2
+
3
3
+
// Common code usable by all feedgens
+35
pkg/feeds/timestamps.go
···
1
1
+
package feeds
2
2
+
3
3
+
import (
4
4
+
"time"
5
5
+
)
6
6
+
7
7
+
func SafeTimestamp(input string) int64 {
8
8
+
utcNow := time.Now().UTC()
9
9
+
if input == "" {
10
10
+
return utcNow.Unix()
11
11
+
}
12
12
+
var t time.Time
13
13
+
var err error
14
14
+
layouts := []string{
15
15
+
time.RFC3339,
16
16
+
}
17
17
+
for _, layout := range layouts {
18
18
+
if t, err = time.Parse(layout, input); err == nil {
19
19
+
break
20
20
+
}
21
21
+
}
22
22
+
if err != nil {
23
23
+
return utcNow.Unix()
24
24
+
}
25
25
+
if t.Unix() <= 0 {
26
26
+
return utcNow.Unix()
27
27
+
} else if t.Add(-2*time.Minute).Compare(utcNow) == -1 {
28
28
+
// accept as long as parsed time is no more than 2 minutes in the future
29
29
+
return t.Unix()
30
30
+
} else if t.Compare(utcNow) == 1 {
31
31
+
return utcNow.Unix()
32
32
+
} else {
33
33
+
return utcNow.Unix()
34
34
+
}
35
35
+
}
+2
-31
pkg/mostliked/handler.go
···
13
13
appbsky "github.com/bluesky-social/indigo/api/bsky"
14
14
jetstream "github.com/bluesky-social/jetstream/pkg/models"
15
15
db "github.com/edavis/bsky-feeds/db/mostliked"
16
16
+
"github.com/edavis/bsky-feeds/pkg/feeds"
16
17
"github.com/karlseguin/ccache/v3"
17
18
_ "github.com/mattn/go-sqlite3"
18
19
"github.com/pemistahl/lingua-go"
···
27
28
Languages []lingua.Language
28
29
Created int64
29
30
Likes int64
30
30
-
}
31
31
-
32
32
-
func safeTimestamp(input string) int64 {
33
33
-
utcNow := time.Now().UTC()
34
34
-
if input == "" {
35
35
-
return utcNow.Unix()
36
36
-
}
37
37
-
var t time.Time
38
38
-
var err error
39
39
-
layouts := []string{
40
40
-
time.RFC3339,
41
41
-
}
42
42
-
for _, layout := range layouts {
43
43
-
if t, err = time.Parse(layout, input); err == nil {
44
44
-
break
45
45
-
}
46
46
-
}
47
47
-
if err != nil {
48
48
-
return utcNow.Unix()
49
49
-
}
50
50
-
if t.Unix() <= 0 {
51
51
-
return utcNow.Unix()
52
52
-
} else if t.Add(-2*time.Minute).Compare(utcNow) == -1 {
53
53
-
// accept as long as parsed time is no more than 2 minutes in the future
54
54
-
return t.Unix()
55
55
-
} else if t.Compare(utcNow) == 1 {
56
56
-
return utcNow.Unix()
57
57
-
} else {
58
58
-
return utcNow.Unix()
59
59
-
}
60
31
}
61
32
62
33
func trimPostsTable(ctx context.Context, queries *db.Queries) {
···
144
115
continue
145
116
}
146
117
draftPost := DraftPost{
147
147
-
Created: safeTimestamp(post.CreatedAt),
118
118
+
Created: feeds.SafeTimestamp(post.CreatedAt),
148
119
Likes: 0,
149
120
}
150
121
if text := findDetectableText(post); text != "" {