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

Add Event Webhook Support with DocRootChanged Event #1156

Merged
merged 24 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from 22 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
8 changes: 8 additions & 0 deletions api/converter/from_pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ func FromProject(pbProject *api.Project) *types.Project {
Name: pbProject.Name,
AuthWebhookURL: pbProject.AuthWebhookUrl,
AuthWebhookMethods: pbProject.AuthWebhookMethods,
EventWebhookURL: pbProject.EventWebhookUrl,
EventWebhookTypes: pbProject.EventWebhookTypes,
ClientDeactivateThreshold: pbProject.ClientDeactivateThreshold,
PublicKey: pbProject.PublicKey,
SecretKey: pbProject.SecretKey,
Expand Down Expand Up @@ -922,6 +924,12 @@ func FromUpdatableProjectFields(pbProjectFields *api.UpdatableProjectFields) (*t
if pbProjectFields.AuthWebhookMethods != nil {
updatableProjectFields.AuthWebhookMethods = &pbProjectFields.AuthWebhookMethods.Methods
}
if pbProjectFields.EventWebhookUrl != nil {
updatableProjectFields.EventWebhookURL = &pbProjectFields.EventWebhookUrl.Value
}
if pbProjectFields.EventWebhookTypes != nil {
updatableProjectFields.EventWebhookTypes = &pbProjectFields.EventWebhookTypes.Types
}
if pbProjectFields.ClientDeactivateThreshold != nil {
updatableProjectFields.ClientDeactivateThreshold = &pbProjectFields.ClientDeactivateThreshold.Value
}
Expand Down
12 changes: 12 additions & 0 deletions api/converter/to_pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ func ToProject(project *types.Project) *api.Project {
Name: project.Name,
AuthWebhookUrl: project.AuthWebhookURL,
AuthWebhookMethods: project.AuthWebhookMethods,
EventWebhookUrl: project.EventWebhookURL,
EventWebhookTypes: project.EventWebhookTypes,
ClientDeactivateThreshold: project.ClientDeactivateThreshold,
PublicKey: project.PublicKey,
SecretKey: project.SecretKey,
Expand Down Expand Up @@ -562,6 +564,16 @@ func ToUpdatableProjectFields(fields *types.UpdatableProjectFields) (*api.Updata
} else {
pbUpdatableProjectFields.AuthWebhookMethods = nil
}
if fields.EventWebhookURL != nil {
pbUpdatableProjectFields.EventWebhookUrl = &wrapperspb.StringValue{Value: *fields.EventWebhookURL}
}
if fields.EventWebhookTypes != nil {
pbUpdatableProjectFields.EventWebhookTypes = &api.UpdatableProjectFields_EventWebhookTypes{
Types: *fields.EventWebhookTypes,
}
} else {
pbUpdatableProjectFields.EventWebhookTypes = nil
}
if fields.ClientDeactivateThreshold != nil {
pbUpdatableProjectFields.ClientDeactivateThreshold = &wrapperspb.StringValue{
Value: *fields.ClientDeactivateThreshold,
Expand Down
46 changes: 41 additions & 5 deletions api/docs/yorkie/v1/admin.openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1463,11 +1463,10 @@ components:
type: object
yorkie.v1.Operation.Select:
additionalProperties: false
description: |-
NOTE(hackerwins): Select Operation is not used in the current version.
In the previous version, it was used to represent selection of Text.
However, it has been replaced by Presence now. It is retained for backward
compatibility purposes.
description: "NOTE(hackerwins): Select Operation is not used in the current
version.\r\n In the previous version, it was used to represent selection of
Text.\r\n However, it has been replaced by Presence now. It is retained for
backward\r\n compatibility purposes."
properties:
executedAt:
$ref: '#/components/schemas/yorkie.v1.TimeTicket'
Expand Down Expand Up @@ -1826,6 +1825,18 @@ components:
description: ""
title: created_at
type: object
eventWebhookTypes:
additionalProperties: false
description: ""
items:
type: string
title: event_webhook_types
type: array
eventWebhookUrl:
additionalProperties: false
description: ""
title: event_webhook_url
type: string
id:
additionalProperties: false
description: ""
Expand Down Expand Up @@ -2132,6 +2143,18 @@ components:
description: ""
title: client_deactivate_threshold
type: object
eventWebhookTypes:
$ref: '#/components/schemas/yorkie.v1.UpdatableProjectFields.EventWebhookTypes'
additionalProperties: false
description: ""
title: event_webhook_types
type: object
eventWebhookUrl:
$ref: '#/components/schemas/google.protobuf.StringValue'
additionalProperties: false
description: ""
title: event_webhook_url
type: object
name:
$ref: '#/components/schemas/google.protobuf.StringValue'
additionalProperties: false
Expand All @@ -2153,6 +2176,19 @@ components:
type: array
title: AuthWebhookMethods
type: object
yorkie.v1.UpdatableProjectFields.EventWebhookTypes:
additionalProperties: false
description: ""
properties:
types:
additionalProperties: false
description: ""
items:
type: string
title: types
type: array
title: EventWebhookTypes
type: object
yorkie.v1.UpdateProjectRequest:
additionalProperties: false
description: ""
Expand Down
12 changes: 12 additions & 0 deletions api/docs/yorkie/v1/cluster.openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,18 @@ components:
description: ""
title: created_at
type: object
eventWebhookTypes:
additionalProperties: false
description: ""
items:
type: string
title: event_webhook_types
type: array
eventWebhookUrl:
additionalProperties: false
description: ""
title: event_webhook_url
type: string
id:
additionalProperties: false
description: ""
Expand Down
57 changes: 45 additions & 12 deletions api/docs/yorkie/v1/resources.openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,8 @@ components:
type: object
yorkie.v1.ChangePack:
additionalProperties: false
description: |-
ChangePack is a message that contains all changes that occurred in a document.
It is used to synchronize changes between clients and servers.
description: "ChangePack is a message that contains all changes that occurred
in a document.\r\n It is used to synchronize changes between clients and servers."
properties:
changes:
additionalProperties: false
Expand Down Expand Up @@ -972,11 +971,10 @@ components:
type: object
yorkie.v1.Operation.Select:
additionalProperties: false
description: |-
NOTE(hackerwins): Select Operation is not used in the current version.
In the previous version, it was used to represent selection of Text.
However, it has been replaced by Presence now. It is retained for backward
compatibility purposes.
description: "NOTE(hackerwins): Select Operation is not used in the current
version.\r\n In the previous version, it was used to represent selection of
Text.\r\n However, it has been replaced by Presence now. It is retained for
backward\r\n compatibility purposes."
properties:
executedAt:
$ref: '#/components/schemas/yorkie.v1.TimeTicket'
Expand Down Expand Up @@ -1335,6 +1333,18 @@ components:
description: ""
title: created_at
type: object
eventWebhookTypes:
additionalProperties: false
description: ""
items:
type: string
title: event_webhook_types
type: array
eventWebhookUrl:
additionalProperties: false
description: ""
title: event_webhook_url
type: string
id:
additionalProperties: false
description: ""
Expand Down Expand Up @@ -1400,10 +1410,8 @@ components:
type: object
yorkie.v1.Snapshot:
additionalProperties: false
description: |-
///////////////////////////////////////
Messages for Snapshot //
///////////////////////////////////////
description: "///////////////////////////////////////\r\n Messages for Snapshot
\ //\r\n///////////////////////////////////////"
properties:
presences:
additionalProperties: false
Expand Down Expand Up @@ -1687,6 +1695,18 @@ components:
description: ""
title: client_deactivate_threshold
type: object
eventWebhookTypes:
$ref: '#/components/schemas/yorkie.v1.UpdatableProjectFields.EventWebhookTypes'
additionalProperties: false
description: ""
title: event_webhook_types
type: object
eventWebhookUrl:
$ref: '#/components/schemas/google.protobuf.StringValue'
additionalProperties: false
description: ""
title: event_webhook_url
type: object
name:
$ref: '#/components/schemas/google.protobuf.StringValue'
additionalProperties: false
Expand All @@ -1708,6 +1728,19 @@ components:
type: array
title: AuthWebhookMethods
type: object
yorkie.v1.UpdatableProjectFields.EventWebhookTypes:
additionalProperties: false
description: ""
properties:
types:
additionalProperties: false
description: ""
items:
type: string
title: types
type: array
title: EventWebhookTypes
type: object
yorkie.v1.User:
additionalProperties: false
description: ""
Expand Down
14 changes: 6 additions & 8 deletions api/docs/yorkie/v1/yorkie.openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -457,9 +457,8 @@ components:
type: object
yorkie.v1.ChangePack:
additionalProperties: false
description: |-
ChangePack is a message that contains all changes that occurred in a document.
It is used to synchronize changes between clients and servers.
description: "ChangePack is a message that contains all changes that occurred
in a document.\r\n It is used to synchronize changes between clients and servers."
properties:
changes:
additionalProperties: false
Expand Down Expand Up @@ -977,11 +976,10 @@ components:
type: object
yorkie.v1.Operation.Select:
additionalProperties: false
description: |-
NOTE(hackerwins): Select Operation is not used in the current version.
In the previous version, it was used to represent selection of Text.
However, it has been replaced by Presence now. It is retained for backward
compatibility purposes.
description: "NOTE(hackerwins): Select Operation is not used in the current
version.\r\n In the previous version, it was used to represent selection of
Text.\r\n However, it has been replaced by Presence now. It is retained for
backward\r\n compatibility purposes."
properties:
executedAt:
$ref: '#/components/schemas/yorkie.v1.TimeTicket'
Expand Down
42 changes: 42 additions & 0 deletions api/types/event_webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2025 The Yorkie Authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package types

// EventWebhookType represents event webhook type
type EventWebhookType string

const (
// DocRootChanged is an event that indicates the document's content was modified.
DocRootChanged EventWebhookType = "DocumentRootChanged"
)

// IsValidEventType checks whether the given event type is valid.
func IsValidEventType(eventType string) bool {
return eventType == string(DocRootChanged)
}

// EventWebhookAttribute represents the attribute of the webhook.
type EventWebhookAttribute struct {
Key string `json:"key"`
IssuedAt string `json:"issuedAt"`
}

// EventWebhookRequest represents the request of the webhook.
type EventWebhookRequest struct {
Type EventWebhookType `json:"type"`
Attributes EventWebhookAttribute `json:"attributes"`
}
14 changes: 14 additions & 0 deletions api/types/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ const (
// modified by a change.
DocChangedEvent DocEventType = "document-changed"

// DocRootChangedEvent is an event indicating that document's root content
// is being changed by operation.
DocRootChangedEvent DocEventType = "document-root-changed"

// DocWatchedEvent is an event that occurs when document is watched
// by other clients.
DocWatchedEvent DocEventType = "document-watched"
Expand All @@ -43,6 +47,16 @@ const (
DocBroadcastEvent DocEventType = "document-broadcast"
)

// WebhookType returns a matched event webhook type.
func (t DocEventType) WebhookType() types.EventWebhookType {
switch t {
case DocRootChangedEvent:
return types.DocRootChanged
default:
return ""
}
}

// DocEventBody includes additional data specific to the DocEvent.
type DocEventBody struct {
Topic string
Expand Down
25 changes: 25 additions & 0 deletions api/types/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ type Project struct {
// AuthWebhookMethods is the methods that run the authorization webhook.
AuthWebhookMethods []string `json:"auth_webhook_methods"`

// EventWebhookURL is the url of the event webhook.
EventWebhookURL string `json:"event_webhook_url"`

// EventWebhookTypes is the types that send the event webhook.
EventWebhookTypes []string `json:"event_webhook_types"`

// ClientDeactivateThreshold is the time after which clients in
// specific project are considered deactivate for housekeeping.
ClientDeactivateThreshold string `bson:"client_deactivate_threshold"`
Expand Down Expand Up @@ -73,3 +79,22 @@ func (p *Project) RequireAuth(method Method) bool {

return false
}

// RequireEventWebhook returns whether the given method requires to send event webhook.
func (p *Project) RequireEventWebhook(eventType EventWebhookType) bool {
if len(p.EventWebhookURL) == 0 {
return false
}

if len(p.EventWebhookTypes) == 0 {
return false
}

for _, t := range p.EventWebhookTypes {
if EventWebhookType(t) == eventType {
return true
}
}

return false
}
Loading
Loading