-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocialDataRepository.cs
More file actions
141 lines (128 loc) · 5.49 KB
/
SocialDataRepository.cs
File metadata and controls
141 lines (128 loc) · 5.49 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using Microsoft.EntityFrameworkCore;
using SocialAgent.Core.Models;
namespace SocialAgent.Data.Repositories;
public class SocialDataRepository(SocialAgentDbContext db) : ISocialDataRepository
{
public async Task UpsertPostsAsync(IEnumerable<SocialPost> posts, CancellationToken ct = default)
{
foreach (var post in posts)
{
var existing = await db.Posts
.FirstOrDefaultAsync(p => p.ProviderId == post.ProviderId && p.PlatformPostId == post.PlatformPostId, ct);
if (existing is null)
{
db.Posts.Add(post);
}
else
{
existing.LikeCount = post.LikeCount;
existing.RepostCount = post.RepostCount;
existing.ReplyCount = post.ReplyCount;
existing.LastUpdated = DateTimeOffset.UtcNow;
}
}
await db.SaveChangesAsync(ct);
}
public async Task<IReadOnlyList<SocialPost>> GetPostsAsync(
string? providerId = null, DateTimeOffset? since = null,
bool? isOwnPost = null, int? limit = null, CancellationToken ct = default)
{
var query = db.Posts.AsQueryable();
if (providerId is not null) query = query.Where(p => p.ProviderId == providerId);
if (since is not null) query = query.Where(p => p.CreatedAt >= since);
if (isOwnPost is not null) query = query.Where(p => p.IsOwnPost == isOwnPost);
query = query.OrderByDescending(p => p.CreatedAt);
if (limit is not null) query = query.Take(limit.Value);
return await query.ToListAsync(ct);
}
public async Task<IReadOnlyList<SocialPost>> GetTopPostsByEngagementAsync(
int count, string? providerId = null, DateTimeOffset? since = null, CancellationToken ct = default)
{
var query = db.Posts.Where(p => p.IsOwnPost);
if (providerId is not null) query = query.Where(p => p.ProviderId == providerId);
if (since is not null) query = query.Where(p => p.CreatedAt >= since);
return await query
.OrderByDescending(p => p.LikeCount + p.RepostCount + p.ReplyCount)
.Take(count)
.ToListAsync(ct);
}
public async Task UpsertNotificationsAsync(IEnumerable<SocialNotification> notifications, CancellationToken ct = default)
{
foreach (var notification in notifications)
{
var existing = await db.Notifications
.FirstOrDefaultAsync(n => n.ProviderId == notification.ProviderId && n.PlatformNotificationId == notification.PlatformNotificationId, ct);
if (existing is null)
{
db.Notifications.Add(notification);
}
else
{
existing.IsRead = notification.IsRead;
}
}
await db.SaveChangesAsync(ct);
}
public async Task<IReadOnlyList<SocialNotification>> GetNotificationsAsync(
string? providerId = null, string? type = null,
DateTimeOffset? since = null, int? limit = null, CancellationToken ct = default)
{
var query = db.Notifications.AsQueryable();
if (providerId is not null) query = query.Where(n => n.ProviderId == providerId);
if (type is not null) query = query.Where(n => n.Type == type);
if (since is not null) query = query.Where(n => n.CreatedAt >= since);
query = query.OrderByDescending(n => n.CreatedAt);
if (limit is not null) query = query.Take(limit.Value);
return await query.ToListAsync(ct);
}
public async Task<IReadOnlyList<SocialNotification>> GetUnreadNotificationsAsync(
string? providerId = null, CancellationToken ct = default)
{
var query = db.Notifications.Where(n => !n.IsRead);
if (providerId is not null) query = query.Where(n => n.ProviderId == providerId);
return await query.OrderByDescending(n => n.CreatedAt).ToListAsync(ct);
}
public async Task UpsertProfileAsync(SocialProfile profile, CancellationToken ct = default)
{
var existing = await db.Profiles.FindAsync([profile.ProviderId], ct);
if (existing is null)
{
db.Profiles.Add(profile);
}
else
{
existing.Handle = profile.Handle;
existing.DisplayName = profile.DisplayName;
existing.Bio = profile.Bio;
existing.AvatarUrl = profile.AvatarUrl;
existing.FollowerCount = profile.FollowerCount;
existing.FollowingCount = profile.FollowingCount;
existing.PostCount = profile.PostCount;
existing.LastUpdated = DateTimeOffset.UtcNow;
}
await db.SaveChangesAsync(ct);
}
public async Task<IReadOnlyList<SocialProfile>> GetProfilesAsync(CancellationToken ct = default)
{
return await db.Profiles.ToListAsync(ct);
}
public async Task<PollState?> GetPollStateAsync(string providerId, CancellationToken ct = default)
{
return await db.PollStates.FindAsync([providerId], ct);
}
public async Task UpsertPollStateAsync(PollState state, CancellationToken ct = default)
{
var existing = await db.PollStates.FindAsync([state.ProviderId], ct);
if (existing is null)
{
db.PollStates.Add(state);
}
else
{
existing.LastPostId = state.LastPostId;
existing.LastNotificationId = state.LastNotificationId;
existing.LastPollTime = state.LastPollTime;
}
await db.SaveChangesAsync(ct);
}
}