/
reaction.go
241 lines (215 loc) · 7.32 KB
/
reaction.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package models
import (
"fmt"
"time"
"github.com/davecheney/pub/internal/snowflake"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"gorm.io/gorm/schema"
)
// Reaction represents an an actors reaction to a status.
type Reaction struct {
StatusID snowflake.ID `gorm:"primarykey;autoIncrement:false"`
Status *Status `gorm:"constraint:OnDelete:CASCADE;<-:false"`
ActorID snowflake.ID `gorm:"primarykey;autoIncrement:false"`
Actor *Actor `gorm:"constraint:OnDelete:CASCADE;<-:false"`
Favourited bool `gorm:"not null;default:false"`
Reblogged bool `gorm:"not null;default:false"`
Muted bool `gorm:"not null;default:false"`
Bookmarked bool `gorm:"not null;default:false"`
Pinned bool `gorm:"not null;default:false"`
}
func (r *Reaction) BeforeUpdate(tx *gorm.DB) error {
return forEach(tx, r.createReactionRequest)
}
func (r *Reaction) AfterSave(tx *gorm.DB) error {
return forEach(tx, r.updateStatusCount)
}
// updateStatusCount updates the favourites_count and reblogs_count fields on the status.
func (r *Reaction) updateStatusCount(tx *gorm.DB) error {
status := &Status{ObjectID: r.StatusID}
favouritesCount := tx.Select("COUNT(*)").Where("status_id = ? and favourited = true", r.StatusID).Table("reactions")
reblogsCount := tx.Select("COUNT(*)").Where("status_id = ? and reblogged = true", r.StatusID).Table("reactions")
return tx.Model(status).UpdateColumns(map[string]interface{}{
"favourites_count": favouritesCount,
"reblogs_count": reblogsCount,
}).Error
}
// createReactionRequest creates a reaction request between the actor and target if needed.
func (r *Reaction) createReactionRequest(tx *gorm.DB) error {
var original Reaction
if err := tx.First(&original, "actor_id = ? and status_id = ?", r.ActorID, r.StatusID).Error; err != nil {
return err
}
fmt.Printf("reaction changed from %+v to %+v\n", original, r)
// if there is a conflict; eg. a like then an unlike before the follow is processed
// update the existing row to reflect the new action.
tx = tx.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "actor_id"}, {Name: "target_id"}},
DoUpdates: clause.AssignmentColumns([]string{
"action",
"created_at",
"updated_at",
"attempts", // resets the attempts counter
}),
})
// what changed?
switch {
case original.Favourited && !r.Favourited:
// undo like
return tx.Create(&ReactionRequest{
ActorID: r.ActorID,
TargetID: r.StatusID,
Action: "unlike",
}).Error
case !original.Favourited && r.Favourited:
// like
return tx.Create(&ReactionRequest{
ActorID: r.ActorID,
TargetID: r.StatusID,
Action: "like",
}).Error
default:
return nil
}
}
// A ReactionRequest is a request to update the reaction to a status.
// ReactionRequests are created by hooks on the Reaction model, and are
// processed by the ReactionRequestProcessor in the background.
type ReactionRequest struct {
Request
// ActorID is the ID of the actor that is requesting the reaction change.
ActorID snowflake.ID `gorm:"uniqueIndex:uidx_reaction_requests_actor_id_target_id;not null;"`
// Actor is the actor that is requesting the reaction change.
Actor *Actor `gorm:"constraint:OnDelete:CASCADE;<-:false"`
TargetID snowflake.ID `gorm:"uniqueIndex:uidx_reaction_requests_actor_id_target_id;not null;"`
// Target is the status that is being reacted to.
Target *Status `gorm:"constraint:OnDelete:CASCADE;<-:false"`
// Action is the action to perform, either follow or unfollow.
Action ReactionRequestAction `gorm:"not null"`
}
type ReactionRequestAction string
func (ReactionRequestAction) GormDBDataType(db *gorm.DB, field *schema.Field) string {
switch db.Dialector.Name() {
case "mysql", "postgres":
return "enum('like', 'unlike')"
case "sqlite":
return "TEXT"
default:
return ""
}
}
type Reactions struct {
db *gorm.DB
}
func NewReactions(db *gorm.DB) *Reactions {
return &Reactions{db: db}
}
func (r *Reactions) Pin(status *Status, actor *Actor) (*Reaction, error) {
reaction, err := findOrCreateReaction(r.db, status, actor)
if err != nil {
return nil, err
}
reaction.Pinned = true
return reaction, r.db.Save(reaction).Error
}
func (r *Reactions) Unpin(status *Status, actor *Actor) (*Reaction, error) {
reaction, err := findOrCreateReaction(r.db, status, actor)
if err != nil {
return nil, err
}
reaction.Pinned = false
return reaction, r.db.Save(reaction).Error
}
func (r *Reactions) Favourite(status *Status, actor *Actor) (*Reaction, error) {
reaction, err := findOrCreateReaction(r.db, status, actor)
if err != nil {
return nil, err
}
reaction.Favourited = true
reaction.Status.FavouritesCount++
return reaction, r.db.Save(reaction).Error
}
func (r *Reactions) Unfavourite(status *Status, actor *Actor) (*Reaction, error) {
reaction, err := findOrCreateReaction(r.db, status, actor)
if err != nil {
return nil, err
}
reaction.Favourited = false
reaction.Status.FavouritesCount--
return reaction, r.db.Save(reaction).Error
}
func (r *Reactions) Bookmark(status *Status, actor *Actor) (*Reaction, error) {
reaction, err := findOrCreateReaction(r.db, status, actor)
if err != nil {
return nil, err
}
reaction.Bookmarked = true
return reaction, r.db.Save(reaction).Error
}
func (r *Reactions) Unbookmark(status *Status, actor *Actor) (*Reaction, error) {
reaction, err := findOrCreateReaction(r.db, status, actor)
if err != nil {
return nil, err
}
reaction.Bookmarked = false
return reaction, r.db.Save(reaction).Error
}
// Reblog creates a new status that is a reblog of the given status.
func (r *Reactions) Reblog(status *Status, actor *Actor) (*Status, error) {
id := snowflake.Now()
if err := r.db.Transaction(func(tx *gorm.DB) error {
reaction, err := findOrCreateReaction(tx, status, actor)
if err != nil {
return err
}
reaction.Reblogged = true
reaction.Status.ReblogsCount++
if err := tx.Save(reaction).Error; err != nil {
return err
}
props := map[string]any{
"type": "Announce",
"id": fmt.Sprintf("https://%s/u/%s/status/%d/activity", actor.Domain, actor.Name, id),
"actor": actor.URI(),
"object": status.URI(),
"published": id.ToTime().Format(time.RFC3339),
}
obj := Object{
ID: id, // populate ID so that it matches the URI, otherwise it will be generated from snowflate.FromDate.
Properties: props,
}
return tx.Create(&obj).Error
}); err != nil {
return nil, err
}
return NewStatuses(r.db).FindByID(id)
}
// Unreblog removes the reblog of the given status with the given actor.
func (r *Reactions) Unreblog(status *Status, actor *Actor) (*Status, error) {
var reblog Status
return &reblog, r.db.Transaction(func(tx *gorm.DB) error {
reaction, err := findOrCreateReaction(tx, status, actor)
if err != nil {
return err
}
reaction.Reblogged = false
reaction.Status.ReblogsCount--
if err := tx.Save(reaction).Error; err != nil {
return err
}
if err := tx.Where("reblog_id = ? AND actor_id = ?", status.ObjectID, actor.ObjectID).Preload("Actor").First(&reblog).Error; err != nil {
return err
}
return tx.Delete(&reblog).Error
})
}
func findOrCreateReaction(tx *gorm.DB, status *Status, actor *Actor) (*Reaction, error) {
status.Reaction = &Reaction{
StatusID: status.ObjectID,
Status: status,
ActorID: actor.ObjectID,
Actor: actor,
}
return status.Reaction, tx.FirstOrCreate(status.Reaction).Error
}