Skip to content

Commit 8aeef83

Browse files
committed
Add documentation on writing a bot that uses the jetstream
Adding back trimming if you target .net 9.0
1 parent 888e623 commit 8aeef83

33 files changed

+1080
-29
lines changed

buildDocs.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
dotnet tool update -g docfx
2-
docfx .\docs\docfx.json --serve
2+
docfx .\docs\docfx.json --serve --port 9000
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
# editorconfig.org
2+
3+
# top-most EditorConfig file
4+
root = true
5+
6+
[*]
7+
insert_final_newline = true
8+
indent_style = space
9+
indent_size = 4
10+
trim_trailing_whitespace = true
11+
spelling_exclusion_path = .\exclusion.dic
12+
spelling_checkable_types = strings,identifiers,comments
13+
spelling_error_severity = information
14+
15+
# Generated code
16+
[*{_AssemblyInfo.cs,.notsupported.cs,AsmOffsets.cs}]
17+
generated_code = true
18+
19+
# C# files
20+
[*.cs]
21+
# New line preferences
22+
csharp_new_line_before_open_brace = all
23+
csharp_new_line_before_else = true
24+
csharp_new_line_before_catch = true
25+
csharp_new_line_before_finally = true
26+
csharp_new_line_before_members_in_object_initializers = true
27+
csharp_new_line_before_members_in_anonymous_types = true
28+
csharp_new_line_between_query_expression_clauses = true
29+
30+
# Indentation preferences
31+
csharp_indent_block_contents = true
32+
csharp_indent_braces = false
33+
csharp_indent_case_contents = true
34+
csharp_indent_case_contents_when_block = true
35+
csharp_indent_switch_labels = true
36+
csharp_indent_labels = one_less_than_current
37+
38+
# Modifier preferences
39+
csharp_preferred_modifier_order = public,private,protected,internal,file,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,required,volatile,async:suggestion
40+
41+
# avoid this. unless absolutely necessary
42+
dotnet_style_qualification_for_field = false:suggestion
43+
dotnet_style_qualification_for_property = false:suggestion
44+
dotnet_style_qualification_for_method = false:suggestion
45+
dotnet_style_qualification_for_event = false:suggestion
46+
47+
# Types: use keywords instead of BCL types, and permit var only when the type is clear
48+
csharp_style_var_for_built_in_types = false:suggestion
49+
csharp_style_var_when_type_is_apparent = false:none
50+
csharp_style_var_elsewhere = false:suggestion
51+
dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion
52+
dotnet_style_predefined_type_for_member_access = true:suggestion
53+
54+
# name all constant fields using PascalCase
55+
dotnet_naming_rule.constant_fields_should_be_pascal_case.severity = suggestion
56+
dotnet_naming_rule.constant_fields_should_be_pascal_case.symbols = constant_fields
57+
dotnet_naming_rule.constant_fields_should_be_pascal_case.style = pascal_case_style
58+
dotnet_naming_symbols.constant_fields.applicable_kinds = field
59+
dotnet_naming_symbols.constant_fields.required_modifiers = const
60+
dotnet_naming_style.pascal_case_style.capitalization = pascal_case
61+
62+
# static fields should have s_ prefix
63+
dotnet_naming_rule.static_fields_should_have_prefix.severity = suggestion
64+
dotnet_naming_rule.static_fields_should_have_prefix.symbols = static_fields
65+
dotnet_naming_rule.static_fields_should_have_prefix.style = static_prefix_style
66+
dotnet_naming_symbols.static_fields.applicable_kinds = field
67+
dotnet_naming_symbols.static_fields.required_modifiers = static
68+
dotnet_naming_symbols.static_fields.applicable_accessibilities = private, internal, private_protected
69+
dotnet_naming_style.static_prefix_style.required_prefix = s_
70+
dotnet_naming_style.static_prefix_style.capitalization = camel_case
71+
72+
# internal and private fields should be _camelCase
73+
dotnet_naming_rule.camel_case_for_private_internal_fields.severity = suggestion
74+
dotnet_naming_rule.camel_case_for_private_internal_fields.symbols = private_internal_fields
75+
dotnet_naming_rule.camel_case_for_private_internal_fields.style = camel_case_underscore_style
76+
dotnet_naming_symbols.private_internal_fields.applicable_kinds = field
77+
dotnet_naming_symbols.private_internal_fields.applicable_accessibilities = private, internal
78+
dotnet_naming_style.camel_case_underscore_style.required_prefix = _
79+
dotnet_naming_style.camel_case_underscore_style.capitalization = camel_case
80+
81+
# Code style defaults
82+
csharp_using_directive_placement = outside_namespace:suggestion
83+
dotnet_sort_system_directives_first = true
84+
csharp_prefer_braces = true:silent
85+
csharp_preserve_single_line_blocks = true:none
86+
csharp_preserve_single_line_statements = false:none
87+
csharp_prefer_static_local_function = true:suggestion
88+
csharp_prefer_simple_using_statement = false:none
89+
csharp_style_prefer_switch_expression = true:suggestion
90+
dotnet_style_readonly_field = true:suggestion
91+
92+
# Expression-level preferences
93+
dotnet_style_object_initializer = true:suggestion
94+
dotnet_style_collection_initializer = true:suggestion
95+
dotnet_style_explicit_tuple_names = true:suggestion
96+
dotnet_style_coalesce_expression = true:suggestion
97+
dotnet_style_null_propagation = true:suggestion
98+
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion
99+
dotnet_style_prefer_inferred_tuple_names = true:suggestion
100+
dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
101+
dotnet_style_prefer_auto_properties = true:suggestion
102+
dotnet_style_prefer_conditional_expression_over_assignment = true:silent
103+
dotnet_style_prefer_conditional_expression_over_return = true:silent
104+
csharp_prefer_simple_default_expression = true:suggestion
105+
106+
# Expression-bodied members
107+
csharp_style_expression_bodied_methods = true:silent
108+
csharp_style_expression_bodied_constructors = true:silent
109+
csharp_style_expression_bodied_operators = true:silent
110+
csharp_style_expression_bodied_properties = true:silent
111+
csharp_style_expression_bodied_indexers = true:silent
112+
csharp_style_expression_bodied_accessors = true:silent
113+
csharp_style_expression_bodied_lambdas = true:silent
114+
csharp_style_expression_bodied_local_functions = true:silent
115+
116+
# Pattern matching
117+
csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion
118+
csharp_style_pattern_matching_over_as_with_null_check = true:suggestion
119+
csharp_style_inlined_variable_declaration = true:suggestion
120+
121+
# Null checking preferences
122+
csharp_style_throw_expression = true:suggestion
123+
csharp_style_conditional_delegate_call = true:suggestion
124+
125+
# Other features
126+
csharp_style_prefer_index_operator = false:none
127+
csharp_style_prefer_range_operator = false:none
128+
csharp_style_pattern_local_over_anonymous_function = false:none
129+
130+
# Space preferences
131+
csharp_space_after_cast = false
132+
csharp_space_after_colon_in_inheritance_clause = true
133+
csharp_space_after_comma = true
134+
csharp_space_after_dot = false
135+
csharp_space_after_keywords_in_control_flow_statements = true
136+
csharp_space_after_semicolon_in_for_statement = true
137+
csharp_space_around_binary_operators = before_and_after
138+
csharp_space_around_declaration_statements = do_not_ignore
139+
csharp_space_before_colon_in_inheritance_clause = true
140+
csharp_space_before_comma = false
141+
csharp_space_before_dot = false
142+
csharp_space_before_open_square_brackets = false
143+
csharp_space_before_semicolon_in_for_statement = false
144+
csharp_space_between_empty_square_brackets = false
145+
csharp_space_between_method_call_empty_parameter_list_parentheses = false
146+
csharp_space_between_method_call_name_and_opening_parenthesis = false
147+
csharp_space_between_method_call_parameter_list_parentheses = false
148+
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
149+
csharp_space_between_method_declaration_name_and_open_parenthesis = false
150+
csharp_space_between_method_declaration_parameter_list_parentheses = false
151+
csharp_space_between_parentheses = false
152+
csharp_space_between_square_brackets = false
153+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Console.WriteLine("hello world");
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<Product>WatcherBot</Product>
9+
<RootNamespace>WatcherBot</RootNamespace>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="idunno.Bluesky" Version="0.9.1-prerelease" />
14+
</ItemGroup>
15+
16+
</Project>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System.Globalization;
2+
using System.Text;
3+
4+
using idunno.AtProto.Jetstream;
5+
6+
CancellationTokenSource cancellationTokenSource = new();
7+
CancellationToken cancellationToken = cancellationTokenSource.Token;
8+
Console.CancelKeyPress += (sender, e) =>
9+
{
10+
cancellationTokenSource?.Cancel();
11+
e.Cancel = true;
12+
};
13+
14+
Console.OutputEncoding = Encoding.UTF8;
15+
16+
using var jetStream = new AtProtoJetstream();
17+
18+
jetStream.RecordReceived += (sender, e) =>
19+
{
20+
string timeStamp = e.ParsedEvent.DateTimeOffset.ToLocalTime().ToString("G", CultureInfo.DefaultThreadCurrentUICulture);
21+
22+
switch (e.ParsedEvent)
23+
{
24+
case AtJetstreamCommitEvent commitEvent:
25+
Console.WriteLine($"{commitEvent.Did} executed a {commitEvent.Commit.Operation} in {commitEvent.Commit.Collection} at {timeStamp}");
26+
break;
27+
28+
default:
29+
break;
30+
}
31+
};
32+
33+
await jetStream.ConnectAsync(cancellationToken: cancellationToken);
34+
35+
while (!cancellationToken.IsCancellationRequested)
36+
{
37+
}
38+
39+
await jetStream.CloseAsync();
40+
41+
return 0;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<Product>WatcherBot</Product>
9+
<RootNamespace>WatcherBot</RootNamespace>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="idunno.Bluesky" Version="0.9.1-prerelease" />
14+
</ItemGroup>
15+
16+
</Project>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using Microsoft.Extensions.Hosting;
2+
using Microsoft.Extensions.DependencyInjection;
3+
4+
using System.Globalization;
5+
using System.Text;
6+
7+
using idunno.AtProto.Jetstream;
8+
9+
Console.OutputEncoding = Encoding.UTF8;
10+
11+
HostApplicationBuilder builder = new(args);
12+
builder.Services.AddHostedService<Worker>();
13+
14+
IHost host = builder.Build();
15+
host.Run();
16+
17+
return 0;
18+
19+
internal sealed class Worker : BackgroundService
20+
{
21+
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
22+
{
23+
using var jetStream = new AtProtoJetstream();
24+
25+
jetStream.RecordReceived += (sender, e) =>
26+
{
27+
string timeStamp = e.ParsedEvent.DateTimeOffset.ToLocalTime().ToString("G", CultureInfo.DefaultThreadCurrentUICulture);
28+
29+
switch (e.ParsedEvent)
30+
{
31+
case AtJetstreamCommitEvent commitEvent:
32+
Console.WriteLine($"{commitEvent.Did} executed a {commitEvent.Commit.Operation} in {commitEvent.Commit.Collection} at {timeStamp}");
33+
break;
34+
35+
default:
36+
break;
37+
}
38+
};
39+
40+
await jetStream.ConnectAsync(cancellationToken: stoppingToken);
41+
42+
while (!stoppingToken.IsCancellationRequested)
43+
{
44+
}
45+
46+
await jetStream.CloseAsync();
47+
}
48+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<Product>WatcherBot</Product>
9+
<RootNamespace>WatcherBot</RootNamespace>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="idunno.Bluesky" Version="0.9.1-prerelease" />
14+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.5" />
15+
</ItemGroup>
16+
17+
</Project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System.ComponentModel.DataAnnotations;
2+
3+
namespace WatcherBot
4+
{
5+
public sealed class BotOptions
6+
{
7+
public const string ConfigurationSectionName = "Bot";
8+
9+
[Required]
10+
public required string[] WatchWords { get; set; }
11+
}
12+
}

0 commit comments

Comments
 (0)