c3stream/Pages/Conference.cshtml.cs

58 lines
1.7 KiB
C#
Raw Normal View History

2020-01-03 15:10:06 +01:00
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
namespace c3stream.Pages {
public class ConferenceModel : PageModel {
2020-01-04 14:23:52 +01:00
public static List<UserStatus> UserData = new List<UserStatus>();
2020-01-03 15:10:06 +01:00
private readonly ILogger<ConferenceModel> _logger;
public ConferenceModel(ILogger<ConferenceModel> logger) => _logger = logger;
public void OnGet() {
2020-01-04 14:23:52 +01:00
var guid = Request.Query["guid"];
var state = Request.Query["state"];
var userid = Request.Cookies["bookmark"];
2020-01-04 00:42:36 +01:00
if (string.IsNullOrWhiteSpace(guid) || string.IsNullOrWhiteSpace(state) || !Request.Cookies.ContainsKey("bookmark"))
2020-01-03 15:10:06 +01:00
return;
lock (c3stream.Lock) {
2020-01-04 14:23:52 +01:00
ReadUserData();
var existing = UserData.FirstOrDefault(p => p.TalkId == guid && p.UserId == userid);
2020-01-03 15:10:06 +01:00
if (existing != null)
2020-01-04 14:23:52 +01:00
if (state == "unwatched")
UserData.Remove(existing);
else
existing.State = state;
2020-01-03 15:10:06 +01:00
else
2020-01-04 14:23:52 +01:00
UserData.Add(new UserStatus(userid, guid, state));
WriteUserData();
2020-01-03 15:10:06 +01:00
Response.Redirect("/");
}
}
2020-01-04 14:23:52 +01:00
public static void ReadUserData() {
2020-01-03 15:10:06 +01:00
lock (c3stream.Lock)
2020-01-04 14:23:52 +01:00
UserData = JsonConvert.DeserializeObject<List<UserStatus>>(System.IO.File.ReadAllText(c3stream.DbPath));
2020-01-03 15:10:06 +01:00
}
2020-01-04 14:23:52 +01:00
public static void WriteUserData() {
lock (c3stream.Lock)
2020-01-04 14:23:52 +01:00
System.IO.File.WriteAllText(c3stream.DbPath, JsonConvert.SerializeObject(UserData));
2020-01-03 15:10:06 +01:00
}
2020-01-04 14:23:52 +01:00
public class UserStatus {
public readonly string TalkId;
public readonly string UserId;
public string State;
2020-01-03 15:10:06 +01:00
2020-01-04 14:23:52 +01:00
public UserStatus(string userId, string talkId, string state = "unwatched") {
UserId = userId;
State = state;
TalkId = talkId;
2020-01-03 15:10:06 +01:00
}
}
}
}