Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

someone posted your work notification #1225

Merged
merged 3 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions db/gen/coredb/query.sql.go

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

3 changes: 3 additions & 0 deletions db/queries/core/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,9 @@ INSERT INTO notifications (id, owner_id, action, data, event_ids, feed_event_id,
-- name: CreateContractNotification :one
INSERT INTO notifications (id, owner_id, action, data, event_ids, feed_event_id, post_id, comment_id, contract_id, mention_id) VALUES ($1, $2, $3, $4, $5, sqlc.narg('feed_event'), sqlc.narg('post'), sqlc.narg('comment'), $6, $7) RETURNING *;

-- name: CreateUserPostedYourWorkNotification :one
INSERT INTO notifications (id, owner_id, action, data, event_ids, post_id, contract_id) VALUES ($1, $2, $3, $4, $5, sqlc.narg('post'), $6) RETURNING *;

-- name: CreateSimpleNotification :one
INSERT INTO notifications (id, owner_id, action, data, event_ids) VALUES ($1, $2, $3, $4, $5) RETURNING *;

Expand Down
4 changes: 4 additions & 0 deletions event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func AddTo(ctx *gin.Context, disableDataloaderCaching bool, notif *notifications
sender.addDelayedHandler(notifications, persist.ActionMentionUser, notificationHandler)
sender.addDelayedHandler(notifications, persist.ActionMentionCommunity, notificationHandler)
sender.addDelayedHandler(notifications, persist.ActionNewTokensReceived, notificationHandler)
sender.addDelayedHandler(notifications, persist.ActionUserPostedYourWork, notificationHandler)

sender.feed = feed
sender.notifications = notifications
Expand Down Expand Up @@ -493,6 +494,8 @@ func (h notificationHandler) createNotificationDataForEvent(event db.Event) (dat
data.NewTokenQuantity = event.Data.NewTokenQuantity
case persist.ActionReplyToComment:
data.OriginalCommentID = event.SubjectID
case persist.ActionUserPostedYourWork:
data.YourContractID = event.Data.YourContractID
default:
logger.For(nil).Debugf("no notification data for event: %s", event.Action)
}
Expand Down Expand Up @@ -555,6 +558,7 @@ func (h notificationHandler) findOwnerForNotificationFromEvent(ctx context.Conte
return "", nil
}
return u.CreatorUserID, nil

}

return "", fmt.Errorf("no owner found for event: %s", event.Action)
Expand Down
55 changes: 53 additions & 2 deletions publicapi/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ func (api FeedAPI) PostTokens(ctx context.Context, tokenIDs []persist.DBID, ment
return "", err
}

contractIDs, _ := util.Map(contracts, func(c db.Contract) (persist.DBID, error) {
return c.ID, nil
contractIDs := util.MapWithoutError(contracts, func(c db.Contract) persist.DBID {
return c.ID
})

tx, err := api.repos.BeginTx(ctx)
Expand Down Expand Up @@ -207,6 +207,25 @@ func (api FeedAPI) PostTokens(ctx context.Context, tokenIDs []persist.DBID, ment
return "", err
}

creators, _ := api.loaders.ContractCreatorByContractID.LoadAll(contractIDs)
for _, creator := range creators {
if creator.CreatorUserID == "" {
continue
}
err = event.Dispatch(ctx, db.Event{
ActorID: persist.DBIDToNullStr(actorID),
Action: persist.ActionUserPostedYourWork,
ResourceTypeID: persist.ResourceTypeContract,
UserID: creator.CreatorUserID,
SubjectID: creator.ContractID,
PostID: postID,
ContractID: creator.ContractID,
})
if err != nil {
logger.For(ctx).Errorf("error dispatching event: %v", err)
}
}

err = event.Dispatch(ctx, db.Event{
ActorID: persist.DBIDToNullStr(actorID),
Action: persist.ActionUserPosted,
Expand Down Expand Up @@ -273,6 +292,22 @@ func (api FeedAPI) ReferralPostToken(ctx context.Context, t persist.TokenIdentif
return postID, err
}

creator, _ := api.loaders.ContractCreatorByContractID.Load(contract.ID)
if creator.CreatorUserID != "" {
err = event.Dispatch(ctx, db.Event{
ActorID: persist.DBIDToNullStr(userID),
Action: persist.ActionUserPostedYourWork,
ResourceTypeID: persist.ResourceTypeContract,
UserID: creator.CreatorUserID,
SubjectID: creator.ContractID,
PostID: postID,
ContractID: creator.ContractID,
})
if err != nil {
logger.For(ctx).Errorf("error dispatching event: %v", err)
}
}

err = event.Dispatch(ctx, db.Event{
ActorID: persist.DBIDToNullStr(user.ID),
Action: persist.ActionUserPosted,
Expand Down Expand Up @@ -311,6 +346,22 @@ func (api FeedAPI) ReferralPostToken(ctx context.Context, t persist.TokenIdentif
return postID, err
}

creator, _ := api.loaders.ContractCreatorByContractID.Load(synced.Contract)
if creator.CreatorUserID != "" {
err = event.Dispatch(ctx, db.Event{
ActorID: persist.DBIDToNullStr(userID),
Action: persist.ActionUserPostedYourWork,
ResourceTypeID: persist.ResourceTypeContract,
UserID: creator.CreatorUserID,
SubjectID: creator.ContractID,
PostID: postID,
ContractID: creator.ContractID,
})
if err != nil {
logger.For(ctx).Errorf("error dispatching event: %v", err)
}
}

err = event.Dispatch(ctx, db.Event{
ActorID: persist.DBIDToNullStr(user.ID),
Action: persist.ActionUserPosted,
Expand Down
38 changes: 38 additions & 0 deletions service/notifications/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ func New(queries *db.Queries, pub *pubsub.Client, taskClient *cloudtasks.Client,
notifDispatcher.AddHandler(persist.ActionReplyToComment, def)
notifDispatcher.AddHandler(persist.ActionMentionUser, def)
notifDispatcher.AddHandler(persist.ActionMentionCommunity, def)
notifDispatcher.AddHandler(persist.ActionUserPostedYourWork, def)

// notification actions that are grouped by token id
notifDispatcher.AddHandler(persist.ActionNewTokensReceived, tokenIDGrouped)
Expand Down Expand Up @@ -723,6 +724,31 @@ func createPushMessage(ctx context.Context, notif db.Notification, queries *db.Q

}

if notif.Action == persist.ActionUserPostedYourWork {

post, err := queries.GetPostByID(ctx, notif.PostID)
if err != nil {
return task.PushNotificationMessage{}, err
}
actor, err := queries.GetUserById(ctx, post.ActorID)
if err != nil {
return task.PushNotificationMessage{}, err
}

if err := limiter.tryMention(ctx, actor.ID, notif.OwnerID, notif.FeedEventID); err != nil {
return task.PushNotificationMessage{}, err
}

if !actor.Username.Valid {
return task.PushNotificationMessage{}, fmt.Errorf("user with ID=%s has no username", actor.ID)
}
contract, err := queries.GetContractByID(ctx, notif.ContractID)
if err != nil {
return task.PushNotificationMessage{}, err
}
message.Body = fmt.Sprintf("%s posted your work: %s", actor.Username.String, contract.Name.String)
}

return task.PushNotificationMessage{}, fmt.Errorf("unsupported notification action: %s", notif.Action)
}

Expand All @@ -740,6 +766,8 @@ func actionSupportsPushNotifications(action persist.Action) bool {
return true
case persist.ActionAdmiredPost:
return true
case persist.ActionUserPostedYourWork:
return true
default:
return false
}
Expand Down Expand Up @@ -972,6 +1000,16 @@ func addNotification(ctx context.Context, notif db.Notification, queries *db.Que
MentionID: notif.MentionID,
ContractID: notif.ContractID,
})
case persist.ActionUserPostedYourWork:
return queries.CreateUserPostedYourWorkNotification(ctx, db.CreateUserPostedYourWorkNotificationParams{
ID: id,
OwnerID: notif.OwnerID,
Action: notif.Action,
Data: notif.Data,
EventIds: notif.EventIds,
Post: util.ToNullString(notif.PostID.String(), true),
ContractID: notif.ContractID,
})
default:
return db.Notification{}, fmt.Errorf("unknown notification action: %s", notif.Action)
}
Expand Down
2 changes: 2 additions & 0 deletions service/persist/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const (
ActionUserCreated Action = "UserCreated"
ActionUserFollowedUsers Action = "UserFollowedUsers"
ActionUserPosted Action = "UserPosted"
ActionUserPostedYourWork Action = "UserPostedYourWork"
ActionCollectorsNoteAddedToToken Action = "CollectorsNoteAddedToToken"
ActionCollectionCreated Action = "CollectionCreated"
ActionCollectorsNoteAddedToCollection Action = "CollectorsNoteAddedToCollection"
Expand Down Expand Up @@ -58,6 +59,7 @@ type EventData struct {
GalleryNewTokenIDs map[DBID]DBIDList `json:"gallery_new_token_ids"`
GalleryNewCollections DBIDList `json:"gallery_new_collections"`
GalleryNewTokenCollectorsNotes map[DBID]string `json:"gallery_new_token_collectors_notes"`
YourContractID DBID `json:"your_contract_id"`
}

type FeedEventData struct {
Expand Down
3 changes: 3 additions & 0 deletions service/persist/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type NotificationData struct {
NewTokenID DBID `json:"new_token_id,omitempty"`
NewTokenQuantity HexString `json:"new_token_quantity,omitempty"`
OriginalCommentID DBID `json:"original_comment_id,omitempty"`
YourContractID DBID `json:"your_contract_id,omitempty"`
}

func (n NotificationData) Validate() NotificationData {
Expand All @@ -25,6 +26,7 @@ func (n NotificationData) Validate() NotificationData {
result.NewTokenID = n.NewTokenID
result.NewTokenQuantity = n.NewTokenQuantity
result.OriginalCommentID = n.OriginalCommentID
result.YourContractID = n.YourContractID

return result
}
Expand All @@ -40,6 +42,7 @@ func (n NotificationData) Concat(other NotificationData) NotificationData {
result.NewTokenQuantity = other.NewTokenQuantity.Add(n.NewTokenQuantity)
result.NewTokenID = DBID(util.FirstNonEmptyString(other.NewTokenID.String(), n.NewTokenID.String()))
result.OriginalCommentID = DBID(util.FirstNonEmptyString(other.OriginalCommentID.String(), n.OriginalCommentID.String()))
result.YourContractID = DBID(util.FirstNonEmptyString(other.YourContractID.String(), n.YourContractID.String()))

return result.Validate()
}
Expand Down
Loading