-
Notifications
You must be signed in to change notification settings - Fork 0
/
mail.go
71 lines (64 loc) · 2.1 KB
/
mail.go
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
package main
import (
"log"
"time"
"github.com/anyproto/anytype-heart/core/block/import/markdown/anymark"
"github.com/anyproto/anytype-heart/pb"
"github.com/anyproto/anytype-heart/pkg/lib/bundle"
"github.com/anyproto/anytype-heart/pkg/lib/pb/model"
"github.com/anyproto/anytype-heart/util/pbtypes"
"github.com/balazsgrill/extractld"
"github.com/gogo/protobuf/types"
"github.com/google/uuid"
)
func MailSnapshot(email extractld.Mail) (*pb.RpcObjectImportRequestSnapshot, error) {
blocks, _, err := anymark.HTMLToBlocks([]byte(email.Body()))
if err != nil {
return nil, err
}
return &pb.RpcObjectImportRequestSnapshot{
Id: uuid.NewMD5(uuid.Nil, []byte(email.Source())).String(),
Snapshot: &model.SmartBlockSnapshotBase{
Blocks: blocks,
Details: &types.Struct{
Fields: map[string]*types.Value{
bundle.RelationKeyName.String(): pbtypes.String(email.Topic()),
bundle.RelationKeySource.String(): pbtypes.String(email.Source()),
bundle.RelationKeyType.String(): pbtypes.String(bundle.TypeKeyPage.String()),
bundle.RelationKeyCreatedDate.String(): pbtypes.Int64(email.SentTime().Unix()),
},
},
OriginalCreatedTimestamp: email.SentTime().Unix(),
},
}, nil
}
func GetMail(provider extractld.MailProvider, filter func(extractld.Mail) bool, start time.Time, end time.Time) ([]*pb.RpcObjectImportRequestSnapshot, error) {
mails, err := provider.MailByDate(start, end)
if err != nil {
return nil, err
}
result := make([]*pb.RpcObjectImportRequestSnapshot, 0, len(mails))
for _, mail := range mails {
if filter(mail) {
ss, err := MailSnapshot(mail)
if err != nil {
log.Println(err)
} else {
result = append(result, ss)
}
}
}
return result, nil
}
type MailProcessor struct {
extractld.MailProvider
sourcefilter func(extractld.Mail) bool
}
func (mp *MailProcessor) Do(now time.Time) []*pb.RpcObjectImportRequestSnapshot {
// Two weeks
mail, err := GetMail(mp.MailProvider, mp.sourcefilter, now.Add(-14*24*time.Hour), now)
if err != nil {
return nil
}
return mail
}