Skip to content

Commit

Permalink
added notifications and minor fixes
Browse files Browse the repository at this point in the history
Signed-off-by: dhruv <[email protected]>
  • Loading branch information
jaydee029 committed Nov 6, 2024
1 parent fbc2c09 commit bf330ea
Show file tree
Hide file tree
Showing 18 changed files with 403 additions and 62 deletions.
19 changes: 13 additions & 6 deletions comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Comment struct {
Likes_count int `json:"likes,omitempty"`
Liked bool `json:"liked,omitempty"`
Mine bool `json:"mine,omitempty"`
User *User `json:"user,omitempty"`
Body string `json:"body"`
}

Expand Down Expand Up @@ -107,12 +108,18 @@ func (cfg *apiconfig) postComment(w http.ResponseWriter, r *http.Request) {
}
tx = nil

respondWithJson(w, http.StatusAccepted, Comment{
Id: comment.ID,
Body: comment.Body,
Proseid: comment.ProseID,
Created_at: comment.CreatedAt,
})
var c Comment

c.Body = comment.Body
c.Created_at = comment.CreatedAt
c.Proseid = comment.ProseID
c.Id = comment.ID
c.Userid = comment.UserID
c.Mine = true

go cfg.BroadcastComment(c)

respondWithJson(w, http.StatusAccepted, c)
}

func (cfg *apiconfig) Getcomments(w http.ResponseWriter, r *http.Request) {
Expand Down
59 changes: 24 additions & 35 deletions create_prose.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"strings"
"time"
Expand Down Expand Up @@ -117,14 +118,6 @@ func (cfg *apiconfig) postProse(w http.ResponseWriter, r *http.Request) {
Mine: true,
})

/*timelineuuid := uuid.New().String()
var timelineId pgtype.UUID
err = timelineId.Scan(timelineuuid)
if err != nil {
fmt.Println("Error setting timline id:", err)
}*/

err = qtx.InserinTimeline(r.Context(), database.InserinTimelineParams{
ProseID: post_pgUUID,
UserID: pgUUID,
Expand All @@ -147,48 +140,44 @@ func (cfg *apiconfig) postProse(w http.ResponseWriter, r *http.Request) {
respondWithError(w, http.StatusInternalServerError, "couldn't commit the transaction")
}

go func(p Prose) {

u, err := cfg.DB.GetUserbyId(r.Context(), p.Userid)
go cfg.prosecreated(tl.Post)

if err != nil {
respondWithError(w, http.StatusInternalServerError, "couldn't fetch the user")
return
}

p.User.Email = u.Email
p.User.ID = u.ID
p.User.Name = u.Name
p.User.Username = u.Username
p.Mine = false
}

tl, err := cfg.fanoutprose(r.Context(), p)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "couldn't fanout the post")
return
}
func (cfg *apiconfig) prosecreated(p Prose) {
u, err := cfg.DB.GetUserbyId(context.Background(), p.Userid)

for _, i := range tl {
fmt.Println(i)
//TODO: Broadcast
}
if err != nil {
log.Println(err)
return
}

}(tl.Post)
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(ctx context.Context, p Prose) ([]database.FetchTimelineItemsRow, error) {
func (cfg *apiconfig) fanoutprose(p Prose) {

items, err := cfg.DB.FetchTimelineItems(ctx, database.FetchTimelineItemsParams{
items, err := cfg.DB.FetchTimelineItems(context.Background(), database.FetchTimelineItemsParams{
ProseID: p.ID,
FolloweeID: p.Userid,
})

if err != nil {
return []database.FetchTimelineItemsRow{}, err
log.Println(err)
return
}

return items, nil
for _, i := range items {
fmt.Println(i)
//TODO: Broadcast
}

}

Expand Down
2 changes: 1 addition & 1 deletion internal/auth/authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func Tokenize(id uuid.UUID, secret_key string) (string, error) {
claims := &jwt.RegisteredClaims{
Issuer: "verses-access",
IssuedAt: jwt.NewNumericDate(time.Now().UTC()),
ExpiresAt: jwt.NewNumericDate(time.Now().UTC().Add(time.Duration(60*60) * time.Second)), // 1 hour
ExpiresAt: jwt.NewNumericDate(time.Now().UTC().AddDate(0, 0, 1)), // 1 day
Subject: id.String(),
}

Expand Down
117 changes: 117 additions & 0 deletions internal/database/mentions.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions internal/database/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 36 additions & 1 deletion internal/database/notification.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/database/prose.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ func main() {
})

s.Get("/healthz", apireadiness)
s.Post("/users", apicfg.createUser)
s.Post("/login", apicfg.userLogin)
s.Post("/prose", apicfg.postProse)
s.Get("/{username}/prose", apicfg.getProse)
s.Get("/prose/{proseId}", apicfg.ProsebyId)
Expand All @@ -95,8 +97,6 @@ func main() {
s.Post("/{proseid}/comments", apicfg.postComment)
s.Get("/{proseid}/comments", apicfg.Getcomments)
s.Post("/comments/{commentid}/togglelike", apicfg.toggCommentLike)
s.Post("/users", apicfg.createUser)
s.Post("/login", apicfg.userLogin)
s.Post("/refresh", apicfg.verifyRefresh)
s.Post("/revoke", apicfg.revokeToken)
s.Put("/users", apicfg.updateUser)
Expand Down
5 changes: 3 additions & 2 deletions notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import (

type Notification struct {
ID pgtype.UUID `json:"id"`
User_id pgtype.UUID `json:"user_id"`
Userid pgtype.UUID `json:"userid"`
Proseid pgtype.UUID `json:"proseid"`
Actors []string `json:"actors"`
Generated_at pgtype.Timestamp `json:"generated_at"`
Read bool `json:"read"`
Expand Down Expand Up @@ -80,7 +81,7 @@ func (cfg *apiconfig) Notifications(w http.ResponseWriter, r *http.Request) {
for _, k := range notifications {
Notifications = append(Notifications, Notification{
ID: k.ID,
User_id: k.UserID,
Userid: k.UserID,
Actors: k.Actors,
Generated_at: k.GeneratedAt,
Type: k.Type,
Expand Down
Loading

0 comments on commit bf330ea

Please sign in to comment.