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

Add JsonFileSessionStore #18

Closed
wants to merge 2 commits into from
Closed

Add JsonFileSessionStore #18

wants to merge 2 commits into from

Conversation

Laiteux
Copy link

@Laiteux Laiteux commented Sep 14, 2020

JSON session file preview

@Laiteux
Copy link
Author

Laiteux commented Sep 14, 2020

Let me know what you think.

@Laiteux
Copy link
Author

Laiteux commented Sep 14, 2020

Note that you have to add Newtonsoft.Json as a reference to the startup project (the one you're using TgSharp with), however this error occurs:

Exception levée : 'System.IO.FileNotFoundException' dans Newtonsoft.Json.dll
Exception levée : 'System.IO.FileNotFoundException' dans System.Private.CoreLib.dll
Exception levée : 'System.IO.FileNotFoundException' dans System.Private.CoreLib.dll
Une exception de type 'System.IO.FileNotFoundException' s'est produite dans System.Private.CoreLib.dll mais n'a pas été gérée dans le code utilisateur
Could not load file or assembly 'System.Security.Permissions, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. Le fichier spécifié est introuvable.

This might be annoying. After a few Google searches, I tend to believe the issue is due to the fact we recently migrated to .NET Standard 2.0 or .NET Core: https://stackoverflow.com/a/48043024/12753809

@knocte
Copy link
Member

knocte commented Sep 14, 2020

we recently migrated to .NET Standard 2.0 or .NET Core

who is 'we'? I don't understand this sentence, can you clarify

@Laiteux
Copy link
Author

Laiteux commented Sep 14, 2020

we recently migrated to .NET Standard 2.0 or .NET Core

who is 'we'? I don't understand this sentence, can you clarify

"we" = TgSharp. Nevermind tho, I used the master branch and it wasn't migrated to .NET Standard 2.0 yet. Forget about that.

@knocte
Copy link
Member

knocte commented Sep 14, 2020

it wasn't migrated to .NET Standard 2.0 yet. Forget about that.

So you mean the System.IO.FileNotFoundException problem will only happen when we change TgSharp to .NETStandard?

@Laiteux
Copy link
Author

Laiteux commented Sep 14, 2020

So you mean the System.IO.FileNotFoundException problem will only happen when we change TgSharp to .NETStandard?

No, it currently is happening. I thought that was because we migrated to .NET Standard but I was wrong. Maybe migrating will fix the issue?

@knocte
Copy link
Member

knocte commented Sep 14, 2020

I see, ok.

BTW you haven't said why is this JsonFileSessionStore useful at all? Why did you include it? What benefit does it bring to you?

@Laiteux
Copy link
Author

Laiteux commented Sep 14, 2020

BTW you haven't said why is this JsonFileSessionStore useful at all? Why did you include it? What benefit does it bring to you?

I mean, it's always better to have a human-readable JSON file than an unreadable DAT one containing bytes.

You can see what the session file contains, what it is all about.

Also, it prevents the file from getting corrupted. Happened to me a few times in the past using FileSessionStore, had to delete the session file and reconnect/auth.

And it's just way better in general for storing data, in my opinion. For example, I currently store a lot of sessions in a SQL database, and doing it with bytes would have been a pain.

I believe a JSON session file is just better in general.

@knocte
Copy link
Member

knocte commented Sep 14, 2020

I agree, thanks for the explanation. I'll be reviewing this PR today.

@knocte
Copy link
Member

knocte commented Sep 14, 2020

2 questions:

  1. Why is the commit Set C# language version to 8.0 for all projects needed?
  2. Along with the changes of commit Use JsonFileSessionStore as default TelegramClient ISessionStore, can you provide a migration path so that, if a .dat file is found, it is converted to json first, and then used?

@Laiteux
Copy link
Author

Laiteux commented Sep 14, 2020

  1. I wanted to use the ??= operator, which was only available from C# 8.0. I then thought upgrading the LangVersion for all projects wouldn't be a bad thing.
  2. Can you please provide more explanations? What exactly do you want?

@knocte
Copy link
Member

knocte commented Sep 14, 2020

I wanted to use the ??= operator

Ah, do you actually mind to not use this operator please? I find it quite unreadable.

Can you please provide more explanations? What exactly do you want?

Imagine some software developer uses TgSharp current master branch, and in the future he upgrades to a version that has this feature merged. If we merge this PR as is, what I guess will happen is that his program won't try to open the .dat file, instead it will try to open a saved JSON store file, it will not find it, and create a new one. However, what should happen is that it should be backwards compatible: TgSharp should see the existence of the .dat file (and no existence of JSON store file), then it should convert the .dat file to JSON, and afterwards, load the session from that JSON file.

@Laiteux
Copy link
Author

Laiteux commented Sep 14, 2020

Ah, do you actually mind to not use this operator please? I find it quite unreadable.

Ok, sure. Should I then revert 306dc8f8b76c8490e5f92eaf6eb288760d073bf4 too?

Imagine some software developer uses TgSharp current master branch, and in the future he upgrades to a version that has this feature merged...

Got it, will write that tomorrow.

@knocte
Copy link
Member

knocte commented Sep 14, 2020

Should I then revert 306dc8f too?

Yup because it's not needed anymore. We will upgrade C# when we really need it.

@Laiteux
Copy link
Author

Laiteux commented Sep 14, 2020

Tested & working 👌

@knocte
Copy link
Member

knocte commented Sep 14, 2020

Cool thanks! BTW, are you good at rebasing, separating commits et al?

@Laiteux
Copy link
Author

Laiteux commented Sep 14, 2020

Never done that. Honestly this is one of my first "serious" pull requests, I'm still a bit of a beginner with git.

@knocte
Copy link
Member

knocte commented Sep 14, 2020

So if I ask you to separate your last commit in 2 commits (one that removes the ??= operator, and one that introduces the migration), will you know how to do it?

@Laiteux
Copy link
Author

Laiteux commented Sep 14, 2020

I have no idea if I did it the way you wanted, but here we go

@knocte
Copy link
Member

knocte commented Sep 14, 2020

There's a merge commit, so no :( you should have used interactive rebase

@Laiteux
Copy link
Author

Laiteux commented Sep 14, 2020

I have no idea of how to do that, sorry

@knocte
Copy link
Member

knocte commented Sep 14, 2020

If you give me access to your fork I'll remove the merge commit

@Laiteux
Copy link
Author

Laiteux commented Sep 14, 2020

@knocte
Copy link
Member

knocte commented Sep 14, 2020

@Laiteux see how I reordered commits, squashed some of them and adjusted commit messages (thanks to the magic of git rebase -i)

@Laiteux
Copy link
Author

Laiteux commented Sep 14, 2020

Looks cool!

@knocte
Copy link
Member

knocte commented Sep 14, 2020

Thanks. By the way I've just realised there may be a couple more improvements to make, I'm going to point them out now, but be careful, given that I pushed with --force into your master branch, I'd recommend to work on a new git clone next.

@knocte
Copy link
Member

knocte commented Sep 14, 2020

(Otherwise, you may get conflicts, or git merge commits, if you pull.)

@Laiteux
Copy link
Author

Laiteux commented Sep 17, 2020

However, that would mean it STILL would be included in the binary session file, and others. JsonIgnoreAttribute will only affect the JsonSerializer/JsonConvert. Let me know what you think

@knocte
Copy link
Member

knocte commented Sep 17, 2020

We don't want the binary session file to store different things than the JSON session file.

@Laiteux
Copy link
Author

Laiteux commented Sep 17, 2020

So what should we do? 😕

@Laiteux
Copy link
Author

Laiteux commented Sep 17, 2020

Consumer could still get TLUser using MakeAuthAsync and store it himself if really needed.

@knocte
Copy link
Member

knocte commented Sep 17, 2020

User could still get TLUser using MakeAuthAsync and store it himself.

So, you meant to answer "no" to Anders' below question?:

Isn't that the only way for knowing the current connected/authed user id?

@Laiteux
Copy link
Author

Laiteux commented Sep 17, 2020

Yes, this is what I meant.

@AndersMad
Copy link

Ye, I just added the id from TLUser to the Session persisting (1 line in session and 1 line in client on auth) - the other (TLUser) values are flux anyways - but the user id is nice to know.

@Laiteux
Copy link
Author

Laiteux commented Sep 17, 2020

Sorry, what do you mean by that? I don't understand

Ye, I just added the id from TLUser to the Session persisting (1 line in session and 1 line in client on auth)

@Laiteux
Copy link
Author

Laiteux commented Sep 17, 2020

Nevermind, got it. Would you mind contributing to this PR?

@AndersMad
Copy link

@Laiteux In my code to make it work.. I just added UserId prop to the session so it gets persisted and then set it where the TLUser was set in the Cient.

@AndersMad
Copy link

AndersMad commented Sep 17, 2020

@Laiteux Yes, but I'm so so bad at git/github - I always f'up something :P .. But was just this to TelegramClient.cs + added the prop to Session

private void OnUserAuthenticated(TLUser TLUser) {
	Session.UserId = TLUser.Id;

@Laiteux
Copy link
Author

Laiteux commented Sep 17, 2020

That's fine, I'll add it then rebase. Ok for you @knocte?

@knocte
Copy link
Member

knocte commented Sep 17, 2020

yes sounds fine; note you may have problems injecting that change in the 2nd commit because the 3rd commit changes stuff around that too (if this is the case, you can just merge the 2nd and 3rd commits into 1, copy+pasting the text from the commit message from the 3rd)

@aarani
Copy link

aarani commented Sep 17, 2020

No problem if you guys want to remove TLUser but you have to have a GetMe function to get the TLUser object ( if user == null (can be overruled with a force_update arg) )

@knocte
Copy link
Member

knocte commented Oct 12, 2020

( if user == null (can be overruled with a force_update arg) )

what do you mean with the above @aarani ?

@aarani
Copy link

aarani commented Oct 12, 2020

( if user == null (can be overruled with a force_update arg) )

what do you mean with the above @aarani ?

@knocte

class Session
{
   internal TLUser TLUser;
}

class TelegramClient
{
   async Task<TLUser> GetMe(bool refresh = false)
   {
      if (refresh || session.TLUser == null)
      {
         session.TLUser = await this.GetUser(self); // pseudo-code
      }
      return session.TLUser;
   }
}

@knocte
Copy link
Member

knocte commented Oct 12, 2020

I rebased this PR in this branch: https://github.com/nblockchain/TgSharp/commits/wip/jsonSessionStore , can you review @aarani ? (Note: CI might be broken in the last commit, I just have to replace the .dat file with a new .json file, I'll do that tomorrow.)

@aarani
Copy link

aarani commented Oct 13, 2020

I rebased this PR in this branch: https://github.com/nblockchain/TgSharp/commits/wip/jsonSessionStore , can you review @aarani ? (Note: CI might be broken in the last commit, I just have to replace the .dat file with a new .json file, I'll do that tomorrow.)

LGTM but it probably needs more testing

knocte added a commit that referenced this pull request Oct 13, 2020
This will help implementing session storage in JSON
format. WIP PRs:
- #18
- #29
@knocte
Copy link
Member

knocte commented Oct 13, 2020

Migrated to System.Text.Json and fixed binary serialization nits pointed by @aarani.
Hey @Laiteux by any chance can you test this before I merge?

It's always better to have a human-readable JSON file than
an unreadable DAT one containing bytes.

You can see what the session file contains, what it is all
about.

Also, it prevents the file from getting corrupted. Happened
to me a few times in the past using FileSessionStore, had
to delete the session file and reconnect/auth.

And it's just way better in general for storing data. For
example, I currently store a lot of sessions in a SQL
database, and doing it with bytes would have been a pain.

JSON session file is just better in general.

NOTE: Reason for migrating to .NET4.6.2 is to be able to
reference .NETStandard2.0 (needed for System.Text.Json).

Co-authored-by: Andres G. Aragoneses <[email protected]>
@omerjacobi
Copy link

what is still missing to finish this pull request?

@Laiteux
Copy link
Author

Laiteux commented Apr 1, 2021

Nothing I believe, iirc it works well but we wanted to do some more tests before merging, which we never ended up doing

@omerjacobi
Copy link

omerjacobi commented Apr 1, 2021

Nothing I believe, iirc it works well but we wanted to do some more tests before merging, which we never ended up doing

Is there anything I can help with? I really like to use this feature in the official NuGet

@Laiteux
Copy link
Author

Laiteux commented Apr 1, 2021

Please test the feature on your side and let us know if everything works fine. If so, @knocte will most likely merge it

@knocte
Copy link
Member

knocte commented Apr 1, 2021

Is there anything I can help with?

@omerjacobi you wanna contribute? tell me your username in telegram and I'll add you to a group where we can chat

@omerjacobi
Copy link

omerjacobi commented Apr 1, 2021

Is there anything I can help with?

@omerjacobi you wanna contribute? tell me your username in telegram and I'll add you to a group where we can chat

@knocte I send it to your gmail

@Laiteux Laiteux closed this by deleting the head repository Sep 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants