Implemented new rating system

This commit is contained in:
Marcel Baumgartner 2023-05-04 12:42:50 +02:00
parent a33b81dd98
commit 4d9ab9763e
10 changed files with 1324 additions and 1 deletions

View file

@ -29,6 +29,8 @@ public class User
public UserStatus Status { get; set; } = UserStatus.Unverified;
public bool Admin { get; set; } = false;
public bool SupportPending { get; set; } = false;
public bool HasRated { get; set; } = false;
public int Rating { get; set; } = 0;
// Security
public bool TotpEnabled { get; set; } = false;

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,40 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Moonlight.App.Database.Migrations
{
/// <inheritdoc />
public partial class AddedRatingDataToUser : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<bool>(
name: "HasRated",
table: "Users",
type: "tinyint(1)",
nullable: false,
defaultValue: false);
migrationBuilder.AddColumn<int>(
name: "Rating",
table: "Users",
type: "int",
nullable: false,
defaultValue: 0);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "HasRated",
table: "Users");
migrationBuilder.DropColumn(
name: "Rating",
table: "Users");
}
}
}

View file

@ -733,6 +733,9 @@ namespace Moonlight.App.Database.Migrations
.IsRequired()
.HasColumnType("longtext");
b.Property<bool>("HasRated")
.HasColumnType("tinyint(1)");
b.Property<string>("LastName")
.IsRequired()
.HasColumnType("longtext");
@ -741,6 +744,9 @@ namespace Moonlight.App.Database.Migrations
.IsRequired()
.HasColumnType("longtext");
b.Property<int>("Rating")
.HasColumnType("int");
b.Property<string>("State")
.IsRequired()
.HasColumnType("longtext");

View file

@ -33,6 +33,7 @@ public class DiscordNotificationService
Event.On<User>("supportChat.new", this, OnNewSupportChat);
Event.On<SupportChatMessage>("supportChat.message", this, OnSupportChatMessage);
Event.On<User>("supportChat.close", this, OnSupportChatClose);
Event.On<User>("user.rating", this, OnUserRated);
}
else
{
@ -40,6 +41,20 @@ public class DiscordNotificationService
}
}
private async Task OnUserRated(User user)
{
await SendNotification("", builder =>
{
builder.Color = Color.Gold;
builder.Title = "New user rating";
builder.AddField("User", user.Email);
builder.AddField("Firstname", user.FirstName);
builder.AddField("Lastname", user.LastName);
builder.AddField("Rating", new string('⭐', user.Rating));
});
}
private async Task OnSupportChatClose(User user)
{
await SendNotification("", builder =>

View file

@ -0,0 +1,86 @@
using Moonlight.App.Database.Entities;
using Moonlight.App.Events;
using Moonlight.App.Repositories;
using Moonlight.App.Services.Sessions;
namespace Moonlight.App.Services;
public class RatingService
{
private readonly IdentityService IdentityService;
private readonly EventSystem Event;
private readonly Repository<User> UserRepository;
private readonly bool Enabled = false;
private readonly string Url = "";
private readonly int MinRating = 4;
private readonly int DaysSince = 5;
public RatingService(
IdentityService identityService,
ConfigService configService,
EventSystem eventSystem,
Repository<User> userRepository)
{
IdentityService = identityService;
Event = eventSystem;
UserRepository = userRepository;
var config = configService.GetSection("Moonlight").GetSection("Rating");
Enabled = config.GetValue<bool>("Enabled");
Url = config.GetValue<string>("Url");
MinRating = config.GetValue<int>("MinRating");
DaysSince = config.GetValue<int>("DaysSince");
}
public async Task<bool> ShouldRate()
{
if (!Enabled)
return false;
var user = await IdentityService.Get();
if (user == null)
return false;
if (user.HasRated)
return false;
if ((DateTime.UtcNow - user.CreatedAt).TotalDays >= DaysSince)
{
return true;
}
return false;
}
public Task<string> GetRateUrl()
{
return Task.FromResult(Url);
}
public async Task<bool> Rate(int rate)
{
var user = await IdentityService.Get();
// Double check states:
if(user == null)
return false;
if(user.HasRated)
return false;
user.HasRated = true;
user.Rating = rate;
UserRepository.Update(user);
await Event.Emit("user.rating", user);
if (rate >= MinRating)
return true;
return false;
}
}

View file

@ -109,6 +109,7 @@ namespace Moonlight
builder.Services.AddScoped<ForgeService>();
builder.Services.AddScoped<FabricService>();
builder.Services.AddSingleton<BucketService>();
builder.Services.AddScoped<RatingService>();
builder.Services.AddScoped<GoogleOAuth2Service>();
builder.Services.AddScoped<DiscordOAuth2Service>();

View file

@ -0,0 +1,132 @@
@using Moonlight.App.Services
@using Moonlight.App.Services.Interop
@inject RatingService RatingService
@inject ModalService ModalService
@inject SmartTranslateService SmartTranslateService
@inject ToastService ToastService
<div class="modal fade" tabindex="-1" style="display: none;" id="rating">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header">
<h3 class="modal-title">
<TL>Hey, can i borrow you for a second?</TL>
</h3>
</div>
<div class="modal-body">
<p class="fs-4">
<TL>We want to improve our services and get a little bit of feedback how we are currently doing. Please leave us a rating</TL>
</p>
</div>
<div class="modal-body">
<div class="d-flex justify-content-center rating">
<input class="rating-input" name="rating" value="0" checked="" type="radio">
<label class="rating-label" for="rating1">
<i class="bx bx-lg bx-star"></i>
</label>
<input class="rating-input" name="rating" value="1" type="radio" id="rating1" @onchange="SetRating">
<label class="rating-label" for="rating2">
<i class="bx bx-lg bx-star"></i>
</label>
<input class="rating-input" name="rating" value="2" type="radio" id="rating2" @onchange="SetRating">
<label class="rating-label" for="rating3">
<i class="bx bx-lg bx-star"></i>
</label>
<input class="rating-input" name="rating" value="3" type="radio" id="rating3" @onchange="SetRating">
<label class="rating-label" for="rating4">
<i class="bx bx-lg bx-star"></i>
</label>
<input class="rating-input" name="rating" value="4" type="radio" id="rating4" @onchange="SetRating">
<label class="rating-label" for="rating5">
<i class="bx bx-lg bx-star"></i>
</label>
<input class="rating-input" name="rating" value="5" type="radio" id="rating5" @onchange="SetRating">
</div>
</div>
<div class="modal-footer">
<WButton Text="@(SmartTranslateService.Translate("Rate"))"
WorkingText="@(SmartTranslateService.Translate("Saving"))"
CssClasses="btn-primary"
OnClick="Save">
</WButton>
</div>
</div>
</div>
</div>
<div class="modal fade" tabindex="-1" style="display: none" id="ratingUrl">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header">
<h3 class="modal-title">
<TL>Thanks for your rating</TL>
</h3>
</div>
<div class="modal-body">
<p class="fs-4">
<TL>It would be really kind of you rating us on a external platform as it will help our project very much</TL>
</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal"><TL>Close</TL></button>
<a href="@(Url)" class="btn btn-primary" target="_blank"><TL>Yes</TL></a>
</div>
</div>
</div>
</div>
@code
{
private int Rate = 0;
private string Url = "";
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
Url = await RatingService.GetRateUrl();
if (await RatingService.ShouldRate())
{
await Task.Run(async () =>
{
await Task.Delay(1000);
await ModalService.Show("rating");
});
}
}
}
private Task SetRating(ChangeEventArgs args)
{
if (args.Value == null)
return Task.CompletedTask;
if (int.TryParse(args.Value.ToString(), out int rate))
{
Rate = rate;
}
return Task.CompletedTask;
}
private async Task Save()
{
await ModalService.Hide("rating");
if (await RatingService.Rate(Rate))
{
await ModalService.Show("ratingUrl");
}
else
{
await ToastService.Success(
SmartTranslateService.Translate("Rating saved")
);
}
}
}

View file

@ -90,6 +90,8 @@
else
{
@Body
<RatingPopup />
}
}
}
@ -115,7 +117,6 @@
<div class="modal-dialog modal-dialog-centered mw-900px">
<div class="modal-content">
<div class="pt-2 modal-body py-lg-10 px-lg-10">
<h2>@(SmartTranslateService.Translate("Authenticating"))...</h2>
<p class="mt-3 fw-normal fs-6">@(SmartTranslateService.Translate("Verifying token, loading user data"))</p>
</div>

View file

@ -83,6 +83,12 @@
"Statistics": {
"Enabled": true,
"Wait": 15
},
"Rating": {
"Enabled": true,
"Url": "link-to-google.page",
"MinRating": 4,
"DaysSince": 5
}
}
}