Fix creating and retrieving subscriptions
This commit is contained in:
parent
eac30f8e3d
commit
9d27da9c47
|
@ -42,7 +42,7 @@ type Subscription struct {
|
|||
ID int64 `xorm:"autoincr not null unique pk" json:"id"`
|
||||
|
||||
EntityType SubscriptionEntityType `xorm:"index not null" json:"-"`
|
||||
Entity string `xorm:"-" json:"-" param:"entity"`
|
||||
Entity string `xorm:"-" json:"entity" param:"entity"`
|
||||
// The id of the entity to subscribe to.
|
||||
EntityID int64 `xorm:"bigint index not null" json:"entity_id" param:"entityID"`
|
||||
|
||||
|
@ -75,6 +75,20 @@ func getEntityTypeFromString(entityType string) SubscriptionEntityType {
|
|||
return SubscriptionEntityUnknown
|
||||
}
|
||||
|
||||
// String returns a human-readable string of an entity
|
||||
func (et SubscriptionEntityType) String() string {
|
||||
switch et {
|
||||
case SubscriptionEntityNamespace:
|
||||
return "namespace"
|
||||
case SubscriptionEntityList:
|
||||
return "list"
|
||||
case SubscriptionEntityTask:
|
||||
return "task"
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
func (et SubscriptionEntityType) validate() error {
|
||||
if et == SubscriptionEntityNamespace ||
|
||||
et == SubscriptionEntityList ||
|
||||
|
@ -103,13 +117,13 @@ func (et SubscriptionEntityType) validate() error {
|
|||
func (sb *Subscription) Create(s *xorm.Session, auth web.Auth) (err error) {
|
||||
// Rights method alread does the validation of the entity type so we don't need to do that here
|
||||
|
||||
exists, err := s.
|
||||
Where("entity_id = ? AND entity_type = ? AND user_id = ?", sb.EntityID, sb.EntityType, sb.UserID).
|
||||
Exist(&Subscription{})
|
||||
sb.UserID = auth.GetID()
|
||||
|
||||
sub, err := GetSubscription(s, sb.EntityType, sb.EntityID, auth)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if exists {
|
||||
if sub != nil {
|
||||
return &ErrSubscriptionAlreadyExists{
|
||||
EntityID: sb.EntityID,
|
||||
EntityType: sb.EntityType,
|
||||
|
@ -141,6 +155,8 @@ func (sb *Subscription) Create(s *xorm.Session, auth web.Auth) (err error) {
|
|||
// @Failure 500 {object} models.Message "Internal error"
|
||||
// @Router /subscriptions/{entity}/{entityID} [delete]
|
||||
func (sb *Subscription) Delete(s *xorm.Session, auth web.Auth) (err error) {
|
||||
sb.UserID = auth.GetID()
|
||||
|
||||
_, err = s.
|
||||
Where("entity_id = ? AND entity_type = ? AND user_id = ?", sb.EntityID, sb.EntityType, sb.UserID).
|
||||
Delete(&Subscription{})
|
||||
|
@ -217,11 +233,17 @@ func GetSubscription(s *xorm.Session, entityType SubscriptionEntityType, entityI
|
|||
|
||||
subscription = &Subscription{}
|
||||
cond := getSubscriberCondForEntity(entityType, entityID)
|
||||
_, err = s.
|
||||
exists, err := s.
|
||||
Where("user_id = ?", u.ID).
|
||||
And(cond).
|
||||
Get(subscription)
|
||||
return
|
||||
if !exists {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
subscription.Entity = subscription.EntityType.String()
|
||||
|
||||
return subscription, err
|
||||
}
|
||||
|
||||
func getSubscribersForEntity(s *xorm.Session, entityType SubscriptionEntityType, entityID int64) (subscriptions []*Subscription, err error) {
|
||||
|
|
|
@ -55,12 +55,12 @@ func (sb *Subscription) CanDelete(s *xorm.Session, a web.Auth) (can bool, err er
|
|||
sb.EntityType = getEntityTypeFromString(sb.Entity)
|
||||
|
||||
realSb := &Subscription{}
|
||||
_, err = s.
|
||||
Where("entity_id = ? AND entity_type = ? AND user_id = ?", sb.EntityID, sb.EntityType, sb.UserID).
|
||||
exists, err := s.
|
||||
Where("entity_id = ? AND entity_type = ? AND user_id = ?", sb.EntityID, sb.EntityType, a.GetID()).
|
||||
Get(realSb)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return realSb.UserID == a.GetID(), nil
|
||||
return exists, nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue