diff options
author | crupest <crupest@outlook.com> | 2021-02-10 02:03:06 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-02-10 02:03:06 +0800 |
commit | 4ea535d93753826ec900879560d876cec4d58c38 (patch) | |
tree | b2d94c4347fddf8641fc41caa86826374c9cc241 /BackEnd/Timeline/Services | |
parent | bf8f849c36bfc42cfd0ef1a66f2adf01dc8e1be7 (diff) | |
download | timeline-4ea535d93753826ec900879560d876cec4d58c38.tar.gz timeline-4ea535d93753826ec900879560d876cec4d58c38.tar.bz2 timeline-4ea535d93753826ec900879560d876cec4d58c38.zip |
...
Diffstat (limited to 'BackEnd/Timeline/Services')
-rw-r--r-- | BackEnd/Timeline/Services/TimelinePostService.cs | 126 | ||||
-rw-r--r-- | BackEnd/Timeline/Services/UserAvatarService.cs | 37 |
2 files changed, 54 insertions, 109 deletions
diff --git a/BackEnd/Timeline/Services/TimelinePostService.cs b/BackEnd/Timeline/Services/TimelinePostService.cs index 66ec8090..98841478 100644 --- a/BackEnd/Timeline/Services/TimelinePostService.cs +++ b/BackEnd/Timeline/Services/TimelinePostService.cs @@ -14,95 +14,69 @@ using static Timeline.Resources.Services.TimelineService; namespace Timeline.Services
{
- public class PostData : ICacheableData
+ public class TimelinePostDataDigest
{
-#pragma warning disable CA1819 // Properties should not return arrays
- public byte[] Data { get; set; } = default!;
-#pragma warning restore CA1819 // Properties should not return arrays
- public string Type { get; set; } = default!;
- public string ETag { get; set; } = default!;
- public DateTime? LastModified { get; set; } // TODO: Why nullable?
- }
+ public TimelinePostDataDigest(string kind, string eTag, DateTime lastModified)
+ {
+ Kind = kind;
+ ETag = eTag;
+ LastModified = lastModified;
+ }
- public abstract class TimelinePostCreateRequestContent
- {
- public abstract string TypeName { get; }
+ public string Kind { get; set; }
+ public string ETag { get; set; }
+ public DateTime LastModified { get; set; }
}
- public class TimelinePostCreateRequestTextContent : TimelinePostCreateRequestContent
+ public class TimelinePostData
{
- private string _text;
-
- public TimelinePostCreateRequestTextContent(string text)
+ public TimelinePostData(string kind, byte[] data, string eTag, DateTime lastModified)
{
- if (text is null)
- throw new ArgumentNullException(nameof(text));
-
- _text = text;
+ Kind = kind;
+ Data = data;
+ ETag = eTag;
+ LastModified = lastModified;
}
- public override string TypeName => TimelinePostContentTypes.Text;
+ public string Kind { get; set; }
- public string Text
- {
- get => _text;
- set
- {
- if (value is null)
- throw new ArgumentNullException(nameof(value));
- _text = value;
- }
- }
+#pragma warning disable CA1819 // Properties should not return arrays
+ public byte[] Data { get; set; }
+#pragma warning restore CA1819 // Properties should not return arrays
+
+ public string ETag { get; set; }
+ public DateTime LastModified { get; set; }
}
- public class TimelinePostCreateRequestImageContent : TimelinePostCreateRequestContent
+ public class TimelinePostCreateRequestData
{
- private byte[] _data;
-
- public TimelinePostCreateRequestImageContent(byte[] data)
+ public TimelinePostCreateRequestData(string kind, byte[] data)
{
- if (data is null)
- throw new ArgumentNullException(nameof(data));
-
- _data = data;
+ Kind = kind;
+ Data = data;
}
- public override string TypeName => TimelinePostContentTypes.Image;
-
+ public string Kind { get; set; }
#pragma warning disable CA1819 // Properties should not return arrays
- public byte[] Data
- {
- get => _data;
- set
- {
- if (value is null)
- throw new ArgumentNullException(nameof(value));
- _data = value;
- }
- }
+ public byte[] Data { get; set; }
#pragma warning restore CA1819 // Properties should not return arrays
}
public class TimelinePostCreateRequest
{
- public TimelinePostCreateRequest(TimelinePostCreateRequestContent content)
- {
- Content = content;
- }
-
public string? Color { get; set; }
/// <summary>If not set, current time is used.</summary>
public DateTime? Time { get; set; }
- public TimelinePostCreateRequestContent Content { get; set; }
+ public List<TimelinePostCreateRequestData> Content { get; set; } = new List<TimelinePostCreateRequestData>();
}
public class TimelinePostPatchRequest
{
public string? Color { get; set; }
public DateTime? Time { get; set; }
- public TimelinePostCreateRequestContent? Content { get; set; }
+ public List<TimelinePostCreateRequestData?>? Content { get; set; }
}
public interface ITimelinePostService
@@ -128,16 +102,7 @@ namespace Timeline.Services /// <exception cref="TimelinePostNotExistException">Thrown when post of <paramref name="postId"/> does not exist or has been deleted.</exception>
Task<TimelinePostEntity> GetPost(long timelineId, long postId, bool includeDelete = false);
- /// <summary>
- /// Get the etag of data of a post.
- /// </summary>
- /// <param name="timelineId">The id of the timeline of the post.</param>
- /// <param name="postId">The id of the post.</param>
- /// <returns>The etag of the data.</returns>
- /// <exception cref="TimelineNotExistException">Thrown when timeline does not exist.</exception>
- /// <exception cref="TimelinePostNotExistException">Thrown when post of <paramref name="postId"/> does not exist or has been deleted.</exception>
- /// <exception cref="TimelinePostNoDataException">Thrown when post has no data.</exception>
- Task<string> GetPostDataETag(long timelineId, long postId);
+ Task<TimelinePostDataDigest> GetPostDataDigest(long timelineId, long postId, long dataIndex);
/// <summary>
/// Get the data of a post.
@@ -148,8 +113,7 @@ namespace Timeline.Services /// <exception cref="TimelineNotExistException">Thrown when timeline does not exist.</exception>
/// <exception cref="TimelinePostNotExistException">Thrown when post of <paramref name="postId"/> does not exist or has been deleted.</exception>
/// <exception cref="TimelinePostNoDataException">Thrown when post has no data.</exception>
- /// <seealso cref="GetPostDataETag(long, long)"/>
- Task<PostData> GetPostData(long timelineId, long postId);
+ Task<TimelinePostData> GetPostData(long timelineId, long postId, long dataIndex);
/// <summary>
/// Create a new post in timeline.
@@ -305,7 +269,7 @@ namespace Timeline.Services if (postEntity.Content == null)
throw new TimelinePostNotExistException(timelineId, postId, true);
- if (postEntity.ContentType != TimelinePostContentTypes.Image)
+ if (postEntity.ContentType != TimelinePostDataKind.Image)
throw new TimelinePostNoDataException(ExceptionGetDataNonImagePost);
var tag = postEntity.Content;
@@ -313,7 +277,7 @@ namespace Timeline.Services return tag;
}
- public async Task<PostData> GetPostData(long timelineId, long postId)
+ public async Task<TimelinePostData> GetPostData(long timelineId, long postId)
{
await CheckTimelineExistence(timelineId);
@@ -325,7 +289,7 @@ namespace Timeline.Services if (postEntity.Content == null)
throw new TimelinePostNotExistException(timelineId, postId, true);
- if (postEntity.ContentType != TimelinePostContentTypes.Image)
+ if (postEntity.ContentType != TimelinePostDataKind.Image)
throw new TimelinePostNoDataException(ExceptionGetDataNonImagePost);
var tag = postEntity.Content;
@@ -349,7 +313,7 @@ namespace Timeline.Services await _database.SaveChangesAsync();
}
- return new PostData
+ return new TimelinePostData
{
Data = data,
Type = postEntity.ExtraContent,
@@ -358,21 +322,21 @@ namespace Timeline.Services };
}
- private async Task SaveContent(TimelinePostEntity entity, TimelinePostCreateRequestContent content)
+ private async Task SaveContent(TimelinePostEntity entity, TimelinePostCreateRequestData content)
{
switch (content)
{
- case TimelinePostCreateRequestTextContent c:
- entity.ContentType = c.TypeName;
- entity.Content = c.Text;
+ case TimelinePostCreateRequestTextData c:
+ entity.ContentType = c.Kind;
+ entity.Content = c.Data;
break;
- case TimelinePostCreateRequestImageContent c:
+ case TimelinePostCreateRequestImageData c:
var imageFormat = await _imageValidator.Validate(c.Data);
var imageFormatText = imageFormat.DefaultMimeType;
var tag = await _dataManager.RetainEntry(c.Data);
- entity.ContentType = content.TypeName;
+ entity.ContentType = content.Kind;
entity.Content = tag;
entity.ExtraContent = imageFormatText;
break;
@@ -383,7 +347,7 @@ namespace Timeline.Services private async Task CleanContent(TimelinePostEntity entity)
{
- if (entity.Content is not null && entity.ContentType == TimelinePostContentTypes.Image)
+ if (entity.Content is not null && entity.ContentType == TimelinePostDataKind.Image)
await _dataManager.FreeEntry(entity.Content);
entity.Content = null;
}
@@ -516,7 +480,7 @@ namespace Timeline.Services {
if (post.Content != null)
{
- if (post.ContentType == TimelinePostContentTypes.Image)
+ if (post.ContentType == TimelinePostDataKind.Image)
{
dataTags.Add(post.Content);
}
diff --git a/BackEnd/Timeline/Services/UserAvatarService.cs b/BackEnd/Timeline/Services/UserAvatarService.cs index b41c45fd..afd6cf0a 100644 --- a/BackEnd/Timeline/Services/UserAvatarService.cs +++ b/BackEnd/Timeline/Services/UserAvatarService.cs @@ -8,28 +8,12 @@ using System.Linq; using System.Threading.Tasks;
using Timeline.Entities;
using Timeline.Helpers;
+using Timeline.Helpers.Cache;
+using Timeline.Models;
using Timeline.Services.Exceptions;
namespace Timeline.Services
{
- public class Avatar
- {
- public string Type { get; set; } = default!;
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1819:Properties should not return arrays", Justification = "DTO Object")]
- public byte[] Data { get; set; } = default!;
- }
-
- public class AvatarInfo
- {
- public Avatar Avatar { get; set; } = default!;
- public DateTime LastModified { get; set; }
-
- public CacheableData ToCacheableData()
- {
- return new CacheableData(Avatar.Type, Avatar.Data, LastModified);
- }
- }
-
/// <summary>
/// Provider for default user avatar.
/// </summary>
@@ -42,29 +26,24 @@ namespace Timeline.Services /// Get the etag of default avatar.
/// </summary>
/// <returns></returns>
- Task<string> GetDefaultAvatarETag();
+ Task<ICacheableDataDigest> GetDefaultAvatarETag();
/// <summary>
/// Get the default avatar.
/// </summary>
- Task<AvatarInfo> GetDefaultAvatar();
+ Task<ByteData> GetDefaultAvatar();
}
public interface IUserAvatarService
{
- /// <summary>
- /// Get the etag of a user's avatar. Warning: This method does not check the user existence.
- /// </summary>
- /// <param name="id">The id of the user to get avatar etag of.</param>
- /// <returns>The etag.</returns>
- Task<string> GetAvatarETag(long id);
+ Task<ICacheableDataDigest> GetAvatarDigest(long id);
/// <summary>
/// Get avatar of a user. If the user has no avatar set, a default one is returned. Warning: This method does not check the user existence.
/// </summary>
/// <param name="id">The id of the user to get avatar of.</param>
/// <returns>The avatar info.</returns>
- Task<AvatarInfo> GetAvatar(long id);
+ Task<ByteData> GetAvatar(long id);
/// <summary>
/// Set avatar for a user. Warning: This method does not check the user existence.
@@ -74,7 +53,9 @@ namespace Timeline.Services /// <returns>The etag of the avatar.</returns>
/// <exception cref="ArgumentException">Thrown if any field in <paramref name="avatar"/> is null when <paramref name="avatar"/> is not null.</exception>
/// <exception cref="ImageException">Thrown if avatar is of bad format.</exception>
- Task<string> SetAvatar(long id, Avatar? avatar);
+ Task<string> SetAvatar(long id, ByteData avatar);
+
+ Task DeleteAvatar(long id);
}
// TODO! : Make this configurable.
|