public Task <ActionResult <UserVariableView> > SetUserVariable([FromRoute] string key, [FromBody] string value)
    {
        return(MatchExceptions(async() =>
        {
            RateLimit(RateUserVariable);

            var uid = GetUserIdStrict();

            UserVariableView variable;

            try
            {
                //Get existing variable, set value
                variable = await shortcuts.LookupVariableByKeyAsync(uid, key);
                variable.value = value;
            }
            catch (NotFoundException)
            {
                //Make new variable, set value/key
                variable = new UserVariableView()
                {
                    key = key,
                    value = value
                };
            }

            return await CachedWriter.WriteAsync(variable, uid);
        }));
    }
    public async Task ListenAsync_UserVariable_Privacy()
    {
        //First, set up two listeners
        var ourUser = await searcher.GetById <UserView>(RequestType.user, NormalUserId, true);

        var otherUser = await searcher.GetById <UserView>(RequestType.user, SuperUserId, true);

        var ourListener   = queue.ListenAsync(ourUser, -1, safetySource.Token);
        var otherListener = queue.ListenAsync(otherUser, -1, safetySource.Token);

        //Now, write a variable for our user
        var variable = new UserVariableView()
        {
            key   = "somekey",
            value = "trash"
        };

        var writtenVariable = await writer.WriteAsync(variable, ourUser.id);

        var result = await ourListener;

        Assert.Contains(result.events, x => x.refId == writtenVariable.id && x.userId == ourUser.id);
        Assert.Contains(EventType.uservariable_event, result.objects.Keys);
        Assert.Contains("uservariable", result.objects[EventType.uservariable_event].Keys);
        Assert.Contains(result.objects[EventType.uservariable_event]["uservariable"], x => (long)x["id"] == writtenVariable.id && (string)x["key"] == "somekey");

        Assert.False(otherListener.Wait(10));
        safetySource.Cancel();

        try { await otherListener; }
        catch (TaskCanceledException) {}
        catch (OperationCanceledException) {}
    }
    public async Task LookupVariableByKey_Simple(long uid, string key)
    {
        //Shouldn't exist at first... we hope?
        await Assert.ThrowsAnyAsync <NotFoundException>(() => service.LookupVariableByKeyAsync(uid, key));

        var variable = new UserVariableView()
        {
            key = key, value = "heck"
        };
        var writtenVariable = await writer.WriteAsync(variable, uid);

        Assert.Equal(variable.value, writtenVariable.value);
        Assert.Equal(variable.key, writtenVariable.key);

        //now go look it up
        var lookupVariable = await service.LookupVariableByKeyAsync(uid, key);

        Assert.Equal(uid, lookupVariable.userId);
        Assert.Equal(key, lookupVariable.key);
        Assert.Equal(writtenVariable.value, lookupVariable.value);
    }
 public Task <ActionResult <UserVariableView> > WriteUserVariableAsync([FromBody] UserVariableView variable) =>
 MatchExceptions(() => WriteAsync(variable));