Add getting subscription for all entity types

This commit is contained in:
kolaente 2021-02-13 18:37:04 +01:00
parent 27be382cac
commit 3e0ce58d4c
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
6 changed files with 160 additions and 1 deletions

View File

@ -2,4 +2,19 @@
entity_type: 3 # Task
entity_id: 2
user_id: 1
created: 2018-12-01 15:13:12
created: 2021-02-01 15:13:12
- id: 2
entity_type: 1 # Namespace
entity_id: 6
user_id: 6
created: 2021-02-01 15:13:12
- id: 3
entity_type: 2 # List
entity_id: 12 # belongs to namespace 7
user_id: 6
created: 2021-02-01 15:13:12
- id: 4
entity_type: 3 # Task
entity_id: 22 # belongs to list 13 which belongs to namespace 8
user_id: 6
created: 2021-02-01 15:13:12

View File

@ -68,6 +68,9 @@ type List struct {
// True if a list is a favorite. Favorite lists show up in a separate namespace.
IsFavorite bool `xorm:"default false" json:"is_favorite"`
// The subscription status for the user reading this list. You can only read this property, use the subscription endpoints to modify it.
Subscription *Subscription `xorm:"-" json:"subscription,omitempty"`
// A timestamp when this list was created. You cannot change this value.
Created time.Time `xorm:"created not null" json:"created"`
// A timestamp when this list was last updated. You cannot change this value.

View File

@ -50,6 +50,9 @@ type Namespace struct {
// The user who owns this namespace
Owner *user.User `xorm:"-" json:"owner" valid:"-"`
// The subscription status for the user reading this namespace. You can only read this property, use the subscription endpoints to modify it.
Subscription *Subscription `xorm:"-" json:"subscription,omitempty"`
// A timestamp when this namespace was created. You cannot change this value.
Created time.Time `xorm:"created not null" json:"created"`
// A timestamp when this namespace was last updated. You cannot change this value.

View File

@ -20,6 +20,7 @@ import (
"code.vikunja.io/api/pkg/user"
"code.vikunja.io/web"
"time"
"xorm.io/builder"
"xorm.io/xorm"
)
@ -72,6 +73,74 @@ func getEntityTypeFromString(entityType string) SubscriptionEntityType {
return SubscriptionEntityUnknown
}
func getSubscription(s *xorm.Session, entityType SubscriptionEntityType, a web.Auth, entityID int64) (subscription *Subscription, err error) {
u, is := a.(*user.User)
if !is {
return
}
subscription = &Subscription{}
var cond builder.Cond
if entityType == SubscriptionEntityNamespace {
cond = builder.And(
builder.Eq{"entity_id": entityID},
builder.Eq{"entity_type": SubscriptionEntityNamespace},
)
}
if entityType == SubscriptionEntityList {
cond = builder.Or(
builder.And(
builder.Eq{"entity_id": entityID},
builder.Eq{"entity_type": SubscriptionEntityList},
),
builder.And(
builder.Eq{"entity_id": builder.
Select("namespace_id").
From("list").
Where(builder.Eq{"id": entityID}),
},
builder.Eq{"entity_type": SubscriptionEntityNamespace},
),
)
}
_, err = s.
Where("user_id = ?", u.ID).
And(cond).
Get(subscription)
return
//
//subscriptions := []*Subscription{}
//err = query.Find(&subscriptions)
//if err != nil {
// return
//}
//
//subscriptionsByType := make(map[SubscriptionEntityType]*Subscription, 3)
//for _, sb := range subscriptions {
// if sb.EntityType == SubscriptionEntityNamespace && entityType == SubscriptionEntityNamespace {
// return sb, nil
// }
// subscriptionsByType[sb.EntityType] = sb
//}
//
//if entityType == SubscriptionEntityList {
// // If there's a subscription for this list, return it directly
// sb, exists := subscriptionsByType[SubscriptionEntityList]
// if exists {
// return sb, nil
// }
//
// // Otherwise check if there's one for the namespace the list belongs to
// //sb, exists := subscriptionsByType[SubscriptionEntityNamespace]
//}
//
//return
}
// Create subscribes the current user to an entity
// @Summary Subscribes the current user to an entity.
// @Description Subscribes the current user to an entity.

View File

@ -262,3 +262,69 @@ func TestSubscription_Delete(t *testing.T) {
assert.False(t, can)
})
}
func TestSubscriptionGet(t *testing.T) {
u := &user.User{ID: 6}
t.Run("test each individually", func(t *testing.T) {
t.Run("namespace", func(t *testing.T) {
db.LoadAndAssertFixtures(t)
s := db.NewSession()
defer s.Close()
sub, err := getSubscription(s, SubscriptionEntityNamespace, u, 6)
assert.NoError(t, err)
assert.NotNil(t, sub)
})
t.Run("list", func(t *testing.T) {
db.LoadAndAssertFixtures(t)
s := db.NewSession()
defer s.Close()
sub, err := getSubscription(s, SubscriptionEntityList, u, 12)
assert.NoError(t, err)
assert.NotNil(t, sub)
})
t.Run("task", func(t *testing.T) {
db.LoadAndAssertFixtures(t)
s := db.NewSession()
defer s.Close()
sub, err := getSubscription(s, SubscriptionEntityTask, u, 22)
assert.NoError(t, err)
assert.NotNil(t, sub)
})
})
t.Run("inherited", func(t *testing.T) {
t.Run("list from namespace", func(t *testing.T) {
db.LoadAndAssertFixtures(t)
s := db.NewSession()
defer s.Close()
// List 6 belongs to namespace 6 where user 6 has subscribed to
sub, err := getSubscription(s, SubscriptionEntityList, u, 6)
assert.NoError(t, err)
assert.NotNil(t, sub)
})
t.Run("task from namespace", func(t *testing.T) {
db.LoadAndAssertFixtures(t)
s := db.NewSession()
defer s.Close()
// Task 20 belongs to list 11 which belongs to namespace 6 where the user has subscribed
sub, err := getSubscription(s, SubscriptionEntityTask, u, 20)
assert.NoError(t, err)
assert.NotNil(t, sub)
})
t.Run("task from list", func(t *testing.T) {
db.LoadAndAssertFixtures(t)
s := db.NewSession()
defer s.Close()
// Task 21 belongs to list 12 which the user has subscribed to
sub, err := getSubscription(s, SubscriptionEntityTask, u, 21)
assert.NoError(t, err)
assert.NotNil(t, sub)
})
})
}

View File

@ -91,6 +91,9 @@ type Task struct {
// True if a task is a favorite task. Favorite tasks show up in a separate "Important" list
IsFavorite bool `xorm:"default false" json:"is_favorite"`
// The subscription status for the user reading this task. You can only read this property, use the subscription endpoints to modify it.
Subscription *Subscription `xorm:"-" json:"subscription,omitempty"`
// A timestamp when this task was created. You cannot change this value.
Created time.Time `xorm:"created not null" json:"created"`
// A timestamp when this task was last updated. You cannot change this value.