Beispiel #1
0
        public async Task <(bool success, string status, string error)> UpdateUser(string userSlackAccessToken, string currentStatus, SpotifyPlayerResponse player)
        {
            var profileData = new SlackUpdateStatusModel();

            if (player.IsPlaying && !player.Device.IsPrivateSession)
            {
                var statusText = player.GetStatusText();
                profileData.Profile.StatusText  = statusText;
                profileData.Profile.StatusEmoji = SlackEmoji.GetEmojiForStatus(statusText);
            }

            if (currentStatus != profileData.Profile.StatusText)
            {
                var client = _httpClientFactory.CreateClient();
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", userSlackAccessToken);

                var response = await client.PostAsync(_options.ProfileUri, new StringContent(JsonSerializer.Serialize(profileData), Encoding.UTF8, "application/json"));

                var responseData = await response.Content.ReadAsStringAsync();

                if (string.IsNullOrWhiteSpace(responseData))
                {
                    return(false, currentStatus, SlackErrorType.InvalidResponse);
                }

                var content = JsonSerializer.Deserialize <SlackUpdateStatusResponse>(responseData);
                if (!content.Ok)
                {
                    _logger.LogError("Error occurred while updating user status: {Error}", content.Error);
                    return(false, currentStatus, content.Error);
                }
            }

            return(true, profileData.Profile.StatusText, string.Empty);
        }
Beispiel #2
0
        public async Task <string> UpdateUser(string userSlackAccessToken, string currentStatus, SpotifyPlayerResponse player)
        {
            var profileData = new SlackUpdateStatusModel();

            if (player.IsPlaying && !player.Device.IsPrivateSession)
            {
                var statusText = player.GetStatusText();
                profileData.Profile.StatusText  = statusText;
                profileData.Profile.StatusEmoji = SlackEmoji.GetEmojiForStatus(statusText);
            }

            if (currentStatus != profileData.Profile.StatusText)
            {
                var client = _httpClientFactory.CreateClient();
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", userSlackAccessToken);

                var response = await client.PostAsync(_options.ProfileUri, new StringContent(JsonSerializer.Serialize(profileData), Encoding.UTF8, "application/json"));

                var responseData = await response.Content.ReadAsStringAsync();
            }

            return(profileData.Profile.StatusText);
        }