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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using Timeline.Filters;
using Timeline.Helpers.Cache;
using Timeline.Models;
using Timeline.Models.Http;
using Timeline.Models.Mapper;
using Timeline.Models.Validation;
using Timeline.Services;
using Timeline.Entities;
namespace Timeline.Controllers
{
/// <summary>
/// Operations about timeline.
/// </summary>
[ApiController]
[Route("timelines/{timeline}/posts")]
[CatchTimelineNotExistException]
[CatchTimelinePostNotExistException]
[CatchTimelinePostDataNotExistException]
[ProducesErrorResponseType(typeof(CommonResponse))]
public class TimelinePostController : Controller
{
private readonly ITimelineService _timelineService;
private readonly ITimelinePostService _postService;
private readonly TimelineMapper _timelineMapper;
/// <summary>
///
/// </summary>
public TimelinePostController(ITimelineService timelineService, ITimelinePostService timelinePostService, TimelineMapper timelineMapper)
{
_timelineService = timelineService;
_postService = timelinePostService;
_timelineMapper = timelineMapper;
}
private bool UserHasAllTimelineManagementPermission => this.UserHasPermission(UserPermission.AllTimelineManagement);
private Task<HttpTimelinePost> Map(TimelinePostEntity post, string timelineName)
{
return _timelineMapper.MapToHttp(post, timelineName, Url, this.GetOptionalUserId(), UserHasAllTimelineManagementPermission);
}
private Task<List<HttpTimelinePost>> Map(List<TimelinePostEntity> post, string timelineName)
{
return _timelineMapper.MapToHttp(post, timelineName, Url, this.GetOptionalUserId(), UserHasAllTimelineManagementPermission);
}
/// <summary>
/// Get posts of a timeline.
/// </summary>
/// <param name="timeline">The name of the timeline.</param>
/// <param name="modifiedSince">If set, only posts modified since the time will return.</param>
/// <param name="includeDeleted">If set to true, deleted post will also return.</param>
/// <returns>The post list.</returns>
[HttpGet]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<ActionResult<List<HttpTimelinePost>>> List([FromRoute][GeneralTimelineName] string timeline, [FromQuery] DateTime? modifiedSince, [FromQuery] bool? includeDeleted)
{
var timelineId = await _timelineService.GetTimelineIdByName(timeline);
if (!UserHasAllTimelineManagementPermission && !await _timelineService.HasReadPermission(timelineId, this.GetOptionalUserId()))
{
return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid());
}
var posts = await _postService.GetPosts(timelineId, modifiedSince, includeDeleted ?? false);
var result = await Map(posts, timeline);
return result;
}
/// <summary>
/// Get a post of a timeline.
/// </summary>
/// <param name="timeline">The name of the timeline.</param>
/// <param name="postId">The post id.</param>
/// <returns>The post.</returns>
[HttpGet("{post}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<ActionResult<HttpTimelinePost>> Get([FromRoute][GeneralTimelineName] string timeline, [FromRoute(Name = "post")] long postId)
{
var timelineId = await _timelineService.GetTimelineIdByName(timeline);
if (!UserHasAllTimelineManagementPermission && !await _timelineService.HasReadPermission(timelineId, this.GetOptionalUserId()))
{
return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid());
}
var post = await _postService.GetPost(timelineId, postId);
var result = await Map(post, timeline);
return result;
}
/// <summary>
/// Get the first data of a post.
/// </summary>
/// <param name="timeline">Timeline name.</param>
/// <param name="post">The id of the post.</param>
/// <returns>The data.</returns>
[HttpGet("{post}/data")]
[Produces(MimeTypes.ImagePng, MimeTypes.ImageJpeg, MimeTypes.ImageGif, MimeTypes.ImageWebp, MimeTypes.TextPlain, MimeTypes.TextMarkdown, MimeTypes.TextPlain, MimeTypes.ApplicationJson)]
[ProducesResponseType(typeof(byte[]), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(void), StatusCodes.Status304NotModified)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<ActionResult<ByteData>> DataIndexGet([FromRoute][GeneralTimelineName] string timeline, [FromRoute] long post)
{
return await DataGet(timeline, post, 0);
}
/// <summary>
/// Get the data of a post. Usually a image post.
/// </summary>
/// <param name="timeline">Timeline name.</param>
/// <param name="post">The id of the post.</param>
/// <param name="dataIndex">Index of the data.</param>
/// <returns>The data.</returns>
[HttpGet("{post}/data/{data_index}")]
[Produces(MimeTypes.ImagePng, MimeTypes.ImageJpeg, MimeTypes.ImageGif, MimeTypes.ImageWebp, MimeTypes.TextPlain, MimeTypes.TextMarkdown, MimeTypes.TextPlain, MimeTypes.ApplicationJson)]
[ProducesResponseType(typeof(byte[]), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(void), StatusCodes.Status304NotModified)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<ActionResult> DataGet([FromRoute][GeneralTimelineName] string timeline, [FromRoute] long post, [FromRoute(Name = "data_index")][Range(0, 100)] long dataIndex)
{
var timelineId = await _timelineService.GetTimelineIdByName(timeline);
if (!UserHasAllTimelineManagementPermission && !await _timelineService.HasReadPermission(timelineId, this.GetOptionalUserId()))
{
return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid());
}
return await DataCacheHelper.GenerateActionResult(this,
() => _postService.GetPostDataDigest(timelineId, post, dataIndex),
() => _postService.GetPostData(timelineId, post, dataIndex)
);
}
/// <summary>
/// Create a new post.
/// </summary>
/// <param name="timeline">Timeline name.</param>
/// <param name="body"></param>
/// <returns>Info of new post.</returns>
[HttpPost]
[Authorize]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
public async Task<ActionResult<HttpTimelinePost>> Post([FromRoute][GeneralTimelineName] string timeline, [FromBody] HttpTimelinePostCreateRequest body)
{
var timelineId = await _timelineService.GetTimelineIdByName(timeline);
var userId = this.GetUserId();
if (!UserHasAllTimelineManagementPermission && !await _timelineService.IsMemberOf(timelineId, userId))
{
return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid());
}
var createRequest = new TimelinePostCreateRequest()
{
Time = body.Time,
Color = body.Color
};
for (int i = 0; i < body.DataList.Count; i++)
{
var data = body.DataList[i];
if (data is null)
return BadRequest(new CommonResponse(ErrorCodes.Common.InvalidModel, $"Data at index {i} is null."));
try
{
var d = Convert.FromBase64String(data.Data);
createRequest.DataList.Add(new TimelinePostCreateRequestData(data.ContentType, d));
}
catch (FormatException)
{
return BadRequest(new CommonResponse(ErrorCodes.Common.InvalidModel, $"Data at index {i} is not a valid base64 string."));
}
}
try
{
var post = await _postService.CreatePost(timelineId, userId, createRequest);
var result = await Map(post, timeline);
return result;
}
catch (TimelinePostCreateDataException e)
{
return BadRequest(new CommonResponse(ErrorCodes.Common.InvalidModel, $"Data at index {e.Index} is invalid. {e.Message}"));
}
}
/// <summary>
/// Update a post except content.
/// </summary>
/// <param name="timeline">Timeline name.</param>
/// <param name="post">Post id.</param>
/// <param name="body">Request body.</param>
/// <returns>New info of post.</returns>
[HttpPatch("{post}")]
[Authorize]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
public async Task<ActionResult<HttpTimelinePost>> Patch([FromRoute][GeneralTimelineName] string timeline, [FromRoute] long post, [FromBody] HttpTimelinePostPatchRequest body)
{
var timelineId = await _timelineService.GetTimelineIdByName(timeline);
if (!UserHasAllTimelineManagementPermission && !await _postService.HasPostModifyPermission(timelineId, post, this.GetUserId(), true))
{
return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid());
}
var entity = await _postService.PatchPost(timelineId, post, new TimelinePostPatchRequest { Time = body.Time, Color = body.Color });
var result = await Map(entity, timeline);
return Ok(result);
}
/// <summary>
/// Delete a post.
/// </summary>
/// <param name="timeline">Timeline name.</param>
/// <param name="post">Post id.</param>
/// <returns>Info of deletion.</returns>
[HttpDelete("{post}")]
[Authorize]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
public async Task<ActionResult> Delete([FromRoute][GeneralTimelineName] string timeline, [FromRoute] long post)
{
var timelineId = await _timelineService.GetTimelineIdByName(timeline);
if (!UserHasAllTimelineManagementPermission && !await _postService.HasPostModifyPermission(timelineId, post, this.GetUserId(), true))
{
return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid());
}
await _postService.DeletePost(timelineId, post);
return Ok();
}
}
}
|