-
Notifications
You must be signed in to change notification settings - Fork 0
/
createprose.go
198 lines (159 loc) · 4.47 KB
/
createprose.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
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"strings"
"time"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgtype"
auth "github.com/jaydee029/Verses/internal/auth"
"github.com/jaydee029/Verses/internal/database"
)
type Prose struct {
ID pgtype.UUID `json:"id,omitempty"`
Userid pgtype.UUID `json:"userid,omitempty"`
Body string `json:"body"`
User *User `json:"user,omitempty"`
Created_at pgtype.Timestamp `json:"created_at"`
Updated_at pgtype.Timestamp `json:"Updated_at"`
Mine bool `json:"mine"`
Liked bool `json:"liked"`
Likes_count int `json:"likes_count"`
Username string `json:"username,omitempty"`
Comments int `json:"comments,omitempty"`
}
type body struct {
Body string `json:"body"`
}
func (cfg *apiconfig) postProse(w http.ResponseWriter, r *http.Request) {
token, err := auth.BearerHeader(r.Header)
if err != nil {
respondWithError(w, http.StatusUnauthorized, "error decoding auth header:"+err.Error())
return
}
authorid, err := auth.ValidateToken(token, cfg.jwtsecret)
if err != nil {
respondWithError(w, http.StatusUnauthorized, "error parsing the userid:"+err.Error())
return
}
decoder := json.NewDecoder(r.Body)
params := body{}
err = decoder.Decode(¶ms)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "parameters couldn't be decoded")
return
}
cleanText := strings.TrimSpace(params.Body)
if len([]rune(cleanText)) > 280 || cleanText == "" {
respondWithError(w, http.StatusBadRequest, "Prose is invalid")
return
}
var pgUUID pgtype.UUID
err = pgUUID.Scan(authorid)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "error parsing into pgtype.uuid")
return
}
uuids := uuid.New().String()
var post_pgUUID pgtype.UUID
err = post_pgUUID.Scan(uuids)
if err != nil {
fmt.Println("Error setting UUID:", err)
}
//total, _ := cfg.DB.Countprose(r.Context(), pgUUID)
content := profane(cleanText)
var pgtime pgtype.Timestamp
err = pgtime.Scan(time.Now().UTC())
if err != nil {
respondWithError(w, http.StatusInternalServerError, "error parsing timestamp into pgtype value")
return
}
tx, err := cfg.DBpool.Begin(r.Context())
if err != nil {
respondWithError(w, http.StatusInternalServerError, "error starting the transaction"+err.Error())
return
}
qtx := cfg.DB.WithTx(tx)
defer tx.Rollback(r.Context())
prose, err := qtx.Createprose(r.Context(), database.CreateproseParams{
ID: post_pgUUID,
AuthorID: pgUUID,
Body: content,
CreatedAt: pgtime,
UpdatedAt: pgtime,
})
if err != nil {
respondWithError(w, http.StatusInternalServerError, "couldn't create Prose")
return
}
respondWithJson(w, http.StatusCreated, Prose{
ID: post_pgUUID,
Body: prose.Body,
Created_at: prose.CreatedAt,
Updated_at: prose.UpdatedAt,
Mine: true,
})
err = qtx.InserinTimeline(r.Context(), database.InserinTimelineParams{
ProseID: post_pgUUID,
UserID: pgUUID,
})
if err != nil {
respondWithError(w, http.StatusInternalServerError, "couldn't insert Prose in the timeline")
return
}
var tl timeline_item
//tl.Userid = pgUUID
tl.Post.ID = post_pgUUID
tl.Post.Mine = true
tl.Post.Userid = pgUUID
tl.Post.Body = cleanText
err = tx.Commit(r.Context())
if err != nil {
respondWithError(w, http.StatusInternalServerError, "couldn't commit the transaction")
}
go cfg.prosecreation(tl.Post)
}
func (cfg *apiconfig) prosecreation(p Prose) {
u, err := cfg.DB.GetUserbyId(context.Background(), p.Userid)
if err != nil {
log.Println(err)
return
}
p.User.Email = u.Email
p.User.ID = u.ID
p.User.Name = u.Name
p.User.Username = u.Username
p.Mine = false
go cfg.fanoutprose(p)
go cfg.notifypostmentions(p)
}
func (cfg *apiconfig) fanoutprose(p Prose) {
items, err := cfg.DB.FetchTimelineItems(context.Background(), database.FetchTimelineItemsParams{
ProseID: p.ID,
FolloweeID: p.Userid,
})
if err != nil {
log.Println(err)
return
}
for _, k := range items {
var ti timeline_item
ti.Id = int(k.ID)
ti.Userid = k.UserID
ti.Post = p
go cfg.Broadcasttimeline(ti)
}
}
func profane(content string) string {
contentslice := strings.Split(content, " ")
for i, word := range contentslice {
wordl := strings.ToLower(word)
if wordl == "fuck" || wordl == "shit" || wordl == "fornax" {
contentslice[i] = "****"
}
}
return strings.Join(contentslice, " ")
}