Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Message Archive #182

Merged
merged 13 commits into from
Jul 4, 2023
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: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

bin/
obj/
out/
node_modules/

emulsion.json

Expand Down
1 change: 1 addition & 0 deletions .idea/.idea.Emulsion/.idea/dictionaries/fried.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ COPY ./Emulsion/Emulsion.fsproj ./Emulsion/
COPY ./Emulsion.ContentProxy/Emulsion.ContentProxy.fsproj ./Emulsion.ContentProxy/
COPY ./Emulsion.Database/Emulsion.Database.fsproj ./Emulsion.Database/
COPY ./Emulsion.Messaging/Emulsion.Messaging.fsproj ./Emulsion.Messaging/
COPY ./Emulsion.MessageArchive.Frontend/Emulsion.MessageArchive.Frontend.proj ./Emulsion.MessageArchive.Frontend/
COPY ./Emulsion.Settings/Emulsion.Settings.fsproj ./Emulsion.Settings/
COPY ./Emulsion.Telegram/Emulsion.Telegram.fsproj ./Emulsion.Telegram/
COPY ./Emulsion.Web/Emulsion.Web.fsproj ./Emulsion.Web/
Expand Down
3 changes: 3 additions & 0 deletions Emulsion.Database/EmulsionDbContext.fs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ type EmulsionDbContext(options: DbContextOptions) =
[<DefaultValue>] val mutable private telegramContents: DbSet<TelegramContent>
member this.TelegramContents with get() = this.telegramContents and set v = this.telegramContents <- v

[<DefaultValue>] val mutable private archiveEntries: DbSet<ArchiveEntry>
member this.ArchiveEntries with get() = this.archiveEntries and set v = this.archiveEntries <- v

/// This type is used by the EFCore infrastructure when creating a new migration.
type EmulsionDbContextDesignFactory() =
interface IDesignTimeDbContextFactory<EmulsionDbContext> with
Expand Down
10 changes: 10 additions & 0 deletions Emulsion.Database/Entities.fs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace Emulsion.Database.Entities

open System
open System.ComponentModel.DataAnnotations

[<CLIMutable>]
Expand All @@ -12,3 +13,12 @@ type TelegramContent = {
FileName: string
MimeType: string
}

[<CLIMutable>]
type ArchiveEntry = {
[<Key>] Id: int64
MessageSystemId: string
DateTime: DateTimeOffset
Sender: string
Text: string
}
121 changes: 121 additions & 0 deletions Emulsion.Database/Migrations/20230625203424_ArchiveEntry.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// <auto-generated />
namespace Emulsion.Database.Migrations

open System
open Emulsion.Database
open Microsoft.EntityFrameworkCore
open Microsoft.EntityFrameworkCore.Infrastructure
open Microsoft.EntityFrameworkCore.Metadata
open Microsoft.EntityFrameworkCore.Migrations
open Microsoft.EntityFrameworkCore.Storage.ValueConversion

[<DbContext(typeof<EmulsionDbContext>)>]
[<Migration("20230625203424_ArchiveEntry")>]
type ArchiveEntry() =
inherit Migration()

override this.Up(migrationBuilder:MigrationBuilder) =
migrationBuilder.CreateTable(
name = "ArchiveEntries"
,columns = (fun table ->
{|
Id =
table.Column<Int64>(
nullable = false
,``type`` = "INTEGER"
).Annotation("Sqlite:Autoincrement", true)
MessageSystemId =
table.Column<string>(
nullable = true
,``type`` = "TEXT"
)
DateTime =
table.Column<DateTimeOffset>(
nullable = false
,``type`` = "TEXT"
)
Sender =
table.Column<string>(
nullable = true
,``type`` = "TEXT"
)
Text =
table.Column<string>(
nullable = true
,``type`` = "TEXT"
)
|})
,constraints =
(fun table ->
table.PrimaryKey("PK_ArchiveEntries", (fun x -> (x.Id) :> obj)) |> ignore
)
) |> ignore


override this.Down(migrationBuilder:MigrationBuilder) =
migrationBuilder.DropTable(
name = "ArchiveEntries"
) |> ignore


