Skip to content

Commit

Permalink
feat: add partial message update (#77)
Browse files Browse the repository at this point in the history
Co-authored-by: peterdeme <peter.deme@getstream.io>
  • Loading branch information
peterdeme and peterdeme committed Dec 6, 2021
1 parent 74cf9f9 commit a3ecf14
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yaml
Expand Up @@ -6,6 +6,8 @@ jobs:
basic:
name: Run tests on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
env:
DOTNET_CLI_TELEMETRY_OPTOUT: "true"
strategy:
max-parallel: 1
matrix:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/release.yaml
Expand Up @@ -9,6 +9,8 @@ jobs:
Release:
if: "${{ startsWith(github.event.head_commit.message, 'chore(release)') }}"
runs-on: ubuntu-latest
env:
DOTNET_CLI_TELEMETRY_OPTOUT: "true"
steps:
- uses: actions/checkout@v2
with:
Expand Down
40 changes: 40 additions & 0 deletions src/stream-chat-net-test/ClientTests.cs
Expand Up @@ -172,6 +172,46 @@ public async Task TestUpdateMessage()
Assert.AreEqual("stuff", updatedMessage.GetData<string>("new"));
}

[Test]
public async Task TestUpdateMessagePartial()
{
var user1 = new User()
{
ID = Guid.NewGuid().ToString(),
Role = Role.Admin,
};

await this._client.Users.Upsert(user1);

var channel = this._client.Channel("messaging", Guid.NewGuid().ToString());
await channel.Create(user1.ID, new string[] { user1.ID });

var initialMsg = new MessageInput
{
Text = Guid.NewGuid().ToString()
};
initialMsg.SetData("foo", "barsky");
initialMsg.SetData("bar", "baz");

var msg = await channel.SendMessage(initialMsg, user1.ID);

var resp = await _client.UpdateMessagePartial(msg.ID, new MessagePartialUpdateRequest
{
UserId = user1.ID,
Set = new Dictionary<string, object>
{
{ "foo", "new" }
},
Unset = new List<string> { "bar"},
});

Assert.AreEqual(msg.ID, resp.Message.ID);
Assert.AreEqual(msg.Text, resp.Message.Text);
Assert.NotNull(resp.Duration);
Assert.AreEqual(resp.Message.GetData<string>("foo"), "new");
Assert.IsNull(resp.Message.GetData<string>("bar"));
}

[Test]
public async Task TestGetMessage()
{
Expand Down
26 changes: 26 additions & 0 deletions src/stream-chat-net/Client.cs
Expand Up @@ -292,6 +292,32 @@ public async Task<Message> UpdateMessage(MessageInput msg)
throw StreamChatException.FromResponse(response);
}

public async Task<MessagePartialUpdateResponse> UpdateMessagePartial(string messageId, MessagePartialUpdateRequest partialUpdateRequest)
{
if (string.IsNullOrWhiteSpace(messageId))
{
throw new ArgumentException("The messageId must be set.", nameof(messageId));
}

var endpoint = string.Format("messages/{0}", messageId);
var request = this.BuildAppRequest(endpoint, HttpMethod.PUT);
request.SetJsonBody(JsonConvert.SerializeObject(partialUpdateRequest));

var response = await MakeRequest(request);
if (response.StatusCode == System.Net.HttpStatusCode.Created)
{
var respObj = JObject.Parse(response.Content);
var msgObj = respObj.Property("message").Value as JObject;

return new MessagePartialUpdateResponse
{
Duration = respObj.Property("duration").Value.ToString(),
Message = Message.FromJObject(msgObj)
};
}
throw StreamChatException.FromResponse(response);
}

public async Task<Message> DeleteMessage(string messageID, bool hardDelete = false)
{
var endpoint = string.Format("messages/{0}", messageID);
Expand Down
1 change: 1 addition & 0 deletions src/stream-chat-net/IClient.cs
Expand Up @@ -32,6 +32,7 @@ public interface IClient
Task<MessageSearchResponse> Search(SearchOptions opts);
Task<Message> GetMessage(string messageID);
Task<Message> UpdateMessage(MessageInput msg);
Task<MessagePartialUpdateResponse> UpdateMessagePartial(string messageId, MessagePartialUpdateRequest partialUpdateRequest);
Task<Message> DeleteMessage(string messageID, bool hardDelete = false);


Expand Down
24 changes: 24 additions & 0 deletions src/stream-chat-net/Message.cs
Expand Up @@ -189,4 +189,28 @@ public class MessageSearchResult

public MessageSearchResult() { }
}
public class MessagePartialUpdateRequest
{
[JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "user_id")]
public string UserId { get; set; }

[JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "user")]
public User User { get; set; }

[JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "set")]
public Dictionary<string, object> Set { get; set; }

[JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "unset")]
public List<string> Unset { get; set; }

[JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "skip_enrich_url")]
public bool? SkipEnrichUrl { get; set; }
}

public class MessagePartialUpdateResponse
{
public Message Message { get; internal set; }

public string Duration { get; internal set; }
}
}

0 comments on commit a3ecf14

Please sign in to comment.