Add watchlist, enable migrations

This commit is contained in:
Laura Hausmann 2022-02-09 20:45:14 +01:00
parent f8aeed318f
commit db4a42171d
Signed by: zotan
GPG key ID: D044E84C5BE01605
4 changed files with 270 additions and 114 deletions

View file

@ -1,8 +1,10 @@
@page
@using global::c3stream.DataModels
@model IndexModel
@{
ViewData["Title"] = "Home";
var cookie = c3stream.UpdateCookie(Request, Response, "/");
var marked = new Database.DbConn().States.Any(p => p.UserId == cookie && p.State == "marked");
}
<div style="text-align: center">
@ -10,6 +12,9 @@
Your bookmark link:<br/>
<code onclick="copyToClipboard(this)">https://@Request.Host.Value?bookmark=@cookie</code><br/><br/>
<div class="btn-group">
@if (marked) {
<a role="button" class="btn btn-primary" href="/Watchlist">watchlist</a>
}
@foreach (var conf in c3stream.Conferences) {
<a role="button" class="btn btn-primary" href="/Conference?c=@conf.Acronym">@conf.Acronym</a>
}

110
Pages/Watchlist.cshtml Normal file
View file

@ -0,0 +1,110 @@
@page
@model WatchlistModel
@using System.Net
@using global::c3stream.DataModels
@using static WatchlistModel
@{
var cookie = c3stream.UpdateCookie(Request, Response, $"/Watchlist");
ViewData["Title"] = "Watchlist";
await using var db = new Database.DbConn();
var states = db.States.ToList();
var marked = db.States.Where(p => p.UserId == cookie && p.State == "marked").Select(p => p.TalkId).ToList();
var watchlist = c3stream.GetEventsByGuid(marked);
}
<table class="table">
<thead>
<tr>
<th scope="col">Conference</th>
<th scope="col">Event</th>
<th scope="col">
@Html.Raw(Request.Query["orderby"] == "published" ? $"<a href=\"/Conference?c={Request.Query["c"]}\">Published" : $"<a href=\"/Conference?c={Request.Query["c"]}&orderby=published\">Date")
</th>
<th scope="col">Category</th>
<th scope="col">Title</th>
<th scope="col">Speaker(s)</th>
<th scope="col">Lang</th>
<th scope="col">Actions</th>
</tr>
</thead>
<tbody>
@foreach (var talk in Request.Query["orderby"] == "published" ? watchlist.OrderByDescending(p => p.ReleaseDate) : watchlist.OrderBy(p => p.Date)) {
var state = states.FirstOrDefault(p => p.TalkId == talk.Guid && p.UserId == cookie)?.State;
var isWatched = state == "watched";
var isMarked = state == "marked";
var file = $"{talk.Slug}.mp4";
var conference = c3stream.GetConferenceByEventGuid(talk.Guid);
var eventName = talk.Tags.Count <= 1 ? conference.Acronym : talk.Tags[0].Replace("-", "-<br/>");
var category = talk.Tags.Count switch {
0 => "<no category>",
1 => talk.Tags[0],
2 => "<no category>",
3 => talk.Tags[2],
4 => talk.Tags[3],
5 => talk.Tags[3],
6 => talk.Tags[3], // rc3: is this correct?
_ => "<unknown tag format>"
};
<tr>
<td>@Html.Raw(eventName)</td>
<td>@(Request.Query["orderby"] == "published" ? talk.ReleaseDate?.Date.ToShortDateString() : talk.Date?.Date.ToShortDateString())</td>
<td>@category</td>
@if (isWatched) {
<td style="color: #95cb7a">@talk.Title</td>
}
else if (isMarked) {
<td style="color: #da7d4f">@talk.Title</td>
}
else {
<td>@talk.Title</td>
}
<td>@(talk.Persons.Any() ? talk.Persons.Aggregate((s, s1) => $"{s}, {s1}") : "<no speakers>")</td>
<td>@talk.OriginalLanguage</td>
<td>
<div class="btn-group" role="group">
<a href="@talk.FrontendLink.AbsoluteUri" role="button" class="btn btn-primary btn-c3saction w-100" data-toggle="tooltip" data-placement="top" title="Play">
<i class="fas fa-play-circle"></i>
</a>
@if (System.IO.File.Exists(System.IO.Path.Combine(c3stream.CachePath, conference.Acronym, file))) {
<a href="@(c3stream.CacheUrl + $"{conference.Acronym}/{file}")" role="button" class="btn btn-primary btn-c3saction w-100" data-toggle="tooltip" data-placement="top" title="Mirror">
<i class="fas fa-cloud-download"></i>
</a>
}
else {
<a href="/" role="button" class="btn btn-primary btn-c3saction disabled">
<i class="fas fa-cloud-download"></i>
</a>
}
<a href="/Info?guid=@talk.Guid" role="button" class="btn btn-primary btn-c3saction w-100" data-toggle="tooltip" data-placement="top" title="Info">
<i class="fas fa-info-circle"></i>
</a>
@if (isWatched) {
<button onclick="SetState('@talk.Guid', 'unwatched')" class="btn btn-primary btn-c3saction w-100" data-toggle="tooltip" data-placement="top" title="Mark unwatched">
<i class="fas fa-times"></i>
</button>
<button class="btn btn-primary btn-c3saction disabled">
<i class="fas fa-clock"></i>
</button>
}
else if (isMarked) {
<button onclick="SetState('@talk.Guid', 'watched')" class="btn btn-primary btn-c3saction w-100" data-toggle="tooltip" data-placement="top" title="Mark watched">
<i class="fas fa-check"></i>
</button>
<button onclick="SetState('@talk.Guid', 'unwatched')" class="btn btn-primary btn-c3saction w-100" data-toggle="tooltip" data-placement="top" title="Remove from watch later">
<i class="fas fa-undo-alt"></i>
</button>
}
else {
<button onclick="SetState('@talk.Guid', 'watched')" class="btn btn-primary btn-c3saction w-100" data-toggle="tooltip" data-placement="top" title="Mark watched">
<i class="fas fa-check"></i>
</button>
<button onclick="SetState('@talk.Guid', 'marked')" class="btn btn-primary btn-c3saction w-100" data-toggle="tooltip" data-placement="top" title="Add to watch later">
<i class="fas fa-clock"></i>
</button>
}
</div>
</td>
</tr>
}
</tbody>
</table>

