Skip to content

Commit

Permalink
Merge pull request #1875 from zarvox/fix-offer-uiview
Browse files Browse the repository at this point in the history
Fix powerbox offer of UiView
  • Loading branch information
kentonv committed Apr 21, 2016
2 parents e1fe345 + afc9e16 commit 60a98fe
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 17 deletions.
2 changes: 1 addition & 1 deletion shell/packages/sandstorm-backend/sandstorm-backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ SandstormBackend.prototype.updateLastActive = function (grainId, userId, identit
Meteor.users.update({ _id: identityId }, { $set: { lastActive: now } });
// Update any API tokens that match this user/grain pairing as well
ApiTokens.update({ grainId: grainId, "owner.user.identityId": identityId },
{ $set: { "owner.user.lastUsed": now } });
{ $set: { lastUsed: now } });
}

if (Meteor.settings.public.quotaEnabled) {
Expand Down
1 change: 1 addition & 0 deletions shell/packages/sandstorm-db/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ ApiTokens = new Mongo.Collection("apiTokens");
// revoked: If true, then this sturdyref has been revoked and can no longer be restored. It may
// become un-revoked in the future.
// expires: Optional expiration Date. If undefined, the token does not expire.
// lastUsed: Optional Date when this token was last used.
// owner: A `ApiTokenOwner` (defined in `supervisor.capnp`, stored as a JSON object)
// as passed to the `save()` call that created this token. If not present, treat
// as `webkey` (the default for `ApiTokenOwner`).
Expand Down
14 changes: 14 additions & 0 deletions shell/packages/sandstorm-db/migrations.js
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,19 @@ function unsetSmtpDefaultHostnameIfNoUsersExist() {
}
}

function extractLastUsedFromApiTokenOwner() {
// We used to store lastUsed as a field on owner.user. It makes more sense to store lastUsed on
// the apiToken as a whole. This migration hoists such values from owner.user onto the apiToken
// itself.
ApiTokens.find({ "owner.user": { $exists: true } }).forEach(function (token) {
const lastUsed = token.owner.user.lastUsed;
ApiTokens.update({ _id: token._id }, {
$set: { lastUsed: lastUsed },
$unset: { "owner.user.lastUsed": true },
});
});
}

// This must come after all the functions named within are defined.
// Only append to this list! Do not modify or remove list entries;
// doing so is likely change the meaning and semantics of user databases.
Expand Down Expand Up @@ -571,6 +584,7 @@ const MIGRATIONS = [
smtpPortShouldBeNumber,
consolidateOrgSettings,
unsetSmtpDefaultHostnameIfNoUsersExist,
extractLastUsedFromApiTokenOwner,
];

function migrateToLatest() {
Expand Down
1 change: 0 additions & 1 deletion shell/packages/sandstorm-permissions/permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,6 @@ SandstormPermissions.createNewApiToken = function (db, provider, grainId, petnam
user: {
identityId: owner.user.identityId,
title: owner.user.title,
// lastUsed: ??
denormalizedGrainMetadata: grainInfo,
}
};
Expand Down
7 changes: 4 additions & 3 deletions shell/packages/sandstorm-ui-grainlist/grainlist-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ SandstormGrainListPage.mapApiTokensToTemplateObject = function (apiTokens, stati
const grainIdsForApiTokens = Object.keys(tokensForGrain);
return grainIdsForApiTokens.map(function (grainId) {
// Pick the most recently used one.
const token = _.sortBy(tokensForGrain[grainId], function (t) {
if (t.owner && t.owner.user && t.owner.user.lastUsed) { return -t.owner.user.lastUsed; } else {return 0; } })[0];
const token = _.sortBy(tokensForGrain[grainId], (t) => {
return t.lastUsed ? -t.lastUsed : 0;
})[0];

const ownerData = token.owner.user;
const grainInfo = ownerData.denormalizedGrainMetadata;
Expand All @@ -49,7 +50,7 @@ SandstormGrainListPage.mapApiTokensToTemplateObject = function (apiTokens, stati
_id: grainId,
title: ownerData.title,
appTitle: appTitle,
lastUsed: ownerData.lastUsed,
lastUsed: token.lastUsed,
iconSrc: iconSrc,
isOwnedByMe: false,
};
Expand Down
2 changes: 1 addition & 1 deletion shell/packages/sandstorm-ui-grainview/grainview.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ GrainView = class GrainView {
grainId: this._grainId,
"owner.user.identityId": { $in: myIdentityIds },
}, {
sort:{ "owner.user.lastUsed": -1 },
sort: { lastUsed: -1 },
});

if (token) {
Expand Down
2 changes: 1 addition & 1 deletion shell/packages/sandstorm-ui-powerbox/powerbox-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ SandstormPowerboxRequest = class SandstormPowerboxRequest {
(window.location.protocol + "//" + staticAssetHost + "/" + grainInfo.icon.assetId) :
Identicon.identiconForApp(
(grainInfo && grainInfo.appId) || "00000000000000000000000000000000");
cardData.lastUsed = ownerData.lastUsed;
cardData.lastUsed = apiToken.lastUsed;
cardData.apiTokenId = apiToken._id;

cardData.callback = () => () => {
Expand Down
1 change: 0 additions & 1 deletion shell/server/hack-session.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ SessionContextImpl = class SessionContextImpl {
identityId: this.identityId,
// The following fields will be overwritten by PersistentUiView.save(), so no need to
// pass them in:
//lastUsed: new Date(),
//title: "", // This will be replaced by the token's title
//denormalizedGrainMetadata: {}, // This will look up the package for the grain referenced.
},
Expand Down
2 changes: 1 addition & 1 deletion shell/server/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ Meteor.methods({
"owner.user.identityId": apiToken.identityId,
}, {
sort: {
"owner.user.lastUsed": -1,
lastUsed: -1,
},
});
if (sharerToken) {
Expand Down
3 changes: 2 additions & 1 deletion shell/server/stats-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ computeStats = function (since) {

const counts = ApiTokens.aggregate([
{ $match: {
"owner.user.lastUsed": timeConstraint,
"owner.user": { $exists: true },
lastUsed: timeConstraint,
grainId: { $in: grainIds },
},
},
Expand Down
10 changes: 3 additions & 7 deletions src/sandstorm/supervisor.capnp
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ struct ApiTokenOwner {
saveLabel @2 :Util.LocalizedText;
# As passed to `save()` in Sandstorm's Persistent interface.

introducerIdentity @10 :Text;
introducerIdentity @9 :Text;
# The identity ID through which a user's powerbox action caused the grain to receive this
# token. This is the identity against which the `requiredPermissions` parameter
# to `restore()` will be checked. This field is only intended to be filled in by the
Expand All @@ -298,7 +298,7 @@ struct ApiTokenOwner {
# Owned by a user's identity. If the token represents a UiView, then it will show up in this
# user's grain list.

identityId @11 :Text;
identityId @10 :Text;
# The identity that is allowed to restore this token.

title @7 :Text;
Expand All @@ -307,11 +307,7 @@ struct ApiTokenOwner {
# Fields below this line are not actually allowed to be passed to save(), but are added
# internally.

lastUsed @8 :Int64;
# The last time the user used this API token with the associated grain, in milliseconds
# since the epoch (equivalent to javascript's new Date().getTime())

denormalizedGrainMetadata @9 :DenormalizedGrainMetadata;
denormalizedGrainMetadata @8 :DenormalizedGrainMetadata;
# Information needed to show the user an app title and icon in the grain list.

userId @6 :Text;
Expand Down

0 comments on commit 60a98fe

Please sign in to comment.