This commit is contained in:
kolaente 2021-02-02 20:14:15 +01:00
parent a2396a093b
commit 8b84105db1
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
24 changed files with 81 additions and 30 deletions

View File

@ -32,10 +32,12 @@ import (
var pubsub *gochannel.GoChannel
// Event represents the event interface used by all events
type Event interface {
Name() string
}
// InitEvents sets up everything needed to work with events
func InitEvents() (err error) {
logger := log.NewWatermillLogger()
@ -78,6 +80,7 @@ func InitEvents() (err error) {
return router.Run(context.Background())
}
// Dispatch dispatches an event
func Dispatch(event Event) error {
if isUnderTest {
dispatchedTestEvents = append(dispatchedTestEvents, event)

View File

@ -18,6 +18,7 @@ package events
import "github.com/ThreeDotsLabs/watermill/message"
// Listener represents something that listens to events
type Listener interface {
Handle(payload message.Payload) error
Name() string
@ -29,6 +30,7 @@ func init() {
listeners = make(map[string][]Listener)
}
func RegisterListener(topicName string, listener Listener) {
listeners[topicName] = append(listeners[topicName], listener)
// RegisterListener is used to register a listener when a specific event happens
func RegisterListener(name string, listener Listener) {
listeners[name] = append(listeners[name], listener)
}

View File

@ -17,8 +17,9 @@
package events
import (
"github.com/stretchr/testify/assert"
"testing"
"github.com/stretchr/testify/assert"
)
var (

View File

@ -17,6 +17,8 @@
package initialize
import (
"time"
"code.vikunja.io/api/pkg/config"
"code.vikunja.io/api/pkg/cron"
"code.vikunja.io/api/pkg/events"
@ -29,7 +31,6 @@ import (
migrator "code.vikunja.io/api/pkg/modules/migration"
"code.vikunja.io/api/pkg/red"
"code.vikunja.io/api/pkg/user"
"time"
)
// LightInit will only fullInit config, redis, logger but no db connection.

View File

@ -17,7 +17,6 @@
package integrations
import (
"code.vikunja.io/api/pkg/events"
"net/http"
"net/http/httptest"
"net/url"
@ -25,6 +24,8 @@ import (
"strings"
"testing"
"code.vikunja.io/api/pkg/events"
"code.vikunja.io/api/pkg/config"
"code.vikunja.io/api/pkg/db"
"code.vikunja.io/api/pkg/files"

View File

@ -24,7 +24,6 @@ import (
"code.vikunja.io/api/pkg/config"
"github.com/ThreeDotsLabs/watermill"
"github.com/op/go-logging"
"xorm.io/xorm/log"
)
const watermillFmt = `%{color}%{time:` + time.RFC3339Nano + `}: %{level}` + "\t" + `▶ [EVENTS] %{id:03x}%{color:reset} %{message}`
@ -33,7 +32,6 @@ const watermillLogModule = `vikunja_events`
type WatermillLogger struct {
logger *logging.Logger
level log.LogLevel
}
func NewWatermillLogger() *WatermillLogger {

View File

@ -25,20 +25,24 @@ import (
// Task Events //
/////////////////
// TaskCreatedEvent represents an event where a task has been created
type TaskCreatedEvent struct {
Task *Task
Doer web.Auth
}
// Name defines the name for TaskCreatedEvent
func (t *TaskCreatedEvent) Name() string {
return "task.created"
}
// TaskUpdatedEvent represents an event where a task has been updated
type TaskUpdatedEvent struct {
Task *Task
Doer web.Auth
}
// Name defines the name for TaskUpdatedEvent
func (t *TaskUpdatedEvent) Name() string {
return "task.updated"
}
@ -49,27 +53,31 @@ type TaskDeletedEvent struct {
Doer web.Auth
}
// TopicName defines the name for TaskDeletedEvent
// Name defines the name for TaskDeletedEvent
func (t *TaskDeletedEvent) Name() string {
return "task.deleted"
}
// TaskAssigneeCreatedEvent represents an event where a task has been assigned to a user
type TaskAssigneeCreatedEvent struct {
Task *Task
Assignee *user.User
Doer web.Auth
}
// Name defines the name for TaskAssigneeCreatedEvent
func (t *TaskAssigneeCreatedEvent) Name() string {
return "task.assignee.created"
}
// TaskCommentCreatedEvent represents an event where a task comment has been created
type TaskCommentCreatedEvent struct {
Task *Task
Comment *TaskComment
Doer web.Auth
}
// Name defines the name for TaskCommentCreatedEvent
func (t *TaskCommentCreatedEvent) Name() string {
return "task.comment.created"
}
@ -78,20 +86,24 @@ func (t *TaskCommentCreatedEvent) Name() string {
// Namespace Events //
//////////////////////
// NamespaceCreatedEvent represents an event where a namespace has been created
type NamespaceCreatedEvent struct {
Namespace *Namespace
Doer web.Auth
}
// Name defines the name for NamespaceCreatedEvent
func (n *NamespaceCreatedEvent) Name() string {
return "namespace.created"
}
// NamespaceUpdatedEvent represents an event where a namespace has been updated
type NamespaceUpdatedEvent struct {
Namespace *Namespace
Doer web.Auth
}
// Name defines the name for NamespaceUpdatedEvent
func (n *NamespaceUpdatedEvent) Name() string {
return "namespace.updated"
}
@ -111,29 +123,35 @@ func (t *NamespaceDeletedEvent) Name() string {
// List Events //
/////////////////
// ListCreatedEvent represents an event where a list has been created
type ListCreatedEvent struct {
List *List
Doer web.Auth
}
// Name defines the name for ListCreatedEvent
func (l *ListCreatedEvent) Name() string {
return "list.created"
}
// ListUpdatedEvent represents an event where a list has been updated
type ListUpdatedEvent struct {
List *List
Doer web.Auth
}
// Name defines the name for ListUpdatedEvent
func (l *ListUpdatedEvent) Name() string {
return "list.updated"
}
// ListDeletedEvent represents an event where a list has been deleted
type ListDeletedEvent struct {
List *List
Doer web.Auth
}
// Name defines the name for ListDeletedEvent
func (t *ListDeletedEvent) Name() string {
return "list.deleted"
}
@ -142,42 +160,50 @@ func (t *ListDeletedEvent) Name() string {
// Sharing Events //
////////////////////
// ListSharedWithUserEvent represents an event where a list has been shared with a user
type ListSharedWithUserEvent struct {
List *List
User *user.User
Doer web.Auth
}
// Name defines the name for ListSharedWithUserEvent
func (l *ListSharedWithUserEvent) Name() string {
return "list.shared.user"
}
// ListSharedWithTeamEvent represents an event where a list has been shared with a team
type ListSharedWithTeamEvent struct {
List *List
Team *Team
Doer web.Auth
}
// Name defines the name for ListSharedWithTeamEvent
func (l *ListSharedWithTeamEvent) Name() string {
return "list.shared.team"
}
// NamespaceSharedWithUserEvent represents an event where a namespace has been shared with a user
type NamespaceSharedWithUserEvent struct {
Namespace *Namespace
User *user.User
Doer web.Auth
}
// Name defines the name for NamespaceSharedWithUserEvent
func (n *NamespaceSharedWithUserEvent) Name() string {
return "namespace.shared.user"
}
// NamespaceSharedWithTeamEvent represents an event where a namespace has been shared with a team
type NamespaceSharedWithTeamEvent struct {
Namespace *Namespace
Team *Team
Doer web.Auth
}
// Name defines the name for NamespaceSharedWithTeamEvent
func (n *NamespaceSharedWithTeamEvent) Name() string {
return "namespace.shared.team"
}
@ -186,12 +212,14 @@ func (n *NamespaceSharedWithTeamEvent) Name() string {
// Team Events //
/////////////////
// TeamMemberAddedEvent defines an event where a user is added to a team
type TeamMemberAddedEvent struct {
Team *Team
Member *user.User
Doer web.Auth
}
// Name defines the name for TeamMemberAddedEvent
func (t *TeamMemberAddedEvent) Name() string {
return "team.member.added"
}
@ -202,7 +230,7 @@ type TeamCreatedEvent struct {
Doer web.Auth
}
// TopicName defines the name for TeamCreatedEvent
// Name defines the name for TeamCreatedEvent
func (t *TeamCreatedEvent) Name() string {
return "team.created"
}
@ -213,7 +241,7 @@ type TeamDeletedEvent struct {
Doer web.Auth
}
// TopicName defines the name for TeamDeletedEvent
// Name defines the name for TeamDeletedEvent
func (t *TeamDeletedEvent) Name() string {
return "team.deleted"
}

View File

@ -17,11 +17,12 @@
package models
import (
"code.vikunja.io/api/pkg/events"
"strconv"
"strings"
"time"
"code.vikunja.io/api/pkg/events"
"code.vikunja.io/api/pkg/log"
"code.vikunja.io/api/pkg/files"

View File

@ -17,9 +17,10 @@
package models
import (
"code.vikunja.io/api/pkg/events"
"time"
"code.vikunja.io/api/pkg/events"
"code.vikunja.io/web"
"xorm.io/xorm"
)
@ -111,6 +112,9 @@ func (tl *TeamList) Create(s *xorm.Session, a web.Auth) (err error) {
Team: team,
Doer: a,
})
if err != nil {
return err
}
err = updateListLastUpdated(s, l)
return

View File

@ -17,9 +17,10 @@
package models
import (
"code.vikunja.io/api/pkg/events"
"time"
"code.vikunja.io/api/pkg/events"
"code.vikunja.io/api/pkg/user"
"code.vikunja.io/web"
"xorm.io/xorm"

View File

@ -17,12 +17,13 @@
package models
import (
"code.vikunja.io/api/pkg/events"
"fmt"
"os"
"testing"
"time"
"code.vikunja.io/api/pkg/events"
"code.vikunja.io/api/pkg/config"
"code.vikunja.io/api/pkg/files"
"code.vikunja.io/api/pkg/user"

View File

@ -17,12 +17,13 @@
package models
import (
"code.vikunja.io/api/pkg/events"
"sort"
"strconv"
"strings"
"time"
"code.vikunja.io/api/pkg/events"
"code.vikunja.io/api/pkg/log"
"code.vikunja.io/api/pkg/user"
"code.vikunja.io/web"

View File

@ -17,9 +17,10 @@
package models
import (
"code.vikunja.io/api/pkg/events"
"time"
"code.vikunja.io/api/pkg/events"
"code.vikunja.io/web"
"xorm.io/xorm"
)

View File

@ -17,9 +17,10 @@
package models
import (
"code.vikunja.io/api/pkg/events"
"time"
"code.vikunja.io/api/pkg/events"
user2 "code.vikunja.io/api/pkg/user"
"code.vikunja.io/web"
"xorm.io/xorm"

View File

@ -17,9 +17,10 @@
package models
import (
"code.vikunja.io/api/pkg/events"
"time"
"code.vikunja.io/api/pkg/events"
"code.vikunja.io/api/pkg/user"
"code.vikunja.io/web"
"xorm.io/xorm"

View File

@ -17,9 +17,10 @@
package models
import (
"code.vikunja.io/api/pkg/events"
"time"
"code.vikunja.io/api/pkg/events"
"xorm.io/xorm"
"code.vikunja.io/api/pkg/user"

View File

@ -17,12 +17,13 @@
package models
import (
"code.vikunja.io/api/pkg/events"
"math"
"sort"
"strconv"
"time"
"code.vikunja.io/api/pkg/events"
"code.vikunja.io/api/pkg/db"
"code.vikunja.io/api/pkg/config"

View File

@ -17,10 +17,11 @@
package models
import (
"code.vikunja.io/api/pkg/events"
"testing"
"time"
"code.vikunja.io/api/pkg/events"
"code.vikunja.io/api/pkg/db"
"code.vikunja.io/api/pkg/user"
"github.com/stretchr/testify/assert"

View File

@ -17,9 +17,10 @@
package models
import (
"code.vikunja.io/api/pkg/events"
"time"
"code.vikunja.io/api/pkg/events"
"xorm.io/xorm"
"code.vikunja.io/api/pkg/user"

View File

@ -17,10 +17,11 @@
package openid
import (
"code.vikunja.io/api/pkg/events"
"os"
"testing"
"code.vikunja.io/api/pkg/events"
"code.vikunja.io/api/pkg/files"
"code.vikunja.io/api/pkg/models"
"code.vikunja.io/api/pkg/user"

View File

@ -17,10 +17,11 @@
package migration
import (
"code.vikunja.io/api/pkg/events"
"os"
"testing"
"code.vikunja.io/api/pkg/events"
"code.vikunja.io/api/pkg/config"
"code.vikunja.io/api/pkg/files"
"code.vikunja.io/api/pkg/models"

View File

@ -16,12 +16,12 @@
package user
// UserCreatedEvent represents a UserCreatedEvent event
type UserCreatedEvent struct {
// CreatedEvent represents a CreatedEvent event
type CreatedEvent struct {
User *User
}
// TopicName defines the name for UserCreatedEvent
func (t *UserCreatedEvent) Name() string {
// TopicName defines the name for CreatedEvent
func (t *CreatedEvent) Name() string {
return "user.created"
}

View File

@ -24,7 +24,7 @@ import (
)
func RegisterListeners() {
events.RegisterListener((&UserCreatedEvent{}).Name(), &IncreaseUserCounter{})
events.RegisterListener((&CreatedEvent{}).Name(), &IncreaseUserCounter{})
}
///////

View File

@ -76,7 +76,7 @@ func CreateUser(s *xorm.Session, user *User) (newUser *User, err error) {
return nil, err
}
err = events.Dispatch(&UserCreatedEvent{
err = events.Dispatch(&CreatedEvent{
User: newUserOut,
})
if err != nil {