-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathfollowing.go
158 lines (142 loc) · 5.31 KB
/
following.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// apcore is a server framework for implementing an ActivityPub application.
// Copyright (C) 2019 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 = &Following{}
// Following is a Model that provides additional database methods for Following.
type Following struct {
insert *sql.Stmt
containsForActor *sql.Stmt
contains *sql.Stmt
get *sql.Stmt
getLastPage *sql.Stmt
prependItem *sql.Stmt
deleteItem *sql.Stmt
getAllForActor *sql.Stmt
}
func (i *Following) Prepare(db *sql.DB, s SqlDialect) error {
return prepareStmtPairs(db,
stmtPairs{
{&(i.insert), s.InsertFollowing()},
{&(i.containsForActor), s.FollowingContainsForActor()},
{&(i.contains), s.FollowingContains()},
{&(i.get), s.GetFollowing()},
{&(i.getLastPage), s.GetFollowingLastPage()},
{&(i.prependItem), s.PrependFollowingItem()},
{&(i.deleteItem), s.DeleteFollowingItem()},
{&(i.getAllForActor), s.GetAllFollowingForActor()},
})
}
func (i *Following) CreateTable(t *sql.Tx, s SqlDialect) error {
if _, err := t.Exec(s.CreateFollowingTable()); err != nil {
return err
}
_, err := t.Exec(s.CreateIndexIDFollowingTable())
return err
}
func (i *Following) Close() {
i.insert.Close()
i.containsForActor.Close()
i.contains.Close()
i.get.Close()
i.getLastPage.Close()
i.prependItem.Close()
i.deleteItem.Close()
i.getAllForActor.Close()
}
// Create a new following entry for the given actor.
func (i *Following) Create(c util.Context, tx *sql.Tx, actor *url.URL, following ActivityStreamsCollection) error {
r, err := tx.Stmt(i.insert).ExecContext(c,
actor.String(),
following)
return mustChangeOneRow(r, err, "Following.Create")
}
// ContainsForActor returns true if the item is in the actor's following's collection.
func (i *Following) ContainsForActor(c util.Context, tx *sql.Tx, actor, item *url.URL) (b bool, err error) {
var rows *sql.Rows
rows, err = tx.Stmt(i.containsForActor).QueryContext(c, actor.String(), item.String())
if err != nil {
return
}
defer rows.Close()
return b, enforceOneRow(rows, "Following.ContainsForActor", func(r SingleRow) error {
return r.Scan(&b)
})
}
// Contains returns true if the item is in the following's collection.
func (i *Following) Contains(c util.Context, tx *sql.Tx, following, item *url.URL) (b bool, err error) {
var rows *sql.Rows
rows, err = tx.Stmt(i.contains).QueryContext(c, following.String(), item.String())
if err != nil {
return
}
defer rows.Close()
return b, enforceOneRow(rows, "Following.Contains", func(r SingleRow) error {
return r.Scan(&b)
})
}
// GetPage returns a CollectionPage of the Following.
//
// The range of elements retrieved are [min, max).
func (i *Following) GetPage(c util.Context, tx *sql.Tx, following *url.URL, min, max int) (page ActivityStreamsCollectionPage, isEnd bool, err error) {
var rows *sql.Rows
rows, err = tx.Stmt(i.get).QueryContext(c, following.String(), min, max-1)
if err != nil {
return
}
defer rows.Close()
return page, isEnd, enforceOneRow(rows, "Following.GetPage", func(r SingleRow) error {
return r.Scan(&page, &isEnd)
})
}
// GetLastPage returns the last CollectionPage of the Following collection.
func (i *Following) GetLastPage(c util.Context, tx *sql.Tx, following *url.URL, n int) (page ActivityStreamsCollectionPage, startIdx int, err error) {
var rows *sql.Rows
rows, err = tx.Stmt(i.getLastPage).QueryContext(c, following.String(), n)
if err != nil {
return
}
defer rows.Close()
return page, startIdx, enforceOneRow(rows, "Following.GetLastPage", func(r SingleRow) error {
return r.Scan(&page, &startIdx)
})
}
// PrependItem prepends the item to the following's ordered items list.
func (i *Following) PrependItem(c util.Context, tx *sql.Tx, following, item *url.URL) error {
r, err := tx.Stmt(i.prependItem).ExecContext(c, following.String(), item.String())
return mustChangeOneRow(r, err, "Following.PrependItem")
}
// DeleteItem removes the item from the following's ordered items list.
func (i *Following) DeleteItem(c util.Context, tx *sql.Tx, following, item *url.URL) error {
r, err := tx.Stmt(i.deleteItem).ExecContext(c, following.String(), item.String())
return mustChangeOneRow(r, err, "Following.DeleteItem")
}
// GetAllForActor returns the entire Following Collection.
func (i *Following) GetAllForActor(c util.Context, tx *sql.Tx, following *url.URL) (col ActivityStreamsCollection, err error) {
var rows *sql.Rows
rows, err = tx.Stmt(i.getAllForActor).QueryContext(c, following.String())
if err != nil {
return
}
defer rows.Close()
return col, enforceOneRow(rows, "Following.GetAllForActor", func(r SingleRow) error {
return r.Scan(&col)
})
}