Skip to content

Commit

Permalink
(#147) ContentProxy: more APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
ForNeVeR committed Oct 11, 2021
1 parent 827ef53 commit d0253eb
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 15 deletions.
24 changes: 22 additions & 2 deletions Emulsion.ContentProxy/ContentStorage.fs
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
module Emulsion.ContentProxy.ContentStorage

open Emulsion.Database
open Emulsion.Database.DataStorage
open Emulsion.Database.Models
open Emulsion.Database.QueryableEx

type MessageIdentity = {
ChatUserName: string
MessageId: int64
FileId: string
}

let getOrCreateMessageRecord (context: EmulsionDbContext) (id: MessageIdentity): Async<TelegramContent> =
failwithf "TODO"
let getOrCreateMessageRecord (context: EmulsionDbContext) (id: MessageIdentity): Async<TelegramContent> = async {
let! existingItem =
query {
for content in context.TelegramContents do
where (content.MessageId = id.MessageId && content.FileId = id.FileId)
tryExactlyOneAsync
}
match existingItem with
| None ->
let newItem = {
Id = 0L
ChatUsername = id.ChatUserName
MessageId = id.MessageId
FileId = id.FileId
}
do! addAsync context.TelegramContents newItem
return newItem
| Some item -> return item
}
4 changes: 0 additions & 4 deletions Emulsion.ContentProxy/Emulsion.ContentProxy.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Nanoid" Version="2.1.0" />
</ItemGroup>

<ItemGroup>
<Compile Include="ContentStorage.fs" />
</ItemGroup>
Expand Down
6 changes: 6 additions & 0 deletions Emulsion.Database/DataStorage.fs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@ let transaction<'a> (settings: DatabaseSettings) (action: EmulsionDbContext -> A
do! Async.AwaitTask(tran.CommitAsync())
return result
}

let addAsync<'a when 'a : not struct> (dbSet: DbSet<'a>) (entity: 'a): Async<unit> = async {
let! ct = Async.CancellationToken
let! _ = Async.AwaitTask(dbSet.AddAsync(entity, ct).AsTask())
return ()
}
1 change: 1 addition & 0 deletions Emulsion.Database/Emulsion.Database.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<Compile Include="Initializer.fs" />
<Compile Include="DatabaseSettings.fs" />
<Compile Include="DataStorage.fs" />
<Compile Include="QueryableEx.fs" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion Emulsion.Database/Models.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ open System.ComponentModel.DataAnnotations

[<CLIMutable>]
type TelegramContent = {
[<Key>] Id: string
[<Key>] Id: int64
ChatUsername: string
MessageId: int64
FileId: string
Expand Down
19 changes: 19 additions & 0 deletions Emulsion.Database/QueryableEx.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Emulsion.Database.QueryableEx

open System.Linq

open Microsoft.EntityFrameworkCore
open Microsoft.FSharp.Linq

type QueryBuilder with
[<CustomOperation("tryExactlyOneAsync")>]
member _.TryExactlyOneAsync<'T, 'Q>(source: QuerySource<'T, 'Q>): Async<'T option> = async {
let source = source.Source :?> IQueryable<'T>
let! ct = Async.CancellationToken
let! item = Async.AwaitTask(EntityFrameworkQueryableExtensions.SingleOrDefaultAsync(source, ct))

// We cannot use Option.ofObj here since not every entity type can be marked with AllowNullLiteral.
match box item with
| null -> return None
| _ -> return Some item
}
11 changes: 11 additions & 0 deletions Emulsion.Tests/ContentProxy/ContentStorageTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Emulsion.Tests.ContentProxy.ContentStorageTests

open Xunit

[<Fact>]
let ``getOrCreateMessageRecord returns a new record``() =
Assert.True false

[<Fact>]
let ``getOrCreateMessageRecord returns an existing record``() =
Assert.True false
1 change: 1 addition & 0 deletions Emulsion.Tests/Emulsion.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<Compile Include="Xmpp\SharpXmppPingHandlerTests.fs" />
<Compile Include="Xmpp\EmulsionXmppTests.fs" />
<Compile Include="Database\InitializerTests.fs" />
<Compile Include="ContentProxy\ContentStorageTests.fs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Akka.TestKit" Version="1.3.12" />
Expand Down
19 changes: 11 additions & 8 deletions Emulsion/Telegram/LinkGenerator.fs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,17 @@ let private getFileId(message: FunogramMessage) =

let private getMessageIdentity message: ContentStorage.MessageIdentity option =
let fileId = getFileId message
match fileId with
| None -> None
| Some fileId -> Some {
MessageId = message.MessageId
FileId = fileId
}
match fileId, message.Chat with
| Some fileId, { Type = SuperGroup
Username = Some chatName } ->
Some {
ChatUserName = chatName
MessageId = message.MessageId
FileId = fileId
}
| _, _ -> None

// TODO: right type for the databaseSettings
// TODO: right type for the hostingSettings
let gatherLinks (databaseSettings: DatabaseSettings option) (message: FunogramMessage): Async<TelegramThreadLinks> = async {
let getMessageBodyLink message =
match databaseSettings with
Expand All @@ -55,7 +58,7 @@ let gatherLinks (databaseSettings: DatabaseSettings option) (message: FunogramMe
let! content = DataStorage.transaction settings (fun ctx ->
ContentStorage.getOrCreateMessageRecord ctx id
)
return Some content.Id
return Some(failwithf "TODO: Generate URL from hosting settings")
}

let! contentLink = getMessageBodyLink message
Expand Down

0 comments on commit d0253eb

Please sign in to comment.