Get/set sharing

This commit is contained in:
Chen-I Lim 2021-01-12 15:35:30 -08:00
parent 7052a022b3
commit 2f71295275
12 changed files with 324 additions and 8 deletions

View file

@ -50,6 +50,9 @@ func (a *API) RegisterRoutes(r *mux.Router) {
r.HandleFunc("/api/v1/blocks/export", a.sessionRequired(a.handleExport)).Methods("GET")
r.HandleFunc("/api/v1/blocks/import", a.sessionRequired(a.handleImport)).Methods("POST")
r.HandleFunc("/api/v1/sharing/{rootID}", a.sessionRequired(a.handlePostSharing)).Methods("POST")
r.HandleFunc("/api/v1/sharing/{rootID}", a.handleGetSharing).Methods("GET")
}
func (a *API) handleGetBlocks(w http.ResponseWriter, r *http.Request) {
@ -382,6 +385,80 @@ func (a *API) handleImport(w http.ResponseWriter, r *http.Request) {
jsonStringResponse(w, http.StatusOK, "{}")
}
// Sharing
func (a *API) handleGetSharing(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
rootID := vars["rootID"]
sharing, err := a.app().GetSharing(rootID)
if err != nil {
log.Printf(`ERROR: %v`, r)
errorResponse(w, http.StatusInternalServerError, nil)
return
}
sharingData, err := json.Marshal(sharing)
if err != nil {
log.Printf(`ERROR: %v`, r)
errorResponse(w, http.StatusInternalServerError, nil)
return
}
log.Printf("GET sharing %s", rootID)
jsonStringResponse(w, http.StatusOK, string(sharingData))
}
func (a *API) handlePostSharing(w http.ResponseWriter, r *http.Request) {
requestBody, err := ioutil.ReadAll(r.Body)
if err != nil {
errorResponse(w, http.StatusInternalServerError, nil)
return
}
// Catch panics from parse errors, etc.
defer func() {
if r := recover(); r != nil {
log.Printf(`ERROR: %v`, r)
errorResponse(w, http.StatusInternalServerError, nil)
return
}
}()
var sharing model.Sharing
err = json.Unmarshal(requestBody, &sharing)
if err != nil {
errorResponse(w, http.StatusInternalServerError, nil)
return
}
// Stamp ModifiedBy
ctx := r.Context()
session := ctx.Value("session").(*model.Session)
userID := session.UserID
if userID == "single-user" {
userID = ""
}
sharing.ModifiedBy = userID
err = a.app().UpsertSharing(sharing)
if err != nil {
log.Printf(`ERROR: %v, REQUEST: %v`, err, r)
errorResponse(w, http.StatusInternalServerError, nil)
return
}
log.Printf("POST sharing %s", sharing.ID)
jsonStringResponse(w, http.StatusOK, "{}")
}
// File upload
func (a *API) handleServeFile(w http.ResponseWriter, r *http.Request) {

22
server/app/sharing.go Normal file
View file

@ -0,0 +1,22 @@
package app
import (
"database/sql"
"github.com/mattermost/mattermost-octo-tasks/server/model"
)
func (a *App) GetSharing(rootID string) (*model.Sharing, error) {
sharing, err := a.store.GetSharing(rootID)
if err == sql.ErrNoRows {
return nil, nil
}
if err != nil {
return nil, err
}
return sharing, nil
}
func (a *App) UpsertSharing(sharing model.Sharing) error {
return a.store.UpsertSharing(sharing)
}

9
server/model/sharing.go Normal file
View file

@ -0,0 +1,9 @@
package model
type Sharing struct {
ID string `json:"id"`
Token string `json:"token"`
Enabled bool `json:"enabled"`
ModifiedBy string `json:"modifiedBy"`
UpdateAt int64 `json:"update_at,omitempty"`
}

View file

@ -193,6 +193,21 @@ func (mr *MockStoreMockRecorder) GetSession(arg0, arg1 interface{}) *gomock.Call
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetSession", reflect.TypeOf((*MockStore)(nil).GetSession), arg0, arg1)
}
// GetSharing mocks base method
func (m *MockStore) GetSharing(arg0 string) (*model.Sharing, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GetSharing", arg0)
ret0, _ := ret[0].(*model.Sharing)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// GetSharing indicates an expected call of GetSharing
func (mr *MockStoreMockRecorder) GetSharing(arg0 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetSharing", reflect.TypeOf((*MockStore)(nil).GetSharing), arg0)
}
// GetSubTree2 mocks base method
func (m *MockStore) GetSubTree2(arg0 string) ([]model.Block, error) {
m.ctrl.T.Helper()
@ -366,3 +381,17 @@ func (mr *MockStoreMockRecorder) UpdateUser(arg0 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateUser", reflect.TypeOf((*MockStore)(nil).UpdateUser), arg0)
}
// UpsertSharing mocks base method
func (m *MockStore) UpsertSharing(arg0 model.Sharing) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "UpsertSharing", arg0)
ret0, _ := ret[0].(error)
return ret0
}
// UpsertSharing indicates an expected call of UpsertSharing
func (mr *MockStoreMockRecorder) UpsertSharing(arg0 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpsertSharing", reflect.TypeOf((*MockStore)(nil).UpsertSharing), arg0)
}

View file

@ -10,6 +10,8 @@
// postgres_files/000004_auth_table.up.sql
// postgres_files/000005_blocks_modifiedby.down.sql
// postgres_files/000005_blocks_modifiedby.up.sql
// postgres_files/000006_sharing_table.down.sql
// postgres_files/000006_sharing_table.up.sql
package postgres
import (
@ -210,7 +212,7 @@ func _000004_auth_tableDownSql() (*asset, error) {
return nil, err
}
info := bindataFileInfo{name: "000004_auth_table.down.sql", size: 39, mode: os.FileMode(420), modTime: time.Unix(1610392216, 0)}
info := bindataFileInfo{name: "000004_auth_table.down.sql", size: 39, mode: os.FileMode(420), modTime: time.Unix(1610481092, 0)}
a := &asset{bytes: bytes, info: info}
return a, nil
}
@ -230,7 +232,7 @@ func _000004_auth_tableUpSql() (*asset, error) {
return nil, err
}
info := bindataFileInfo{name: "000004_auth_table.up.sql", size: 491, mode: os.FileMode(420), modTime: time.Unix(1610392216, 0)}
info := bindataFileInfo{name: "000004_auth_table.up.sql", size: 491, mode: os.FileMode(420), modTime: time.Unix(1610481092, 0)}
a := &asset{bytes: bytes, info: info}
return a, nil
}
@ -250,7 +252,7 @@ func _000005_blocks_modifiedbyDownSql() (*asset, error) {
return nil, err
}
info := bindataFileInfo{name: "000005_blocks_modifiedby.down.sql", size: 44, mode: os.FileMode(420), modTime: time.Unix(1610412613, 0)}
info := bindataFileInfo{name: "000005_blocks_modifiedby.down.sql", size: 44, mode: os.FileMode(420), modTime: time.Unix(1610481092, 0)}
a := &asset{bytes: bytes, info: info}
return a, nil
}
@ -270,7 +272,47 @@ func _000005_blocks_modifiedbyUpSql() (*asset, error) {
return nil, err
}
info := bindataFileInfo{name: "000005_blocks_modifiedby.up.sql", size: 55, mode: os.FileMode(420), modTime: time.Unix(1610412615, 0)}
info := bindataFileInfo{name: "000005_blocks_modifiedby.up.sql", size: 55, mode: os.FileMode(420), modTime: time.Unix(1610481092, 0)}
a := &asset{bytes: bytes, info: info}
return a, nil
}
var __000006_sharing_tableDownSql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x72\x09\xf2\x0f\x50\x08\x71\x74\xf2\x71\x55\x28\xce\x48\x2c\xca\xcc\x4b\xb7\xe6\x02\x04\x00\x00\xff\xff\xdd\x4c\x62\xe8\x14\x00\x00\x00")
func _000006_sharing_tableDownSqlBytes() ([]byte, error) {
return bindataRead(
__000006_sharing_tableDownSql,
"000006_sharing_table.down.sql",
)
}
func _000006_sharing_tableDownSql() (*asset, error) {
bytes, err := _000006_sharing_tableDownSqlBytes()
if err != nil {
return nil, err
}
info := bindataFileInfo{name: "000006_sharing_table.down.sql", size: 20, mode: os.FileMode(420), modTime: time.Unix(1610482431, 0)}
a := &asset{bytes: bytes, info: info}
return a, nil
}
var __000006_sharing_tableUpSql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x5c\xcc\xb1\xca\xc2\x30\x10\x00\xe0\x39\xf7\x14\x37\xb6\xd0\xa1\x3f\x3f\xb8\x38\x5d\xcb\xa9\xc1\xda\x4a\x1a\xc4\x4e\x25\xe5\xa2\x06\xb5\x15\x8d\x83\x6f\x2f\x2e\x0e\xee\x1f\x5f\x69\x98\x2c\xa3\xa5\xa2\x62\xd4\x0b\xac\x1b\x8b\xbc\xd7\xad\x6d\xf1\x71\x72\xf7\x30\x1e\x31\x01\x15\x04\x77\x64\xca\x15\x99\xe4\x7f\x96\x66\xa0\xfc\xe8\x86\x8b\x17\x2c\x9a\xa6\x62\xaa\x33\x50\x71\x3a\xfb\xf1\xab\xfe\xf2\xfc\xc3\xae\x93\x84\x43\xf0\xd2\x0f\xaf\x9f\xe0\x79\x13\x17\x7d\xef\x22\x16\x7a\xa9\x6b\x9b\x81\xda\x1a\xbd\x21\xd3\xe1\x9a\x3b\x4c\x82\xa4\x90\xce\xe1\x1d\x00\x00\xff\xff\x6c\x91\x98\xb6\x9f\x00\x00\x00")
func _000006_sharing_tableUpSqlBytes() ([]byte, error) {
return bindataRead(
__000006_sharing_tableUpSql,
"000006_sharing_table.up.sql",
)
}
func _000006_sharing_tableUpSql() (*asset, error) {
bytes, err := _000006_sharing_tableUpSqlBytes()
if err != nil {
return nil, err
}
info := bindataFileInfo{name: "000006_sharing_table.up.sql", size: 159, mode: os.FileMode(420), modTime: time.Unix(1610483324, 0)}
a := &asset{bytes: bytes, info: info}
return a, nil
}
@ -337,6 +379,8 @@ var _bindata = map[string]func() (*asset, error){
"000004_auth_table.up.sql": _000004_auth_tableUpSql,
"000005_blocks_modifiedby.down.sql": _000005_blocks_modifiedbyDownSql,
"000005_blocks_modifiedby.up.sql": _000005_blocks_modifiedbyUpSql,
"000006_sharing_table.down.sql": _000006_sharing_tableDownSql,
"000006_sharing_table.up.sql": _000006_sharing_tableUpSql,
}
// AssetDir returns the file names below a certain
@ -389,6 +433,8 @@ var _bintree = &bintree{nil, map[string]*bintree{
"000004_auth_table.up.sql": &bintree{_000004_auth_tableUpSql, map[string]*bintree{}},
"000005_blocks_modifiedby.down.sql": &bintree{_000005_blocks_modifiedbyDownSql, map[string]*bintree{}},
"000005_blocks_modifiedby.up.sql": &bintree{_000005_blocks_modifiedbyUpSql, map[string]*bintree{}},
"000006_sharing_table.down.sql": &bintree{_000006_sharing_tableDownSql, map[string]*bintree{}},
"000006_sharing_table.up.sql": &bintree{_000006_sharing_tableUpSql, map[string]*bintree{}},
}}
// RestoreAsset restores an asset under the given directory

