reduce store boiler plate (#7585)
Этот коммит содержится в:
коммит произвёл
Joram Wilander
родитель
12501673d0
Коммит
363568b4eb
@@ -40,11 +40,7 @@ func (s SqlStatusStore) CreateIndexesIfNotExists() {
|
||||
}
|
||||
|
||||
func (s SqlStatusStore) SaveOrUpdate(status *model.Status) store.StoreChannel {
|
||||
storeChannel := make(store.StoreChannel, 1)
|
||||
|
||||
go func() {
|
||||
result := store.StoreResult{}
|
||||
|
||||
return store.Do(func(result *store.StoreResult) {
|
||||
if err := s.GetReplica().SelectOne(&model.Status{}, "SELECT * FROM Status WHERE UserId = :UserId", map[string]interface{}{"UserId": status.UserId}); err == nil {
|
||||
if _, err := s.GetMaster().Update(status); err != nil {
|
||||
result.Err = model.NewAppError("SqlStatusStore.SaveOrUpdate", "store.sql_status.update.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
@@ -56,20 +52,11 @@ func (s SqlStatusStore) SaveOrUpdate(status *model.Status) store.StoreChannel {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
storeChannel <- result
|
||||
close(storeChannel)
|
||||
}()
|
||||
|
||||
return storeChannel
|
||||
})
|
||||
}
|
||||
|
||||
func (s SqlStatusStore) Get(userId string) store.StoreChannel {
|
||||
storeChannel := make(store.StoreChannel, 1)
|
||||
|
||||
go func() {
|
||||
result := store.StoreResult{}
|
||||
|
||||
return store.Do(func(result *store.StoreResult) {
|
||||
var status model.Status
|
||||
|
||||
if err := s.GetReplica().SelectOne(&status,
|
||||
@@ -87,20 +74,11 @@ func (s SqlStatusStore) Get(userId string) store.StoreChannel {
|
||||
} else {
|
||||
result.Data = &status
|
||||
}
|
||||
|
||||
storeChannel <- result
|
||||
close(storeChannel)
|
||||
}()
|
||||
|
||||
return storeChannel
|
||||
})
|
||||
}
|
||||
|
||||
func (s SqlStatusStore) GetByIds(userIds []string) store.StoreChannel {
|
||||
storeChannel := make(store.StoreChannel, 1)
|
||||
|
||||
go func() {
|
||||
result := store.StoreResult{}
|
||||
|
||||
return store.Do(func(result *store.StoreResult) {
|
||||
props := make(map[string]interface{})
|
||||
idQuery := ""
|
||||
|
||||
@@ -119,60 +97,33 @@ func (s SqlStatusStore) GetByIds(userIds []string) store.StoreChannel {
|
||||
} else {
|
||||
result.Data = statuses
|
||||
}
|
||||
|
||||
storeChannel <- result
|
||||
close(storeChannel)
|
||||
}()
|
||||
|
||||
return storeChannel
|
||||
})
|
||||
}
|
||||
|
||||
func (s SqlStatusStore) GetOnlineAway() store.StoreChannel {
|
||||
storeChannel := make(store.StoreChannel, 1)
|
||||
|
||||
go func() {
|
||||
result := store.StoreResult{}
|
||||
|
||||
return store.Do(func(result *store.StoreResult) {
|
||||
var statuses []*model.Status
|
||||
if _, err := s.GetReplica().Select(&statuses, "SELECT * FROM Status WHERE Status = :Online OR Status = :Away LIMIT 300", map[string]interface{}{"Online": model.STATUS_ONLINE, "Away": model.STATUS_AWAY}); err != nil {
|
||||
result.Err = model.NewAppError("SqlStatusStore.GetOnlineAway", "store.sql_status.get_online_away.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
} else {
|
||||
result.Data = statuses
|
||||
}
|
||||
|
||||
storeChannel <- result
|
||||
close(storeChannel)
|
||||
}()
|
||||
|
||||
return storeChannel
|
||||
})
|
||||
}
|
||||
|
||||
func (s SqlStatusStore) GetOnline() store.StoreChannel {
|
||||
storeChannel := make(store.StoreChannel, 1)
|
||||
|
||||
go func() {
|
||||
result := store.StoreResult{}
|
||||
|
||||
return store.Do(func(result *store.StoreResult) {
|
||||
var statuses []*model.Status
|
||||
if _, err := s.GetReplica().Select(&statuses, "SELECT * FROM Status WHERE Status = :Online", map[string]interface{}{"Online": model.STATUS_ONLINE}); err != nil {
|
||||
result.Err = model.NewAppError("SqlStatusStore.GetOnline", "store.sql_status.get_online.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
} else {
|
||||
result.Data = statuses
|
||||
}
|
||||
|
||||
storeChannel <- result
|
||||
close(storeChannel)
|
||||
}()
|
||||
|
||||
return storeChannel
|
||||
})
|
||||
}
|
||||
|
||||
func (s SqlStatusStore) GetAllFromTeam(teamId string) store.StoreChannel {
|
||||
storeChannel := make(store.StoreChannel, 1)
|
||||
|
||||
go func() {
|
||||
result := store.StoreResult{}
|
||||
|
||||
return store.Do(func(result *store.StoreResult) {
|
||||
var statuses []*model.Status
|
||||
if _, err := s.GetReplica().Select(&statuses,
|
||||
`SELECT s.* FROM Status AS s INNER JOIN
|
||||
@@ -181,37 +132,19 @@ func (s SqlStatusStore) GetAllFromTeam(teamId string) store.StoreChannel {
|
||||
} else {
|
||||
result.Data = statuses
|
||||
}
|
||||
|
||||
storeChannel <- result
|
||||
close(storeChannel)
|
||||
}()
|
||||
|
||||
return storeChannel
|
||||
})
|
||||
}
|
||||
|
||||
func (s SqlStatusStore) ResetAll() store.StoreChannel {
|
||||
storeChannel := make(store.StoreChannel, 1)
|
||||
|
||||
go func() {
|
||||
result := store.StoreResult{}
|
||||
|
||||
return store.Do(func(result *store.StoreResult) {
|
||||
if _, err := s.GetMaster().Exec("UPDATE Status SET Status = :Status WHERE Manual = false", map[string]interface{}{"Status": model.STATUS_OFFLINE}); err != nil {
|
||||
result.Err = model.NewAppError("SqlStatusStore.ResetAll", "store.sql_status.reset_all.app_error", nil, "", http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
storeChannel <- result
|
||||
close(storeChannel)
|
||||
}()
|
||||
|
||||
return storeChannel
|
||||
})
|
||||
}
|
||||
|
||||
func (s SqlStatusStore) GetTotalActiveUsersCount() store.StoreChannel {
|
||||
storeChannel := make(store.StoreChannel, 1)
|
||||
|
||||
go func() {
|
||||
result := store.StoreResult{}
|
||||
|
||||
return store.Do(func(result *store.StoreResult) {
|
||||
time := model.GetMillis() - (1000 * 60 * 60 * 24)
|
||||
|
||||
if count, err := s.GetReplica().SelectInt("SELECT COUNT(UserId) FROM Status WHERE LastActivityAt > :Time", map[string]interface{}{"Time": time}); err != nil {
|
||||
@@ -219,27 +152,13 @@ func (s SqlStatusStore) GetTotalActiveUsersCount() store.StoreChannel {
|
||||
} else {
|
||||
result.Data = count
|
||||
}
|
||||
|
||||
storeChannel <- result
|
||||
close(storeChannel)
|
||||
}()
|
||||
|
||||
return storeChannel
|
||||
})
|
||||
}
|
||||
|
||||
func (s SqlStatusStore) UpdateLastActivityAt(userId string, lastActivityAt int64) store.StoreChannel {
|
||||
storeChannel := make(store.StoreChannel, 1)
|
||||
|
||||
go func() {
|
||||
result := store.StoreResult{}
|
||||
|
||||
return store.Do(func(result *store.StoreResult) {
|
||||
if _, err := s.GetMaster().Exec("UPDATE Status SET LastActivityAt = :Time WHERE UserId = :UserId", map[string]interface{}{"UserId": userId, "Time": lastActivityAt}); err != nil {
|
||||
result.Err = model.NewAppError("SqlStatusStore.UpdateLastActivityAt", "store.sql_status.update_last_activity_at.app_error", nil, "", http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
storeChannel <- result
|
||||
close(storeChannel)
|
||||
}()
|
||||
|
||||
return storeChannel
|
||||
})
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user