Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/IStreamFeed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public interface IStreamFeed
/// <remarks>https://getstream.io/activity-feeds/docs/dotnet-csharp/feeds_101/?language=csharp</remarks>
Task<AddActivitiesResponse> AddActivitiesAsync(IEnumerable<Activity> activities);

Task<UpdateToTargetsResponse> BatchUpdateActivityToTargetsAsync(List<UpdateToTargetsRequest> reqs);

/// <summary>Add a new activity to the feed.</summary>
/// <remarks>https://getstream.io/activity-feeds/docs/dotnet-csharp/feeds_101/?language=csharp</remarks>
Task<Activity> AddActivityAsync(Activity activity);
Expand Down
27 changes: 27 additions & 0 deletions src/Models/UpdateToTargets.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;

Check warning on line 2 in src/Models/UpdateToTargets.cs

View workflow job for this annotation

GitHub Actions / Run tests

Using directives should be ordered alphabetically by the namespaces

Check warning on line 2 in src/Models/UpdateToTargets.cs

View workflow job for this annotation

GitHub Actions / Run tests

Using directives should be ordered alphabetically by the namespaces

Check warning on line 2 in src/Models/UpdateToTargets.cs

View workflow job for this annotation

GitHub Actions / Run tests

Using directives should be ordered alphabetically by the namespaces

Check warning on line 2 in src/Models/UpdateToTargets.cs

View workflow job for this annotation

GitHub Actions / Run tests

Using directives should be ordered alphabetically by the namespaces
using Newtonsoft.Json;

namespace Stream.Models
{
public class UpdateToTargetsRequest
{
[JsonProperty("foreign_id")]
public string ForeignID { get; set; }

[JsonProperty("time")]
public string Time { get; set; }

[JsonProperty("id")]
public string Id { get; set; }

[JsonProperty("new_targets")]
public List<string> NewTargets { get; set; }

[JsonProperty("added_targets")]
public List<string> Adds { get; set; }

[JsonProperty("removed_targets")]
public List<string> RemovedTargets { get; set; }
}
}
20 changes: 20 additions & 0 deletions src/StreamFeed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,24 @@
throw StreamException.FromResponse(response);
}

public async Task<UpdateToTargetsResponse> BatchUpdateActivityToTargetsAsync(List<UpdateToTargetsRequest> reqs)
{
var endpoint = $"feed_targets/{_feedSlug}/{_userId}/activity_to_targets/";

var request = _client.BuildAppRequest(endpoint, HttpMethod.Post);
request.SetJsonBody(StreamJsonConverter.SerializeObject(reqs));
var response = await _client.MakeRequestAsync(request);
if (response.StatusCode != HttpStatusCode.Created)
{
throw new HttpRequestException($"Request failed with status code {response.StatusCode}");
}

if (response.StatusCode == HttpStatusCode.Created)
return StreamJsonConverter.DeserializeObject<UpdateToTargetsResponse>(response.Content);

throw StreamException.FromResponse(response);
}

