-
Notifications
You must be signed in to change notification settings - Fork 14
/
GraphController.cs
88 lines (76 loc) · 2.92 KB
/
GraphController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using FurlandGraph.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using MessagePack;
using FurlandGraph.Services;
namespace FurlandGraph.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class GraphController : ControllerBase
{
public GraphController(FurlandContext context, StatusService statusService)
{
Context = context;
StatusService = statusService;
}
public FurlandContext Context { get; }
public StatusService StatusService { get; }
[HttpGet]
[Route("user/{username}/status")]
public async Task<LoadStatus> GetUserStatus(string username, long userId, string nodes = "friends", string relationship = "friends")
{
//long? userId = null;
//var userIdStr = HttpContext.Session.GetString("userId");
//if (long.TryParse(userIdStr, out var userIdParsed))
//{
// userId = userIdParsed;
//}
long? requesterId = userId == 0 ? null : userId;
if (nodes != "friends" && nodes != "followers")
{
throw new Exception();
}
if (relationship != "friends" && relationship != "followers")
{
throw new Exception();
}
try
{
return await StatusService.CheckStatus(username, nodes, relationship, requesterId);
}
catch (StatusServiceException e)
{
return new LoadStatus()
{
Finished = false,
Error = e.Message,
};
}
}
[HttpGet]
[Route("user/{screenName}/matrix")]
public async Task<IActionResult> Get(string screenName, string nodes = "friends", string relationship = "friends")
{
var user = await Context.Users.FirstOrDefaultAsync(t => t.ScreenName == screenName);
if (user == null)
{
throw new Exception("Could not find: " + screenName);
}
var workItemName = $"{nodes}+{relationship}";
var userId = user.Id;
var lz4Options = MessagePackSerializerOptions.Standard.WithCompression(MessagePackCompression.Lz4BlockArray);
var cache = await Context.GraphCache.Where(t => t.UserId == userId && t.Type == workItemName).FirstOrDefaultAsync();
if (cache == null)
{
throw new Exception();
}
var cacheData = MessagePackSerializer.Deserialize<GraphCacheItem>(cache.Data, lz4Options);
Response.Headers["cache-control"] = "public, max-age=604800";
return new FileContentResult(MessagePackSerializer.Serialize(cacheData), "application/json")
{
FileDownloadName = $"userdata.msgpack",
};
}
}
}