Skip to content

Commit da75d33

Browse files
committed
fixing comments
Signed-off-by: Dhruv Jain <92215138+jaydee029@users.noreply.github.com>
1 parent fc47187 commit da75d33

File tree

7 files changed

+62
-52
lines changed

7 files changed

+62
-52
lines changed

api/handler/comments.go

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"fmt"
77
"mime"
88
"net/http"
9-
"strconv"
109
"strings"
1110
"time"
1211

@@ -181,32 +180,35 @@ func (cfg *Handler) Getcomments(w http.ResponseWriter, r *http.Request) {
181180
return
182181
}
183182

184-
var before int
185-
beforestr := r.URL.Query().Get("before")
183+
//var before int
184+
// before := sql.NullInt32{}
186185

187-
if beforestr != "" {
188-
before, err = strconv.Atoi(beforestr)
189-
if err != nil {
190-
respondWithError(w, http.StatusInternalServerError, err.Error())
191-
return
192-
}
193-
}
186+
// beforestr := r.URL.Query().Get("before")
194187

195-
limitstr := r.URL.Query().Get("limit")
196-
if limitstr == "" {
197-
limitstr = "10"
198-
}
199-
limit, err := strconv.Atoi(limitstr)
200-
if err != nil {
201-
respondWithError(w, http.StatusInternalServerError, err.Error())
202-
return
203-
}
188+
// if beforestr != "" {
189+
// beforeint, err := strconv.Atoi(beforestr)
190+
// if err != nil {
191+
// respondWithError(w, http.StatusInternalServerError, err.Error())
192+
// return
193+
// }
194+
// before = sql.NullInt32{Int32: int32(beforeint)}
195+
// }
196+
197+
// limitstr := r.URL.Query().Get("limit")
198+
// if limitstr == "" {
199+
// limitstr = "10"
200+
// }
201+
// limit, err := strconv.Atoi(limitstr)
202+
// if err != nil {
203+
// respondWithError(w, http.StatusInternalServerError, err.Error())
204+
// return
205+
// }
204206

205207
comments, err := cfg.DB.GetComments(r.Context(), database.GetCommentsParams{
206208
UserID: pgUUID,
207209
ProseID: proseid,
208-
Column3: int32(before),
209-
Limit: int32(limit),
210+
// ID: before.Int32,
211+
// Limit: int32(limit),
210212
})
211213

212214
if err != nil {

api/handler/notifycomment.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,14 @@ func (cfg *Handler) CommentNotification(c Comment) {
2626
log.Println("error while converting timestamp to pgtype:", zap.Error(err))
2727
return
2828
}
29+
user_id, err := cfg.DB.GetUserfromProse(context.Background(), c.Proseid)
30+
if err != nil {
31+
cfg.logger.Info("error while fetching user id from prose during comment notifications:", zap.Error(err))
32+
return
33+
}
2934

3035
notification, err := cfg.DB.InsertCommentNotification(context.Background(), database.InsertCommentNotificationParams{
31-
UserID: c.Userid,
36+
UserID: user_id,
3237
ProseID: c.Proseid,
3338
GeneratedAt: generated_at_pgtype,
3439
ID: nid_pgtype,
@@ -43,7 +48,7 @@ func (cfg *Handler) CommentNotification(c Comment) {
4348

4449
n.ID = nid_pgtype
4550
n.Actors = notification.Actors
46-
n.Userid = c.Userid
51+
n.Userid = user_id
4752
n.Proseid = c.Proseid
4853
n.Generated_at = notification.GeneratedAt
4954
n.Type = "comment"

api/handler/realtime.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ package handler
22

33
import (
44
"context"
5-
"fmt"
65
"net/http"
7-
"time"
86

97
"github.com/google/uuid"
108
"github.com/jackc/pgx/v5/pgtype"
@@ -24,8 +22,8 @@ func (cfg *Handler) subscribeTotimeline(w http.ResponseWriter, ctx context.Conte
2422
if !ok {
2523
respondWithError(w, http.StatusBadRequest, "streaming unsupported")
2624
}
27-
heartbeat := 10 * time.Second
28-
ticker := time.NewTicker(heartbeat)
25+
//heartbeat := 10 * time.Second
26+
//ticker := time.NewTicker(heartbeat)
2927
subch, err := pubsub.Consume[timeline_item](cfg.pubsub, "timeline_direct", "timeline_queue", "timeline_item."+uuid.UUID(userid.Bytes).String())
3028
if err != nil {
3129
cfg.logger.Info("error consuming items:", zap.Error(err))
@@ -41,14 +39,14 @@ func (cfg *Handler) subscribeTotimeline(w http.ResponseWriter, ctx context.Conte
4139
cfg.logger.Info("Received timeline item", zap.String("body", item.Post.Body))
4240
writesse(w, "timeline", item)
4341
f.Flush()
44-
case <-ticker.C:
45-
cfg.logger.Info("Sending SSE heartbeat", zap.String("routingKey", "timeline_item."+uuid.UUID(userid.Bytes).String()))
46-
_, err := fmt.Fprintf(w, ": heartbeat\n\n")
47-
if err != nil {
48-
cfg.logger.Error("Error writing SSE heartbeat", zap.Error(err), zap.String("routingKey", "timeline_item."+uuid.UUID(userid.Bytes).String()))
49-
return
50-
}
51-
f.Flush()
42+
// case <-ticker.C:
43+
// cfg.logger.Info("Sending SSE heartbeat", zap.String("routingKey", "timeline_item."+uuid.UUID(userid.Bytes).String()))
44+
// _, err := fmt.Fprintf(w, ": heartbeat\n\n")
45+
// if err != nil {
46+
// cfg.logger.Error("Error writing SSE heartbeat", zap.Error(err), zap.String("routingKey", "timeline_item."+uuid.UUID(userid.Bytes).String()))
47+
// return
48+
// }
49+
// f.Flush()
5250
case <-ctx.Done():
5351
return
5452

internal/database/comments.sql.go

Lines changed: 4 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/database/users.sql.go

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sql/queries/comments.sql

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@ INSERT INTO comments(prose_id,user_id,body,created_at) VALUES($1,$2,$3,$4)
33
RETURNING *;
44

55
-- name: GetComments :many
6+
-- $3:nullable
67
SELECT c.id, c.body, c.created_at, c.likes_count, u.username,
78
CASE WHEN c.user_id=$1 THEN true ELSE false END AS Mine,
89
CASE WHEN Likes.user_id IS NOT NULL THEN true ELSE false END AS Liked
910
From comments AS c INNER JOIN users as u ON
1011
c.user_id=u.id
1112
LEFT JOIN comment_likes as Likes
1213
ON Likes.user_id=$1 AND Likes.comment_id=c.id
13-
WHERE c.prose_id=$2 AND
14-
$3::INT IS NULL OR c.id<$3
15-
ORDER BY c.id DESC
16-
LIMIT $4;
14+
WHERE c.prose_id=$2
15+
ORDER BY c.id DESC;
1716

1817
-- name: UpdateCommentCount :exec
1918
UPDATE prose

sql/queries/users.sql

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,7 @@ FROM users LEFT JOIN follows as followers
4141
ON followers.followee_id=$1 AND followers.follower_id=id
4242
LEFT JOIN follows as followees
4343
ON followees.follower_id=$1 AND followees.followee_id=id
44-
WHERE username =$2;
44+
WHERE username =$2;
45+
46+
-- name: GetUserfromProse :one
47+
SELECT author_id FROM prose WHERE id=$1;

0 commit comments

Comments
 (0)