public async Task<UpdateToTargetsResponse> UpdateActivityToTargetsAsync(string id,
IEnumerable<string> adds = null,
IEnumerable<string> newTargets = null,
Expand All @@ -135,7 +153,9 @@
adds?.ForEach(FeedIdValidator.ThrowIfFeedIdIsInvalid);
newTargets?.ForEach(FeedIdValidator.ThrowIfFeedIdIsInvalid);
removed?.ForEach(FeedIdValidator.ThrowIfFeedIdIsInvalid);

Check warning on line 156 in src/StreamFeed.cs

View workflow job for this annotation

GitHub Actions / Run tests

Code should not contain multiple blank lines in a row

Check warning on line 156 in src/StreamFeed.cs

View workflow job for this annotation

GitHub Actions / Run tests

Code should not contain multiple blank lines in a row

Check warning on line 156 in src/StreamFeed.cs

View workflow job for this annotation

GitHub Actions / Run tests

Code should not contain multiple blank lines in a row

Check warning on line 156 in src/StreamFeed.cs

View workflow job for this annotation

GitHub Actions / Run tests

Code should not contain multiple blank lines in a row


var payload = new
{
id = id,
Expand Down
90 changes: 90 additions & 0 deletions tests/ActivityTests/UpdateActivityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,96 @@ public async Task TestUpdateToTargets()
Assert.AreEqual(2, updatedAct.To.ToList().FindAll(t => newOnes.Contains(t)).Count);
}

[Test]
public async Task TestBatchUpdateToTargets()
{
var fidTime = new ForeignIdTime(Guid.NewGuid().ToString(), DateTime.UtcNow);

var targets = new List<string>()
{
"flat:" + Guid.NewGuid().ToString(),
"user:" + Guid.NewGuid().ToString(),
};

var act = new Activity("upd", "test", "1")
{
ForeignId = fidTime.ForeignId,
Time = fidTime.Time,
To = targets,
};

var insertedAct = await this.UserFeed.AddActivityAsync(act);
Assert.AreEqual(2, insertedAct.To.Count);

// add 1
var add = "user:" + Guid.NewGuid().ToString();
var updateReqs = new List<UpdateToTargetsRequest>
{
new UpdateToTargetsRequest
{
Id = insertedAct.Id,
Adds = new List<string> { add },
}
};
var updateResp = await this.UserFeed.BatchUpdateActivityToTargetsAsync(updateReqs);
Assert.AreEqual(insertedAct.Id, updateResp.Activity.Id);
Assert.AreEqual(1, updateResp.Added.Count);
Assert.AreEqual(add, updateResp.Added[0]);
Assert.AreEqual(3, updateResp.Activity.To.Count);
Assert.IsNotNull(updateResp.Activity.To.ToList().Find(t => t == add));

var updatedAct = (await this.UserFeed.GetActivitiesAsync(0, 1, FeedFilter.Where().IdLessThanEqual(insertedAct.Id))).Results.FirstOrDefault();
Assert.NotNull(updatedAct);
Assert.AreEqual(3, updatedAct.To.Count);
Assert.IsNotNull(updatedAct.To.ToList().Find(t => t == add));

// remove 1
var remove = targets[0];
updateReqs = new List<UpdateToTargetsRequest>
{
new UpdateToTargetsRequest
{
Id = insertedAct.Id,
RemovedTargets = new List<string> { remove },
}
};
updateResp = await this.UserFeed.BatchUpdateActivityToTargetsAsync(updateReqs);
Assert.AreEqual(insertedAct.Id, updateResp.Activity.Id);
Assert.AreEqual(1, updateResp.Removed.Count);
Assert.AreEqual(remove, updateResp.Removed[0]);
Assert.AreEqual(2, updateResp.Activity.To.Count);
Assert.IsNull(updateResp.Activity.To.ToList().Find(t => t == remove));

updatedAct = (await this.UserFeed.GetActivitiesAsync(0, 1, FeedFilter.Where().IdLessThanEqual(insertedAct.Id))).Results.FirstOrDefault();
Assert.NotNull(updatedAct);
Assert.AreEqual(2, updatedAct.To.Count);
Assert.IsNull(updatedAct.To.ToList().Find(t => t == remove));

// new ones
var newOnes = new List<string>()
{
"flat:" + Guid.NewGuid().ToString(),
"user:" + Guid.NewGuid().ToString(),
};
updateReqs = new List<UpdateToTargetsRequest>
{
new UpdateToTargetsRequest
{
Id = insertedAct.Id,
NewTargets = newOnes,
}
};
updateResp = await this.UserFeed.BatchUpdateActivityToTargetsAsync(updateReqs);
Assert.AreEqual(insertedAct.Id, updateResp.Activity.Id);
Assert.AreEqual(2, updateResp.Activity.To.Count);
Assert.AreEqual(2, updateResp.Added.Count);
Assert.AreEqual(2, updateResp.Added.ToList().FindAll(t => newOnes.Contains(t)).Count);
updatedAct = (await this.UserFeed.GetActivitiesAsync(0, 1, FeedFilter.Where().IdLessThanEqual(insertedAct.Id))).Results.FirstOrDefault();
Assert.NotNull(updatedAct);
Assert.AreEqual(2, updatedAct.To.Count);
Assert.AreEqual(2, updatedAct.To.ToList().FindAll(t => newOnes.Contains(t)).Count);
}

[Test]
public async Task TestActivityPartialUpdateByForeignIDTime()
{
Expand Down
17 changes: 17 additions & 0 deletions tests/ClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,24 @@
Assert.AreEqual(true, (bool)result["testing"]);
Assert.False(result.ContainsKey("missing"));
}

Check warning on line 118 in tests/ClientTests.cs

View workflow job for this annotation

GitHub Actions / Run tests

Code should not contain trailing whitespace
[Test]
public void TestModerationToken()
{
var result = DecodeJwt(Client.CreateUserToken("user"));
Assert.AreEqual("user", (string)result["user_id"]);

var extra = new Dictionary<string, object>()
{
{ "client", "dotnet" },
{ "required_moderation_template", "mod_template_1" },
};
result = DecodeJwt(Client.CreateUserToken("user2", extra));

Assert.AreEqual("mod_template_1", (string)result["required_moderation_template"]);
Assert.False(result.ContainsKey("missing"));
}

Check warning on line 135 in tests/ClientTests.cs

View workflow job for this annotation

GitHub Actions / Run tests

Code should not contain trailing whitespace
private Dictionary<string, object> DecodeJwt(string token)
{
var segment = token.Split('.')[1];
Expand Down