forked from
tangled.org/core
fork
Configure Feed
Select the types of activity you want to include in your feed.
Monorepo for Tangled
fork
Configure Feed
Select the types of activity you want to include in your feed.
1package db
2
3import (
4 "fmt"
5 "strings"
6 "time"
7
8 "github.com/go-git/go-git/v5/plumbing"
9 "github.com/ipfs/go-cid"
10 "tangled.org/core/appview/models"
11)
12
13func AddArtifact(e Execer, artifact models.Artifact) error {
14 _, err := e.Exec(
15 `insert or ignore into artifacts (
16 did,
17 rkey,
18 repo_at,
19 tag,
20 created,
21 blob_cid,
22 name,
23 size,
24 mimetype
25 )
26 values (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
27 artifact.Did,
28 artifact.Rkey,
29 artifact.RepoAt,
30 artifact.Tag[:],
31 artifact.CreatedAt.Format(time.RFC3339),
32 artifact.BlobCid.String(),
33 artifact.Name,
34 artifact.Size,
35 artifact.MimeType,
36 )
37 return err
38}
39
40func GetArtifact(e Execer, filters ...filter) ([]models.Artifact, error) {
41 var artifacts []models.Artifact
42
43 var conditions []string
44 var args []any
45 for _, filter := range filters {
46 conditions = append(conditions, filter.Condition())
47 args = append(args, filter.Arg()...)
48 }
49
50 whereClause := ""
51 if conditions != nil {
52 whereClause = " where " + strings.Join(conditions, " and ")
53 }
54
55 query := fmt.Sprintf(`select
56 did,
57 rkey,
58 repo_at,
59 tag,
60 created,
61 blob_cid,
62 name,
63 size,
64 mimetype
65 from artifacts %s`,
66 whereClause,
67 )
68
69 rows, err := e.Query(query, args...)
70 if err != nil {
71 return nil, err
72 }
73 defer rows.Close()
74
75 for rows.Next() {
76 var artifact models.Artifact
77 var createdAt string
78 var tag []byte
79 var blobCid string
80
81 if err := rows.Scan(
82 &artifact.Did,
83 &artifact.Rkey,
84 &artifact.RepoAt,
85 &tag,
86 &createdAt,
87 &blobCid,
88 &artifact.Name,
89 &artifact.Size,
90 &artifact.MimeType,
91 ); err != nil {
92 return nil, err
93 }
94
95 artifact.CreatedAt, err = time.Parse(time.RFC3339, createdAt)
96 if err != nil {
97 artifact.CreatedAt = time.Now()
98 }
99 artifact.Tag = plumbing.Hash(tag)
100 artifact.BlobCid = cid.MustParse(blobCid)
101
102 artifacts = append(artifacts, artifact)
103 }
104
105 if err := rows.Err(); err != nil {
106 return nil, err
107 }
108
109 return artifacts, nil
110}
111
112func DeleteArtifact(e Execer, filters ...filter) error {
113 var conditions []string
114 var args []any
115 for _, filter := range filters {
116 conditions = append(conditions, filter.Condition())
117 args = append(args, filter.Arg()...)
118 }
119
120 whereClause := ""
121 if conditions != nil {
122 whereClause = " where " + strings.Join(conditions, " and ")
123 }
124
125 query := fmt.Sprintf(`delete from artifacts %s`, whereClause)
126
127 _, err := e.Exec(query, args...)
128 return err
129}