View file

@ -0,0 +1 @@
DROP TABLE sharing;

View file

@ -0,0 +1,8 @@
CREATE TABLE IF NOT EXISTS sharing (
id VARCHAR(36),
enabled BOOLEAN,
token VARCHAR(100),
modified_by VARCHAR(36),
update_at BIGINT,
PRIMARY KEY (id)
);

View file

@ -10,6 +10,8 @@
// sqlite_files/000004_auth_table.up.sql
// sqlite_files/000005_blocks_modifiedby.down.sql
// sqlite_files/000005_blocks_modifiedby.up.sql
// sqlite_files/000006_sharing_table.down.sql
// sqlite_files/000006_sharing_table.up.sql
package sqlite
import (
@ -210,7 +212,7 @@ func _000004_auth_tableDownSql() (*asset, error) {
return nil, err
}
info := bindataFileInfo{name: "000004_auth_table.down.sql", size: 39, mode: os.FileMode(420), modTime: time.Unix(1610392216, 0)}
info := bindataFileInfo{name: "000004_auth_table.down.sql", size: 39, mode: os.FileMode(420), modTime: time.Unix(1610481092, 0)}
a := &asset{bytes: bytes, info: info}
return a, nil
}
@ -230,7 +232,7 @@ func _000004_auth_tableUpSql() (*asset, error) {
return nil, err
}
info := bindataFileInfo{name: "000004_auth_table.up.sql", size: 491, mode: os.FileMode(420), modTime: time.Unix(1610392216, 0)}
info := bindataFileInfo{name: "000004_auth_table.up.sql", size: 491, mode: os.FileMode(420), modTime: time.Unix(1610481092, 0)}
a := &asset{bytes: bytes, info: info}
return a, nil
}
@ -250,7 +252,7 @@ func _000005_blocks_modifiedbyDownSql() (*asset, error) {
return nil, err
}
info := bindataFileInfo{name: "000005_blocks_modifiedby.down.sql", size: 44, mode: os.FileMode(420), modTime: time.Unix(1610412613, 0)}
info := bindataFileInfo{name: "000005_blocks_modifiedby.down.sql", size: 44, mode: os.FileMode(420), modTime: time.Unix(1610481092, 0)}
a := &asset{bytes: bytes, info: info}
return a, nil
}
@ -270,7 +272,47 @@ func _000005_blocks_modifiedbyUpSql() (*asset, error) {
return nil, err
}
info := bindataFileInfo{name: "000005_blocks_modifiedby.up.sql", size: 55, mode: os.FileMode(420), modTime: time.Unix(1610412615, 0)}
info := bindataFileInfo{name: "000005_blocks_modifiedby.up.sql", size: 55, mode: os.FileMode(420), modTime: time.Unix(1610481092, 0)}
a := &asset{bytes: bytes, info: info}
return a, nil
}
var __000006_sharing_tableDownSql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x72\x09\xf2\x0f\x50\x08\x71\x74\xf2\x71\x55\x28\xce\x48\x2c\xca\xcc\x4b\xb7\xe6\x02\x04\x00\x00\xff\xff\xdd\x4c\x62\xe8\x14\x00\x00\x00")
func _000006_sharing_tableDownSqlBytes() ([]byte, error) {
return bindataRead(
__000006_sharing_tableDownSql,
"000006_sharing_table.down.sql",
)
}
func _000006_sharing_tableDownSql() (*asset, error) {
bytes, err := _000006_sharing_tableDownSqlBytes()
if err != nil {
return nil, err
}
info := bindataFileInfo{name: "000006_sharing_table.down.sql", size: 20, mode: os.FileMode(420), modTime: time.Unix(1610482438, 0)}
a := &asset{bytes: bytes, info: info}
return a, nil
}
var __000006_sharing_tableUpSql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x5c\xcc\xb1\xca\xc2\x30\x10\x00\xe0\x39\xf7\x14\x37\xb6\xd0\xa1\x3f\x3f\xb8\x38\x5d\xcb\xa9\xc1\xda\x4a\x1a\xc4\x4e\x25\xe5\xa2\x06\xb5\x15\x8d\x83\x6f\x2f\x2e\x0e\xee\x1f\x5f\x69\x98\x2c\xa3\xa5\xa2\x62\xd4\x0b\xac\x1b\x8b\xbc\xd7\xad\x6d\xf1\x71\x72\xf7\x30\x1e\x31\x01\x15\x04\x77\x64\xca\x15\x99\xe4\x7f\x96\x66\xa0\xfc\xe8\x86\x8b\x17\x2c\x9a\xa6\x62\xaa\x33\x50\x71\x3a\xfb\xf1\xab\xfe\xf2\xfc\xc3\xae\x93\x84\x43\xf0\xd2\x0f\xaf\x9f\xe0\x79\x13\x17\x7d\xef\x22\x16\x7a\xa9\x6b\x9b\x81\xda\x1a\xbd\x21\xd3\xe1\x9a\x3b\x4c\x82\xa4\x90\xce\xe1\x1d\x00\x00\xff\xff\x6c\x91\x98\xb6\x9f\x00\x00\x00")
func _000006_sharing_tableUpSqlBytes() ([]byte, error) {
return bindataRead(
__000006_sharing_tableUpSql,
"000006_sharing_table.up.sql",
)
}
func _000006_sharing_tableUpSql() (*asset, error) {
bytes, err := _000006_sharing_tableUpSqlBytes()
if err != nil {
return nil, err
}
info := bindataFileInfo{name: "000006_sharing_table.up.sql", size: 159, mode: os.FileMode(420), modTime: time.Unix(1610483328, 0)}
a := &asset{bytes: bytes, info: info}
return a, nil
}
@ -337,6 +379,8 @@ var _bindata = map[string]func() (*asset, error){
"000004_auth_table.up.sql": _000004_auth_tableUpSql,
"000005_blocks_modifiedby.down.sql": _000005_blocks_modifiedbyDownSql,
"000005_blocks_modifiedby.up.sql": _000005_blocks_modifiedbyUpSql,
"000006_sharing_table.down.sql": _000006_sharing_tableDownSql,
"000006_sharing_table.up.sql": _000006_sharing_tableUpSql,
}
// AssetDir returns the file names below a certain
@ -389,6 +433,8 @@ var _bintree = &bintree{nil, map[string]*bintree{
"000004_auth_table.up.sql": &bintree{_000004_auth_tableUpSql, map[string]*bintree{}},
"000005_blocks_modifiedby.down.sql": &bintree{_000005_blocks_modifiedbyDownSql, map[string]*bintree{}},
"000005_blocks_modifiedby.up.sql": &bintree{_000005_blocks_modifiedbyUpSql, map[string]*bintree{}},
"000006_sharing_table.down.sql": &bintree{_000006_sharing_tableDownSql, map[string]*bintree{}},
"000006_sharing_table.up.sql": &bintree{_000006_sharing_tableUpSql, map[string]*bintree{}},
}}
// RestoreAsset restores an asset under the given directory