override this.BuildTargetModel(modelBuilder: ModelBuilder) =
modelBuilder
.HasAnnotation("ProductVersion", "5.0.10")
|> ignore

modelBuilder.Entity("Emulsion.Database.Entities.ArchiveEntry", (fun b ->

b.Property<Int64>("Id")
.IsRequired(true)
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER") |> ignore
b.Property<DateTimeOffset>("DateTime")
.IsRequired(true)
.HasColumnType("TEXT") |> ignore
b.Property<string>("MessageSystemId")
.IsRequired(false)
.HasColumnType("TEXT") |> ignore
b.Property<string>("Sender")
.IsRequired(false)
.HasColumnType("TEXT") |> ignore
b.Property<string>("Text")
.IsRequired(false)
.HasColumnType("TEXT") |> ignore

b.HasKey("Id") |> ignore

b.ToTable("ArchiveEntries") |> ignore

)) |> ignore

modelBuilder.Entity("Emulsion.Database.Entities.TelegramContent", (fun b ->

b.Property<Int64>("Id")
.IsRequired(true)
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER") |> ignore
b.Property<Int64>("ChatId")
.IsRequired(true)
.HasColumnType("INTEGER") |> ignore
b.Property<string>("ChatUserName")
.IsRequired(false)
.HasColumnType("TEXT") |> ignore
b.Property<string>("FileId")
.IsRequired(false)
.HasColumnType("TEXT") |> ignore
b.Property<string>("FileName")
.IsRequired(false)
.HasColumnType("TEXT") |> ignore
b.Property<Int64>("MessageId")
.IsRequired(true)
.HasColumnType("INTEGER") |> ignore
b.Property<string>("MimeType")
.IsRequired(false)
.HasColumnType("TEXT") |> ignore

b.HasKey("Id") |> ignore

b.ToTable("TelegramContents") |> ignore

)) |> ignore

28 changes: 28 additions & 0 deletions Emulsion.Database/Migrations/EmulsionDbContextModelSnapshot.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ open System
open Emulsion.Database
open Microsoft.EntityFrameworkCore
open Microsoft.EntityFrameworkCore.Infrastructure
open Microsoft.EntityFrameworkCore.Metadata
open Microsoft.EntityFrameworkCore.Migrations
open Microsoft.EntityFrameworkCore.Storage.ValueConversion

[<DbContext(typeof<EmulsionDbContext>)>]
type EmulsionDbContextModelSnapshot() =
Expand All @@ -15,6 +18,31 @@ type EmulsionDbContextModelSnapshot() =
.HasAnnotation("ProductVersion", "5.0.10")
|> ignore

modelBuilder.Entity("Emulsion.Database.Entities.ArchiveEntry", (fun b ->

b.Property<Int64>("Id")
.IsRequired(true)
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER") |> ignore
b.Property<DateTimeOffset>("DateTime")
.IsRequired(true)
.HasColumnType("TEXT") |> ignore
b.Property<string>("MessageSystemId")
.IsRequired(false)
.HasColumnType("TEXT") |> ignore
b.Property<string>("Sender")
.IsRequired(false)
.HasColumnType("TEXT") |> ignore
b.Property<string>("Text")
.IsRequired(false)
.HasColumnType("TEXT") |> ignore

b.HasKey("Id") |> ignore

b.ToTable("ArchiveEntries") |> ignore

)) |> ignore

