Merge pull request #1079 from hmhealey/initprefs
Added an initial call to get all user preferences on page load
Этот коммит содержится в:
@@ -14,11 +14,22 @@ func InitPreference(r *mux.Router) {
|
||||
l4g.Debug("Initializing preference api routes")
|
||||
|
||||
sr := r.PathPrefix("/preferences").Subrouter()
|
||||
sr.Handle("/", ApiUserRequired(getAllPreferences)).Methods("GET")
|
||||
sr.Handle("/save", ApiUserRequired(savePreferences)).Methods("POST")
|
||||
sr.Handle("/{category:[A-Za-z0-9_]+}", ApiUserRequired(getPreferenceCategory)).Methods("GET")
|
||||
sr.Handle("/{category:[A-Za-z0-9_]+}/{name:[A-Za-z0-9_]+}", ApiUserRequired(getPreference)).Methods("GET")
|
||||
}
|
||||
|
||||
func getAllPreferences(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
if result := <-Srv.Store.Preference().GetAll(c.Session.UserId); result.Err != nil {
|
||||
c.Err = result.Err
|
||||
} else {
|
||||
data := result.Data.(model.Preferences)
|
||||
|
||||
w.Write([]byte(data.ToJson()))
|
||||
}
|
||||
}
|
||||
|
||||
func savePreferences(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
preferences, err := model.PreferencesFromJson(r.Body)
|
||||
if err != nil {
|
||||
|
||||
@@ -9,6 +9,64 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetAllPreferences(t *testing.T) {
|
||||
Setup()
|
||||
|
||||
team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
|
||||
team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)
|
||||
|
||||
user1 := &model.User{TeamId: team.Id, Email: model.NewId() + "corey@test.com", Nickname: "Corey Hulen", Password: "pwd"}
|
||||
user1 = Client.Must(Client.CreateUser(user1, "")).Data.(*model.User)
|
||||
store.Must(Srv.Store.User().VerifyEmail(user1.Id))
|
||||
|
||||
user2 := &model.User{TeamId: team.Id, Email: model.NewId() + "corey@test.com", Nickname: "Corey Hulen", Password: "pwd"}
|
||||
user2 = Client.Must(Client.CreateUser(user2, "")).Data.(*model.User)
|
||||
store.Must(Srv.Store.User().VerifyEmail(user2.Id))
|
||||
|
||||
category := model.NewId()
|
||||
|
||||
preferences1 := model.Preferences{
|
||||
{
|
||||
UserId: user1.Id,
|
||||
Category: category,
|
||||
Name: model.NewId(),
|
||||
},
|
||||
{
|
||||
UserId: user1.Id,
|
||||
Category: category,
|
||||
Name: model.NewId(),
|
||||
},
|
||||
{
|
||||
UserId: user1.Id,
|
||||
Category: model.NewId(),
|
||||
Name: model.NewId(),
|
||||
},
|
||||
}
|
||||
|
||||
Client.LoginByEmail(team.Name, user1.Email, "pwd")
|
||||
Client.Must(Client.SetPreferences(&preferences1))
|
||||
|
||||
if result, err := Client.GetAllPreferences(); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if data := result.Data.(model.Preferences); len(data) != 3 {
|
||||
t.Fatal("received the wrong number of preferences")
|
||||
} else if !((data[0] == preferences1[0] && data[1] == preferences1[1]) || (data[0] == preferences1[1] && data[1] == preferences1[0])) {
|
||||
for i := 0; i < 3; i++ {
|
||||
if data[0] != preferences1[i] && data[1] != preferences1[i] && data[2] != preferences1[i] {
|
||||
t.Fatal("got incorrect preferences")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Client.LoginByEmail(team.Name, user2.Email, "pwd")
|
||||
|
||||
if result, err := Client.GetAllPreferences(); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if data := result.Data.(model.Preferences); len(data) != 0 {
|
||||
t.Fatal("received the wrong number of preferences")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetPreferences(t *testing.T) {
|
||||
Setup()
|
||||
|
||||
|
||||
@@ -844,6 +844,15 @@ func (c *Client) ListIncomingWebhooks() (*Result, *AppError) {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) GetAllPreferences() (*Result, *AppError) {
|
||||
if r, err := c.DoApiGet("/preferences/", "", ""); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
preferences, _ := PreferencesFromJson(r.Body)
|
||||
return &Result{r.Header.Get(HEADER_REQUEST_ID), r.Header.Get(HEADER_ETAG_SERVER), preferences}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) SetPreferences(preferences *Preferences) (*Result, *AppError) {
|
||||
if r, err := c.DoApiPost("/preferences/save", preferences.ToJson()); err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -212,3 +212,30 @@ func (s SqlPreferenceStore) GetCategory(userId string, category string) StoreCha
|
||||
|
||||
return storeChannel
|
||||
}
|
||||
|
||||
func (s SqlPreferenceStore) GetAll(userId string) StoreChannel {
|
||||
storeChannel := make(StoreChannel)
|
||||
|
||||
go func() {
|
||||
result := StoreResult{}
|
||||
|
||||
var preferences model.Preferences
|
||||
|
||||
if _, err := s.GetReplica().Select(&preferences,
|
||||
`SELECT
|
||||
*
|
||||
FROM
|
||||
Preferences
|
||||
WHERE
|
||||
UserId = :UserId`, map[string]interface{}{"UserId": userId}); err != nil {
|
||||
result.Err = model.NewAppError("SqlPreferenceStore.GetAll", "We encounted an error while finding preferences", err.Error())
|
||||
} else {
|
||||
result.Data = preferences
|
||||
}
|
||||
|
||||
storeChannel <- result
|
||||
close(storeChannel)
|
||||
}()
|
||||
|
||||
return storeChannel
|
||||
}
|
||||
|
||||
@@ -144,3 +144,51 @@ func TestPreferenceGetCategory(t *testing.T) {
|
||||
t.Fatal("shouldn't have got any preferences")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreferenceGetAll(t *testing.T) {
|
||||
Setup()
|
||||
|
||||
userId := model.NewId()
|
||||
category := model.PREFERENCE_CATEGORY_DIRECT_CHANNEL_SHOW
|
||||
name := model.NewId()
|
||||
|
||||
preferences := model.Preferences{
|
||||
{
|
||||
UserId: userId,
|
||||
Category: category,
|
||||
Name: name,
|
||||
},
|
||||
// same user/category, different name
|
||||
{
|
||||
UserId: userId,
|
||||
Category: category,
|
||||
Name: model.NewId(),
|
||||
},
|
||||
// same user/name, different category
|
||||
{
|
||||
UserId: userId,
|
||||
Category: model.NewId(),
|
||||
Name: name,
|
||||
},
|
||||
// same name/category, different user
|
||||
{
|
||||
UserId: model.NewId(),
|
||||
Category: category,
|
||||
Name: name,
|
||||
},
|
||||
}
|
||||
|
||||
Must(store.Preference().Save(&preferences))
|
||||
|
||||
if result := <-store.Preference().GetAll(userId); result.Err != nil {
|
||||
t.Fatal(result.Err)
|
||||
} else if data := result.Data.(model.Preferences); len(data) != 3 {
|
||||
t.Fatal("got the wrong number of preferences")
|
||||
} else {
|
||||
for i := 0; i < 3; i++ {
|
||||
if data[0] != preferences[i] && data[1] != preferences[i] && data[2] != preferences[i] {
|
||||
t.Fatal("got incorrect preferences")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -156,4 +156,5 @@ type PreferenceStore interface {
|
||||
Save(preferences *model.Preferences) StoreChannel
|
||||
Get(userId string, category string, name string) StoreChannel
|
||||
GetCategory(userId string, category string) StoreChannel
|
||||
GetAll(userId string) StoreChannel
|
||||
}
|
||||
|
||||
@@ -132,11 +132,7 @@ export default class Sidebar extends React.Component {
|
||||
SocketStore.addChangeListener(this.onSocketChange);
|
||||
PreferenceStore.addChangeListener(this.onChange);
|
||||
|
||||
AsyncClient.getDirectChannelPreferences();
|
||||
|
||||
if ($(window).width() > 768) {
|
||||
$('.nav-pills__container').perfectScrollbar();
|
||||
}
|
||||
$('.nav-pills__container').perfectScrollbar();
|
||||
|
||||
this.updateTitle();
|
||||
this.updateUnreadIndicators();
|
||||
|
||||
@@ -37,6 +37,7 @@ var RegisterAppModal = require('../components/register_app_modal.jsx');
|
||||
var ImportThemeModal = require('../components/user_settings/import_theme_modal.jsx');
|
||||
var TeamStore = require('../stores/team_store.jsx');
|
||||
|
||||
var AsyncClient = require('../utils/async_client.jsx');
|
||||
var Constants = require('../utils/constants.jsx');
|
||||
var ActionTypes = Constants.ActionTypes;
|
||||
|
||||
@@ -54,6 +55,8 @@ function setupChannelPage(props) {
|
||||
id: props.TeamId
|
||||
});
|
||||
|
||||
AsyncClient.getAllPreferences();
|
||||
|
||||
// ChannelLoader must be rendered first
|
||||
ReactDOM.render(
|
||||
<ChannelLoader/>,
|
||||
|
||||
@@ -120,3 +120,4 @@ class PreferenceStoreClass extends EventEmitter {
|
||||
|
||||
const PreferenceStore = new PreferenceStoreClass();
|
||||
export default PreferenceStore;
|
||||
window.PreferenceStore = PreferenceStore;
|
||||
|
||||
@@ -638,16 +638,15 @@ export function getMyTeam() {
|
||||
);
|
||||
}
|
||||
|
||||
export function getDirectChannelPreferences() {
|
||||
if (isCallInProgress('getDirectChannelPreferences')) {
|
||||
export function getAllPreferences() {
|
||||
if (isCallInProgress('getAllPreferences')) {
|
||||
return;
|
||||
}
|
||||
|
||||
callTracker.getDirectChannelPreferences = utils.getTimestamp();
|
||||
client.getPreferenceCategory(
|
||||
Constants.Preferences.CATEGORY_DIRECT_CHANNEL_SHOW,
|
||||
callTracker.getAllPreferences = utils.getTimestamp();
|
||||
client.getAllPreferences(
|
||||
(data, textStatus, xhr) => {
|
||||
callTracker.getDirectChannelPreferences = 0;
|
||||
callTracker.getAllPreferences = 0;
|
||||
|
||||
if (xhr.status === 304 || !data) {
|
||||
return;
|
||||
@@ -659,8 +658,8 @@ export function getDirectChannelPreferences() {
|
||||
});
|
||||
},
|
||||
(err) => {
|
||||
callTracker.getDirectChannelPreferences = 0;
|
||||
dispatchError(err, 'getDirectChannelPreferences');
|
||||
callTracker.getAllPreferences = 0;
|
||||
dispatchError(err, 'getAllPreferences');
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1142,6 +1142,19 @@ export function listIncomingHooks(success, error) {
|
||||
});
|
||||
}
|
||||
|
||||
export function getAllPreferences(success, error) {
|
||||
$.ajax({
|
||||
url: `/api/v1/preferences/`,
|
||||
dataType: 'json',
|
||||
type: 'GET',
|
||||
success,
|
||||
error: (xhr, status, err) => {
|
||||
var e = handleError('getAllPreferences', xhr, status, err);
|
||||
error(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function getPreferenceCategory(category, success, error) {
|
||||
$.ajax({
|
||||
url: `/api/v1/preferences/${category}`,
|
||||
|
||||
Ссылка в новой задаче
Block a user