/
update.go
128 lines (115 loc) · 3.44 KB
/
update.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
package blockedlists
import (
"database/sql"
"net/http"
"github.com/OdyseeTeam/commentron/commentapi"
"github.com/OdyseeTeam/commentron/db"
"github.com/OdyseeTeam/commentron/model"
"github.com/OdyseeTeam/commentron/server/auth"
"github.com/lbryio/lbry.go/v2/extras/api"
"github.com/lbryio/lbry.go/v2/extras/errors"
"github.com/volatiletech/sqlboiler/v4/boil"
)
func update(r *http.Request, args *commentapi.SharedBlockedListUpdateArgs, reply *commentapi.SharedBlockedList) error {
ownerChannel, _, err := auth.Authenticate(r, &args.Authorization)
if err != nil {
return err
}
list, err := model.BlockedLists(model.BlockedListWhere.ChannelID.EQ(ownerChannel.ClaimID)).One(db.RO)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return errors.Err(err)
}
if args.Remove {
err := list.BlockedListInvites().DeleteAll(db.RW)
if err != nil {
return errors.Err(err)
}
err = list.Delete(db.RW)
if err != nil {
return errors.Err(err)
}
}
var created bool
if list == nil {
if args.Name == nil {
return api.StatusError{Err: errors.Err("a name must be specified if a new list will get created")}
}
list = &model.BlockedList{ChannelID: ownerChannel.ClaimID, Name: *args.Name}
err := list.Insert(db.RW, boil.Infer())
if err != nil {
return errors.Err(err)
}
created = true
}
if args.Name != nil {
list.Name = *args.Name
}
if args.Category != nil {
list.Category = *args.Category
}
if args.Description != nil {
list.Description = *args.Description
}
if args.MemberInviteEnabled != nil {
list.MemberInviteEnabled.SetValid(*args.MemberInviteEnabled)
}
if args.InviteExpiration != nil {
list.InviteExpiration.SetValid(*args.InviteExpiration)
}
if args.StrikeOne != nil {
list.StrikeOne.SetValid(*args.StrikeOne)
}
if args.StrikeTwo != nil {
list.StrikeTwo.SetValid(*args.StrikeTwo)
}
if args.StrikeThree != nil {
list.StrikeThree.SetValid(*args.StrikeThree)
}
if args.CurseJarAmount != nil {
list.CurseJarAmount.SetValid(*args.CurseJarAmount)
}
err = list.Update(db.RW, boil.Infer())
if err != nil {
return errors.Err(err)
}
if created {
blockedList := map[string]interface{}{model.BlockedEntryColumns.BlockedListID: list.ID}
err := ownerChannel.CreatorChannelBlockedEntries().UpdateAll(db.RW, blockedList)
if err != nil {
return errors.Err(err)
}
ownerChannel.BlockedListID.SetValid(list.ID)
ownerChannel.BlockedListInviteID.SetValid(list.ID)
err = ownerChannel.Update(db.RW, boil.Infer())
if err != nil {
return errors.Err(err)
}
}
return PopulateSharedBlockedList(reply, list)
}
// PopulateSharedBlockedList populates the information from a blocked list into a an api response format
func PopulateSharedBlockedList(list *commentapi.SharedBlockedList, modelList *model.BlockedList) error {
list.ID = modelList.ID
list.Name = &modelList.Name
list.Description = &modelList.Description
list.Category = &modelList.Category
if modelList.MemberInviteEnabled.Valid {
list.MemberInviteEnabled = &modelList.MemberInviteEnabled.Bool
}
if modelList.InviteExpiration.Valid {
list.InviteExpiration = &modelList.InviteExpiration.Uint64
}
if modelList.StrikeOne.Valid {
list.StrikeOne = &modelList.StrikeOne.Uint64
}
if modelList.StrikeTwo.Valid {
list.StrikeTwo = &modelList.StrikeTwo.Uint64
}
if modelList.StrikeThree.Valid {
list.StrikeThree = &modelList.StrikeThree.Uint64
}
if modelList.CurseJarAmount.Valid {
list.CurseJarAmount = &modelList.CurseJarAmount.Uint64
}
return nil
}