modelBuilder.Entity("Emulsion.Database.Entities.TelegramContent", (fun b ->

b.Property<Int64>("Id")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.Build.NoTargets/3.7.0">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Content Include="*.html" />
<Content Include="*.tsx" />
<Content Include="*.d.ts" />
<Content Include="*.less" />
<None Include="*.json" />
</ItemGroup>

<Target Name="NpmInstall"
BeforeTargets="NpmBuild"
Inputs="package.json;package-lock.json"
Outputs="$(IntermediateOutputPath)\npm-build.timestamp">
<Exec Command="npm install" />
<Touch Files="$(IntermediateOutputPath)\npm-build.timestamp" AlwaysCreate="true" />
</Target>

<Target Name="NpmBuild" BeforeTargets="Build"
Inputs="@(Content)"
Outputs="bin\index.html">
<Exec Command="npm run build" />
</Target>
</Project>
10 changes: 10 additions & 0 deletions Emulsion.MessageArchive.Frontend/api.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
type Statistics = {
messageCount: number;
}

type Message = {
messageSystemId: string;
sender: string;
dateTime: string;
text: string;
}
100 changes: 100 additions & 0 deletions Emulsion.MessageArchive.Frontend/app.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import React, {useState} from 'react';
import {render} from 'react-dom';

class LoadedPage {
constructor(
public readonly statistics: Statistics,
public readonly pageIndex: number,
public readonly messages: Message[]
) {}
}

class ErrorState {
constructor(public readonly error: string) {}
}

type State = 'Loading' | LoadedPage | ErrorState;

const getStatistics = async (): Promise<Statistics> => {
let url = window.location.href
url = url.substring(0, url.lastIndexOf('/'));

let response = await fetch(`${url}/api/history/statistics`);
return await response.json();
};

const getMessages = async (offset: number, limit: number): Promise<Message[]> => {
let url = window.location.href
url = url.substring(0, url.lastIndexOf('/'));

let response = await fetch(`${url}/api/history/messages?offset=${offset}&limit=${limit}`);
return await response.json();
}

const DefaultLimit = 10;

const getPage = async (pageIndex: number): Promise<LoadedPage> => {
const offset = pageIndex * DefaultLimit;
const statistics = await getStatistics();
const messages = await getMessages(offset, DefaultLimit);
return new LoadedPage(statistics, pageIndex, messages);
}

const loadPage = (index: number, setState: (state: State) => void) => {
getPage(index)
.then(page => setState(page))
.catch(error => setState(new ErrorState(error.message)));
}

const PageControls = ({page, setState}: {page: LoadedPage, setState: (state: State) => void}) => {
const lastPageNumber= Math.ceil(page.statistics.messageCount / DefaultLimit);
const lastPageIndex= lastPageNumber - 1;
return <>
Count: {page.statistics.messageCount}<br/>
Page: {page.pageIndex + 1} of {Math.ceil(page.statistics.messageCount / DefaultLimit)}<br/>
<button onClick={() => loadPage(0, setState)}>⇐</button>
<button onClick={() => loadPage(page.pageIndex - 1, setState)}>←</button>
<button onClick={() => loadPage(page.pageIndex + 1, setState)}>→</button>
<button onClick={() => loadPage(lastPageIndex, setState)}>⇒</button>
</>;
}

const dateTimeToText = (dateTime: string) => {
const fullText = new Date(dateTime).toISOString();
return fullText.substring(0, fullText.lastIndexOf('.')) + 'Z';
}

const renderMessageText = (text: string) => {
const lines= text.split('\n');
return lines.map((line) => <p>{line}</p>);
}

const renderMessage = (message: Message) => <div className="message">
<div className="datetime">{dateTimeToText(message.dateTime)}</div>
<div className="sender">{message.sender}</div>
<div className="text">{renderMessageText(message.text)}</div>
</div>

const MessageList = ({list}: {list: Message[]}) => <div className="message-list">
{list.map(renderMessage)}
</div>;

const App = () => {
const [state, setState] = useState<State>('Loading');
if (state === 'Loading') {
loadPage(0, setState)
}

if (state === 'Loading') {
return <div className="loading">Loading…</div>
} else if (state instanceof ErrorState) {
return <div className="error">Error: {state.error}</div>
} else {
return <div className="page">
<PageControls page={state} setState={setState}/>
<MessageList list={state.messages}/>
</div>
}
};

render(<App/>, document.getElementById('app'));
10 changes: 10 additions & 0 deletions Emulsion.MessageArchive.Frontend/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<html lang="en">
<head>
<title>Emulsion Message Archive</title>
<script type="module" src="./app.tsx"></script>
<link rel="stylesheet" href="./style.less" />
</head>

<div id="app"></div>

</html>
Loading
Loading