Skip to content

Commit

Permalink
Add automating updating rep
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Mar 29, 2024
1 parent 96505a9 commit 19d7589
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 9 deletions.
10 changes: 10 additions & 0 deletions MyApp.ServiceInterface/QuestionServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,16 @@ public async Task<object> Any(GetMeta request)
return meta;
}

public object Any(GetUserReputations request)
{
var to = new GetUserReputationsResponse();
foreach (var userName in request.UserNames.Safe())
{
to.Results[userName] = appConfig.GetReputation(userName);
}
return to;
}

private string GetUserName()
{
var userName = Request.GetClaimsPrincipal().GetUserName()
Expand Down
12 changes: 11 additions & 1 deletion MyApp.ServiceModel/Posts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -434,4 +434,14 @@ public class DeleteQuestion : IGet, IReturn<EmptyResponse>
public string? ReturnUrl { get; set; }
}

public class GetRequestInfo : IGet, IReturn<string> {}
public class GetRequestInfo : IGet, IReturn<string> {}

public class GetUserReputations : IGet, IReturn<GetUserReputationsResponse>
{
public List<string> UserNames { get; set; } = [];
}
public class GetUserReputationsResponse
{
public Dictionary<string, int> Results { get; set; } = [];
public ResponseStatus? ResponseStatus { get; set; }
}
2 changes: 1 addition & 1 deletion MyApp/Components/Shared/QuestionPost.razor
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@
<div id=@($"edit-{answer.Id}") class="edit w-full pl-2 hidden"></div>
</div>

<div class="flex flex-1 items-end">
<div class="mt-2 flex flex-1 items-end">
<div class="ml-8 sm:pl-6 xl:ml-20 xl:pl-12 w-full">
<div class="answer-footer flex space-x-4 divide-x divide-gray-200 dark:divide-gray-800 text-sm sm:space-x-6 w-full">

Expand Down
27 changes: 24 additions & 3 deletions MyApp/wwwroot/mjs/dtos.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* Options:
Date: 2024-03-29 18:01:15
Date: 2024-03-29 23:35:25
Version: 8.22
Tip: To override a DTO option, remove "//" prefix before updating
BaseUrl: https://localhost:5001
Expand Down Expand Up @@ -68,12 +68,14 @@ export class Post {
lockedReason;
}
export class StatTotals {
/** @param {{id?:string,postId?:number,favoriteCount?:number,viewCount?:number,upVotes?:number,downVotes?:number,startingUpVotes?:number}} [init] */
/** @param {{id?:string,postId?:number,createdBy?:string,favoriteCount?:number,viewCount?:number,upVotes?:number,downVotes?:number,startingUpVotes?:number}} [init] */
constructor(init) { Object.assign(this, init) }
/** @type {string} */
id;
/** @type {number} */
postId;
/** @type {?string} */
createdBy;
/** @type {number} */
favoriteCount;
/** @type {number} */
Expand Down Expand Up @@ -434,6 +436,14 @@ export class CommentsResponse {
/** @type {ResponseStatus} */
responseStatus;
}
export class GetUserReputationsResponse {
/** @param {{results?:{ [index: string]: number; },responseStatus?:ResponseStatus}} [init] */
constructor(init) { Object.assign(this, init) }
/** @type {{ [index: string]: number; }} */
results;
/** @type {?ResponseStatus} */
responseStatus;
}
export class UpdateUserProfileResponse {
/** @param {{responseStatus?:ResponseStatus}} [init] */
constructor(init) { Object.assign(this, init) }
Expand Down Expand Up @@ -607,10 +617,12 @@ export class AskQuestion {
createResponse() { return new AskQuestionResponse() }
}
export class DeleteQuestion {
/** @param {{id?:number}} [init] */
/** @param {{id?:number,returnUrl?:string}} [init] */
constructor(init) { Object.assign(this, init) }
/** @type {number} */
id;
/** @type {?string} */
returnUrl;
getTypeName() { return 'DeleteQuestion' }
getMethod() { return 'GET' }
createResponse() { return new EmptyResponse() }
Expand Down Expand Up @@ -733,6 +745,15 @@ export class GetMeta {
getMethod() { return 'GET' }
createResponse() { return new Meta() }
}
export class GetUserReputations {
/** @param {{userNames?:string[]}} [init] */
constructor(init) { Object.assign(this, init) }
/** @type {string[]} */
userNames;
getTypeName() { return 'GetUserReputations' }
getMethod() { return 'GET' }
createResponse() { return new GetUserReputationsResponse() }
}
export class UpdateUserProfile {
constructor(init) { Object.assign(this, init) }
getTypeName() { return 'UpdateUserProfile' }
Expand Down
41 changes: 37 additions & 4 deletions MyApp/wwwroot/mjs/question.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { mount, alreadyMounted } from "app.mjs"
import {
UserPostData, PostVote, GetQuestionFile,
AnswerQuestion, UpdateQuestion, PreviewMarkdown, GetAnswerBody, CreateComment, GetMeta,
DeleteQuestion, DeleteComment,
DeleteQuestion, DeleteComment, GetUserReputations,
} from "dtos.mjs"

let meta = null
Expand Down Expand Up @@ -80,6 +80,8 @@ async function loadVoting(ctx) {
if (!api.succeeded) {
setValue(refId, prevValue)
updateVote(el)
} else {
setTimeout(() => loadUserReputations(ctx), 2000)
}
}

Expand Down Expand Up @@ -608,6 +610,34 @@ async function loadEditAnswers(ctx) {
})
}

async function loadUserReputations(ctx) {
const { client, postId, userName, user, hasRole } = ctx

const userNames = new Set()
if (userName) userNames.add(userName)
$$('[data-rep-user]').forEach(x => {
userNames.add(x.dataset.repUser)
})
console.log('userNames', userNames.size, userNames)
if (userNames.size > 0) {
const api = await client.api(new GetUserReputations({ userNames: Array.from(userNames) }))
if (api.succeeded) {
const results = api.response.results
Object.keys(api.response.results).forEach(userName => {
$$(`[data-rep-user="${userName}"]`).forEach(el => {
console.log('updating rep', userName, results[userName])
el.innerHTML = results[userName] || 1
})
})
if (userName) {
$$('[data-rep]').forEach(el => {
el.innerHTML = results[userName] || 1
})
}
}
}
}

export default {
async load() {
const client = new JsonServiceClient()
Expand All @@ -628,9 +658,12 @@ export default {

if (!isNaN(postId)) {
const ctx = { client, userName, postId, user, hasRole }
await loadVoting(ctx)
await loadEditQuestion(ctx)
await loadEditAnswers(ctx)
await Promise.all([
loadVoting(ctx),
loadEditQuestion(ctx),
loadEditAnswers(ctx),
loadUserReputations(ctx)
])
}
}
}

0 comments on commit 19d7589

Please sign in to comment.