39
Pages/Watchlist.cshtml.cs Normal file
View file

@ -0,0 +1,39 @@
using System.Collections.Generic;
using System.Linq;
using c3stream.DataModels;
using c3stream.DataModels.Tables;
using LinqToDB;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
namespace c3stream.Pages {
public class WatchlistModel : PageModel {
private readonly ILogger<ConferenceModel> _logger;
public WatchlistModel(ILogger<ConferenceModel> logger) => _logger = logger;
public void OnGet() {
var guid = Request.Query["guid"].ToString();
var state = Request.Query["state"].ToString();
var userid = Request.Cookies["bookmark"];
if (string.IsNullOrWhiteSpace(guid) || string.IsNullOrWhiteSpace(state) || !Request.Cookies.ContainsKey("bookmark"))
return;
using var db = new Database.DbConn();
var existing = db.States.FirstOrDefault(p => p.TalkId == guid && p.UserId == userid);
if (existing != null)
if (state == "unwatched")
db.States.Delete(p => p == existing);
else {
existing.State = state;
db.Update(existing);
}
else
db.Insert(new States {
State = state, TalkId = guid, UserId = userid
});
Response.Redirect("/");
}
}
}

View file

@ -9,8 +9,9 @@ using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Hosting;
namespace c3stream {
public static class c3stream {
namespace c3stream;
public static class c3stream {
public const string DataPath = "data";
public const string DbFile = "c3stream.sqlite";
public const string CachePath = "/mnt/storage/archive/Video/congress/";
@ -39,15 +40,15 @@ namespace c3stream {
File.Copy(Path.Combine(DataPath, "database.init.sqlite"), DbPath);
DataConnection.DefaultSettings = new Database.Settings();
Migrations.RunMigrations();
foreach (var conference in Conferences)
UpdateConference(conference);
if (args.Length != 0) {
if (args[0] == "logo")
foreach (var conference in Conferences) {
foreach (var conference in Conferences)
Console.WriteLine($"wget {conference.LogoUri} -O {Path.Combine(CachePath, conference.Acronym, "logo.png")}");
}
else if (Conferences.All(p => p.Acronym != args[0]))
Console.WriteLine("No matching conference found.");
else
@ -102,28 +103,30 @@ namespace c3stream {
cookie = request.Cookies["bookmark"];
}
if (request.Query.ContainsKey("bookmark")) {
if (request.Query.ContainsKey("bookmark"))
response.Redirect(redirectUri);
}
return cookie;
}
public static Event GetEventByGuid(string guid) {
return Conferences.SelectMany(c => c.Talks.Where(talk => talk.Guid.ToString() == guid)).FirstOrDefault();
return Conferences.SelectMany(c => c.Talks.Where(e => e.Guid == guid)).FirstOrDefault();
}
public static IEnumerable<Event> GetEventsByGuid(IEnumerable<string> guids) {
return Conferences.SelectMany(c => c.Talks.Where(e => guids.Contains(e.Guid)));
}
public static ConferenceObject GetConferenceByEventGuid(string guid) {
return Conferences.FirstOrDefault(c => c.Talks.Any(t => t.Guid.ToString() == guid));
return Conferences.FirstOrDefault(c => c.Talks.Any(t => t.Guid == guid));
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
public class ConferenceObject {
public string Acronym;
public bool Ongoing;
public string LogoUri;
public bool Ongoing;
public List<Event> Talks = new();
public ConferenceObject(string acronym, bool ongoing = false) {
@ -131,5 +134,4 @@ namespace c3stream {
Ongoing = ongoing;
}
}
}
}