/
local_data.go
131 lines (116 loc) · 3.83 KB
/
local_data.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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// apcore is a server framework for implementing an ActivityPub application.
// Copyright (C) 2020 Cory Slep
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package models
import (
"database/sql"
"net/url"
"github.com/go-fed/apcore/util"
)
var _ Model = &LocalData{}
// LocalData is a Model that provides additional database methods for
// ActivityStreams data generated by this instance.
type LocalData struct {
exists *sql.Stmt
get *sql.Stmt
localCreate *sql.Stmt
localUpdate *sql.Stmt
localDelete *sql.Stmt
stats *sql.Stmt
}
func (f *LocalData) Prepare(db *sql.DB, s SqlDialect) error {
return prepareStmtPairs(db,
stmtPairs{
{&(f.exists), s.LocalExists()},
{&(f.get), s.LocalGet()},
{&(f.localCreate), s.LocalCreate()},
{&(f.localUpdate), s.LocalUpdate()},
{&(f.localDelete), s.LocalDelete()},
{&(f.stats), s.LocalStats()},
})
}
func (f *LocalData) CreateTable(t *sql.Tx, s SqlDialect) error {
if _, err := t.Exec(s.CreateLocalDataTable()); err != nil {
return err
}
_, err := t.Exec(s.CreateIndexIDLocalDataTable())
return err
}
func (f *LocalData) Close() {
f.exists.Close()
f.get.Close()
f.localCreate.Close()
f.localUpdate.Close()
f.localDelete.Close()
f.stats.Close()
}
// Exists determines if the ID is stored in the local table.
func (f *LocalData) Exists(c util.Context, tx *sql.Tx, id *url.URL) (exists bool, err error) {
var rows *sql.Rows
rows, err = tx.Stmt(f.exists).QueryContext(c, id.String())
if err != nil {
return
}
defer rows.Close()
err = enforceOneRow(rows, "LocalData.Exists", func(r SingleRow) error {
return r.Scan(&exists)
})
return
}
// Get retrieves the ID from the local table.
func (f *LocalData) Get(c util.Context, tx *sql.Tx, id *url.URL) (v ActivityStreams, err error) {
var rows *sql.Rows
rows, err = tx.Stmt(f.get).QueryContext(c, id.String())
if err != nil {
return
}
defer rows.Close()
err = enforceOneRow(rows, "LocalData.Get", func(r SingleRow) error {
return r.Scan(&v)
})
return
}
// Create inserts the local data into the table.
func (f *LocalData) Create(c util.Context, tx *sql.Tx, v ActivityStreams) error {
v.SanitizeContentSummaryHTML()
r, err := tx.Stmt(f.localCreate).ExecContext(c, v)
return mustChangeOneRow(r, err, "LocalData.Create")
}
// Update replaces the local data for the specified IRI.
func (f *LocalData) Update(c util.Context, tx *sql.Tx, localIDIRI *url.URL, v ActivityStreams) error {
v.SanitizeContentSummaryHTML()
r, err := tx.Stmt(f.localUpdate).ExecContext(c, localIDIRI.String(), v)
return mustChangeOneRow(r, err, "LocalData.Update")
}
// Delete removes the local data with the specified IRI.
func (f *LocalData) Delete(c util.Context, tx *sql.Tx, localIDIRI *url.URL) error {
r, err := tx.Stmt(f.localDelete).ExecContext(c, localIDIRI.String())
return mustChangeOneRow(r, err, "LocalData.Delete")
}
type LocalDataActivity struct {
NLocalPosts int
NLocalComments int
}
func (f *LocalData) Stats(c util.Context, tx *sql.Tx) (la LocalDataActivity, err error) {
var rows *sql.Rows
rows, err = tx.Stmt(f.stats).QueryContext(c)
if err != nil {
return
}
defer rows.Close()
return la, enforceOneRow(rows, "LocalData.Stats", func(r SingleRow) error {
return r.Scan(&(la.NLocalPosts), &(la.NLocalComments))
})
}