Skip to content

Commit

Permalink
Selectable misskey instance (#48)
Browse files Browse the repository at this point in the history
* update iroiro

* support multi-version for misskey instance

* 🚀

* add Misq project (fork)
  • Loading branch information
marihachi authored Jan 17, 2023
1 parent c2be5c5 commit a330a68
Show file tree
Hide file tree
Showing 25 changed files with 817 additions and 102 deletions.
10 changes: 8 additions & 2 deletions LegatoNowPlaying.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26730.10
# Visual Studio Version 16
VisualStudioVersion = 16.0.31624.102
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LegatoNowPlaying", "LegatoNowPlaying\LegatoNowPlaying.csproj", "{98ED99F2-1323-4153-B1D1-44874270FD31}"
EndProject
Expand All @@ -10,6 +10,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
.editorconfig = .editorconfig
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Misq", "Misq\Misq.csproj", "{79333B32-8A49-4937-9732-DBAC9BE770FF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -20,6 +22,10 @@ Global
{98ED99F2-1323-4153-B1D1-44874270FD31}.Debug|Any CPU.Build.0 = Debug|Any CPU
{98ED99F2-1323-4153-B1D1-44874270FD31}.Release|Any CPU.ActiveCfg = Release|Any CPU
{98ED99F2-1323-4153-B1D1-44874270FD31}.Release|Any CPU.Build.0 = Release|Any CPU
{79333B32-8A49-4937-9732-DBAC9BE770FF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{79333B32-8A49-4937-9732-DBAC9BE770FF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{79333B32-8A49-4937-9732-DBAC9BE770FF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{79333B32-8A49-4937-9732-DBAC9BE770FF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
27 changes: 22 additions & 5 deletions LegatoNowPlaying/Accounts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Drawing;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LegatoNowPlaying
{
Expand All @@ -11,18 +12,34 @@ public class Accounts

public async void Init()
{
Services.Add(new Services.Misskey.Service());
Services.Add(new Services.Twitter.Service());
this.Services.Add(new Services.Misskey.Service());
this.Services.Add(new Services.Twitter.Service());

foreach (var service in Services)
for (var i = 0; i < this.Services.Count; i++)
{
await service.Setup();
try
{
await this.Services[i].Setup();
}
catch
{
MessageBox.Show(
$"サービス「{this.Services[i].Name}」の設定ファイル読込み時にエラーが発生しました。\r\n" +
$"サービス「{this.Services[i].Name}」は無効化されます。\r\n" +
"設定ファイルが古い形式で保存されている等が考えられるため、設定ファイルを削除して再設定すると問題が解決する可能性があります。",
"エラー",
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);

this.Services.RemoveAt(i);
i--;
}
}
}

public async Task Post(string text, Image albumArt)
{
foreach (var service in Services)
foreach (var service in this.Services)
{
if (service.Enabled && service.IsInstalled)
{
Expand Down
8 changes: 8 additions & 0 deletions LegatoNowPlaying/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,12 @@
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7" />
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-11.0.0.0" newVersion="11.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
39 changes: 39 additions & 0 deletions LegatoNowPlaying/ErrorLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;

namespace LegatoNowPlaying
{
public static class ErrorLogger
{
public static async Task LogException(Exception ex, bool isInnerException = false)
{
using (var log = new StreamWriter("errors.log", true, Encoding.UTF8))
{
if (!isInnerException)
{
await log.WriteLineAsync("------------------------------");
await log.WriteLineAsync("");
await log.WriteLineAsync("# Exception");
await log.WriteLineAsync($"{DateTime.Now.ToShortDateString()} {DateTime.Now.ToShortTimeString()}");
}
else
{
await log.WriteLineAsync("# Inner Exception");
}

await log.WriteLineAsync("");
await log.WriteLineAsync($"[Message] {ex.Message}");
await log.WriteLineAsync("[StackTrace]");
await log.WriteLineAsync($"{ex.StackTrace}");
await log.WriteLineAsync("");
}

if (ex.InnerException != null)
{
await LogException(ex.InnerException, true);
}
}
}
}
10 changes: 7 additions & 3 deletions LegatoNowPlaying/LegatoNowPlaying.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@
<Reference Include="Legato, Version=1.1.2.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Legato.1.1.2\lib\net461\Legato.dll</HintPath>
</Reference>
<Reference Include="Misq, Version=6.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Misq.6.0.0\lib\net461\Misq.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
Expand All @@ -67,6 +64,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Common.cs" />
<Compile Include="ErrorLogger.cs" />
<Compile Include="JsonFile.cs" />
<Compile Include="MainForm.cs">
<SubType>Form</SubType>
Expand Down Expand Up @@ -155,6 +153,12 @@
<ItemGroup>
<None Include="Resources\legato.ico" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Misq\Misq.csproj">
<Project>{79333b32-8a49-4937-9732-dbac9be770ff}</Project>
<Name>Misq</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>
Expand Down
2 changes: 2 additions & 0 deletions LegatoNowPlaying/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ private async Task _PostAsync()

var albumArt = this._GetAlbumArt();



await _Accounts.Post(text, this.checkBoxNeedAlbumArt.Checked ? albumArt : null);
}

Expand Down
5 changes: 3 additions & 2 deletions LegatoNowPlaying/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Windows.Forms;

namespace LegatoNowPlaying
Expand All @@ -19,7 +19,8 @@ static void Main()
}
catch (Exception ex)
{
MessageBox.Show($"内容:\r\n{ex.Message}\r\n\r\nスタックトレース:\r\n{ex.StackTrace}", "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error);
ErrorLogger.LogException(ex).Wait();
MessageBox.Show($"エラーが発生しました。\r\n詳細についてはerrors.logを確認してください。", "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
Expand Down
38 changes: 37 additions & 1 deletion LegatoNowPlaying/Services/Misskey/AuthForm.Designer.cs

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

63 changes: 58 additions & 5 deletions LegatoNowPlaying/Services/Misskey/AuthForm.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace LegatoNowPlaying.Services.Misskey
{
public partial class AuthForm : Form
{
public delegate void OnComplete(Misq.Me me);
public delegate void OnComplete(Misq.Me me, Misq.App app);

private OnComplete onComplete;

Expand All @@ -15,15 +17,66 @@ public AuthForm(OnComplete onComplete)
InitializeComponent();
}

private async void AuthForm_Load(object sender, EventArgs e)
private async void button1_Click(object sender, EventArgs e)
{
var app = new Misq.App("https://misskey.xyz", Misskey.Service.appKey);
try
{
new Uri(textBox1.Text);
}
catch(UriFormatException ex)
{
MessageBox.Show(
$"url of target instance is invalid",
"Error",
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
return;
}

panel1.Visible = false;

JToken metaRes = await Misq.Core.Request(textBox1.Text, "meta", new Dictionary<string, object> { });
var versionSource = metaRes.Value<string>("version");
var versionMajorStr = versionSource.Split('.')[0];

if (!int.TryParse(versionMajorStr, out int version))
{
MessageBox.Show(
$"invalid format of the Misskey version",
"Error",
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
return;
}

var permissionsList = new List<string>();

if (version == 10)
{
permissionsList.AddRange(new[] { "note-write", "drive-write" });
}
else if (version >= 11)
{
permissionsList.AddRange(new[] { "write:notes", "write:drive" });
}
else
{
MessageBox.Show(
$"Unsupported the Misskey instance version",
"Error",
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
return;
}

var app = await Misq.App.Register(
textBox1.Text, "Legato Nowplaying", "A NowPlaying App for AIMP4", permissionsList);
var me = await app.Authorize();
this.Close();
this.onComplete(me);
this.onComplete(me, app);

MessageBox.Show(
$"WELCOME {me.Username}",
$"WELCOME @{me.Username}",
"Done",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
Expand Down
Loading

0 comments on commit a330a68

Please sign in to comment.