-
Notifications
You must be signed in to change notification settings - Fork 14
/
Add-Interaction.ps1
144 lines (120 loc) · 11.4 KB
/
Add-Interaction.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
function Add-Interaction
{
<#
.Synopsis
Adds a user interaction to user storage
.Description
Adds a user interaction to user storage.
Interactions are things a user has done within a service.
For instance, you might Add-Interaction "VoteCount" 1 when someone votes on a video.
You can use interactions for personalized statistical tracking, and you can also use interactions to control if someone can use a command.
Interactions may only contain numbers. Interactions can only be used within a Pipeworks web site
.Example
$session["User"] |
Add-Interaction "VoteCount" 1
.Link
Get-Person
.Link
Confirm-Person
#>
[OutputType([Nullable], [PSObject])]
[CmdletBinding(DefaultParameterSetName='ChangeInteraction')]
param(
# The object that is being interacted with,
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
[PSObject]
$InputObject,
# The name of the interaction
[Parameter(Mandatory=$true,Position=0,ValueFromPipelineByPropertyName=$true,ParameterSetName='ChangeInteraction')]
[string]
$InteractionName,
# The amount the interaction has changed.
[Parameter(Position=1,ValueFromPipelineByPropertyName=$true, ParameterSetName='ChangeInteraction')]
[double]
$Change = 1,
# If set, the interaction count will be set to the change, instead of added to the change.
[Parameter(ValueFromPipelineByPropertyName=$true, ParameterSetName='ChangeInteraction')]
[Switch]
$ResetInteractionCount,
# The name of the award that will be given to the object
[Parameter(ValueFromPipelineByPropertyName=$true,ParameterSetName='AddAward',Mandatory=$true)]
[Parameter(ValueFromPipelineByPropertyName=$true,ParameterSetName='RemoveAward',Mandatory=$true)]
[string]
$AwardName,
# If set, the award will be removed from the object, instead of added to it.
[Parameter(ValueFromPipelineByPropertyName=$true,ParameterSetName='RemoveAward',Mandatory=$true)]
[Switch]
$RemoveAward,
# If set, will output the modified object.
[Switch]
$PassThru
)
process {
$shouldCommit = $false
if ($PSCmdlet.ParameterSetName -eq 'AddAward') {
$awardsList = @($InputObject.Awards -split ';' -ne '')
$oldAwardsCount = $awardsList.Count
$awardsList = @($awardsList) + $AwardName
$awardsList = @($awardsList | Select-Object -Unique | Sort-Object)
$awardsText = $awardsList -join ';'
$InputObject | Add-member NoteProperty Awards $awardsText -Force
$newAwardsCount = $awardsList.Count
if ($newAwardsCount -ne $oldAwardsCount) {
$shouldCommit = $true
}
} elseif ($PSCmdlet.ParameterSetName -eq 'RemoveAward') {
$awardsList = @($InputObject.Awards -split ';' -ne '')
$oldAwardsCount = $awardsList.Count
$awardsList = @($awardsList | Where-Object {$_ -ne $AwardName })
$newAwardsCount = $awardsList.Count
$awardsText = @($awardsList | Select-Object -Unique | Sort-Object) -join ';'
$InputObject | Add-member NoteProperty Awards $awardsText -Force
if ($oldAwardsCount -ne $newAwardsCount) {
$shouldCommit = $true
}
} elseif ($PSCmdlet.ParameterSetName -eq 'ChangeInteraction') {
$interactionTable = if ($InputObject.InteractionCount) {
ConvertFrom-StringData -StringData "$($InputObject.InteractionCount)".Replace(":", "=")
} else {
@{}
}
if (-not $interactionTable[$InteractionName]) {
$interactionTable[$InteractionName] = $Change
} else {
if ($ResetInteractionCount) {
$interactionTable[$InteractionName] = $Change
} else {
$interactionTable[$InteractionName] = ($interactionTable[$InteractionName] -as [double]) + $Change
}
}
$Interactions =
@(foreach ($kv in $interactionTable.GetEnumerator() | Sort-Object Key) {
"$($kv.Key):$($kv.Value)"
}) -join ([Environment]::NewLine)
$InputObject |
Add-Member NoteProperty InteractionCount $Interactions -Force
$shouldCommit = $true
}
#region Commit Changes if needed
if ($shouldCommit) {
if ($StorageAccount -and $StorageKey) {
if (-not $TableName) {
$TableName = $InputObject.TableName
}
$InputObject | Update-AzureTable -StorageAccount $StorageAccount -StorageKey $StorageKey -TableName $TableName -Value { $_ }
} elseif ($ConnectionString) {
$InputObject | Update-Sql -TableName $TableName -ConnectionStringOrSetting $ConnectionString
} else {
if ($InputObject.partitionKey -and $InputObject.RowKey -and $InputObject.TableName) {
$InputObject | Update-AzureTable -Value { $_ } -TableName $InputObject.TableName
} elseif ($InputObject.getParentRow -and $pipeworksManifest.UserDB.Name) {
$InputObject | Update-Sql -TableName $pipeworksManifest.UserDb.Name
}
}
}
#endregion Commit Changes if needed
if ($passthru) {
$InputObject
}
}
}