View file

@ -0,0 +1 @@
DROP TABLE sharing;

View file

@ -0,0 +1,8 @@
CREATE TABLE IF NOT EXISTS sharing (
id VARCHAR(36),
enabled BOOLEAN,
token VARCHAR(100),
modified_by VARCHAR(36),
update_at BIGINT,
PRIMARY KEY (id)
);

View file

@ -0,0 +1,62 @@
package sqlstore
import (
"time"
"github.com/mattermost/mattermost-octo-tasks/server/model"
sq "github.com/Masterminds/squirrel"
)
func (s *SQLStore) UpsertSharing(sharing model.Sharing) error {
now := time.Now().Unix()
query := s.getQueryBuilder().
Insert("sharing").
Columns(
"id",
"enabled",
"token",
"modified_by",
"update_at",
).
Values(
sharing.ID,
sharing.Enabled,
sharing.Token,
sharing.ModifiedBy,
now,
).
Suffix("ON CONFLICT (id) DO SET enabled = EXCLUDED.enabled, token = EXCLUDED.token, modified_by = EXCLUDED.modified_by, update_at = EXCLUDED.update_at")
_, err := query.Exec()
return err
}
func (s *SQLStore) GetSharing(rootID string) (*model.Sharing, error) {
query := s.getQueryBuilder().
Select(
"id",
"enabled",
"token",
"modified_by",
"update_at",
).
From("sharing").
Where(sq.Eq{"id": rootID})
row := query.QueryRow()
sharing := model.Sharing{}
err := row.Scan(
&sharing.ID,
&sharing.Enabled,
&sharing.Token,
&sharing.ModifiedBy,
&sharing.UpdateAt,
)
if err != nil {
return nil, err
}
return &sharing, nil
}

View file

@ -14,18 +14,25 @@ type Store interface {
GetParentID(blockID string) (string, error)
InsertBlock(block model.Block) error
DeleteBlock(blockID string, modifiedBy string) error
Shutdown() error
GetSystemSettings() (map[string]string, error)
SetSystemSetting(key string, value string) error
GetUserById(userID string) (*model.User, error)
GetUserByEmail(email string) (*model.User, error)
GetUserByUsername(username string) (*model.User, error)
CreateUser(user *model.User) error
UpdateUser(user *model.User) error
GetSession(token string, expireTime int64) (*model.Session, error)
CreateSession(session *model.Session) error
RefreshSession(session *model.Session) error
UpdateSession(session *model.Session) error
DeleteSession(sessionId string) error
CleanUpSessions(expireTime int64) error
UpsertSharing(sharing model.Sharing) error
GetSharing(rootID string) (*model.Sharing, error)
}