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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -24,7 +24,7 @@ import (
) )
func RegisterListeners() { 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 return nil, err
} }
err = events.Dispatch(&UserCreatedEvent{ err = events.Dispatch(&CreatedEvent{
User: newUserOut, User: newUserOut,
}) })
if err != nil { if err != nil {