Skip to content

Commit

Permalink
View migration support (#430)
Browse files Browse the repository at this point in the history
* Tweak

* Tweak

* Fix comment.

---------

Co-authored-by: lbwexler <[email protected]>
  • Loading branch information
cnrudd and lbwexler authored Dec 27, 2024
1 parent c6a33ac commit 8675f25
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions grails-app/services/io/xh/hoist/view/ViewService.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class ViewService extends BaseService {
/**
* Get all the views, and the current view state for a user
*/
Map getAllData(String type, String viewInstance) {
def blobs = jsonBlobService.list(type).split { it.name == STATE_BLOB_NAME }
Map getAllData(String type, String viewInstance, String username = username) {
def blobs = jsonBlobService.list(type, username).split { it.name == STATE_BLOB_NAME }
def (rawState, views) = [blobs[0], blobs[1]]

// Transform state
Expand All @@ -54,8 +54,8 @@ class ViewService extends BaseService {
}

/** Update state for this user */
void updateState(String type, String viewInstance, Map update) {
def currBlob = jsonBlobService.find(type, STATE_BLOB_NAME, username),
void updateState(String type, String viewInstance, Map update, String username = username) {
def currBlob = jsonBlobService.find(type, STATE_BLOB_NAME, username, username),
currValue = parseObject(currBlob?.value),
newValue = [
currentView: currValue?.currentView ?: [:],
Expand All @@ -67,34 +67,34 @@ class ViewService extends BaseService {
if (update.containsKey('userPinned')) newValue.userPinned = update.userPinned
if (update.containsKey('autoSave')) newValue.autoSave = update.autoSave

jsonBlobService.createOrUpdate(type, STATE_BLOB_NAME, [value: newValue])
jsonBlobService.createOrUpdate(type, STATE_BLOB_NAME, [value: newValue], username)
}

//---------------------------
// Individual View management
//----------------------------
/** Fetch the latest version of a view. */
Map get(String token) {
jsonBlobService.get(token).formatForClient(true)
Map get(String token, String username = username) {
jsonBlobService.get(token, username).formatForClient(true)
}

/** Fetch the latest version of a view. */
Map create(Map data) {
def ret = jsonBlobService.create(
/** Create a new view. */
Map create(Map data, String username = username) {
def ret = jsonBlobService.create([
type: data.type,
name: data.name,
description: data.description,
acl: data.isShared ? '*' : null,
meta: [group: data.group, isShared: data.isShared],
value: data.value
)
], username)
trackChange('Created View', ret)
ret.formatForClient(true)
}

/** Update a views metadata */
Map updateInfo(String token, Map data) {
def existing = jsonBlobService.get(token),
Map updateInfo(String token, Map data, String username = username) {
def existing = jsonBlobService.get(token, username),
existingMeta = parseObject(existing.meta),
isGlobal = existingMeta.isGlobal,
isShared = data.containsKey('isShared') ? data.isShared : existingMeta.isShared;
Expand All @@ -107,38 +107,39 @@ class ViewService extends BaseService {
meta: isGlobal ?
[group: data.group, isDefaultPinned: !!data.isDefaultPinned]:
[group: data.group, isShared: !!data.isShared],
]
],
username
)
trackChange('Updated View Info', ret)
ret.formatForClient(true)
}

/** Update a views value */
Map updateValue(String token, Map value) {
def ret = jsonBlobService.update(token, [value: value]);
Map updateValue(String token, Map value, String username = username) {
def ret = jsonBlobService.update(token, [value: value], username);
if (ret.owner == null) {
trackChange('Updated Global View definition', ret);
}
ret.formatForClient(true)
}

/** Make a view global */
Map makeGlobal(String token) {
def existing = jsonBlobService.get(token),
Map makeGlobal(String token, String username = username) {
def existing = jsonBlobService.get(token, username),
meta = parseObject(existing.meta)?.findAll { it.key != 'isShared' },
ret = jsonBlobService.update(token, [owner: null, acl: '*', meta: meta])
ret = jsonBlobService.update(token, [owner: null, acl: '*', meta: meta], username)

this.trackChange('Made View Global', ret)
ret.formatForClient(true)
}


/** Bulk Delete views */
void delete(List<String> tokens) {
void delete(List<String> tokens, String username = username) {
List<Exception> failures = []
tokens.each {
try {
jsonBlobService.archive(it)
jsonBlobService.archive(it, username)
} catch (Exception e) {
failures << e
logError('Failed to delete View', [token: it], e)
Expand Down

0 comments on commit 8675f25

Please sign in to comment.